from __future__ import absolute_import

from six.moves.urllib.parse import urlencode
from django.test import RequestFactory
from django.contrib.auth.models import AnonymousUser

from sentry.auth.helper import handle_new_user
from sentry.models import AuthProvider, InviteStatus, OrganizationMember
from sentry.testutils import TestCase
from sentry.utils.compat import mock


class HandleNewUserTest(TestCase):
    @mock.patch("sentry.analytics.record")
    def test_simple(self, mock_record):
        provider = "dummy"
        request = RequestFactory().post("/auth/sso/")
        request.user = AnonymousUser()

        auth_provider = AuthProvider.objects.create(
            organization=self.organization, provider=provider
        )
        identity = {"id": "1234", "email": "test@example.com", "name": "Morty"}

        auth_identity = handle_new_user(auth_provider, self.organization, request, identity)
        user = auth_identity.user

        assert user.email == identity["email"]
        assert OrganizationMember.objects.filter(organization=self.organization, user=user).exists()

        signup_record = [r for r in mock_record.call_args_list if r[0][0] == "user.signup"]
        assert signup_record == [
            mock.call(
                "user.signup", user_id=user.id, source="sso", provider=provider, referrer="in-app"
            )
        ]

    def test_associated_existing_member_invite_by_email(self):
        request = RequestFactory().post("/auth/sso/")
        request.user = AnonymousUser()

        provider = AuthProvider.objects.create(organization=self.organization, provider="dummy")
        identity = {"id": "1234", "email": "test@example.com", "name": "Morty"}

        member = OrganizationMember.objects.create(
            organization=self.organization, email=identity["email"]
        )

        auth_identity = handle_new_user(provider, self.organization, request, identity)

        assigned_member = OrganizationMember.objects.get(
            organization=self.organization, user=auth_identity.user
        )

        assert assigned_member.id == member.id

    def test_associated_existing_member_invite_request(self):
        request = RequestFactory().post("/auth/sso/")
        request.user = AnonymousUser()

        provider = AuthProvider.objects.create(organization=self.organization, provider="dummy")
        identity = {"id": "1234", "email": "test@example.com", "name": "Morty"}

        member = self.create_member(
            organization=self.organization,
            email=identity["email"],
            invite_status=InviteStatus.REQUESTED_TO_BE_INVITED.value,
        )

        auth_identity = handle_new_user(provider, self.organization, request, identity)

        assert OrganizationMember.objects.filter(
            organization=self.organization,
            user=auth_identity.user,
            invite_status=InviteStatus.APPROVED.value,
        ).exists()

        assert not OrganizationMember.objects.filter(id=member.id).exists()

    def test_associate_pending_invite(self):
        provider = AuthProvider.objects.create(organization=self.organization, provider="dummy")
        identity = {"id": "1234", "email": "test@example.com", "name": "Morty"}

        # The org member invite should have a non matching email, but the
        # member id and token will match from the cookie, allowing association
        member = OrganizationMember.objects.create(
            organization=self.organization, email="different.email@example.com", token="abc"
        )

        request = RequestFactory().post("/auth/sso/")
        request.user = AnonymousUser()
        request.COOKIES["pending-invite"] = urlencode(
            {"memberId": member.id, "token": member.token, "url": ""}
        )

        auth_identity = handle_new_user(provider, self.organization, request, identity)

        assigned_member = OrganizationMember.objects.get(
            organization=self.organization, user=auth_identity.user
        )

        assert assigned_member.id == member.id

