from django.test import TestCase

from builds.models import Version
from projects.models import Project


class RedirectTests(TestCase):
    fixtures = ["eric", "test_data"]

    def setUp(self):
        self.client.login(username='eric', password='test')
        self.client.post(
            '/dashboard/import/',
            {'repo_type': 'git', 'name': 'Pip',
             'tags': 'big, fucking, monkey', 'default_branch': '',
             'project_url': 'http://pip.rtfd.org',
             'repo': 'https://github.com/fail/sauce',
             'csrfmiddlewaretoken': '34af7c8a5ba84b84564403a280d9a9be',
             'default_version': 'latest',
             'privacy_level': 'public',
             'version_privacy_level': 'public',
             'description': 'wat',
             'documentation_type': 'sphinx'})
        pip = Project.objects.get(slug='pip')
        Version.objects.create(project=pip, identifier='latest',
                               verbose_name='latest', slug='latest',
                               active=True)

    def test_proper_url_no_slash(self):
        r = self.client.get('/docs/pip')
        # This is triggered by Django, so its a 301, basically just
        # APPEND_SLASH
        self.assertEqual(r.status_code, 301)
        self.assertEqual(r._headers['location'],
                         ('Location', 'http://testserver/docs/pip/'))
        r = self.client.get(r._headers['location'][1])
        self.assertEqual(r.status_code, 302)
        r = self.client.get(r._headers['location'][1])
        self.assertEqual(r.status_code, 200)

    def test_proper_url(self):
        r = self.client.get('/docs/pip/')
        self.assertEqual(r.status_code, 302)
        self.assertEqual(r._headers['location'],
                         ('Location', 'http://testserver/docs/pip/en/latest/'))
        r = self.client.get(r._headers['location'][1])
        self.assertEqual(r.status_code, 200)

    def test_inproper_url(self):
        r = self.client.get('/docs/pip/en/')
        self.assertEqual(r.status_code, 404)

    def test_proper_url_full(self):
        r = self.client.get('/docs/pip/en/latest/')
        self.assertEqual(r.status_code, 200)

    # Subdomains

    def test_proper_subdomain(self):
        r = self.client.get('/', HTTP_HOST='pip.readthedocs.org')
        self.assertEqual(r.status_code, 302)
        self.assertEqual(r._headers['location'],
                         ('Location', 'http://pip.readthedocs.org/en/latest/'))

    # Keep this around for now, until we come up with a nicer interface
    """
    def test_inproper_subdomain(self):
        r = self.client.get('/en/', HTTP_HOST='pip.readthedocs.org')
        self.assertEqual(r.status_code, 404)
    """

    def test_proper_subdomain_and_url(self):
        r = self.client.get('/en/latest/', HTTP_HOST='pip.readthedocs.org')
        self.assertEqual(r.status_code, 200)

    # Specific Page Redirects
    def test_proper_page_on_subdomain(self):
        r = self.client.get('/page/test.html', HTTP_HOST='pip.readthedocs.org')
        self.assertEqual(r.status_code, 302)
        self.assertEqual(r._headers['location'],
                         ('Location',
                          'http://pip.readthedocs.org/en/latest/test.html'))

    # Specific Page Redirects
    def test_proper_page_on_main_site(self):
        r = self.client.get('/docs/pip/page/test.html')
        self.assertEqual(r.status_code, 302)
        self.assertEqual(r._headers['location'],
                         ('Location',
                          'http://testserver/docs/pip/en/latest/test.html'))

    # This is currently turned off.
    """
    # Test _ -> -
    def test_underscore_redirect(self):
        r = self.client.get('/en/latest/',
                            HTTP_HOST='django_kong.readthedocs.org')
        self.assertEqual(r.status_code, 301)
        self.assertEqual(r._headers['location'],
                         ('Location',
                          'http://django-kong.readthedocs.org/en/latest/'))
    """

@@ -1276,36 +1276,27 @@
 ssertEqual(r
-._headers
 %5B'
-l
+L
 ocation'%5D,%0A 
@@ -1297,46 +1297,8 @@
 n'%5D,
-%0A                         ('Location',
  'ht
@@ -1324,17 +1324,16 @@
 s/pip/')
-)
 %0A       
@@ -1350,36 +1350,27 @@
 client.get(r
-._headers
 %5B'
-l
+L
 ocation'%5D%5B1%5D
@@ -1358,35 +1358,32 @@
 et(r%5B'Location'%5D
-%5B1%5D
 )%0A        self.a
@@ -1438,36 +1438,27 @@
 client.get(r
-._headers
 %5B'
-l
+L
 ocation'%5D%5B1%5D
@@ -1446,35 +1446,32 @@
 et(r%5B'Location'%5D
-%5B1%5D
 )%0A        self.a
@@ -1650,57 +1650,9 @@
 al(r
-._headers%5B'location'%5D,%0A                         (
+%5B
 'Loc
@@ -1649,32 +1649,33 @@
 ual(r%5B'Location'
+%5D
 , 'http://testse
@@ -1696,25 +1696,24 @@
 en/latest/')
-)
 %0A        r =
@@ -1730,28 +1730,19 @@
 nt.get(r
-._headers
 %5B'
-l
+L
 ocation'
@@ -1746,11 +1746,8 @@
 on'%5D
-%5B1%5D
 )%0A  
@@ -2244,57 +2244,9 @@
 al(r
-._headers%5B'location'%5D,%0A                         (
+%5B
 'Loc
@@ -2251,16 +2251,17 @@
 ocation'
+%5D
 , 'http:
@@ -2294,17 +2294,16 @@
 atest/')
-)
 %0A%0A    # 
@@ -2927,36 +2927,27 @@
 ssertEqual(r
-._headers
 %5B'
-l
+L
 ocation'%5D,%0A 
@@ -2949,46 +2949,8 @@
 '%5D,%0A
-                         ('Location',%0A
     
@@ -3012,33 +3012,32 @@
 test/test.html')
-)
 %0A%0A    # Specific
@@ -3220,36 +3220,27 @@
 ssertEqual(r
-._headers
 %5B'
-l
+L
 ocation'%5D,%0A 
@@ -3242,46 +3242,8 @@
 '%5D,%0A
-                         ('Location',%0A
     
@@ -3313,17 +3313,16 @@
 t.html')
-)
 %0A%0A    # 
@@ -3611,17 +3611,17 @@
 eaders%5B'
-l
+L
 ocation'

Simplify reading of headers in RedirectTests.