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 setUp(self): super(ContributionListAPITestCase, self).setUp() self.client = JSONAPITestClient() self.user = BlueBottleUserFactory.create() ParticipantFactory.create_batch(2, user=self.user) ApplicantFactory.create_batch(2, user=self.user) DonationFactory.create_batch(2, user=self.user, status='succeeded') DonationFactory.create_batch(2, user=self.user, status='new') ParticipantFactory.create() ApplicantFactory.create() DonationFactory.create() self.url = reverse('contribution-list')
def test_date_changed(self): assignment = AssignmentFactory( title='Test Title', status='open', date=now() + timedelta(days=4), ) ApplicantFactory.create_batch(3, activity=assignment, status='new') ApplicantFactory.create_batch(3, activity=assignment, status='accepted') withdrawn = ApplicantFactory.create(activity=assignment, status='new') withdrawn.states.withdraw(save=True) mail.outbox = [] assignment.date = assignment.date + timedelta(days=1) assignment.save() messages = dict( (message.to[0], message.body) for message in mail.outbox) for participant in assignment.contributions.instance_of( Applicant).all(): if participant.status in ('new', 'accepted'): self.assertTrue(participant.user.email in messages) self.assertTrue( formats.date_format(assignment.date) in messages[ participant.user.email]) else: self.assertFalse(participant.user.email in messages)
def test_retrieve_applicant(self): applicant = ApplicantFactory.create(activity=self.assignment) response = self.client.get(self.url, user=applicant.user) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['status'], 'draft') self.assertEqual( self.assignment.online_meeting_url, response.json()['data']['attributes']['online-meeting-url'])
def test_export_user_segments(self): from_date = now() - timedelta(weeks=2) to_date = now() + timedelta(weeks=1) users = BlueBottleUserFactory.create_batch(5) segment_type = SegmentTypeFactory.create(name='Department') engineering = SegmentFactory.create(type=segment_type, name='Engineering') rubbish = SegmentFactory.create(type=segment_type, name='Rubbish') users[0].segments.add(engineering) initiative = InitiativeFactory.create(owner=users[0]) assignment = AssignmentFactory.create(owner=users[1], initiative=initiative) assignment.segments.add(engineering) assignment.segments.add(rubbish) ApplicantFactory.create(activity=assignment, user=users[2]) data = {'from_date': from_date, 'to_date': to_date, '_save': 'Confirm'} tenant = connection.tenant result = plain_export(Exporter, tenant=tenant, **data) book = xlrd.open_workbook(result) self.assertEqual(book.sheet_by_name('Users').ncols, 12) self.assertEqual( book.sheet_by_name('Users').cell(0, 11).value, 'Department') t = 0 while t < book.sheet_by_name('Users').nrows: if book.sheet_by_name('Users').cell(t, 5).value == users[0].email: self.assertEqual( book.sheet_by_name('Users').cell(t, 11).value, 'Engineering') t += 1 self.assertEqual( book.sheet_by_name('Tasks').cell(0, 23).value, 'Department') t = 0 while t < book.sheet_by_name('Users').nrows: if book.sheet_by_name('Users').cell(t, 5).value == users[0].email: self.assertTrue( book.sheet_by_name('Tasks').cell(t, 23).value in ['Engineering, Rubbish', 'Rubbish, Engineering']) t += 1
def test_export_custom_user_fields(self): from_date = now() - timedelta(weeks=2) to_date = now() + timedelta(weeks=1) colour = CustomMemberFieldSettings.objects.create( name='colour', description='Favourite colour') BlueBottleUserFactory.create_batch(2) user = BlueBottleUserFactory.create(email='*****@*****.**') BlueBottleUserFactory.create_batch(2) CustomMemberField.objects.create(member=user, field=colour, value='Parblue Yellow') initiative = InitiativeFactory.create(owner=user) assignment = AssignmentFactory.create(owner=user, initiative=initiative) ApplicantFactory.create(activity=assignment, user=user) data = {'from_date': from_date, 'to_date': to_date, '_save': 'Confirm'} tenant = connection.tenant result = plain_export(Exporter, tenant=tenant, **data) book = xlrd.open_workbook(result) self.assertEqual(book.sheet_by_name('Users').ncols, 12) t = 1 while t < book.sheet_by_name('Users').nrows: if book.sheet_by_name('Users').cell( t, 5).value == '*****@*****.**': self.assertEqual( book.sheet_by_name('Users').cell(t, 11).value, 'Parblue Yellow') t += 1 self.assertEqual( book.sheet_by_name('Task contributions').cell(0, 13).value, 'Favourite colour') self.assertEqual( book.sheet_by_name('Task contributions').cell(1, 13).value, 'Parblue Yellow')
def setUp(self): super(ApplicantStateMachineTestCase, self).setUp() self.settings = InitiativePlatformSettingsFactory.create( activity_types=['assignment']) self.initiative = InitiativeFactory() self.initiative.states.submit() self.initiative.states.approve(save=True) self.assignment = AssignmentFactory.create(owner=self.initiative.owner, initiative=self.initiative) self.assignment.states.submit(save=True) mail.outbox = [] self.applicant = ApplicantFactory.create(activity=self.assignment)
def test_participant_rejected(self): contribution = ApplicantFactory.create(activity=self.assignment, user=self.other_user) contribution.states.reject(save=True) self.assignment.states.start(save=True) self.assignment.states.succeed(save=True) self.assertEqual(self.stats.activities_online, 0) self.assertEqual(self.stats.activities_succeeded, 1) self.assertEqual(self.stats.assignments_succeeded, 1) self.assertEqual(self.stats.time_spent, 0) self.assertEqual(self.stats.assignment_members, 0) self.assertEqual(self.stats.people_involved, 1)
def test_applicant(self): contribution = ApplicantFactory.create(activity=self.assignment, user=self.other_user) contribution.states.accept(save=True) self.assignment.states.start(save=True) self.assignment.states.succeed(save=True) contribution.refresh_from_db() contribution.time_spent = 32 contribution.save() self.assertEqual(self.stats.activities_online, 0) self.assertEqual(self.stats.activities_succeeded, 1) self.assertEqual(self.stats.assignments_succeeded, 1) self.assertEqual(self.stats.time_spent, 32) self.assertEqual(self.stats.assignment_members, 1) self.assertEqual(self.stats.people_involved, 2)
def test_date_not_changed(self): assignment = AssignmentFactory( title='Test Title', status='open', date=now() + timedelta(days=4), ) ApplicantFactory.create_batch(3, activity=assignment, status='new') withdrawn = ApplicantFactory.create(activity=assignment, status='new') withdrawn.states.withdraw(save=True) mail.outbox = [] assignment.title = 'New title' assignment.save() self.assertEqual(len(mail.outbox), 0)
def setUp(self): super(ApplicantTransitionAPITestCase, self).setUp() self.settings = InitiativePlatformSettingsFactory.create( activity_types=['assignment']) self.client = JSONAPITestClient() self.transition_url = reverse('applicant-transition-list') self.user = BlueBottleUserFactory() self.someone_else = BlueBottleUserFactory() self.manager = BlueBottleUserFactory(first_name="Boss") self.owner = BlueBottleUserFactory(first_name="Owner") self.initiative = InitiativeFactory.create( activity_manager=self.manager) self.initiative.states.submit() self.initiative.states.approve(save=True) self.assignment = AssignmentFactory.create(owner=self.owner, initiative=self.initiative) self.assignment.states.submit(save=True) document = PrivateDocumentFactory.create() self.applicant = ApplicantFactory.create(activity=self.assignment, document=document, user=self.user) self.participant_url = reverse('applicant-detail', args=(self.applicant.id, )) self.transition_data = { 'data': { 'type': 'contributions/applicant-transitions', 'attributes': { 'transition': 'accept', }, 'relationships': { 'resource': { 'data': { 'type': 'contributions/applicants', 'id': self.applicant.pk } } } } } mail.outbox = []
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_applicant_status_change_on_time_spent(self): assignment = AssignmentFactory( title='Test Title', status='open', date=now() + timedelta(days=4), ) applicant = ApplicantFactory.create(activity=assignment) applicant.states.accept(save=True) assignment.states.start(save=True) assignment.states.succeed(save=True) applicant.refresh_from_db() self.assertEqual(assignment.status, 'succeeded') self.assertEqual(applicant.status, 'succeeded') applicant.time_spent = 0 applicant.save() self.assertEqual(applicant.status, 'no_show') applicant.time_spent = 10 applicant.save() self.assertEqual(applicant.status, 'succeeded') self.assertEqual(applicant.contribution_date, assignment.date)
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')