From 7fe45817ea9adb48c449d8e7d0c58093172aafd5 Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Mon, 20 Nov 2017 14:57:16 +0100 Subject: [PATCH] mail with kit request doesn't persist new worker --- .../tests/view/test_KitRequestEmailSendJob.py | 41 +++++++++++++++++++ smash/web/views/kit.py | 4 +- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 smash/web/tests/view/test_KitRequestEmailSendJob.py diff --git a/smash/web/tests/view/test_KitRequestEmailSendJob.py b/smash/web/tests/view/test_KitRequestEmailSendJob.py new file mode 100644 index 00000000..f6e58798 --- /dev/null +++ b/smash/web/tests/view/test_KitRequestEmailSendJob.py @@ -0,0 +1,41 @@ +import datetime + +from django.core import mail +from django_cron.models import CronJobLog +from mockito import when + +from web.models.constants import KIT_EMAIL_HOUR_CONFIGURATION_TYPE +from web.models import Item, AppointmentTypeLink, Worker, ConfigurationItem +from web.tests import LoggedInTestCase +from web.tests.functions import create_appointment_type, create_appointment +from web.views.kit import KitRequestEmailSendJob +from web.views.notifications import get_today_midnight_date + + +class KitRequestEmailSendJobTests(LoggedInTestCase): + def test_kit_requests_send_email(self): + CronJobLog.objects.all().delete() + configuration = ConfigurationItem.objects.get(type=KIT_EMAIL_HOUR_CONFIGURATION_TYPE) + configuration.value = "00:00" + configuration.save() + + job = KitRequestEmailSendJob() + when(job).match_day_of_week().thenReturn(True) + + item_name = "Test item to be ordered" + item = Item.objects.create(disposable=True, name=item_name) + appointment_type = create_appointment_type() + appointment_type.required_equipment.add(item) + appointment_type.save() + + appointment = create_appointment() + appointment.datetime_when = get_today_midnight_date() + datetime.timedelta(days=2) + appointment.save() + AppointmentTypeLink.objects.create(appointment=appointment, appointment_type=appointment_type) + + workers_count = Worker.objects.all().count() + status = job.do() + + self.assertEqual("mail sent", status) + self.assertEqual(1, len(mail.outbox)) + self.assertEqual(workers_count, Worker.objects.all().count()) diff --git a/smash/web/views/kit.py b/smash/web/views/kit.py index e119ded1..fc4cbf37 100644 --- a/smash/web/views/kit.py +++ b/smash/web/views/kit.py @@ -148,8 +148,10 @@ class KitRequestEmailSendJob(CronJobBase): if jobs.count() == 0: if pytz.utc.localize(datetime.datetime.utcnow()) > date: if self.match_day_of_week(): - data = get_kit_requests(Worker.objects.create()) + worker = Worker.objects.create() + data = get_kit_requests(worker) send_mail(data) + worker.delete() return "mail sent" else: return "day of week doesn't match" -- GitLab