Esempio n. 1
0
class KumaSocialAccountAdapterTestCase(UserTestCase):
    rf = RequestFactory()

    def setUp(self):
        """ extra setUp to make a working session """
        super(KumaSocialAccountAdapterTestCase, self).setUp()
        self.adapter = KumaSocialAccountAdapter()

    @attr('bug1055870')
    def test_pre_social_login_overwrites_session_var(self):
        """ https://bugzil.la/1055870 """
        # Set up a pre-existing GitHub sign-in session
        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session.save()
        request.session = session

        # Set up a Persona SocialLogin
        account = SocialAccount.objects.get(user__username='******')
        sociallogin = SocialLogin(account=account)

        # Verify the social_login receiver over-writes the provider
        # stored in the session
        self.adapter.pre_social_login(request, sociallogin)
        eq_(
            account.provider, request.session['sociallogin_provider'],
            "receiver should have over-written sociallogin_provider "
            "session variable")

    @attr('bug1063830')
    def test_pre_social_login_error_for_unmatched_login(self):
        """ https://bugzil.la/1063830 """

        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get('/')
        session = self.client.session
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session
        messages = self.get_messages(request)

        # Set up an un-matching Persona SocialLogin for request
        persona_account = SocialAccount(user=self.user_model(),
                                        provider='persona',
                                        uid='*****@*****.**')
        persona_login = SocialLogin(account=persona_account)

        self.assertRaises(ImmediateHttpResponse, self.adapter.pre_social_login,
                          request, persona_login)
        queued_messages = list(messages)
        eq_(len(queued_messages), 1)
        eq_(django_messages.ERROR, queued_messages[0].level)
Esempio n. 2
0
class KumaSocialAccountAdapterTestCase(UserTestCase):
    rf = RequestFactory()

    def setUp(self):
        """ extra setUp to make a working session """
        super(KumaSocialAccountAdapterTestCase, self).setUp()
        self.adapter = KumaSocialAccountAdapter()

    @attr('bug1055870')
    def test_pre_social_login_overwrites_session_var(self):
        """ https://bugzil.la/1055870 """
        # Set up a pre-existing GitHub sign-in session
        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session.save()
        request.session = session

        # Set up a Persona SocialLogin
        account = SocialAccount.objects.get(user__username='******')
        sociallogin = SocialLogin(account=account)

        # Verify the social_login receiver over-writes the provider
        # stored in the session
        self.adapter.pre_social_login(request, sociallogin)
        eq_(account.provider,
            request.session['sociallogin_provider'],
            "receiver should have over-written sociallogin_provider "
            "session variable")

    @attr('bug1063830')
    def test_pre_social_login_error_for_unmatched_login(self):
        """ https://bugzil.la/1063830 """

        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get('/')
        session = self.client.session
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session
        messages = self.get_messages(request)

        # Set up an un-matching Persona SocialLogin for request
        persona_account = SocialAccount(user=self.user_model(),
                                        provider='persona',
                                        uid='*****@*****.**')
        persona_login = SocialLogin(account=persona_account)

        self.assertRaises(ImmediateHttpResponse,
                          self.adapter.pre_social_login, request, persona_login)
        queued_messages = list(messages)
        eq_(len(queued_messages), 1)
        eq_(django_messages.ERROR, queued_messages[0].level)
Esempio n. 3
0
 def setUp(self):
     """ extra setUp to make a working session """
     from django.conf import settings
     engine = import_module(settings.SESSION_ENGINE)
     store = engine.SessionStore()
     store.save()
     self.client = LocalizingClient()
     self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key
     self.adapter = KumaSocialAccountAdapter()
Esempio n. 4
0
 def setUp(self):
     """ extra setUp to make a working session """
     from django.conf import settings
     engine = import_module(settings.SESSION_ENGINE)
     store = engine.SessionStore()
     store.save()
     self.client = LocalizingClient()
     self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key
     self.adapter = KumaSocialAccountAdapter()
