2020-01-05 00:18:31 +01:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.test import Client, TestCase
|
2020-01-05 12:02:24 +01:00
|
|
|
from django.urls import reverse_lazy, reverse
|
2020-01-05 00:18:31 +01:00
|
|
|
|
|
|
|
from gestion.models import ErnestoUser
|
2020-01-05 15:58:16 +01:00
|
|
|
from propositions.models import Answer, Proposition
|
2020-01-05 00:18:31 +01:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2020-01-05 17:27:31 +01:00
|
|
|
def test_anonymous_cannot_get(self):
|
2020-01-05 00:18:31 +01:00
|
|
|
response = Client().get(self.url)
|
|
|
|
self.assertRedirects(response, "/login?next={}".format(self.url))
|
|
|
|
|
2020-01-05 17:27:31 +01:00
|
|
|
def test_ernesto_user_can_get(self):
|
2020-01-05 00:18:31 +01:00
|
|
|
user = new_user("toto")
|
|
|
|
client = Client()
|
|
|
|
client.force_login(user)
|
|
|
|
|
|
|
|
response = client.get(self.url)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2020-01-05 17:27:31 +01:00
|
|
|
def test_ernesto_user_can_post(self):
|
2020-01-05 00:18:31 +01:00
|
|
|
user = new_user("toto")
|
|
|
|
client = Client()
|
|
|
|
client.force_login(user)
|
|
|
|
|
2020-01-05 15:58:16 +01:00
|
|
|
data = {"name": "foo", "artist": "bar", "link": "example.com"}
|
2020-01-05 00:18:31 +01:00
|
|
|
client.post(self.url, data)
|
2020-01-05 15:58:16 +01:00
|
|
|
proposition = Proposition.objects.all()
|
2020-01-05 00:18:31 +01:00
|
|
|
self.assertEqual(1, proposition.count())
|
|
|
|
|
|
|
|
proposition = proposition.get()
|
2020-01-05 15:58:16 +01:00
|
|
|
self.assertEqual(proposition.name, "foo")
|
|
|
|
self.assertEqual(proposition.artist, "bar")
|
|
|
|
self.assertEqual(proposition.link, "http://example.com")
|
2020-01-05 00:18:31 +01:00
|
|
|
self.assertEqual(proposition.user, user.profile)
|
2020-01-05 00:32:02 +01:00
|
|
|
|
|
|
|
|
2020-01-05 17:22:00 +01:00
|
|
|
class PropositionDeleteTest(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.owner = new_user("owner")
|
|
|
|
self.random_user = new_user("toto")
|
|
|
|
self.chef = new_user("chef")
|
|
|
|
self.chef.profile.is_chef = True
|
|
|
|
self.chef.profile.save()
|
|
|
|
|
|
|
|
proposition = Proposition.objects.create(name="prop", user=self.owner.profile)
|
|
|
|
self.url = reverse("propositions:delete", args=(proposition.id,))
|
|
|
|
|
|
|
|
def test_anonymous_cannot_get(self):
|
|
|
|
response = Client().get(self.url)
|
|
|
|
self.assertRedirects(response, "/login?next={}".format(self.url))
|
|
|
|
|
|
|
|
def test_anonymous_cannot_post(self):
|
|
|
|
response = Client().post(self.url, {})
|
|
|
|
self.assertRedirects(response, "/login?next={}".format(self.url))
|
|
|
|
self.assertTrue(Proposition.objects.exists())
|
|
|
|
|
|
|
|
def test_random_user_cannot_get(self):
|
|
|
|
client = Client()
|
|
|
|
client.force_login(self.random_user)
|
|
|
|
response = client.get(self.url)
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
def test_not_owner_cannot_post(self):
|
|
|
|
client = Client()
|
|
|
|
client.force_login(self.random_user)
|
|
|
|
response = client.post(self.url, {})
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
self.assertTrue(Proposition.objects.exists())
|
|
|
|
|
|
|
|
def test_chef_can_get(self):
|
|
|
|
client = Client()
|
|
|
|
client.force_login(self.chef)
|
|
|
|
response = client.get(self.url)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_chef_can_post(self):
|
|
|
|
client = Client()
|
|
|
|
client.force_login(self.chef)
|
|
|
|
client.post(self.url, {})
|
|
|
|
self.assertFalse(Proposition.objects.exists())
|
|
|
|
|
|
|
|
def test_owner_can_get(self):
|
|
|
|
client = Client()
|
|
|
|
client.force_login(self.owner)
|
|
|
|
response = client.get(self.url)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_owner_can_post(self):
|
|
|
|
client = Client()
|
|
|
|
client.force_login(self.owner)
|
|
|
|
client.post(self.url, {})
|
|
|
|
self.assertFalse(Proposition.objects.exists())
|
|
|
|
|
|
|
|
|
2020-01-05 00:32:02 +01:00
|
|
|
class PropositionListTest(TestCase):
|
|
|
|
url = reverse_lazy("propositions:list")
|
|
|
|
|
2020-01-05 15:31:11 +01:00
|
|
|
def setUp(self):
|
|
|
|
self.user = new_user("toto")
|
|
|
|
for name in ["foo", "bar", "baz"]:
|
2020-01-05 15:58:16 +01:00
|
|
|
p = Proposition.objects.create(name=name, user=self.user.profile)
|
2020-01-05 15:31:11 +01:00
|
|
|
Answer.objects.create(proposition=p, user=self.user, answer=Answer.YES)
|
|
|
|
for name in ["oof", "rab", "zab"]:
|
2020-01-05 15:58:16 +01:00
|
|
|
p = Proposition.objects.create(name=name, user=self.user.profile)
|
2020-01-05 15:31:11 +01:00
|
|
|
Answer.objects.create(proposition=p, user=self.user, answer=Answer.NO)
|
|
|
|
|
2020-01-05 17:27:31 +01:00
|
|
|
def test_anonymous_cannot_get(self):
|
2020-01-05 00:32:02 +01:00
|
|
|
response = Client().get(self.url)
|
|
|
|
self.assertRedirects(response, "/login?next={}".format(self.url))
|
|
|
|
|
2020-01-05 17:27:31 +01:00
|
|
|
def test_ernesto_user_can_get(self):
|
2020-01-05 00:32:02 +01:00
|
|
|
client = Client()
|
2020-01-05 15:31:11 +01:00
|
|
|
client.force_login(self.user)
|
2020-01-05 00:32:02 +01:00
|
|
|
|
|
|
|
response = client.get(self.url)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2020-01-05 12:02:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ReponseTest(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.user = new_user("toto")
|
2020-01-05 15:58:16 +01:00
|
|
|
self.prop = Proposition.objects.create(name="foo", user=self.user.profile)
|
2020-01-05 12:02:24 +01:00
|
|
|
|
|
|
|
def _url(self, rep):
|
2020-01-05 17:27:31 +01:00
|
|
|
assert rep in Answer.REP_CHOICES
|
2020-01-05 12:02:24 +01:00
|
|
|
return reverse("propositions:{}".format(rep), args=(self.prop.id,))
|
|
|
|
|
2020-01-05 17:27:31 +01:00
|
|
|
def test_anonymous_cannot_get(self):
|
2020-01-05 12:02:24 +01:00
|
|
|
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))
|
|
|
|
|
2020-01-05 17:27:31 +01:00
|
|
|
def test_ernesto_user_can_get(self):
|
2020-01-05 12:02:24 +01:00
|
|
|
client = Client()
|
|
|
|
client.force_login(self.user)
|
|
|
|
|
|
|
|
client.get(reverse("propositions:oui", args=(self.prop.id,)))
|
|
|
|
self.prop.refresh_from_db()
|
2020-01-05 16:34:10 +01:00
|
|
|
self.assertEqual(
|
|
|
|
list(self.prop.answer_set.values_list("answer", flat=True)), [Answer.YES],
|
|
|
|
)
|
2020-01-05 12:02:24 +01:00
|
|
|
|
|
|
|
client.get(reverse("propositions:non", args=(self.prop.id,)))
|
|
|
|
self.prop.refresh_from_db()
|
2020-01-05 16:34:10 +01:00
|
|
|
self.assertEqual(
|
|
|
|
list(self.prop.answer_set.values_list("answer", flat=True)), [Answer.NO]
|
|
|
|
)
|