ernestophone.ens.fr/propositions/tests.py
2020-01-05 12:03:41 +01:00

101 lines
3.1 KiB
Python

from django.contrib.auth import get_user_model
from django.test import Client, TestCase
from django.urls import reverse_lazy, reverse
from gestion.models import ErnestoUser
from propositions.models import Prop
User = get_user_model()
def new_user(username):
u = User.objects.create_user(username=username)
ErnestoUser.objects.create(user=u, slug=username, is_ernesto=True)
return u
class PropositionCreateTest(TestCase):
url = reverse_lazy("propositions:create")
def test_anonymous_get(self):
response = Client().get(self.url)
self.assertRedirects(response, "/login?next={}".format(self.url))
def test_get(self):
user = new_user("toto")
client = Client()
client.force_login(user)
response = client.get(self.url)
self.assertEqual(response.status_code, 200)
def test_post(self):
user = new_user("toto")
client = Client()
client.force_login(user)
data = {"nom": "foo", "artiste": "bar", "lien": "example.com"}
client.post(self.url, data)
proposition = Prop.objects.all()
self.assertEqual(1, proposition.count())
proposition = proposition.get()
self.assertEqual(proposition.nom, "foo")
self.assertEqual(proposition.artiste, "bar")
self.assertEqual(proposition.lien, "http://example.com")
self.assertEqual(proposition.user, user.profile)
self.assertEqual(proposition.nboui, 0)
self.assertEqual(proposition.nbnon, 0)
class PropositionListTest(TestCase):
url = reverse_lazy("propositions:list")
def test_anonymous_get(self):
response = Client().get(self.url)
self.assertRedirects(response, "/login?next={}".format(self.url))
def test_get(self):
user = new_user("toto")
client = Client()
client.force_login(user)
response = client.get(self.url)
self.assertEqual(response.status_code, 200)
class ReponseTest(TestCase):
def setUp(self):
self.user = new_user("toto")
self.prop = Prop.objects.create(nom="foo", user=self.user.profile)
def _url(self, rep):
assert rep in ["oui", "non"]
return reverse("propositions:{}".format(rep), args=(self.prop.id,))
def test_anonymous_get(self):
client = Client()
url = reverse("propositions:oui", args=(self.prop.id,))
response = client.get(url)
self.assertRedirects(response, "/login?next={}".format(url))
url = reverse("propositions:non", args=(self.prop.id,))
response = client.get(url)
self.assertRedirects(response, "/login?next={}".format(url))
def test_get(self):
client = Client()
client.force_login(self.user)
# Oui
client.get(reverse("propositions:oui", args=(self.prop.id,)))
self.prop.refresh_from_db()
self.assertEqual(self.prop.nboui, 1)
self.assertEqual(self.prop.nbnon, 0)
# Oui
client.get(reverse("propositions:non", args=(self.prop.id,)))
self.prop.refresh_from_db()
self.assertEqual(self.prop.nboui, 0)
self.assertEqual(self.prop.nbnon, 1)