# coding: utf-8
from django.contrib.auth.models import User
from django.http import HttpRequest
from django.test import TestCase, Client
from django.utils import timezone

from factories import factories as f

from karspexet.economy import views

class TestOverview(TestCase):
    def test_only_staff_can_access_overview_page(self):
        client = Client()
        response = client.get("/economy/")
        assert response.status_code == 302

        user = User.objects.create_user("ture", email="ture@example.com")
        user.set_password("test")
        user.is_staff = True
        user.save()

        assert client.login(username="ture", password="test")

        response = client.get("/economy/")
        assert "Föreställningsöversikt" in response.content.decode("utf-8")


class TestShowDetail(TestCase):
    def test_only_staff_can_access_detail_page(self):
        production = f.CreateProduction()
        venue = f.CreateVenue()
        seating_group = f.CreateSeatingGroup(venue=venue)
        seat = f.CreateSeat(group=seating_group)
        show = f.CreateShow(date=timezone.now(), production=production, venue=venue)

        client = Client()
        response = client.get(f"/economy/{show.id}")
        assert response.status_code == 302

        user = User.objects.create_user("ture", email="ture@example.com")
        user.set_password("test")
        user.is_staff = True
        user.save()

        assert client.login(username="ture", password="test")

        response = client.get(f"/economy/{show.id}")
        assert "Föreställningsöversikt" in response.content.decode("utf-8")

@@ -262,32 +262,85 @@
 view(TestCase):%0A
+    def setUp(self):%0A        self.client = Client()%0A%0A
     def test_onl
@@ -383,34 +383,8 @@
 f):%0A
-        client = Client()%0A
     
@@ -390,32 +390,37 @@
      response = 
+self.
 client.get(%22/eco
@@ -483,173 +483,63 @@
     
-user = User.objects.create_user(%22ture%22, email=%22ture@example.com%22)%0A        user.set_password(%22test%22)%0A        user.is_staff = True%0A        user.save()%0A%0A        assert 
+_create_staff_user(%22ture%22, %22test%22)%0A        assert self.
 clie
@@ -593,32 +593,37 @@
      response = 
+self.
 client.get(%22/eco
@@ -732,32 +732,85 @@
 tail(TestCase):%0A
+    def setUp(self):%0A        self.client = Client()%0A%0A
     def test_onl
@@ -1118,34 +1118,8 @@
 e)%0A%0A
-        client = Client()%0A
     
@@ -1125,32 +1125,37 @@
      response = 
+self.
 client.get(f%22/ec
@@ -1228,173 +1228,63 @@
     
-user = User.objects.create_user(%22ture%22, email=%22ture@example.com%22)%0A        user.set_password(%22test%22)%0A        user.is_staff = True%0A        user.save()%0A%0A        assert 
+_create_staff_user(%22ture%22, %22test%22)%0A        assert self.
 clie
@@ -1346,16 +1346,21 @@
 ponse = 
+self.
 client.g
@@ -1441,28 +1441,234 @@
 nse.content.decode(%22utf-8%22)%0A
+%0Adef _create_staff_user(username, password):%0A    user = User.objects.create_user(username, email=%22ture@example.com%22)%0A    user.set_password(password)%0A    user.is_staff = True%0A    user.save()%0A    return user%0A

Tidy up economy view tests a bit