'Tests that:
* shib credentials that match an existing ExternalAuthMap with a linked active user logs the user in
* shib credentials that match an existing ExternalAuthMap with a linked inactive user shows error page
* shib credentials that match an existing ExternalAuthMap without a linked user and also match the email
of an existing user without an existing ExternalAuthMap links the two and log the user in
* shib credentials that match an existing ExternalAuthMap without a linked user and also match the email
of an existing user that already has an ExternalAuthMap causes an error (403)
* shib credentials that do not match an existing ExternalAuthMap causes the registration form to appear'

@unittest.skipUnless(settings.FEATURES.get('AUTH_USE_SHIB'), 'AUTH_USE_SHIB    not    set')
def test_shib_login(self):

    user_w_map = UserFactory.create(email='withmap@stanford.edu')
    extauth = ExternalAuthMap(external_id='withmap@stanford.edu', external_email='', external_domain='shib:https://idp.stanford.edu/', external_credentials='', user=user_w_map)
    user_wo_map = UserFactory.create(email='womap@stanford.edu')
    user_w_map.save()
    user_wo_map.save()
    extauth.save()
    inactive_user = UserFactory.create(email='inactive@stanford.edu')
    inactive_user.is_active = False
    inactive_extauth = ExternalAuthMap(external_id='inactive@stanford.edu', external_email='', external_domain='shib:https://idp.stanford.edu/', external_credentials='', user=inactive_user)
    inactive_user.save()
    inactive_extauth.save()
    idps = ['https://idp.stanford.edu/', 'https://someother.idp.com/']
    remote_users = ['withmap@stanford.edu', 'womap@stanford.edu', 'testuser2@someother_idp.com', 'inactive@stanford.edu']
    for idp in idps:
        for remote_user in remote_users:
            self.client.logout()
            with patch('openedx.core.djangoapps.external_auth.views.AUDIT_LOG') as mock_audit_log:
                response = self.client.get(reverse('shib-login'), **{'Shib-Identity-Provider': idp, 'mail': remote_user, 'REMOTE_USER': remote_user})
            audit_log_calls = mock_audit_log.method_calls
            if ((idp == 'https://idp.stanford.edu/') and (remote_user == 'withmap@stanford.edu')):
                self.assertRedirects(response, '/dashboard')
                self.assertEquals(int(self.client.session['_auth_user_id']), user_w_map.id)
                self.assertEquals(len(audit_log_calls), 2)
                self._assert_shib_login_is_logged(audit_log_calls[0], remote_user)
                (method_name, args, _kwargs) = audit_log_calls[1]
                self.assertEquals(method_name, 'info')
                self.assertEquals(len(args), 1)
                self.assertIn(u'Login    success', args[0])
                self.assertIn(remote_user, args[0])
            elif ((idp == 'https://idp.stanford.edu/') and (remote_user == 'inactive@stanford.edu')):
                self.assertEqual(response.status_code, 403)
                self.assertIn('Account    not    yet    activated:    please    look    for    link    in    your    email', response.content)
                self.assertEquals(len(audit_log_calls), 2)
                self._assert_shib_login_is_logged(audit_log_calls[0], remote_user)
                (method_name, args, _kwargs) = audit_log_calls[1]
                self.assertEquals(method_name, 'warning')
                self.assertEquals(len(args), 1)
                self.assertIn(u'is    not    active    after    external    login', args[0])
            elif ((idp == 'https://idp.stanford.edu/') and (remote_user == 'womap@stanford.edu')):
                self.assertIsNotNone(ExternalAuthMap.objects.get(user=user_wo_map))
                self.assertRedirects(response, '/dashboard')
                self.assertEquals(int(self.client.session['_auth_user_id']), user_wo_map.id)
                self.assertEquals(len(audit_log_calls), 2)
                self._assert_shib_login_is_logged(audit_log_calls[0], remote_user)
                (method_name, args, _kwargs) = audit_log_calls[1]
                self.assertEquals(method_name, 'info')
                self.assertEquals(len(args), 1)
                self.assertIn(u'Login    success', args[0])
                self.assertIn(remote_user, args[0])
            elif ((idp == 'https://someother.idp.com/') and (remote_user in ['withmap@stanford.edu', 'womap@stanford.edu', 'inactive@stanford.edu'])):
                self.assertEqual(response.status_code, 403)
                self.assertIn('You    have    already    created    an    account    using    an    external    login', response.content)
                self.assertEquals(len(audit_log_calls), 0)
            else:
                self.assertEqual(response.status_code, 200)
                self.assertContains(response, u'Preferences    for    {platform_name}'.format(platform_name=settings.PLATFORM_NAME))
                self.assertEquals(len(audit_log_calls), 0)
