ernestophone.ens.fr/propositions/tests.py

65 lines
1.9 KiB
Python
Raw Normal View History

from django.contrib.auth import get_user_model
from django.test import Client, TestCase
from django.urls import reverse_lazy
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)
2020-01-05 00:32:02 +01:00
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)