"""
Tests for Django views.
"""

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import Client
from django.test import TestCase

from main.models import Project
from scripts.bootstrap_data import bootstrap_fake_data
from scripts.bootstrap_data import TEST_USERNAME
from scripts.bootstrap_data import TEST_PASSWORD


STATUS_CODE__SUCCESS = 200
STATUS_CODE__NOT_LOGGED_IN = 302
STATUS_CODE__NOT_FOUND = 404


class TestViews(TestCase):

    def setUp(self):
        bootstrap_fake_data()

        test_project = Project.objects.all()[0]
        ref_genome = test_project.referencegenome_set.get(label='test_genome')
        alignment_group = ref_genome.alignmentgroup_set.all()[0]

        # Urls that do not require the user to be logged in.
        self.no_login_required_urls = [
                reverse('genome_designer.main.views.home_view'),
        ]

        # Urls that require the user to be logged in, but do not try any
        # particular entity.
        self.non_specific_login_required_urls = [
                reverse('genome_designer.main.views.project_list_view'),
                reverse('genome_designer.main.views.project_create_view'),
        ]

        # Urls for a specific entity.
        self.specific_entity_urls = [
                # Project-specific views
                reverse('genome_designer.main.views.project_view',
                        args=(test_project.uid,)),

                # Reference genomes
                reverse('genome_designer.main.views.reference_genome_list_view',
                        args=(test_project.uid,)),
                reverse('genome_designer.main.views.reference_genome_view',
                        args=(test_project.uid, ref_genome.uid)),

                # Alignments
                reverse('genome_designer.main.views.alignment_list_view',
                        args=(test_project.uid,)),
                reverse('genome_designer.main.views.alignment_create_view',
                        args=(test_project.uid,)),
                reverse('genome_designer.main.views.alignment_view',
                        args=(test_project.uid, alignment_group.uid)),

                # Variant sets
                reverse('genome_designer.main.views.variant_set_list_view',
                        args=(test_project.uid,)),

                # Samples
                reverse('genome_designer.main.views.sample_list_view',
                        args=(test_project.uid,)),

                # Variants
                reverse('genome_designer.main.views.variant_list_view',
                        args=(test_project.uid, ref_genome.uid)),

                # Genes
                reverse('genome_designer.main.views.gene_list_view',
                        args=(test_project.uid,)),

                # GO terms
                reverse('genome_designer.main.views.goterm_list_view',
                        args=(test_project.uid,)),
        ]

        # The fake web browser client used to make requests.
        self.client = Client()

    def assert_url_response(self, url, expected_status_code):
        """Helper method that calls a URL and compares the response status
        code to expected_status_code.
        """
        response = self.client.get(url)
        self.assertEqual(expected_status_code, response.status_code,
                ("Simple url test failed for %s with status code %d. " +
                        "Expected status code %d.") % (
                                url, response.status_code, expected_status_code))


    def test_views__logged_out(self):
        """Tests calling the views without a logged in user.
        """
        login_error_urls = (self.non_specific_login_required_urls +
                self.specific_entity_urls)
        for url in login_error_urls:
            self.assert_url_response(url, STATUS_CODE__NOT_LOGGED_IN)

        success_urls = self.no_login_required_urls
        for url in success_urls:
            self.assert_url_response(url, STATUS_CODE__SUCCESS)


    def test_views__logged_in_owner(self):
        """Tests calling views with the owner logged in.
        """
        self.client.login(username=TEST_USERNAME, password=TEST_PASSWORD)
        all_urls = (self.no_login_required_urls +
                self.non_specific_login_required_urls +
                self.specific_entity_urls)
        for url in all_urls:
            self.assert_url_response(url, STATUS_CODE__SUCCESS)


    def test_views__logged_in_non_owner(self):
         """Tests calling views with the non-owner logged in.
         """
         OTHER_USERNAME = 'justtest'
         OTHER_PASSWORD = 'other_password'
         OTHER_EMAIL = 'justtest@me.com'
         user = User.objects.create_user(
                 OTHER_USERNAME, password=OTHER_PASSWORD, email=OTHER_EMAIL)
         self.client.login(username=OTHER_USERNAME, password=OTHER_PASSWORD)

         error_urls = self.specific_entity_urls
         for url in error_urls:
             self.assert_url_response(url, STATUS_CODE__NOT_FOUND)

         success_urls = (self.non_specific_login_required_urls +
                 self.no_login_required_urls)
         for url in success_urls:
             self.assert_url_response(url, STATUS_CODE__SUCCESS)

@@ -208,515 +208,888 @@
 ort 
-Project%0Afrom scripts.bootstrap_data import bootstrap_fake_data%0Afrom scripts.bootstrap_data import TEST_USERNAME%0Afrom scripts.bootstrap_data import TEST_PASSWORD%0A%0A%0ASTATUS_CODE__SUCCESS = 200%0ASTATUS_CODE__NOT_LOGGED_IN = 302%0ASTATUS_CODE__NOT_FOUND = 404%0A%0A%0Aclass TestViews(TestCase):%0A%0A    def setUp(self):%0A        bootstrap_fake_data()%0A%0A        test_project = Project.objects.all()%5B0%5D%0A        ref_genome = test_project.referencegenome_set.get(
+AlignmentGroup%0Afrom main.models import Project%0Afrom main.models import ReferenceGenome%0A%0ATEST_USERNAME = 'gmcdev'%0ATEST_PASSWORD = 'g3n3d3z'%0ATEST_EMAIL = 'gmcdev@genomedesigner.freelogy.org'%0A%0ASTATUS_CODE__SUCCESS = 200%0ASTATUS_CODE__NOT_LOGGED_IN = 302%0ASTATUS_CODE__NOT_FOUND = 404%0A%0A%0Aclass TestViews(TestCase):%0A%0A    def setUp(self):%0A        # Test models.%0A        user = User.objects.create_user(TEST_USERNAME, password=TEST_PASSWORD,%0A                email=TEST_EMAIL)%0A        test_project = Project.objects.create(owner=user.get_profile(),%0A                title='Test Project')%0A        ref_genome = ReferenceGenome.objects.create(project=test_project,%0A                
 label='
-test_
+ref
 genome'
-)%0A        alignment_group = ref_genome.a
+, num_chromosomes=1, num_bases=1000)%0A        alignment_group = AlignmentGroup.objects.create(%0A            label='Alignment 1',%0A            reference_genome=ref_genome,%0A            aligner=A
 lignment
 grou
@@ -1088,26 +1088,27 @@
 ment
-g
+G
 roup
-_set.all()%5B0%5D
+.ALIGNER.BWA)%0A
 %0A%0A  

Make test_views.py independent of bootstrap_data.py.