Esempio n. 5
0
 def setUp(self):
     """ extra setUp to make a working session """
     super(KumaSocialAccountAdapterTestCase, self).setUp()
     self.adapter = KumaSocialAccountAdapter()
Esempio n. 6
0
 def setUp(self):
     """ extra setUp to make a working session """
     super(KumaSocialAccountAdapterTestCase, self).setUp()
     self.adapter = KumaSocialAccountAdapter()
Esempio n. 7
0
class KumaSocialAccountAdapterTestCase(UserTestCase):
    rf = RequestFactory()

    def setUp(self):
        """ extra setUp to make a working session """
        super(KumaSocialAccountAdapterTestCase, self).setUp()
        self.adapter = KumaSocialAccountAdapter()

    def test_pre_social_login_overwrites_session_var(self):
        """
        When a user logs in a second time, second login wins the session.

        https://bugzil.la/1055870
        """
        # Set up a pre-existing "Alternate" sign-in session
        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'alternate'
        session.save()
        request.session = session

        # Set up a in-process GitHub SocialLogin (unsaved)
        account = SocialAccount.objects.get(user__username='******')
        assert account.provider == 'github'
        sociallogin = SocialLogin(account=account)

        # Verify the social_login receiver over-writes the provider
        # stored in the session
        self.adapter.pre_social_login(request, sociallogin)
        eq_(
            account.provider, request.session['sociallogin_provider'],
            "receiver should have over-written sociallogin_provider "
            "session variable")

    def test_pre_social_login_error_for_unmatched_login(self):
        """
        When we suspect the signup form is used as a connection form, abort.

        https://bugzil.la/1063830
        """
        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get('/')
        session = self.client.session
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session
        messages = self.get_messages(request)

        # Set up an un-matching alternate SocialLogin for request
        other_account = SocialAccount(user=self.user_model(),
                                      provider='other',
                                      uid='*****@*****.**')
        other_login = SocialLogin(account=other_account)

        self.assertRaises(ImmediateHttpResponse, self.adapter.pre_social_login,
                          request, other_login)
        queued_messages = list(messages)
        eq_(len(queued_messages), 1)
        eq_(django_messages.ERROR, queued_messages[0].level)

    def test_pre_social_login_matched_login(self):
        """
        When we detected a legacy Persona account, advise recovery of account.

        A user tries to sign in with GitHub, but their GitHub email matches
        an existing MDN account backed by Persona. They are prompted to
        recover the existing account.

        https://bugzil.la/1063830, happy path
        """
        # Set up a session-only GitHub SocialLogin
        # These are created at the start of the signup process, and saved on
        #  profile completion.
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        # Setup existing Persona SocialLogin for the same email
        SocialAccount.objects.create(user=github_account.user,
                                     provider='persona',
                                     uid=github_account.user.email)

        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session

        # Verify the social_login receiver over-writes the provider
        # stored in the session
        self.adapter.pre_social_login(request, github_login)
        session = request.session
        eq_(session['sociallogin_provider'], 'github')

    def test_pre_social_login_same_provider(self):
        """
        pre_social_login passes if existing provider is the same.

        I'm not sure what the real-world counterpart of this is. Logging
        in with a different GitHub account? Needed for branch coverage.
        """

        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session

        # Set up an un-matching GitHub SocialLogin for request
        github2_account = SocialAccount(user=self.user_model(),
                                        provider='github',
                                        uid=github_account.uid + '2')
        github2_login = SocialLogin(account=github2_account)

        self.adapter.pre_social_login(request, github2_login)
        eq_(request.session['sociallogin_provider'], 'github')

    def test_pre_social_login_banned_user(self):
        """A banned user is not allowed to login."""
        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session
        request.user = AnonymousUser()
        request.LANGUAGE_CODE = 'en-US'

        # Ban the user
        banned_by = User.objects.get(username='******')
        UserBan.objects.create(user=github_account.user,
                               by=banned_by,
                               reason='Banned by unit test.')

        with pytest.raises(ImmediateHttpResponse) as e_info:
            self.adapter.pre_social_login(request, github_login)
        resp = e_info.value.response
        assert 'Banned by unit test.' in resp.content
        assert not resp.has_header('Vary')
        never_cache = 'no-cache, no-store, must-revalidate, max-age=0'
        assert resp['Cache-Control'] == never_cache
