from __future__ import absolute_import

from django.core.urlresolvers import reverse

from sentry.models import Project
from sentry.testutils import TestCase, PermissionTestCase


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

    def test_teamless_admin_cannot_load(self):
        self.assert_teamless_admin_cannot_access(self.path)

    def test_team_member_can_load(self):
        self.assert_team_member_cannot_access(self.path)

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

    def test_org_member_cannot_load(self):
        self.assert_org_member_cannot_access(self.path)

    def test_org_admin_can_load(self):
        self.assert_org_admin_can_access(self.path)


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

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

        project = Project.objects.get(team__organization=organization, name='bar')

        assert project.platform == 'python'
        assert project.team == team

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