Merge branch 'master' into Aufinal/bda_fixes
This commit is contained in:
commit
501d592d2f
49 changed files with 482 additions and 300 deletions
0
bda/tests/__init__.py
Normal file
0
bda/tests/__init__.py
Normal file
100
bda/tests/test_models.py
Normal file
100
bda/tests/test_models.py
Normal file
|
@ -0,0 +1,100 @@
|
|||
from datetime import timedelta
|
||||
from unittest import mock
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core import mail
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
|
||||
from bda.models import (
|
||||
Attribution, Participant, Salle, Spectacle, SpectacleRevente, Tirage,
|
||||
)
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class SpectacleReventeTests(TestCase):
|
||||
fixtures = ['gestioncof/management/data/custommail.json']
|
||||
|
||||
def setUp(self):
|
||||
now = timezone.now()
|
||||
|
||||
self.t = Tirage.objects.create(
|
||||
title='Tirage',
|
||||
ouverture=now - timedelta(days=7),
|
||||
fermeture=now - timedelta(days=3),
|
||||
active=True,
|
||||
)
|
||||
self.s = Spectacle.objects.create(
|
||||
title='Spectacle',
|
||||
date=now + timedelta(days=20),
|
||||
location=Salle.objects.create(name='Salle', address='Address'),
|
||||
price=10.5,
|
||||
slots=5,
|
||||
tirage=self.t,
|
||||
listing=False,
|
||||
)
|
||||
|
||||
self.seller = Participant.objects.create(
|
||||
user=User.objects.create(
|
||||
username='seller', email='seller@mail.net'),
|
||||
tirage=self.t,
|
||||
)
|
||||
self.p1 = Participant.objects.create(
|
||||
user=User.objects.create(username='part1', email='part1@mail.net'),
|
||||
tirage=self.t,
|
||||
)
|
||||
self.p2 = Participant.objects.create(
|
||||
user=User.objects.create(username='part2', email='part2@mail.net'),
|
||||
tirage=self.t,
|
||||
)
|
||||
self.p3 = Participant.objects.create(
|
||||
user=User.objects.create(username='part3', email='part3@mail.net'),
|
||||
tirage=self.t,
|
||||
)
|
||||
|
||||
self.attr = Attribution.objects.create(
|
||||
participant=self.seller,
|
||||
spectacle=self.s,
|
||||
)
|
||||
|
||||
self.rev = SpectacleRevente.objects.create(
|
||||
attribution=self.attr,
|
||||
seller=self.seller,
|
||||
)
|
||||
|
||||
def test_tirage(self):
|
||||
revente = self.rev
|
||||
|
||||
wanted_by = [self.p1, self.p2, self.p3]
|
||||
revente.answered_mail = wanted_by
|
||||
|
||||
with mock.patch('bda.models.random.choice') as mc:
|
||||
# Set winner to self.p1.
|
||||
mc.return_value = self.p1
|
||||
|
||||
revente.tirage()
|
||||
|
||||
# Call to random.choice used participants in wanted_by.
|
||||
mc_args, _ = mc.call_args
|
||||
|
||||
self.assertEqual(set(mc_args[0]), set(wanted_by))
|
||||
|
||||
self.assertEqual(revente.soldTo, self.p1)
|
||||
self.assertTrue(revente.tirage_done)
|
||||
|
||||
mails = {m.to[0]: m for m in mail.outbox}
|
||||
|
||||
self.assertEqual(len(mails), 4)
|
||||
|
||||
m_seller = mails['seller@mail.net']
|
||||
self.assertListEqual(m_seller.to, ['seller@mail.net'])
|
||||
self.assertListEqual(m_seller.reply_to, ['part1@mail.net'])
|
||||
|
||||
m_winner = mails['part1@mail.net']
|
||||
self.assertListEqual(m_winner.to, ['part1@mail.net'])
|
||||
|
||||
self.assertCountEqual(
|
||||
[mails['part2@mail.net'].to, mails['part3@mail.net'].to],
|
||||
[['part2@mail.net'], ['part3@mail.net']],
|
||||
)
|
105
bda/tests/test_views.py
Normal file
105
bda/tests/test_views.py
Normal file
|
@ -0,0 +1,105 @@
|
|||
import json
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.test import TestCase, Client
|
||||
from django.utils import timezone
|
||||
|
||||
from bda.models import Tirage, Spectacle, Salle, CategorieSpectacle
|
||||
|
||||
|
||||
class TestBdAViews(TestCase):
|
||||
def setUp(self):
|
||||
self.tirage = Tirage.objects.create(
|
||||
title="Test tirage",
|
||||
appear_catalogue=True,
|
||||
ouverture=timezone.now(),
|
||||
fermeture=timezone.now(),
|
||||
)
|
||||
self.category = CategorieSpectacle.objects.create(name="Category")
|
||||
self.location = Salle.objects.create(name="here")
|
||||
Spectacle.objects.bulk_create([
|
||||
Spectacle(
|
||||
title="foo", date=timezone.now(), location=self.location,
|
||||
price=0, slots=42, tirage=self.tirage, listing=False,
|
||||
category=self.category
|
||||
),
|
||||
Spectacle(
|
||||
title="bar", date=timezone.now(), location=self.location,
|
||||
price=1, slots=142, tirage=self.tirage, listing=False,
|
||||
category=self.category
|
||||
),
|
||||
Spectacle(
|
||||
title="baz", date=timezone.now(), location=self.location,
|
||||
price=2, slots=242, tirage=self.tirage, listing=False,
|
||||
category=self.category
|
||||
),
|
||||
])
|
||||
|
||||
self.bda_user = User.objects.create_user(
|
||||
username="bda_user", password="bda4ever"
|
||||
)
|
||||
self.bda_user.profile.is_cof = True
|
||||
self.bda_user.profile.is_buro = True
|
||||
self.bda_user.profile.save()
|
||||
|
||||
def bda_participants(self):
|
||||
"""The BdA participants views can be queried"""
|
||||
client = Client()
|
||||
show = self.tirage.spectacle_set.first()
|
||||
|
||||
client.login(self.bda_user.username, "bda4ever")
|
||||
tirage_resp = client.get("/bda/spectacles/{}".format(self.tirage.id))
|
||||
show_resp = client.get(
|
||||
"/bda/spectacles/{}/{}".format(self.tirage.id, show.id)
|
||||
)
|
||||
reminder_url = "/bda/mails-rappel/{}".format(show.id)
|
||||
reminder_get_resp = client.get(reminder_url)
|
||||
reminder_post_resp = client.post(reminder_url)
|
||||
self.assertEqual(200, tirage_resp.status_code)
|
||||
self.assertEqual(200, show_resp.status_code)
|
||||
self.assertEqual(200, reminder_get_resp.status_code)
|
||||
self.assertEqual(200, reminder_post_resp.status_code)
|
||||
|
||||
def test_catalogue(self):
|
||||
"""Test the catalogue JSON API"""
|
||||
client = Client()
|
||||
|
||||
# The `list` hook
|
||||
resp = client.get("/bda/catalogue/list")
|
||||
self.assertJSONEqual(
|
||||
resp.content.decode("utf-8"),
|
||||
[{"id": self.tirage.id, "title": self.tirage.title}]
|
||||
)
|
||||
|
||||
# The `details` hook
|
||||
resp = client.get(
|
||||
"/bda/catalogue/details?id={}".format(self.tirage.id)
|
||||
)
|
||||
self.assertJSONEqual(
|
||||
resp.content.decode("utf-8"),
|
||||
{
|
||||
"categories": [{
|
||||
"id": self.category.id,
|
||||
"name": self.category.name
|
||||
}],
|
||||
"locations": [{
|
||||
"id": self.location.id,
|
||||
"name": self.location.name
|
||||
}],
|
||||
}
|
||||
)
|
||||
|
||||
# The `descriptions` hook
|
||||
resp = client.get(
|
||||
"/bda/catalogue/descriptions?id={}".format(self.tirage.id)
|
||||
)
|
||||
raw = resp.content.decode("utf-8")
|
||||
try:
|
||||
results = json.loads(raw)
|
||||
except ValueError:
|
||||
self.fail("Not valid JSON: {}".format(raw))
|
||||
self.assertEqual(len(results), 3)
|
||||
self.assertEqual(
|
||||
{(s["title"], s["price"], s["slots"]) for s in results},
|
||||
{("foo", 0, 42), ("bar", 1, 142), ("baz", 2, 242)}
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue