from __future__ import absolute_import

from django.core.urlresolvers import reverse

from sentry.models import File, Release, ReleaseFile
from sentry.testutils import APITestCase


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

        project = self.create_project(name='foo')
        release = Release.objects.create(
            project=project,
            version='1',
        )

        url = reverse('sentry-api-0-release-details', kwargs={
            'organization_slug': project.organization.slug,
            'project_slug': project.slug,
            'version': release.version,
        })
        response = self.client.get(url)

        assert response.status_code == 200, response.content
        assert response.data['version'] == release.version

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

        project = self.create_project(name='foo')
        release = Release.objects.create(
            project=project,
            version='release/1',
        )

        url = reverse('sentry-api-0-release-details', kwargs={
            'organization_slug': project.organization.slug,
            'project_slug': project.slug,
            'version': 'release%2F1',
        })
        response = self.client.get(url)

        assert response.status_code == 200, response.content
        assert response.data['version'] == release.version


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

        project = self.create_project(name='foo')
        release = Release.objects.create(
            project=project,
            version='1',
        )

        url = reverse('sentry-api-0-release-details', kwargs={
            'organization_slug': project.organization.slug,
            'project_slug': project.slug,
            'version': release.version,
        })
        response = self.client.put(url, {'ref': 'master'})

        assert response.status_code == 200, response.content
        assert response.data['version'] == release.version

        release = Release.objects.get(id=release.id)
        assert release.ref == 'master'


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

        project = self.create_project(name='foo')
        release = Release.objects.create(
            project=project,
            version='1',
        )
        ReleaseFile.objects.create(
            project=project,
            release=release,
            file=File.objects.create(
                path='http://example.com',
                name='application.js',
                type='release.file',
            ),
            name='http://example.com/application.js'
        )

        url = reverse('sentry-api-0-release-details', kwargs={
            'organization_slug': project.organization.slug,
            'project_slug': project.slug,
            'version': release.version,
        })
        response = self.client.delete(url)

        assert response.status_code == 204, response.content

        assert not Release.objects.filter(id=release.id).exists()

@@ -819,630 +819,8 @@
 on%0A%0A
-    def test_with_slash_in_version(self):%0A        self.login_as(user=self.user)%0A%0A        project = self.create_project(name='foo')%0A        release = Release.objects.create(%0A            project=project,%0A            version='release/1',%0A        )%0A%0A        url = reverse('sentry-api-0-release-details', kwargs=%7B%0A            'organization_slug': project.organization.slug,%0A            'project_slug': project.slug,%0A            'version': 'release%252F1',%0A        %7D)%0A        response = self.client.get(url)%0A%0A        assert response.status_code == 200, response.content%0A        assert response.data%5B'version'%5D == release.version%0A%0A
 %0Acla

Remove bad test which assumed incorrect behavior for quoting of versions.