'Tests user creation after the registration form that pops is submitted.  If there is no shib
ExternalAuthMap in the session, then the created user should take the username and email from the
request.
Uses django test client for its session support'

@unittest.skipUnless(settings.FEATURES.get('AUTH_USE_SHIB'), 'AUTH_USE_SHIB    not    set')
@data(*gen_all_identities())
def test_registration_form_submit(self, identity):

    self.client.get(path='/shib-login/', data={}, follow=False, **identity)
    postvars = {'email': u'post_email@stanford.edu', 'username': u'post_username', 'password': u'post_p\xe1ssword', 'name': u'post_n\xe1me', 'terms_of_service': u'true', 'honor_code': u'true'}
    with patch('student.views.AUDIT_LOG') as mock_audit_log:
        self.client.post('/create_account', data=postvars)
    mail = identity.get('mail')
    audit_log_calls = mock_audit_log.method_calls
    self.assertEquals(len(audit_log_calls), 3)
    (method_name, args, _kwargs) = audit_log_calls[0]
    self.assertEquals(method_name, 'info')
    self.assertEquals(len(args), 1)
    self.assertIn(u'Login    success    on    new    account    creation', args[0])
    self.assertIn(u'post_username', args[0])
    (method_name, args, _kwargs) = audit_log_calls[1]
    self.assertEquals(method_name, 'info')
    self.assertEquals(len(args), 2)
    self.assertIn(u'User    registered    with    external_auth', args[0])
    self.assertEquals(u'post_username', args[1])
    (method_name, args, _kwargs) = audit_log_calls[2]
    self.assertEquals(method_name, 'info')
    self.assertEquals(len(args), 3)
    self.assertIn(u'Updated    ExternalAuthMap    for    ', args[0])
    self.assertEquals(u'post_username', args[1])
    self.assertEquals(u'test_user@stanford.edu', args[2].external_id)
    user = User.objects.get(id=self.client.session['_auth_user_id'])
    if mail:
        self.assertEqual(user.email, mail)
        self.assertEqual(list(User.objects.filter(email=postvars['email'])), [])
        self.assertIsNotNone(User.objects.get(email=mail))
    else:
        self.assertEqual(user.email, postvars['email'])
        self.assertEqual(list(User.objects.filter(email=mail)), [])
        self.assertIsNotNone(User.objects.get(email=postvars['email']))
    profile = UserProfile.objects.get(user=user)
    sn_empty = (not identity.get('sn'))
    given_name_empty = (not identity.get('givenName'))
    displayname_empty = (not identity.get('displayName'))
    if displayname_empty:
        if (sn_empty and given_name_empty):
            self.assertEqual(profile.name, postvars['name'])
        else:
            self.assertEqual(profile.name, self.client.session['ExternalAuthMap'].external_name)
            self.assertNotIn(u';', profile.name)
    else:
        self.assertEqual(profile.name, self.client.session['ExternalAuthMap'].external_name)
        self.assertEqual(profile.name, identity.get('displayName').decode('utf-8'))
