from __future__ import absolute_import

from django.core.urlresolvers import reverse

from sentry.models import OrganizationMember, OrganizationMemberTeam, Team
from sentry.testutils import TestCase, PermissionTestCase


class CreateTeamPermissionTest(PermissionTestCase):
    def setUp(self):
        super(CreateTeamPermissionTest, self).setUp()
        self.path = reverse('sentry-create-team', args=[self.organization.slug])

    def test_teamless_admin_can_load(self):
        self.assert_teamless_admin_can_access(self.path)

    def test_team_admin_can_load(self):
        self.assert_team_admin_can_access(self.path)

    def test_member_cannot_load(self):
        self.assert_member_cannot_access(self.path)

    def test_owner_can_load(self):
        self.assert_owner_can_access(self.path)


class CreateTeamTest(TestCase):
    def test_renders_with_context(self):
        organization = self.create_organization()
        path = reverse('sentry-create-team', args=[organization.slug])
        self.login_as(self.user)
        resp = self.client.get(path)
        assert resp.status_code == 200
        self.assertTemplateUsed(resp, 'sentry/create-team.html')
        assert resp.context['organization'] == organization
        assert resp.context['form']

    def test_submission(self):
        organization = self.create_organization()
        path = reverse('sentry-create-team', args=[organization.slug])
        self.login_as(self.user)
        resp = self.client.post(path, {
            'name': 'bar',
        })
        assert resp.status_code == 302, resp.context['form'].errors

        team = Team.objects.get(organization=organization, name='bar')

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

        assert OrganizationMemberTeam.objects.filter(
            organizationmember=member,
            team=team,
            is_active=True,
        ).exists()

        redirect_uri = reverse('sentry-create-project', args=[organization.slug])
        assert resp['Location'] == 'http://testserver%s?team=%s' % (
            redirect_uri, team.slug)

    def test_admin_can_create_team(self):
        organization = self.create_organization()
        path = reverse('sentry-create-team', args=[organization.slug])

        admin = self.create_user('admin@example.com')
        self.create_member(
            organization=organization,
            user=admin,
            role='admin',
            teams=[],
        )

        self.login_as(admin)

        resp = self.client.post(path, {
            'name': 'bar',
        })
        assert resp.status_code == 302, resp.context['form'].errors

        assert Team.objects.filter(
            organization=organization,
            name='bar',
        ).exists()

[((368, 428), 'django.core.urlresolvers.reverse', 'reverse', (['"""sentry-create-team"""'], {'args': '[self.organization.slug]'}), "('sentry-create-team', args=[self.organization.slug])\n", (375, 428), False, 'from django.core.urlresolvers import reverse\n'), ((941, 996), 'django.core.urlresolvers.reverse', 'reverse', (['"""sentry-create-team"""'], {'args': '[organization.slug]'}), "('sentry-create-team', args=[organization.slug])\n", (948, 996), False, 'from django.core.urlresolvers import reverse\n'), ((1364, 1419), 'django.core.urlresolvers.reverse', 'reverse', (['"""sentry-create-team"""'], {'args': '[organization.slug]'}), "('sentry-create-team', args=[organization.slug])\n", (1371, 1419), False, 'from django.core.urlresolvers import reverse\n'), ((1615, 1670), 'sentry.models.Team.objects.get', 'Team.objects.get', ([], {'organization': 'organization', 'name': '"""bar"""'}), "(organization=organization, name='bar')\n", (1631, 1670), False, 'from sentry.models import OrganizationMember, OrganizationMemberTeam, Team\n'), ((1689, 1762), 'sentry.models.OrganizationMember.objects.get', 'OrganizationMember.objects.get', ([], {'user': 'self.user', 'organization': 'organization'}), '(user=self.user, organization=organization)\n', (1719, 1762), False, 'from sentry.models import OrganizationMember, OrganizationMemberTeam, Team\n'), ((1986, 2044), 'django.core.urlresolvers.reverse', 'reverse', (['"""sentry-create-project"""'], {'args': '[organization.slug]'}), "('sentry-create-project', args=[organization.slug])\n", (1993, 2044), False, 'from django.core.urlresolvers import reverse\n'), ((2259, 2314), 'django.core.urlresolvers.reverse', 'reverse', (['"""sentry-create-team"""'], {'args': '[organization.slug]'}), "('sentry-create-team', args=[organization.slug])\n", (2266, 2314), False, 'from django.core.urlresolvers import reverse\n'), ((1814, 1909), 'sentry.models.OrganizationMemberTeam.objects.filter', 'OrganizationMemberTeam.objects.filter', ([], {'organizationmember': 'member', 'team': 'team', 'is_active': '(True)'}), '(organizationmember=member, team=team,\n    is_active=True)\n', (1851, 1909), False, 'from sentry.models import OrganizationMember, OrganizationMemberTeam, Team\n'), ((2712, 2770), 'sentry.models.Team.objects.filter', 'Team.objects.filter', ([], {'organization': 'organization', 'name': '"""bar"""'}), "(organization=organization, name='bar')\n", (2731, 2770), False, 'from sentry.models import OrganizationMember, OrganizationMemberTeam, Team\n')]