Esempio n. 8
0
class KumaSocialAccountAdapterTestCase(UserTestCase):
    rf = RequestFactory()

    def setUp(self):
        """ extra setUp to make a working session """
        super(KumaSocialAccountAdapterTestCase, self).setUp()
        self.adapter = KumaSocialAccountAdapter()

    def test_pre_social_login_overwrites_session_var(self):
        """ https://bugzil.la/1055870 """
        # Set up a pre-existing GitHub sign-in session
        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session.save()
        request.session = session

        # Set up a Persona SocialLogin
        account = SocialAccount.objects.get(user__username='******')
        sociallogin = SocialLogin(account=account)

        # Verify the social_login receiver over-writes the provider
        # stored in the session
        self.adapter.pre_social_login(request, sociallogin)
        eq_(account.provider,
            request.session['sociallogin_provider'],
            "receiver should have over-written sociallogin_provider "
            "session variable")

    def test_pre_social_login_error_for_unmatched_login(self):
        """ https://bugzil.la/1063830 """

        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get('/')
        session = self.client.session
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session
        messages = self.get_messages(request)

        # Set up an un-matching Persona SocialLogin for request
        persona_account = SocialAccount(user=self.user_model(),
                                        provider='persona',
                                        uid='*****@*****.**')
        persona_login = SocialLogin(account=persona_account)

        self.assertRaises(ImmediateHttpResponse,
                          self.adapter.pre_social_login, request, persona_login)
        queued_messages = list(messages)
        eq_(len(queued_messages), 1)
        eq_(django_messages.ERROR, queued_messages[0].level)

    def test_pre_social_login_matched_login(self):
        """
        https://bugzil.la/1063830, happy path

        A user tries to sign in with GitHub, but their GitHub email matches
        an existing Persona-backed MDN account. They follow the prompt to login
        with Persona, and the accounts are connected.
        """

        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session

        # Set up an matching Persona SocialLogin for request
        persona_account = SocialAccount.objects.create(
            user=github_account.user,
            provider='persona',
            uid=github_account.user.email)
        persona_login = SocialLogin(account=persona_account)

        # Verify the social_login receiver over-writes the provider
        # stored in the session
        self.adapter.pre_social_login(request, persona_login)
        session = request.session
        eq_(session['sociallogin_provider'], 'persona')

    def test_pre_social_login_same_provider(self):
        """
        pre_social_login passes if existing provider is the same.

        I'm not sure what the real-world counterpart of this is. Logging
        in with a different GitHub account? Needed for branch coverage.
        """

        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session

        # Set up an un-matching GitHub SocialLogin for request
        github2_account = SocialAccount(user=self.user_model(),
                                        provider='github',
                                        uid=github_account.uid + '2')
        github2_login = SocialLogin(account=github2_account)

        self.adapter.pre_social_login(request, github2_login)
        eq_(request.session['sociallogin_provider'], 'github')