[((14, 5, 14, 42), 'sentry.utils.compat.mock.patch', 'mock.patch', ({(14, 16, 14, 41): '"""sentry.analytics.record"""'}, {}), "('sentry.analytics.record')", False, 'from sentry.utils.compat import mock\n'), ((18, 23, 18, 38), 'django.contrib.auth.models.AnonymousUser', 'AnonymousUser', ({}, {}), '()', False, 'from django.contrib.auth.models import AnonymousUser\n'), ((20, 24, 22, 9), 'sentry.models.AuthProvider.objects.create', 'AuthProvider.objects.create', (), '', False, 'from sentry.models import AuthProvider, InviteStatus, OrganizationMember\n'), ((25, 24, 25, 92), 'sentry.auth.helper.handle_new_user', 'handle_new_user', ({(25, 40, 25, 53): 'auth_provider', (25, 55, 25, 72): 'self.organization', (25, 74, 25, 81): 'request', (25, 83, 25, 91): 'identity'}, {}), '(auth_provider, self.organization, request, identity)', False, 'from sentry.auth.helper import handle_new_user\n'), ((40, 23, 40, 38), 'django.contrib.auth.models.AnonymousUser', 'AnonymousUser', ({}, {}), '()', False, 'from django.contrib.auth.models import AnonymousUser\n'), ((42, 19, 42, 96), 'sentry.models.AuthProvider.objects.create', 'AuthProvider.objects.create', (), '', False, 'from sentry.models import AuthProvider, InviteStatus, OrganizationMember\n'), ((45, 17, 47, 9), 'sentry.models.OrganizationMember.objects.create', 'OrganizationMember.objects.create', (), '', False, 'from sentry.models import AuthProvider, InviteStatus, OrganizationMember\n'), ((49, 24, 49, 87), 'sentry.auth.helper.handle_new_user', 'handle_new_user', ({(49, 40, 49, 48): 'provider', (49, 50, 49, 67): 'self.organization', (49, 69, 49, 76): 'request', (49, 78, 49, 86): 'identity'}, {}), '(provider, self.organization, request, identity)', False, 'from sentry.auth.helper import handle_new_user\n'), ((51, 26, 53, 9), 'sentry.models.OrganizationMember.objects.get', 'OrganizationMember.objects.get', (), '', False, 'from sentry.models import AuthProvider, InviteStatus, OrganizationMember\n'), ((59, 23, 59, 38), 'django.contrib.auth.models.AnonymousUser', 'AnonymousUser', ({}, {}), '()', False, 'from django.contrib.auth.models import AnonymousUser\n'), ((61, 19, 61, 96), 'sentry.models.AuthProvider.objects.create', 'AuthProvider.objects.create', (), '', False, 'from sentry.models import AuthProvider, InviteStatus, OrganizationMember\n'), ((70, 24, 70, 87), 'sentry.auth.helper.handle_new_user', 'handle_new_user', ({(70, 40, 70, 48): 'provider', (70, 50, 70, 67): 'self.organization', (70, 69, 70, 76): 'request', (70, 78, 70, 86): 'identity'}, {}), '(provider, self.organization, request, identity)', False, 'from sentry.auth.helper import handle_new_user\n'), ((81, 19, 81, 96), 'sentry.models.AuthProvider.objects.create', 'AuthProvider.objects.create', (), '', False, 'from sentry.models import AuthProvider, InviteStatus, OrganizationMember\n'), ((86, 17, 88, 9), 'sentry.models.OrganizationMember.objects.create', 'OrganizationMember.objects.create', (), '', False, 'from sentry.models import AuthProvider, InviteStatus, OrganizationMember\n'), ((91, 23, 91, 38), 'django.contrib.auth.models.AnonymousUser', 'AnonymousUser', ({}, {}), '()', False, 'from django.contrib.auth.models import AnonymousUser\n'), ((92, 44, 94, 9), 'six.moves.urllib.parse.urlencode', 'urlencode', ({(93, 12, 93, 69): "{'memberId': member.id, 'token': member.token, 'url': ''}"}, {}), "({'memberId': member.id, 'token': member.token, 'url': ''})", False, 'from six.moves.urllib.parse import urlencode\n'), ((96, 24, 96, 87), 'sentry.auth.helper.handle_new_user', 'handle_new_user', ({(96, 40, 96, 48): 'provider', (96, 50, 96, 67): 'self.organization', (96, 69, 96, 76): 'request', (96, 78, 96, 86): 'identity'}, {}), '(provider, self.organization, request, identity)', False, 'from sentry.auth.helper import handle_new_user\n'), ((98, 26, 100, 9), 'sentry.models.OrganizationMember.objects.get', 'OrganizationMember.objects.get', (), '', False, 'from sentry.models import AuthProvider, InviteStatus, OrganizationMember\n'), ((17, 18, 17, 34), 'django.test.RequestFactory', 'RequestFactory', ({}, {}), '()', False, 'from django.test import RequestFactory\n'), ((29, 15, 29, 91), 'sentry.models.OrganizationMember.objects.filter', 'OrganizationMember.objects.filter', (), '', False, 'from sentry.models import AuthProvider, InviteStatus, OrganizationMember\n'), ((33, 12, 35, 13), 'sentry.utils.compat.mock.call', 'mock.call', (), '', False, 'from sentry.utils.compat import mock\n'), ((39, 18, 39, 34), 'django.test.RequestFactory', 'RequestFactory', ({}, {}), '()', False, 'from django.test import RequestFactory\n'), ((58, 18, 58, 34), 'django.test.RequestFactory', 'RequestFactory', ({}, {}), '()', False, 'from django.test import RequestFactory\n'), ((72, 15, 76, 9), 'sentry.models.OrganizationMember.objects.filter', 'OrganizationMember.objects.filter', (), '', False, 'from sentry.models import AuthProvider, InviteStatus, OrganizationMember\n'), ((90, 18, 90, 34), 'django.test.RequestFactory', 'RequestFactory', ({}, {}), '()', False, 'from django.test import RequestFactory\n'), ((78, 19, 78, 66), 'sentry.models.OrganizationMember.objects.filter', 'OrganizationMember.objects.filter', (), '', False, 'from sentry.models import AuthProvider, InviteStatus, OrganizationMember\n')]