def test_assignment_reminder_task_on_date(self):
        user = BlueBottleUserFactory.create(first_name='Nono')
        end = now() + timedelta(days=4)
        assignment = AssignmentFactory.create(
            owner=user,
            status='open',
            end_date_type='on_date',
            initiative=self.initiative,
            date=end
        )

        ApplicantFactory.create_batch(2, activity=assignment, status='new')
        ApplicantFactory.create(activity=assignment, status='accepted')
        withdrawn = ApplicantFactory.create(activity=assignment, status='new')
        withdrawn.states.withdraw(save=True)

        mail.outbox = []
        tenant = connection.tenant
        assignment_tasks()

        with LocalTenant(tenant, clear_tenant=True):
            assignment.refresh_from_db()

        recipients = [message.to[0] for message in mail.outbox]

        for applicant in assignment.contributions.instance_of(Applicant).all():
            if applicant.status in ['new', 'accepted']:
                self.assertTrue(applicant.user.email in recipients)
            else:
                self.assertFalse(applicant.user.email in recipients)
        self.assertEqual(
            mail.outbox[0].subject,
            '"{}" will take place in 5 days!'.format(assignment.title)
        )
    def test_assignment_check_end_date_future(self):
        user = BlueBottleUserFactory.create(first_name='Nono')

        deadline = now() - timedelta(days=4)
        date = now() + timedelta(days=2)

        assignment = AssignmentFactory.create(
            owner=user,
            status='open',
            capacity=3,
            registration_deadline=deadline.date(),
            initiative=self.initiative,
            date=date,
        )

        applicants = ApplicantFactory.create_batch(3, activity=assignment)
        for applicant in applicants:
            applicant.states.accept(save=True)

        ApplicantFactory.create_batch(3, activity=assignment)

        tenant = connection.tenant

        future = timezone.now() + timedelta(days=3)
        with mock.patch.object(timezone, 'now', return_value=future):
            assignment_tasks()

        with LocalTenant(tenant, clear_tenant=True):
            assignment.refresh_from_db()

        self.assertEqual(assignment.status, 'succeeded')
        for applicant in assignment.applicants:
            self.assertEqual(applicant.status, 'succeeded')
    def test_assignment_check_start_date(self):
        user = BlueBottleUserFactory.create(first_name='Nono')

        registration_deadline = now() - timedelta(days=1)
        date = now() + timedelta(hours=6)

        assignment = AssignmentFactory.create(
            owner=user,
            status='open',
            capacity=3,
            registration_deadline=registration_deadline.date(),
            initiative=self.initiative,
            end_date_type='on_date',
            duration=10,
            date=date
        )
        applicants = ApplicantFactory.create_batch(3, activity=assignment, status='new')
        for applicant in applicants:
            applicant.states.accept(save=True)

        tenant = connection.tenant
        with mock.patch.object(timezone, 'now', return_value=(timezone.now() + timedelta(hours=7))):
            assignment_tasks()

        with LocalTenant(tenant, clear_tenant=True):
            assignment.refresh_from_db()

        self.assertEqual(assignment.status, 'running')
    def test_assignment_check_registration_deadline(self):
        user = BlueBottleUserFactory.create(first_name='Nono')

        deadline = now() - timedelta(days=1)
        end = now() + timedelta(days=4)

        assignment = AssignmentFactory.create(
            owner=user,
            status='open',
            capacity=3,
            end_date_type='on_date',
            registration_deadline=deadline.date(),
            initiative=self.initiative,
            duration=4,
            date=end,
        )
        applicants = ApplicantFactory.create_batch(3, activity=assignment, status='new')
        for applicant in applicants:
            applicant.states.accept(save=True)

        tenant = connection.tenant
        assignment_tasks()

        with LocalTenant(tenant, clear_tenant=True):
            assignment.refresh_from_db()

        self.assertEqual(assignment.status, 'full')
    def test_assignment_reminder_task_twice(self):
        user = BlueBottleUserFactory.create(first_name='Nono')
        end = now() + timedelta(days=4)
        assignment = AssignmentFactory.create(
            owner=user,
            status='open',
            initiative=self.initiative,
            date=end,
        )

        ApplicantFactory.create_batch(3, activity=assignment, status='new')
        withdrawn = ApplicantFactory.create(activity=assignment, status='new')
        withdrawn.states.withdraw(save=True)

        assignment_tasks()
        mail.outbox = []
        assignment_tasks()

        self.assertEqual(len(mail.outbox), 0)
    def test_assignment_check_start_date_no_applicants(self):
        user = BlueBottleUserFactory.create(first_name='Nono')

        deadline = now() - timedelta(days=1)
        date = now() - timedelta(hours=2)

        assignment = AssignmentFactory.create(
            owner=user,
            status='open',
            capacity=3,
            registration_deadline=deadline.date(),
            initiative=self.initiative,
            duration=10,
            date=date
        )
        tenant = connection.tenant
        assignment_tasks()

        with LocalTenant(tenant, clear_tenant=True):
            assignment.refresh_from_db()

        self.assertEqual(assignment.status, 'cancelled')
示例#7
0
    def test_confirm_hours(self):
        self.assertEqual(self.assignment.status, 'open')
        applicant = ApplicantFactory.create(user=self.user,
                                            activity=self.assignment)
        applicant.states.accept(save=True)

        no_show = ApplicantFactory.create(activity=self.assignment)
        no_show.states.accept(save=True)
        tenant = connection.tenant
        assignment_tasks()

        with mock.patch.object(timezone,
                               'now',
                               return_value=self.assignment.date +
                               timedelta(days=5)):
            assignment_tasks()

        with LocalTenant(tenant, clear_tenant=True):
            applicant.refresh_from_db()
        self.assertEqual(applicant.status, 'succeeded')
        self.assertEqual(applicant.time_spent, 4)

        url = reverse('applicant-detail', args=(applicant.id, ))
        self.apply_data['data']['id'] = applicant.id
        self.apply_data['data']['attributes']['time-spent'] = 8

        # User should not be able to set hours
        response = self.client.patch(url,
                                     json.dumps(self.apply_data),
                                     user=self.user)
        self.assertEqual(response.status_code, 200)
        applicant.refresh_from_db()
        self.assertEqual(applicant.time_spent, 8)

        # Owner should be able to set hours
        response = self.client.patch(url,
                                     json.dumps(self.apply_data),
                                     user=self.owner)
        self.assertEqual(response.status_code, 200)
        applicant.refresh_from_db()
        self.assertEqual(applicant.time_spent, 8)

        # Setting zero hours should fail the applicant
        url = reverse('applicant-detail', args=(no_show.id, ))
        self.apply_data['data']['id'] = no_show.id
        self.apply_data['data']['attributes']['time-spent'] = 0
        response = self.client.patch(url,
                                     json.dumps(self.apply_data),
                                     user=self.owner)
        self.assertEqual(response.status_code, 200)
        no_show.refresh_from_db()
        self.assertEqual(no_show.time_spent, 0.0)
        self.assertEqual(no_show.status, 'no_show')

        # And put the no show back to success
        url = reverse('applicant-detail', args=(no_show.id, ))
        self.apply_data['data']['id'] = no_show.id
        self.apply_data['data']['attributes']['time-spent'] = 2
        response = self.client.patch(url,
                                     json.dumps(self.apply_data),
                                     user=self.owner)
        self.assertEqual(response.status_code, 200)
        no_show.refresh_from_db()
        self.assertEqual(no_show.time_spent, 2)
        self.assertEqual(no_show.status, 'succeeded')