Esempio n. 9
0
class KumaSocialAccountAdapterTestCase(UserTestCase):
    rf = RequestFactory()

    def setUp(self):
        """ extra setUp to make a working session """
        super(KumaSocialAccountAdapterTestCase, self).setUp()
        self.adapter = KumaSocialAccountAdapter()

    def test_pre_social_login_overwrites_session_var(self):
        """
        When a user logs in a second time, second login wins the session.

        https://bugzil.la/1055870
        """
        # Set up a pre-existing "Alternate" sign-in session
        request = self.rf.get("/")
        session = self.client.session
        session["sociallogin_provider"] = "alternate"
        session.save()
        request.session = session

        # Set up a in-process GitHub SocialLogin (unsaved)
        account = SocialAccount.objects.get(user__username="******")
        assert account.provider == "github"
        sociallogin = SocialLogin(account=account)

        # Verify the social_login receiver over-writes the provider
        # stored in the session
        self.adapter.pre_social_login(request, sociallogin)
        assert account.provider == request.session["sociallogin_provider"]

    def test_pre_social_login_error_for_unmatched_login(self):
        """
        When we suspect the signup form is used as a connection form, abort.

        https://bugzil.la/1063830
        """
        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username="******")
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get("/")
        session = self.client.session
        session["socialaccount_sociallogin"] = github_login.serialize()
        session.save()
        request.session = session
        messages = self.get_messages(request)

        # Set up an un-matching alternate SocialLogin for request
        other_account = SocialAccount(user=self.user_model(),
                                      provider="other",
                                      uid="*****@*****.**")
        other_login = SocialLogin(account=other_account)

        self.assertRaises(ImmediateHttpResponse, self.adapter.pre_social_login,
                          request, other_login)
        queued_messages = list(messages)
        assert len(queued_messages) == 1
        assert queued_messages[0].level == django_messages.ERROR

    def test_pre_social_login_matched_github_login(self):
        """
        When we detected a legacy Persona account, advise recovery of account.

        A user tries to sign in with GitHub, but their GitHub email matches
        an existing MDN account backed by Persona. They are prompted to
        recover the existing account.

        https://bugzil.la/1063830, happy path
        """
        # Set up a session-only GitHub SocialLogin
        # These are created at the start of the signup process, and saved on
        #  profile completion.
        github_account = SocialAccount.objects.get(user__username="******")
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        # Setup existing Persona SocialLogin for the same email
        SocialAccount.objects.create(user=github_account.user,
                                     provider="persona",
                                     uid=github_account.user.email)

        request = self.rf.get("/")
        session = self.client.session
        session["sociallogin_provider"] = "github"
        session["socialaccount_sociallogin"] = github_login.serialize()
        session.save()
        request.session = session

        # Verify the social_login receiver over-writes the provider
        # stored in the session
        self.adapter.pre_social_login(request, github_login)
        session = request.session
        assert "github" == session["sociallogin_provider"]

    def test_pre_social_login_matched_google_login(self):
        """
        When we detected a legacy Persona account, advise recovery of account.

        A user tries to sign in with Google, but their Google email matches
        an existing MDN account backed by Persona. They are prompted to
        recover the existing account.

        Same as above, but with Google instead of GitHub
        """
        # Set up a session-only Google SocialLogin
        # These are created at the start of the signup process, and saved on
        #  profile completion.
        google_account = SocialAccount.objects.get(user__username="******")
        google_login = SocialLogin(account=google_account,
                                   user=google_account.user)

        # Setup existing Persona SocialLogin for the same email
        SocialAccount.objects.create(user=google_account.user,
                                     provider="persona",
                                     uid=google_account.user.email)

        request = self.rf.get("/")
        session = self.client.session
        session["sociallogin_provider"] = "google"
        session["socialaccount_sociallogin"] = google_login.serialize()
        session.save()
        request.session = session

        # Verify the social_login receiver over-writes the provider
        # stored in the session
        self.adapter.pre_social_login(request, google_login)
        session = request.session
        assert "google" == session["sociallogin_provider"]

    def test_pre_social_login_same_provider(self):
        """
        pre_social_login passes if existing provider is the same.

        I'm not sure what the real-world counterpart of this is. Logging
        in with a different GitHub account? Needed for branch coverage.
        """

        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username="******")
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get("/")
        session = self.client.session
        session["sociallogin_provider"] = "github"
        session["socialaccount_sociallogin"] = github_login.serialize()
        session.save()
        request.session = session

        # Set up an un-matching GitHub SocialLogin for request
        github2_account = SocialAccount(user=self.user_model(),
                                        provider="github",
                                        uid=github_account.uid + "2")
        github2_login = SocialLogin(account=github2_account)

        self.adapter.pre_social_login(request, github2_login)
        assert "github" == request.session["sociallogin_provider"]

    def test_pre_social_login_banned_user(self):
        """A banned user is not allowed to login."""
        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username="******")
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get("/")
        session = self.client.session
        session["sociallogin_provider"] = "github"
        session["socialaccount_sociallogin"] = github_login.serialize()
        session.save()
        request.session = session
        request.user = AnonymousUser()
        request.LANGUAGE_CODE = "en-US"

        # Ban the user
        banned_by = User.objects.get(username="******")
        UserBan.objects.create(user=github_account.user,
                               by=banned_by,
                               reason="Banned by unit test.")

        with pytest.raises(ImmediateHttpResponse) as e_info:
            self.adapter.pre_social_login(request, github_login)
        resp = e_info.value.response
        assert b"Banned by unit test." in resp.content
        assert not resp.has_header("Vary")

        never_cache = ["no-cache", "no-store", "must-revalidate", "max-age=0"]
        assert set(resp["Cache-Control"].split(", ")) == set(never_cache)
