<reponame>ChadKillingsworth/sentry
from __future__ import absolute_import

from django.core.urlresolvers import reverse
from mock import patch

from sentry.models import (
    AuthProvider, OrganizationMember, OrganizationMemberType
)
from sentry.testutils import APITestCase


class UpdateOrganizationMemberTest(APITestCase):
    @patch('sentry.models.OrganizationMember.send_invite_email')
    def test_reinvite_pending_member(self, mock_send_invite_email):
        self.login_as(user=self.user)

        organization = self.create_organization(name='foo', owner=self.user)
        member_om = OrganizationMember.objects.create(
            organization=organization,
            email='<EMAIL>',
            type=OrganizationMemberType.MEMBER,
            has_global_access=False,
        )

        path = reverse('sentry-api-0-organization-member-details', args=[organization.slug, member_om.id])

        self.login_as(self.user)

        resp = self.client.put(path, data={'reinvite': 1})

        assert resp.status_code == 204
        mock_send_invite_email.assert_called_once_with()

    @patch('sentry.models.OrganizationMember.send_sso_link_email')
    def test_reinvite_sso_link(self, mock_send_sso_link_email):
        self.login_as(user=self.user)

        organization = self.create_organization(name='foo', owner=self.user)
        member = self.create_user('<EMAIL>')
        member_om = OrganizationMember.objects.create(
            organization=organization,
            user=member,
            type=OrganizationMemberType.MEMBER,
            has_global_access=False,
        )
        AuthProvider.objects.create(organization=organization, provider='dummy')

        path = reverse('sentry-api-0-organization-member-details', args=[organization.slug, member_om.id])

        self.login_as(self.user)

        resp = self.client.put(path, data={'reinvite': 1})

        assert resp.status_code == 204
        mock_send_sso_link_email.assert_called_once_with()

    @patch('sentry.models.OrganizationMember.send_sso_link_email')
    def test_cannot_reinvite_normal_member(self, mock_send_sso_link_email):
        self.login_as(user=self.user)

        organization = self.create_organization(name='foo', owner=self.user)
        member = self.create_user('<EMAIL>')
        member_om = OrganizationMember.objects.create(
            organization=organization,
            user=member,
            type=OrganizationMemberType.MEMBER,
            has_global_access=False,
        )

        path = reverse('sentry-api-0-organization-member-details', args=[organization.slug, member_om.id])

        self.login_as(self.user)

        resp = self.client.put(path, data={'reinvite': 1})

        assert resp.status_code == 400


class DeleteOrganizationMemberTest(APITestCase):
    def test_simple(self):
        self.login_as(user=self.user)

        organization = self.create_organization(name='foo', owner=self.user)
        member = self.create_user('<EMAIL>')

        member_om = OrganizationMember.objects.create(
            organization=organization,
            user=member,
            type=OrganizationMemberType.MEMBER,
            has_global_access=False,
        )

        path = reverse('sentry-api-0-organization-member-details', args=[organization.slug, member_om.id])

        self.login_as(self.user)

        resp = self.client.delete(path)

        assert resp.status_code == 204

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

    def test_cannot_delete_only_owner(self):
        self.login_as(user=self.user)

        organization = self.create_organization(name='foo', owner=self.user)

        # create a pending member, which shouldn't be counted in the checks
        OrganizationMember.objects.create(
            organization=organization,
            type=OrganizationMemberType.OWNER,
            email='<EMAIL>',
        )

        owner_om = OrganizationMember.objects.get(
            organization=organization,
            user=self.user,
        )

        path = reverse('sentry-api-0-organization-member-details', args=[organization.slug, owner_om.id])

        self.login_as(self.user)

        resp = self.client.delete(path)

        assert resp.status_code == 403

        assert OrganizationMember.objects.filter(id=owner_om.id).exists()

from __future__ import absolute_import

from django.core.urlresolvers import reverse
from mock import patch

from sentry.models import (
    AuthProvider, OrganizationMember, OrganizationMemberType
)
from sentry.testutils import APITestCase


class UpdateOrganizationMemberTest(APITestCase):
    @patch('sentry.models.OrganizationMember.send_invite_email')
    def test_reinvite_pending_member(self, mock_send_invite_email):
        self.login_as(user=self.user)

        organization = self.create_organization(name='foo', owner=self.user)
        member_om = OrganizationMember.objects.create(
            organization=organization,
            email='<EMAIL>',
            type=OrganizationMemberType.MEMBER,
            has_global_access=False,
        )

        path = reverse('sentry-api-0-organization-member-details', args=[organization.slug, member_om.id])

        self.login_as(self.user)

        resp = self.client.put(path, data={'reinvite': 1})

        assert resp.status_code == 204
        mock_send_invite_email.assert_called_once_with()

    @patch('sentry.models.OrganizationMember.send_sso_link_email')
    def test_reinvite_sso_link(self, mock_send_sso_link_email):
        self.login_as(user=self.user)

        organization = self.create_organization(name='foo', owner=self.user)
        member = self.create_user('<EMAIL>')
        member_om = OrganizationMember.objects.create(
            organization=organization,
            user=member,
            type=OrganizationMemberType.MEMBER,
            has_global_access=False,
        )
        AuthProvider.objects.create(organization=organization, provider='dummy')

        path = reverse('sentry-api-0-organization-member-details', args=[organization.slug, member_om.id])

        self.login_as(self.user)

        resp = self.client.put(path, data={'reinvite': 1})

        assert resp.status_code == 204
        mock_send_sso_link_email.assert_called_once_with()

    @patch('sentry.models.OrganizationMember.send_sso_link_email')
    def test_cannot_reinvite_normal_member(self, mock_send_sso_link_email):
        self.login_as(user=self.user)

        organization = self.create_organization(name='foo', owner=self.user)
        member = self.create_user('<EMAIL>')
        member_om = OrganizationMember.objects.create(
            organization=organization,
            user=member,
            type=OrganizationMemberType.MEMBER,
            has_global_access=False,
        )

        path = reverse('sentry-api-0-organization-member-details', args=[organization.slug, member_om.id])

        self.login_as(self.user)

        resp = self.client.put(path, data={'reinvite': 1})

        assert resp.status_code == 400


class DeleteOrganizationMemberTest(APITestCase):
    def test_simple(self):
        self.login_as(user=self.user)

        organization = self.create_organization(name='foo', owner=self.user)
        member = self.create_user('<EMAIL>')

        member_om = OrganizationMember.objects.create(
            organization=organization,
            user=member,
            type=OrganizationMemberType.MEMBER,
            has_global_access=False,
        )

        path = reverse('sentry-api-0-organization-member-details', args=[organization.slug, member_om.id])

        self.login_as(self.user)

        resp = self.client.delete(path)

        assert resp.status_code == 204

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

    def test_cannot_delete_only_owner(self):
        self.login_as(user=self.user)

        organization = self.create_organization(name='foo', owner=self.user)

        # create a pending member, which shouldn't be counted in the checks
        OrganizationMember.objects.create(
            organization=organization,
            type=OrganizationMemberType.OWNER,
            email='<EMAIL>',
        )

        owner_om = OrganizationMember.objects.get(
            organization=organization,
            user=self.user,
        )

        path = reverse('sentry-api-0-organization-member-details', args=[organization.slug, owner_om.id])

        self.login_as(self.user)

        resp = self.client.delete(path)

        assert resp.status_code == 403

        assert OrganizationMember.objects.filter(id=owner_om.id).exists()
# create a pending member, which shouldn't be counted in the checks