2017-11-24 06:53:31 +01:00
|
|
|
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 (
|
2018-10-06 12:35:49 +02:00
|
|
|
Attribution,
|
|
|
|
Participant,
|
|
|
|
Salle,
|
|
|
|
Spectacle,
|
|
|
|
SpectacleRevente,
|
|
|
|
Tirage,
|
2017-11-24 06:53:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
|
|
|
|
|
|
class SpectacleReventeTests(TestCase):
|
2018-10-06 12:35:49 +02:00
|
|
|
fixtures = ["gestioncof/management/data/custommail.json"]
|
2017-11-24 06:53:31 +01:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
now = timezone.now()
|
|
|
|
|
|
|
|
self.t = Tirage.objects.create(
|
2018-10-06 12:35:49 +02:00
|
|
|
title="Tirage",
|
2017-11-24 06:53:31 +01:00
|
|
|
ouverture=now - timedelta(days=7),
|
|
|
|
fermeture=now - timedelta(days=3),
|
|
|
|
active=True,
|
|
|
|
)
|
|
|
|
self.s = Spectacle.objects.create(
|
2018-10-06 12:35:49 +02:00
|
|
|
title="Spectacle",
|
2017-11-24 06:53:31 +01:00
|
|
|
date=now + timedelta(days=20),
|
2018-10-06 12:35:49 +02:00
|
|
|
location=Salle.objects.create(name="Salle", address="Address"),
|
2017-11-24 06:53:31 +01:00
|
|
|
price=10.5,
|
|
|
|
slots=5,
|
|
|
|
tirage=self.t,
|
|
|
|
listing=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.seller = Participant.objects.create(
|
2018-10-06 12:35:49 +02:00
|
|
|
user=User.objects.create(username="seller", email="seller@mail.net"),
|
2017-11-24 06:53:31 +01:00
|
|
|
tirage=self.t,
|
|
|
|
)
|
|
|
|
self.p1 = Participant.objects.create(
|
2018-10-06 12:35:49 +02:00
|
|
|
user=User.objects.create(username="part1", email="part1@mail.net"),
|
2017-11-24 06:53:31 +01:00
|
|
|
tirage=self.t,
|
|
|
|
)
|
|
|
|
self.p2 = Participant.objects.create(
|
2018-10-06 12:35:49 +02:00
|
|
|
user=User.objects.create(username="part2", email="part2@mail.net"),
|
2017-11-24 06:53:31 +01:00
|
|
|
tirage=self.t,
|
|
|
|
)
|
|
|
|
self.p3 = Participant.objects.create(
|
2018-10-06 12:35:49 +02:00
|
|
|
user=User.objects.create(username="part3", email="part3@mail.net"),
|
2017-11-24 06:53:31 +01:00
|
|
|
tirage=self.t,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.attr = Attribution.objects.create(
|
2018-10-06 12:35:49 +02:00
|
|
|
participant=self.seller, spectacle=self.s
|
2017-11-24 06:53:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
self.rev = SpectacleRevente.objects.create(
|
2018-10-06 12:35:49 +02:00
|
|
|
attribution=self.attr, seller=self.seller
|
2017-11-24 06:53:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_tirage(self):
|
|
|
|
revente = self.rev
|
|
|
|
|
|
|
|
wanted_by = [self.p1, self.p2, self.p3]
|
2019-04-17 18:21:59 +02:00
|
|
|
revente.confirmed_entry.set(wanted_by)
|
2017-11-24 06:53:31 +01:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
with mock.patch("bda.models.random.choice") as mc:
|
2017-11-24 06:53:31 +01:00
|
|
|
# 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)
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
m_seller = mails["seller@mail.net"]
|
|
|
|
self.assertListEqual(m_seller.to, ["seller@mail.net"])
|
|
|
|
self.assertListEqual(m_seller.reply_to, ["part1@mail.net"])
|
2017-11-24 06:53:31 +01:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
m_winner = mails["part1@mail.net"]
|
|
|
|
self.assertListEqual(m_winner.to, ["part1@mail.net"])
|
2017-11-24 06:53:31 +01:00
|
|
|
|
|
|
|
self.assertCountEqual(
|
2018-10-06 12:35:49 +02:00
|
|
|
[mails["part2@mail.net"].to, mails["part3@mail.net"].to],
|
|
|
|
[["part2@mail.net"], ["part3@mail.net"]],
|
2017-11-24 06:53:31 +01:00
|
|
|
)
|