Esempio n. 10
0
class KumaSocialAccountAdapterTestCase(UserTestCase):
    rf = RequestFactory()

    def setUp(self):
        """ extra setUp to make a working session """
        super(KumaSocialAccountAdapterTestCase, self).setUp()
        self.adapter = KumaSocialAccountAdapter()

    def test_pre_social_login_overwrites_session_var(self):
        """
        When a user logs in a second time, second login wins the session.

        https://bugzil.la/1055870
        """
        # Set up a pre-existing "Alternate" sign-in session
        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'alternate'
        session.save()
        request.session = session

        # Set up a in-process GitHub SocialLogin (unsaved)
        account = SocialAccount.objects.get(user__username='******')
        assert account.provider == 'github'
        sociallogin = SocialLogin(account=account)

        # Verify the social_login receiver over-writes the provider
        # stored in the session
        self.adapter.pre_social_login(request, sociallogin)
        eq_(account.provider,
            request.session['sociallogin_provider'],
            "receiver should have over-written sociallogin_provider "
            "session variable")

    def test_pre_social_login_error_for_unmatched_login(self):
        """
        When we suspect the signup form is used as a connection form, abort.

        https://bugzil.la/1063830
        """
        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get('/')
        session = self.client.session
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session
        messages = self.get_messages(request)

        # Set up an un-matching alternate SocialLogin for request
        other_account = SocialAccount(user=self.user_model(),
                                      provider='other',
                                      uid='*****@*****.**')
        other_login = SocialLogin(account=other_account)

        self.assertRaises(ImmediateHttpResponse,
                          self.adapter.pre_social_login, request, other_login)
        queued_messages = list(messages)
        eq_(len(queued_messages), 1)
        eq_(django_messages.ERROR, queued_messages[0].level)

    def test_pre_social_login_matched_login(self):
        """
        When we detected a legacy Persona account, advise recovery of account.

        A user tries to sign in with GitHub, but their GitHub email matches
        an existing MDN account backed by Persona. They are prompted to
        recover the existing account.

        https://bugzil.la/1063830, happy path
        """
        # Set up a session-only GitHub SocialLogin
        # These are created at the start of the signup process, and saved on
        #  profile completion.
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        # Setup existing Persona SocialLogin for the same email
        SocialAccount.objects.create(
            user=github_account.user,
            provider='persona',
            uid=github_account.user.email)

        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session

        # Verify the social_login receiver over-writes the provider
        # stored in the session
        self.adapter.pre_social_login(request, github_login)
        session = request.session
        eq_(session['sociallogin_provider'], 'github')

    def test_pre_social_login_same_provider(self):
        """
        pre_social_login passes if existing provider is the same.

        I'm not sure what the real-world counterpart of this is. Logging
        in with a different GitHub account? Needed for branch coverage.
        """

        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session

        # Set up an un-matching GitHub SocialLogin for request
        github2_account = SocialAccount(user=self.user_model(),
                                        provider='github',
                                        uid=github_account.uid + '2')
        github2_login = SocialLogin(account=github2_account)

        self.adapter.pre_social_login(request, github2_login)
        eq_(request.session['sociallogin_provider'], 'github')

    def test_pre_social_login_banned_user(self):
        """A banned user is not allowed to login."""
        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session
        request.user = AnonymousUser()
        request.LANGUAGE_CODE = 'en-US'

        # Ban the user
        banned_by = User.objects.get(username='******')
        UserBan.objects.create(user=github_account.user, by=banned_by,
                               reason='Banned by unit test.')

        with pytest.raises(ImmediateHttpResponse) as e_info:
            self.adapter.pre_social_login(request, github_login)
        resp = e_info.value.response
        assert 'Banned by unit test.' in resp.content
        assert not resp.has_header('Vary')
        never_cache = 'no-cache, no-store, must-revalidate, max-age=0'
        assert resp['Cache-Control'] == never_cache
