from django.test import TestCase
from django.test import Client
from django.contrib.auth.models import User


from squad.core import models
from squad.core.tasks import ReceiveTestRun


class FrontendTest(TestCase):

    def setUp(self):
        self.group = models.Group.objects.create(slug='mygroup')
        self.project = self.group.projects.create(slug='myproject')
        self.user = User.objects.create(username='theuser')

        self.client = Client()
        self.client.force_login(self.user)

        ReceiveTestRun(self.project)(
            version='1.0',
            environment_slug='myenv',
            log_file='log file contents ...',
            tests_file='{}',
            metrics_file='{}',
            metadata_file='{ "job_id" : "1" }',
        )
        self.test_run = models.TestRun.objects.last()

    def hit(self, url, expected_status=200):
        response = self.client.get(url)
        self.assertEqual(expected_status, response.status_code)
        return response

    def test_home(self):
        self.hit('/')

    def test_group(self):
        self.hit('/mygroup/')

    def test_group_404(self):
        self.hit('/unexistinggroup/', 404)

    def test_project(self):
        self.hit('/mygroup/myproject/')

    def test_project_badge(self):
        self.hit('/mygroup/myproject/badge')

    def test_project_404(self):
        self.hit('/mygroup/unexistingproject/', 404)

    def test_project_no_build(self):
        self.project.builds.all().delete()
        self.hit('/mygroup/myproject/')

    def test_builds(self):
        self.hit('/mygroup/myproject/builds/')

    def test_build_404(self):
        self.hit('/mygroup/myproject/build/999/', 404)

    def test_build_latest_finished(self):
        self.hit('/mygroup/myproject/build/latest-finished/')

    def test_build_latest_finished_404(self):
        self.group.projects.create(slug='otherproject')
        self.hit('/mygroup/otherproject/')
        self.hit('/mygroup/otherproject/build/latest-finished/', 404)

    def test_test_run_build_404(self):
        self.hit('/mygroup/myproject/build/2.0.missing/testrun/999/', 404)

    def test_test_run_404(self):
        self.hit('/mygroup/myproject/build/1.0/testrun/999/', 404)

    def test_attachment(self):
        data = bytes('text file', 'utf-8')
        self.test_run.attachments.create(filename='foo.txt', data=data, length=len(data))
        response = self.hit('/mygroup/myproject/build/1.0/testrun/1/attachments/foo.txt')
        self.assertEqual('text/plain', response['Content-Type'])

    def test_log(self):
        response = self.hit('/mygroup/myproject/build/1.0/testrun/1/log')
        self.assertEqual('text/plain', response['Content-Type'])

    def test_no_log(self):
        self.test_run.log_file = None
        self.test_run.save()

        response = self.client.get('/mygroup/myproject/build/1.0/testrun/1/log')
        self.assertEqual(404, response.status_code)

    def test_tests(self):
        response = self.hit('/mygroup/myproject/build/1.0/testrun/1/tests')
        self.assertEqual('application/json', response['Content-Type'])

    def test_metrics(self):
        response = self.hit('/mygroup/myproject/build/1.0/testrun/1/metrics')
        self.assertEqual('application/json', response['Content-Type'])

    def test_metadata(self):
        response = self.hit('/mygroup/myproject/build/1.0/testrun/1/metadata')
        self.assertEqual('application/json', response['Content-Type'])

@@ -820,16 +820,165 @@
 s.last()
+%0A        attachment_data = %22bar%22.encode()%0A        self.test_run.attachments.create(filename=%22foo%22, data=attachment_data, length=len(attachment_data))
 %0A%0A    de
@@ -1189,24 +1189,283 @@
 f.hit('/')%0A%0A
+    def test_compare(self):%0A        self.hit('/_/compare/')%0A%0A    def test_comparetest(self):%0A        self.hit('/_/comparetest/')%0A%0A    def test_settings(self):%0A        # check if redirection to /_/settings/profile/ works%0A        self.hit('/_/settings/', 302)%0A%0A
     def test
@@ -1728,24 +1728,209 @@
 ct/badge')%0A%0A
+    def test_project_metrics(self):%0A        self.hit('/mygroup/myproject/metrics/')%0A%0A    def test_project_test_history_404(self):%0A        self.hit('/mygroup/myproject/tests/foo', 404)%0A%0A
     def test
@@ -2221,32 +2221,32 @@
 uild_404(self):%0A
-
         self.hit
@@ -2277,32 +2277,234 @@
 ld/999/', 404)%0A%0A
+    def test_build_tests_404(self):%0A        self.hit('/mygroup/myproject/build/999/tests/', 404)%0A%0A    def test_build_testjobs_404(self):%0A        self.hit('/mygroup/myproject/build/999/testjobs/', 404)%0A%0A
     def test_bui

add tests to cover all basic URLs