def test_event_end_task(self): user = BlueBottleUserFactory.create(first_name='Nono') start = timezone.now() + timedelta(hours=5) event = EventFactory.create( owner=user, initiative=self.initiative, title='Finish them translations, Rolfertjan!', start=start, duration=1 ) event.states.submit(save=True) ParticipantFactory.create_batch(3, activity=event) tenant = connection.tenant mail.outbox = [] future = timezone.now() + timedelta(hours=6) with mock.patch.object(timezone, 'now', return_value=future): event_tasks() with LocalTenant(tenant, clear_tenant=True): event = Event.objects.get(pk=event.pk) self.assertEqual(event.status, EventStateMachine.succeeded.value) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, u'Your event "{}" took place! \U0001f389'.format(event.title)) self.assertTrue("Hi Nono,", mail.outbox[0].body)
def test_event_scheduled_task_expire(self): tenant = connection.tenant with mock.patch.object(timezone, 'now', return_value=(timezone.now() + timedelta(days=10, hours=12))): event_tasks() with LocalTenant(tenant, clear_tenant=True): self.event.refresh_from_db() event = Event.objects.get(pk=self.event.pk) self.assertEqual(event.status, 'cancelled')
def test_event_scheduled_task_start(self): ParticipantFactory.create_batch(2, activity=self.event) self.assertEqual(self.event.status, 'full') tenant = connection.tenant with mock.patch.object(timezone, 'now', return_value=(timezone.now() + timedelta(days=10, hours=12))): event_tasks() with LocalTenant(tenant, clear_tenant=True): self.event.refresh_from_db() event = Event.objects.get(pk=self.event.pk) self.assertEqual(event.status, 'running')
def test_event_scheduled_task_reminder(self): ParticipantFactory.create_batch(2, activity=self.event) tenant = connection.tenant mail.outbox = [] with mock.patch.object(timezone, 'now', return_value=(timezone.now() + timedelta(days=7))): event_tasks() with LocalTenant(tenant, clear_tenant=True): self.event.refresh_from_db() event = Event.objects.get(pk=self.event.pk) self.assertEqual(event.status, 'full') self.assertEqual(len(mail.outbox), 2)
def test_event_start_task_no_participants(self): start = timezone.now() + timedelta(hours=1) event = EventFactory.create( initiative=self.initiative, start=start, duration=3 ) event.states.submit(save=True) self.assertEqual(event.status, 'open') tenant = connection.tenant future = timezone.now() + timedelta(hours=2) with mock.patch.object(timezone, 'now', return_value=future): event_tasks() with LocalTenant(tenant, clear_tenant=True): event = Event.objects.get(pk=event.pk) self.assertEqual(event.status, 'cancelled')
def test_succeed_in_future(self): self.event.states.submit(save=True) ParticipantFactory.create(activity=self.event) future = self.event.start + timedelta(days=2) self.event.save() tenant = connection.tenant with mock.patch.object(timezone, 'now', return_value=future): event_tasks() with LocalTenant(tenant, clear_tenant=True): self.event.refresh_from_db() self.assertEqual(self.event.status, EventStateMachine.succeeded.value) self.event.refresh_from_db() for participant in self.event.contributions.instance_of(Participant): self.assertEqual(participant.status, ParticipantStateMachine.succeeded.value) self.assertEqual(participant.time_spent, self.event.duration)
def test_event_reminder_task(self): user = BlueBottleUserFactory.create(first_name='Nono') start = timezone.now() + timedelta(days=4) event = EventFactory.create( owner=user, status='open', initiative=self.initiative, start=start, duration=1 ) ParticipantFactory.create_batch(3, activity=event, status='new') ParticipantFactory.create(activity=event, status='withdrawn') tenant = connection.tenant event_tasks() recipients = [message.to[0] for message in mail.outbox] with LocalTenant(tenant, clear_tenant=True): event.refresh_from_db() for participant in event.contributions.all(): if participant.status == 'new': self.assertTrue(participant.user.email in recipients) else: self.assertFalse(participant.user.email in recipients) recipients = [message.to[0] for message in mail.outbox] for participant in event.contributions.all(): if participant.status == 'new': self.assertTrue(participant.user.email in recipients) else: self.assertFalse(participant.user.email in recipients) mail.outbox = []
def test_event_reminder_task_twice(self): user = BlueBottleUserFactory.create(first_name='Nono') start = timezone.now() + timedelta(days=4) event = EventFactory.create( owner=user, status='open', initiative=self.initiative, start=start, duration=1 ) ParticipantFactory.create_batch(3, activity=event, status='new') ParticipantFactory.create(activity=event, status='withdrawn') event_tasks() mail.outbox = [] event_tasks() event_tasks() self.assertEqual(len(mail.outbox), 0)