Esempio n. 11
0
class KumaSocialAccountAdapterTestCase(UserTestCase):
    rf = RequestFactory()

    def setUp(self):
        """ extra setUp to make a working session """
        super(KumaSocialAccountAdapterTestCase, self).setUp()
        self.adapter = KumaSocialAccountAdapter()

    def test_pre_social_login_overwrites_session_var(self):
        """ https://bugzil.la/1055870 """
        # Set up a pre-existing GitHub sign-in session
        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session.save()
        request.session = session

        # Set up a Persona SocialLogin
        account = SocialAccount.objects.get(user__username='******')
        sociallogin = SocialLogin(account=account)

        # Verify the social_login receiver over-writes the provider
        # stored in the session
        self.adapter.pre_social_login(request, sociallogin)
        eq_(
            account.provider, request.session['sociallogin_provider'],
            "receiver should have over-written sociallogin_provider "
            "session variable")

    def test_pre_social_login_error_for_unmatched_login(self):
        """ https://bugzil.la/1063830 """

        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get('/')
        session = self.client.session
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session
        messages = self.get_messages(request)

        # Set up an un-matching Persona SocialLogin for request
        persona_account = SocialAccount(user=self.user_model(),
                                        provider='persona',
                                        uid='*****@*****.**')
        persona_login = SocialLogin(account=persona_account)

        self.assertRaises(ImmediateHttpResponse, self.adapter.pre_social_login,
                          request, persona_login)
        queued_messages = list(messages)
        eq_(len(queued_messages), 1)
        eq_(django_messages.ERROR, queued_messages[0].level)

    def test_pre_social_login_matched_login(self):
        """
        https://bugzil.la/1063830, happy path

        A user tries to sign in with GitHub, but their GitHub email matches
        an existing Persona-backed MDN account. They follow the prompt to login
        with Persona, and the accounts are connected.
        """

        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session

        # Set up an matching Persona SocialLogin for request
        persona_account = SocialAccount.objects.create(
            user=github_account.user,
            provider='persona',
            uid=github_account.user.email)
        persona_login = SocialLogin(account=persona_account)

        # Verify the social_login receiver over-writes the provider
        # stored in the session
        self.adapter.pre_social_login(request, persona_login)
        session = request.session
        eq_(session['sociallogin_provider'], 'persona')

    def test_pre_social_login_same_provider(self):
        """
        pre_social_login passes if existing provider is the same.

        I'm not sure what the real-world counterpart of this is. Logging
        in with a different GitHub account? Needed for branch coverage.
        """

        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account,
                                   user=github_account.user)

        request = self.rf.get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session

        # Set up an un-matching GitHub SocialLogin for request
        github2_account = SocialAccount(user=self.user_model(),
                                        provider='github',
                                        uid=github_account.uid + '2')
        github2_login = SocialLogin(account=github2_account)

        self.adapter.pre_social_login(request, github2_login)
        eq_(request.session['sociallogin_provider'], 'github')
Esempio n. 12
0
class KumaSocialAccountAdapterTestCase(TestCase):
    fixtures = ['test_users.json']

    def setUp(self):
        """ extra setUp to make a working session """
        from django.conf import settings
        engine = import_module(settings.SESSION_ENGINE)
        store = engine.SessionStore()
        store.save()
        self.client = LocalizingClient()
        self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key
        self.adapter = KumaSocialAccountAdapter()

    @attr('bug1055870')
    def test_pre_social_login_overwrites_session_var(self):
        """ https://bugzil.la/1055870 """
        # Set up a pre-existing GitHub sign-in session
        request = RequestFactory().get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session.save()
        request.session = session

        # Set up a Persona SocialLogin
        account = SocialAccount.objects.get(user__username='******')
        sociallogin = SocialLogin(account=account)

        # Verify the social_login receiver over-writes the provider
        # stored in the session
        self.adapter.pre_social_login(request, sociallogin)
        eq_(account.provider,
            request.session['sociallogin_provider'],
            "receiver should have over-written sociallogin_provider "
            "session variable")

    @attr('bug1063830')
    def test_pre_social_login_error_for_unmatched_login(self):
        """ https://bugzil.la/1063830 """

        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account)

        request = RequestFactory().get('/')
        session = self.client.session
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session

        # django 1.4 RequestFactory requests can't be used to test views that
        # call messages.add (https://code.djangoproject.com/ticket/17971)
        # FIXME: HACK from http://stackoverflow.com/q/11938164/571420
        messages = FallbackStorage(request)
        request._messages = messages

        # Set up an un-matching Persona SocialLogin for request
        persona_account = SocialAccount(user=User(), provider='persona',
                                        uid='*****@*****.**')
        persona_login = SocialLogin(account=persona_account)

        assert_raises(ImmediateHttpResponse,
                      self.adapter.pre_social_login, request, persona_login)
        for m in messages:
            eq_(django_messages.ERROR, m.level)
Esempio n. 13
0
class KumaSocialAccountAdapterTestCase(TestCase):
    fixtures = ['test_users.json']

    def setUp(self):
        """ extra setUp to make a working session """
        from django.conf import settings
        engine = import_module(settings.SESSION_ENGINE)
        store = engine.SessionStore()
        store.save()
        self.client = LocalizingClient()
        self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key
        self.adapter = KumaSocialAccountAdapter()

    @attr('bug1055870')
    def test_pre_social_login_overwrites_session_var(self):
        """ https://bugzil.la/1055870 """
        # Set up a pre-existing GitHub sign-in session
        request = RequestFactory().get('/')
        session = self.client.session
        session['sociallogin_provider'] = 'github'
        session.save()
        request.session = session

        # Set up a Persona SocialLogin
        account = SocialAccount.objects.get(user__username='******')
        sociallogin = SocialLogin(account=account)

        # Verify the social_login receiver over-writes the provider
        # stored in the session
        self.adapter.pre_social_login(request, sociallogin)
        eq_(
            account.provider, request.session['sociallogin_provider'],
            "receiver should have over-written sociallogin_provider "
            "session variable")

    @attr('bug1063830')
    def test_pre_social_login_error_for_unmatched_login(self):
        """ https://bugzil.la/1063830 """

        # Set up a GitHub SocialLogin in the session
        github_account = SocialAccount.objects.get(user__username='******')
        github_login = SocialLogin(account=github_account)

        request = RequestFactory().get('/')
        session = self.client.session
        session['socialaccount_sociallogin'] = github_login.serialize()
        session.save()
        request.session = session

        # django 1.4 RequestFactory requests can't be used to test views that
        # call messages.add (https://code.djangoproject.com/ticket/17971)
        # FIXME: HACK from http://stackoverflow.com/q/11938164/571420
        messages = FallbackStorage(request)
        request._messages = messages

        # Set up an un-matching Persona SocialLogin for request
        persona_account = SocialAccount(user=User(),
                                        provider='persona',
                                        uid='*****@*****.**')
        persona_login = SocialLogin(account=persona_account)

        assert_raises(ImmediateHttpResponse, self.adapter.pre_social_login,
                      request, persona_login)
        for m in messages:
            eq_(django_messages.ERROR, m.level)