def test_dsa_regions_view(self): workspace = self.unicef_staff.profile.country workspace.business_area_code = '1234' workspace.save() business_area = PublicsBusinessAreaFactory( code=workspace.business_area_code) country = PublicsCountryFactory(business_area=business_area) region_1 = PublicsDSARegionFactory(country=country) region_2 = PublicsDSARegionFactory(country=country) region_3 = PublicsDSARegionFactory(country=country) PublicsDSARateFactory(region=region_1) PublicsDSARateFactory(region=region_2) PublicsDSARateFactory(region=region_3) with self.assertNumQueries(4): response = self.forced_auth_req('get', reverse('public:dsa_regions'), user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(len(response_json), 3) expected_keys = [ 'unique_name', 'dsa_amount_usd', 'country', 'area_name', 'area_code', 'label', 'dsa_amount_60plus_usd', 'long_name', 'effective_from_date', 'dsa_amount_60plus_local', 'id', 'unique_id', 'dsa_amount_local' ] self.assertKeysIn(expected_keys, response_json[0], exact=True)
def test_dsa_region(self): country = PublicsCountryFactory.build(name=b'xyz') instance = PublicsDSARegionFactory.build(area_name=b'xyz', country=country) self.assertEqual(six.text_type(instance), u'xyz - xyz') # Island (Iceland) country = PublicsCountryFactory.build(name=u'\xccsland') instance = PublicsDSARegionFactory.build(area_name=b'xyz', country=country) self.assertEqual(six.text_type(instance), u'\xccsland - xyz')
def test_dsa_rate(self): country = PublicsCountryFactory.build(name=b'xyz') region = PublicsDSARegionFactory.build(area_name=b'xyz', country=country) instance = PublicsDSARateFactory.build(region=region) self.assertTrue(six.text_type(instance).startswith(u'xyz - xyz')) country = PublicsCountryFactory.build(name=u'\xccsland') region = PublicsDSARegionFactory.build(area_name=b'xyz', country=country) instance = PublicsDSARateFactory.build(region=region) self.assertTrue(six.text_type(instance).startswith(u'\xccsland - xyz'))
def test_endpoint(self): # This line is duplicated on purpose. Currency will have always 1+N number of queries # because of the exchange rate factory.build_batch(PublicsCurrencyFactory, 4) # Create one of each model to check if all serializers are working fine PublicsAirlineCompanyFactory() country = PublicsCountryFactory(currency=None) PublicsDSARegionFactory(country=country) PublicsBusinessAreaFactory() PublicsWBSFactory(business_area=None) PublicsGrantFactory() PublicsFundFactory() PublicsTravelExpenseTypeFactory() with self.assertNumQueries(10): response = self.forced_auth_req('get', reverse('public:static'), user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertKeysIn([ 'currencies', 'travel_types', 'countries', 'airlines', 'travel_modes', 'expense_types', 'business_areas' ], response_json, exact=True)
def test_dsa_regions_view(self): workspace = self.unicef_staff.profile.country workspace.business_area_code = '1234' workspace.save() business_area = PublicsBusinessAreaFactory( code=workspace.business_area_code) country = PublicsCountryFactory(business_area=business_area) region = PublicsDSARegionFactory(country=country, rates=[]) with self.assertNumQueries(1): response = self.forced_auth_req('get', reverse('public:dsa_regions'), user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(len(response_json), 0) rate = PublicsDSARateFactory(region=region) response = self.forced_auth_req('get', reverse('public:dsa_regions'), user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(len(response_json), 1) self.assertEqual(response_json[0]['id'], region.id) # Expire rate - region should be excluded rate.delete() response = self.forced_auth_req('get', reverse('public:dsa_regions'), user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(len(response_json), 0)
def test_values_history_retrieval(self): workspace = self.unicef_staff.profile.country workspace.business_area_code = '1234' workspace.save() business_area = PublicsBusinessAreaFactory( code=workspace.business_area_code) country = PublicsCountryFactory(business_area=business_area) region = PublicsDSARegionFactory(country=country, rates=[]) with freeze_time('2017-04-01'): rate_1 = PublicsDSARateFactory(region=region, effective_from_date=date( 2017, 4, 1), dsa_amount_usd=50) with freeze_time('2017-04-10'): rate_2 = PublicsDSARateFactory(region=region, effective_from_date=date( 2017, 4, 10), dsa_amount_usd=80) date_str = date(2017, 4, 12).isoformat() response = self.forced_auth_req('get', reverse('public:dsa_regions'), data={'values_at': date_str}, user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(Decimal(response_json[0]['dsa_amount_usd']), rate_2.dsa_amount_usd) date_str = date(2017, 4, 6).isoformat() response = self.forced_auth_req('get', reverse('public:dsa_regions'), data={'values_at': date_str}, user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(Decimal(response_json[0]['dsa_amount_usd']), rate_1.dsa_amount_usd) date_str = date(2017, 3, 31).isoformat() response = self.forced_auth_req('get', reverse('public:dsa_regions'), data={'values_at': date_str}, user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(len(response_json), 0) rate_2.effective_to_date = date(2017, 4, 15) rate_2.save() date_str = datetime(2017, 4, 16, tzinfo=UTC).isoformat() response = self.forced_auth_req('get', reverse('public:dsa_regions'), data={'values_at': date_str}, user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(len(response_json), 0)
def test_effective_from_date(self): region = PublicsDSARegionFactory(rates=[]) now_date = now().date() rate_1 = PublicsDSARateFactory(region=region, effective_from_date=None) self.assertEqual(rate_1.effective_from_date, now_date) effective_from_date = date(2017, 4, 1) rate_2 = PublicsDSARateFactory(region=region, effective_from_date=effective_from_date) self.assertEqual(rate_2.effective_from_date, effective_from_date)
def prepare_travel(self): # Currencies huf = PublicsCurrencyFactory(name='HUF', code='huf') # Add wbs/grant/fund wbs_1 = PublicsWBSFactory(name='WBS #1') grant_1 = PublicsGrantFactory(name='Grant #1') wbs_1.grants.add(grant_1) fund_1 = PublicsFundFactory(name='Fund #1') grant_1.funds.add(fund_1) dsa_region = PublicsDSARegionFactory() PublicsDSARateFactory(region=dsa_region) # Expense types et_t_food = PublicsTravelExpenseTypeFactory( title='Food', vendor_number=TravelExpenseType.USER_VENDOR_NUMBER_PLACEHOLDER) # Make a travel travel = Travel.objects.create(traveler=self.traveler, supervisor=self.unicef_staff, currency=huf) # Add expenses Expense.objects.create(travel=travel, type=et_t_food, currency=huf, amount=35) ItineraryItemFactory(travel=travel, departure_date=datetime(2017, 5, 10, tzinfo=UTC), arrival_date=datetime(2017, 5, 11, tzinfo=UTC), dsa_region=dsa_region) ItineraryItemFactory(travel=travel, departure_date=datetime(2017, 5, 20, tzinfo=UTC), arrival_date=datetime(2017, 5, 21, tzinfo=UTC), dsa_region=dsa_region) # Add cost assignments CostAssignment.objects.create(travel=travel, share=100, wbs=wbs_1, grant=grant_1, fund=fund_1) return travel
def test_new_rate_addition(self): region = PublicsDSARegionFactory(rates=[]) rate_1 = PublicsDSARateFactory(region=region, effective_from_date=date(2017, 4, 17)) self.assertEqual(rate_1.effective_to_date, DSARate.DEFAULT_EFFECTIVE_TILL) rate_2 = PublicsDSARateFactory(region=region, effective_from_date=date(2017, 4, 18)) rate_1.refresh_from_db() self.assertNotEqual(rate_1.effective_to_date, DSARate.DEFAULT_EFFECTIVE_TILL) self.assertNotEqual(rate_1.effective_to_date, DSARate.DEFAULT_EFFECTIVE_TILL) self.assertLess(rate_1.effective_to_date, rate_2.effective_from_date)
def test_travel_creation(self): dsa_region = PublicsDSARegionFactory() currency = PublicsCurrencyFactory() wbs = PublicsWBSFactory() grant = wbs.grants.first() fund = grant.funds.first() location = LocationFactory() purpose = 'Some purpose to check later' data = { 'deductions': [{ 'date': '2016-12-15', 'breakfast': False, 'lunch': False, 'dinner': False, 'accomodation': False, 'no_dsa': False }, { 'date': '2016-12-16', 'breakfast': False, 'lunch': False, 'dinner': False, 'accomodation': False, 'no_dsa': False }], 'itinerary': [{ 'origin': 'Berlin', 'destination': 'Budapest', 'departure_date': '2017-04-14T17:06:55.821490', 'arrival_date': '2017-04-15T17:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': [] }, { 'origin': 'Budapest', 'destination': 'Berlin', 'departure_date': '2017-05-20T12:06:55.821490', 'arrival_date': '2017-05-21T12:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': [] }], 'activities': [{ 'is_primary_traveler': True, 'locations': [location.id], 'travel_type': TravelType.ADVOCACY, 'date': '2016-12-15T15:02:13+01:00' }], 'cost_assignments': [{ 'wbs': wbs.id, 'grant': grant.id, 'fund': fund.id, 'share': '100' }], 'clearances': { 'medical_clearance': 'requested', 'security_clearance': 'requested', 'security_course': 'requested' }, 'ta_required': True, 'international_travel': False, 'mode_of_travel': [ModeOfTravel.BOAT], 'traveler': self.traveler.id, 'supervisor': self.unicef_staff.id, 'start_date': '2016-12-15T15:02:13+01:00', 'end_date': '2016-12-16T15:02:13+01:00', 'estimated_travel_cost': '123', 'currency': currency.id, 'purpose': purpose, 'additional_note': 'Notes', 'medical_clearance': 'requested', 'security_clearance': 'requested', 'security_course': 'requested' } response = self.forced_auth_req( 'post', reverse('t2f:travels:list:state_change', kwargs={'transition_name': 'save_and_submit'}), data=data, user=self.traveler) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['purpose'], purpose) self.assertEqual(response_json['status'], Travel.SUBMITTED) travel_id = response_json['id'] response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'approve' }), user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['status'], Travel.APPROVED) data = {'purpose': 'Some totally different purpose than before'} response = self.forced_auth_req( 'patch', reverse('t2f:travels:details:index', kwargs={'travel_pk': response_json['id']}), data=data, user=self.traveler) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['purpose'], purpose)
def test_completed_counts(self): currency = PublicsCurrencyFactory() expense_type = PublicsTravelExpenseTypeFactory() business_area = PublicsBusinessAreaFactory() dsa_region = PublicsDSARegionFactory() wbs = PublicsWBSFactory(business_area=business_area) grant = wbs.grants.first() fund = grant.funds.first() traveler = UserFactory(is_staff=True) traveler.profile.vendor_number = 'usrvend' traveler.profile.save() travel = TravelFactory(reference_number=make_travel_reference_number(), traveler=traveler, status=Travel.CERTIFIED, supervisor=self.unicef_staff) data = {'cost_assignments': [{'wbs': wbs.id, 'grant': grant.id, 'fund': fund.id, 'share': 100}], 'deductions': [{'date': '2016-11-03', 'breakfast': True, 'lunch': True, 'dinner': False, 'accomodation': True}], 'itinerary': [{'origin': 'Berlin', 'destination': 'Budapest', 'departure_date': '2017-04-14T17:06:55.821490', 'arrival_date': '2017-04-15T17:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': []}, {'origin': 'Budapest', 'destination': 'Berlin', 'departure_date': '2017-05-20T12:06:55.821490', 'arrival_date': '2017-05-21T12:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': []}], 'traveler': traveler.id, 'ta_required': True, 'report': 'Some report', 'currency': currency.id, 'supervisor': self.unicef_staff.id, 'expenses': [{'amount': '120', 'type': expense_type.id, 'currency': currency.id, 'document_currency': currency.id}]} act1 = TravelActivityFactory(travel_type=TravelType.PROGRAMME_MONITORING, primary_traveler=traveler) act2 = TravelActivityFactory(travel_type=TravelType.SPOT_CHECK, primary_traveler=traveler) act1.travels.add(travel) act2.travels.add(travel) partner_programmatic_visits = PartnerOrganization.objects.get(id=act1.partner.id) partner_spot_checks = PartnerOrganization.objects.get(id=act2.partner.id) response = self.forced_auth_req('post', reverse('t2f:travels:details:state_change', kwargs={'travel_pk': travel.id, 'transition_name': 'mark_as_completed'}), user=traveler, data=data) response_json = json.loads(response.rendered_content) partner_programmatic_visits_after_complete = PartnerOrganization.objects.get(id=act1.partner.id) partner_spot_checks_after_complete = PartnerOrganization.objects.get(id=act2.partner.id) self.assertEqual(response_json['status'], Travel.COMPLETED) self.assertEqual(partner_programmatic_visits.hact_values['programmatic_visits']['completed']['total'] + 1, partner_programmatic_visits_after_complete.hact_values[ 'programmatic_visits']['completed']['total']) self.assertEqual(partner_spot_checks.hact_values['spot_checks']['completed']['total'] + 1, partner_spot_checks_after_complete.hact_values['spot_checks']['completed']['total'])
def _prepare_test(self): currency = PublicsCurrencyFactory() expense_type = PublicsTravelExpenseTypeFactory() dsaregion = PublicsDSARegionFactory() airlines = PublicsAirlineCompanyFactory() data = { 'cost_assignments': [], 'deductions': [{ 'date': '2016-11-03', 'breakfast': True, 'lunch': True, 'dinner': False, 'accomodation': True }], 'itinerary': [{ 'origin': 'Berlin', 'destination': 'Budapest', 'departure_date': '2016-11-15T12:06:55.821490', 'arrival_date': '2016-11-16T12:06:55.821490', 'dsa_region': dsaregion.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': [airlines.id] }, { 'origin': 'Budapest', 'destination': 'Berlin', 'departure_date': '2016-11-16T12:06:55.821490', 'arrival_date': '2016-11-17T12:06:55.821490', 'dsa_region': dsaregion.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': [airlines.id] }], 'traveler': self.traveler.id, 'ta_required': True, 'supervisor': self.unicef_staff.id, 'expenses': [{ 'amount': '120', 'type': expense_type.id, 'currency': currency.id, 'document_currency': currency.id }] } response = self.forced_auth_req('post', reverse('t2f:travels:list:index'), data=data, user=self.traveler) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['cost_summary']['preserved_expenses'], None) travel_id = response_json['id'] response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'submit_for_approval' }), data=data, user=self.traveler) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['cost_summary']['preserved_expenses'], None) return travel_id, data
def test_travel_admin_export(self): dsa_brd = PublicsDSARegionFactory(area_code='BRD') PublicsDSARateFactory(region=dsa_brd) dsa_lan = PublicsDSARegionFactory(area_code='LAN') PublicsDSARateFactory(region=dsa_lan) airline_jetstar = PublicsAirlineCompanyFactory(name='JetStar') airline_spiceair = PublicsAirlineCompanyFactory(name='SpiceAir') # First travel setup travel_1 = TravelFactory(traveler=self.traveler, supervisor=self.unicef_staff) travel_1.itinerary.all().delete() itinerary_item_1 = ItineraryItemFactory( travel=travel_1, origin='Origin1', destination='Origin2', departure_date=datetime(2016, 12, 3, 11, tzinfo=UTC), arrival_date=datetime(2016, 12, 3, 12, tzinfo=UTC), mode_of_travel=ModeOfTravel.CAR, dsa_region=dsa_brd) itinerary_item_1.airlines.all().delete() itinerary_item_2 = ItineraryItemFactory( travel=travel_1, origin='Origin2', destination='Origin3', departure_date=datetime(2016, 12, 5, 11, tzinfo=UTC), arrival_date=datetime(2016, 12, 5, 12, tzinfo=UTC), mode_of_travel=ModeOfTravel.PLANE, dsa_region=dsa_lan) itinerary_item_2.airlines.all().delete() itinerary_item_2.airlines.add(airline_jetstar) itinerary_item_3 = ItineraryItemFactory( travel=travel_1, origin='Origin3', destination='Origin1', departure_date=datetime(2016, 12, 6, 11, tzinfo=UTC), arrival_date=datetime(2016, 12, 6, 12, tzinfo=UTC), mode_of_travel=ModeOfTravel.PLANE, dsa_region=None) itinerary_item_3.airlines.all().delete() itinerary_item_3.airlines.add(airline_spiceair) # Second travel setup another_traveler = UserFactory(first_name='Max', last_name='Mustermann') travel_2 = TravelFactory(traveler=another_traveler, supervisor=self.unicef_staff) travel_2.itinerary.all().delete() itinerary_item_4 = ItineraryItemFactory( travel=travel_2, origin='Origin2', destination='Origin1', departure_date=datetime(2016, 12, 5, 11, tzinfo=UTC), arrival_date=datetime(2016, 12, 5, 12, tzinfo=UTC), mode_of_travel=ModeOfTravel.PLANE, dsa_region=dsa_lan) itinerary_item_4.airlines.all().delete() itinerary_item_4.airlines.add(airline_jetstar) itinerary_item_5 = ItineraryItemFactory( travel=travel_2, origin='Origin3', destination='Origin1', departure_date=datetime(2016, 12, 6, 11, tzinfo=UTC), arrival_date=datetime(2016, 12, 6, 12, tzinfo=UTC), mode_of_travel=ModeOfTravel.CAR, dsa_region=None) itinerary_item_5.airlines.all().delete() itinerary_item_5.airlines.add(airline_spiceair) with self.assertNumQueries(6): response = self.forced_auth_req( 'get', reverse('t2f:travels:list:travel_admin_export'), user=self.unicef_staff) export_csv = csv.reader(StringIO(response.content.decode('utf-8'))) rows = [r for r in export_csv] self.assertEqual(len(rows), 6) # check header self.assertEqual(rows[0], [ 'reference_number', 'traveler', 'office', 'section', 'status', 'origin', 'destination', 'departure_time', 'arrival_time', 'dsa_area', 'overnight_travel', 'mode_of_travel', 'airline' ]) self.assertEqual(rows[1], [ '{}/1'.format(datetime.now().year), 'John Doe', 'An Office', travel_1.sector.name, 'planned', 'Origin1', 'Origin2', '03-Dec-2016 11:00 AM', '03-Dec-2016 12:00 PM', 'BRD', '', 'Car', '' ]) self.assertEqual(rows[2], [ '{}/1'.format(datetime.now().year), 'John Doe', 'An Office', travel_1.sector.name, 'planned', 'Origin2', 'Origin3', '05-Dec-2016 11:00 AM', '05-Dec-2016 12:00 PM', 'LAN', '', 'Plane', 'JetStar' ]) self.assertEqual(rows[3], [ '{}/1'.format(datetime.now().year), 'John Doe', 'An Office', travel_1.sector.name, 'planned', 'Origin3', 'Origin1', '06-Dec-2016 11:00 AM', '06-Dec-2016 12:00 PM', 'NODSA', '', 'Plane', 'SpiceAir' ]) self.assertEqual(rows[4], [ '{}/2'.format(datetime.now().year), 'Max Mustermann', 'An Office', travel_2.sector.name, 'planned', 'Origin2', 'Origin1', '05-Dec-2016 11:00 AM', '05-Dec-2016 12:00 PM', 'LAN', '', 'Plane', 'JetStar' ]) self.assertEqual(rows[5], [ '{}/2'.format(datetime.now().year), 'Max Mustermann', 'An Office', travel_2.sector.name, 'planned', 'Origin3', 'Origin1', '06-Dec-2016 11:00 AM', '06-Dec-2016 12:00 PM', 'NODSA', '', 'Car', 'SpiceAir' ])
def setUpTestData(cls): cls.unicef_staff = UserFactory(is_staff=True) cls.currency_usd = PublicsCurrencyFactory(code='USD') cls.currency_huf = PublicsCurrencyFactory(name='Hungarian Forint', code='HUF') cls.user_et_1 = PublicsTravelExpenseTypeFactory( title='Train cost', vendor_number=TravelExpenseType.USER_VENDOR_NUMBER_PLACEHOLDER ) cls.user_et_2 = PublicsTravelExpenseTypeFactory( title='Other expenses', vendor_number=TravelExpenseType.USER_VENDOR_NUMBER_PLACEHOLDER ) cls.ta_et = PublicsTravelExpenseTypeFactory(title='Travel agent') netherlands = PublicsCountryFactory(name='Netherlands', long_name='Netherlands') hungary = PublicsCountryFactory(name='Hungary', long_name='Hungary') denmark = PublicsCountryFactory(name='Denmark', long_name='Denmark') germany = PublicsCountryFactory(name='Germany', long_name='Germany') cls.amsterdam = PublicsDSARegionFactory( country=netherlands, area_name='Amsterdam', area_code='ds1' ) PublicsDSARateFactory( region=cls.amsterdam, dsa_amount_usd=100, dsa_amount_60plus_usd=60 ) cls.budapest = PublicsDSARegionFactory( country=hungary, area_name='Budapest', area_code='ds2' ) PublicsDSARateFactory( region=cls.budapest, dsa_amount_usd=200, dsa_amount_60plus_usd=120 ) cls.copenhagen = PublicsDSARegionFactory( country=denmark, area_name='Copenhagen', area_code='ds3' ) PublicsDSARateFactory( region=cls.copenhagen, dsa_amount_usd=300, dsa_amount_60plus_usd=180 ) cls.dusseldorf = PublicsDSARegionFactory( country=germany, area_name='Duesseldorf', area_code='ds4' ) PublicsDSARateFactory( region=cls.dusseldorf, dsa_amount_usd=400, dsa_amount_60plus_usd=240 ) # Delete default items created by factory cls.travel = TravelFactory(currency=cls.currency_huf) cls.travel.itinerary.all().delete() cls.travel.expenses.all().delete() cls.travel.deductions.all().delete()
def setUp(self): super(InvoiceMaking, self).setUp() self.unicef_staff = UserFactory(is_staff=True) self.traveler = UserFactory() profile = self.traveler.profile profile.vendor_number = 'user0001' profile.save() country = profile.country country.business_area_code = '0060' country.save() dsa_region = PublicsDSARegionFactory() PublicsDSARateFactory(region=dsa_region) # Currencies self.huf = PublicsCurrencyFactory(name='HUF', code='huf') self.usd = PublicsCurrencyFactory(name='USD', code='usd') # Add wbs/grant/fund self.wbs_1 = PublicsWBSFactory(name='WBS #1') self.wbs_2 = PublicsWBSFactory(name='WBS #2') self.grant_1 = PublicsGrantFactory(name='Grant #1') self.grant_2 = PublicsGrantFactory(name='Grant #2') self.grant_3 = PublicsGrantFactory(name='Grant #3') self.wbs_1.grants.add(self.grant_1) self.wbs_2.grants.add(self.grant_2, self.grant_3) self.fund_1 = PublicsFundFactory(name='Fund #1') self.fund_2 = PublicsFundFactory(name='Fund #4') self.grant_1.funds.add(self.fund_1) self.grant_3.funds.add(self.fund_2) # Expense types self.et_t_food = PublicsTravelExpenseTypeFactory( title='Food', vendor_number=TravelExpenseType.USER_VENDOR_NUMBER_PLACEHOLDER) self.et_t_travel = PublicsTravelExpenseTypeFactory( title='Travel', vendor_number=TravelExpenseType.USER_VENDOR_NUMBER_PLACEHOLDER) self.et_t_other = PublicsTravelExpenseTypeFactory( title='Other', vendor_number=TravelExpenseType.USER_VENDOR_NUMBER_PLACEHOLDER) self.et_a_nico = PublicsTravelExpenseTypeFactory( title='Nico Travel', vendor_number='a_nico') self.et_a_torben = PublicsTravelExpenseTypeFactory( title='Torben Travel', vendor_number='a_torben') # Make a travel self.travel = Travel.objects.create(traveler=self.traveler, supervisor=self.unicef_staff, currency=self.huf) ItineraryItemFactory(travel=self.travel, departure_date=datetime(2017, 5, 10, tzinfo=UTC), arrival_date=datetime(2017, 5, 11, tzinfo=UTC), dsa_region=dsa_region) ItineraryItemFactory(travel=self.travel, departure_date=datetime(2017, 5, 20, tzinfo=UTC), arrival_date=datetime(2017, 5, 21, tzinfo=UTC), dsa_region=dsa_region)
def test_overlapping_trips(self): currency = PublicsCurrencyFactory() dsa_region = PublicsDSARegionFactory() data = { 'deductions': [], 'itinerary': [{ 'origin': 'Berlin', 'destination': 'Budapest', 'departure_date': '2017-04-07T17:06:55.821490', 'arrival_date': '2017-04-08T17:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': [] }, { 'origin': 'Budapest', 'destination': 'Berlin', 'departure_date': '2017-05-20T12:06:55.821490', 'arrival_date': '2017-05-21T12:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': [] }], 'activities': [], 'cost_assignments': [], 'clearances': { 'medical_clearance': 'requested', 'security_clearance': 'requested', 'security_course': 'requested' }, 'ta_required': True, 'international_travel': False, 'mode_of_travel': [ModeOfTravel.BOAT], 'traveler': self.traveler.id, 'supervisor': self.unicef_staff.id, 'start_date': '2017-04-07T15:02:13+01:00', 'end_date': '2017-05-22T15:02:13+01:00', 'currency': currency.id, 'purpose': 'Purpose', 'additional_note': 'Notes' } response = self.forced_auth_req('post', reverse('t2f:travels:list:index'), data=data, user=self.traveler) response_json = json.loads(response.rendered_content) travel_id = response_json['id'] with freeze_time(datetime(2017, 4, 14, 16, 00, tzinfo=UTC)): response = self.forced_auth_req( 'post', reverse('t2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'submit_for_approval' }), data=response_json, user=self.traveler) response_json = json.loads(response.rendered_content) self.assertEqual( response_json, { 'non_field_errors': [ 'You have an existing trip with overlapping dates. ' 'Please adjust your trip accordingly.' ] })
def test_daylight_saving(self): budapest_tz = pytz.timezone('Europe/Budapest') self.travel.end_date = budapest_tz.localize(datetime( 2017, 10, 29, 2, 0), is_dst=True) self.travel.save() # Same date as the previous, but it's already after daylight saving start_date = budapest_tz.localize(datetime(2017, 10, 29, 2, 0), is_dst=False).isoformat() currency = PublicsCurrencyFactory() dsa_region = PublicsDSARegionFactory() data = { 'deductions': [], 'itinerary': [{ 'origin': 'Berlin', 'destination': 'Budapest', 'departure_date': '2017-04-14T17:06:55.821490', 'arrival_date': '2017-04-15T17:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': [] }, { 'origin': 'Budapest', 'destination': 'Berlin', 'departure_date': '2017-05-20T12:06:55.821490', 'arrival_date': '2017-05-21T12:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': [] }], 'activities': [], 'cost_assignments': [], 'clearances': { 'medical_clearance': 'requested', 'security_clearance': 'requested', 'security_course': 'requested' }, 'ta_required': True, 'international_travel': False, 'mode_of_travel': [ModeOfTravel.BOAT], 'traveler': self.traveler.id, 'supervisor': self.unicef_staff.id, 'start_date': start_date, 'end_date': '2017-05-22T15:02:13+00:00', 'currency': currency.id, 'purpose': 'Purpose', 'additional_note': 'Notes' } response = self.forced_auth_req('post', reverse('t2f:travels:list:index'), data=data, user=self.traveler) self.assertEqual(response.status_code, 201)
def test_state_machine_flow(self): currency = PublicsCurrencyFactory() expense_type = PublicsTravelExpenseTypeFactory() business_area = PublicsBusinessAreaFactory() dsa_region = PublicsDSARegionFactory() wbs = PublicsWBSFactory(business_area=business_area) grant = wbs.grants.first() fund = grant.funds.first() workspace = self.unicef_staff.profile.country workspace.business_area_code = business_area.code workspace.save() data = { 'cost_assignments': [{ 'wbs': wbs.id, 'grant': grant.id, 'fund': fund.id, 'share': 100 }], 'deductions': [{ 'date': '2016-11-03', 'breakfast': True, 'lunch': True, 'dinner': False, 'accomodation': True }], 'itinerary': [{ 'origin': 'Berlin', 'destination': 'Budapest', 'departure_date': '2017-04-14T17:06:55.821490', 'arrival_date': '2017-04-15T17:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': [] }, { 'origin': 'Budapest', 'destination': 'Berlin', 'departure_date': '2017-05-20T12:06:55.821490', 'arrival_date': '2017-05-21T12:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': [] }], 'traveler': self.traveler.id, 'ta_required': True, 'currency': currency.id, 'supervisor': self.unicef_staff.id, 'expenses': [{ 'amount': '120', 'type': expense_type.id, 'currency': currency.id, 'document_currency': currency.id }] } response = self.forced_auth_req('post', reverse('t2f:travels:list:index'), data=data, user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['cost_summary']['preserved_expenses'], None) travel_id = response_json['id'] response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'submit_for_approval' }), data=data, user=self.traveler) response_json = json.loads(response.rendered_content) travel = Travel.objects.get(id=travel_id) self.assertIsNotNone(travel.submitted_at) self.assertIsNotNone(travel.first_submission_date) response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'approve' }), data=response_json, user=self.traveler) response_json = json.loads(response.rendered_content) response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'send_for_payment' }), data=response_json, user=self.traveler) response_json = json.loads(response.rendered_content) response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'mark_as_certified' }), data=response_json, user=self.traveler) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['non_field_errors'], [ 'Your TA has pending payments to be processed through ' 'VISION. Until payments are completed, you can not certify' ' your TA. Please check with your Finance focal point on ' 'how to proceed.' ]) travel = Travel.objects.get(id=travel_id) travel.invoices.all().update(status=Invoice.SUCCESS) response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'mark_as_certified' }), data=data, user=self.traveler) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['status'], Travel.CERTIFIED) response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'mark_as_completed' }), data=data, user=self.traveler) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['non_field_errors'], ['Field report has to be filled.']) self.assertEqual(travel.report_note, '') # None should be handled as empty string travel.report_note = None # This has to be set explicitly since serializer does not accept None response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'mark_as_completed' }), data=data, user=self.traveler) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['non_field_errors'], ['Field report has to be filled.']) self.assertEqual(travel.report_note, None) data = response_json data['report'] = 'Something' response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'mark_as_completed' }), data=data, user=self.traveler) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['status'], Travel.COMPLETED)
def test_expense_required_on_send_for_payment(self): business_area = PublicsBusinessAreaFactory() dsa_region = PublicsDSARegionFactory() currency = PublicsCurrencyFactory() wbs = PublicsWBSFactory(business_area=business_area) grant = wbs.grants.first() fund = grant.funds.first() workspace = self.unicef_staff.profile.country workspace.business_area_code = business_area.code workspace.save() data = { 'cost_assignments': [{ 'wbs': wbs.id, 'grant': grant.id, 'fund': fund.id, 'share': 100 }], 'deductions': [{ 'date': '2016-11-03', 'breakfast': True, 'lunch': True, 'dinner': False, 'accomodation': True }], 'itinerary': [{ 'origin': 'Berlin', 'destination': 'Budapest', 'departure_date': '2017-04-14T17:06:55.821490', 'arrival_date': '2017-04-15T17:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': [] }, { 'origin': 'Budapest', 'destination': 'Berlin', 'departure_date': '2017-05-20T12:06:55.821490', 'arrival_date': '2017-05-21T12:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': [] }], 'traveler': self.traveler.id, 'ta_required': True, 'supervisor': self.unicef_staff.id, 'expenses': [], 'currency': currency.id } response = self.forced_auth_req('post', reverse('t2f:travels:list:index'), data=data, user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['cost_summary']['preserved_expenses'], None) travel_id = response_json['id'] response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'submit_for_approval' }), data=data, user=self.unicef_staff) response_json = json.loads(response.rendered_content) response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'approve' }), data=response_json, user=self.unicef_staff) response_json = json.loads(response.rendered_content) response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'send_for_payment' }), data=response_json, user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(response.status_code, 200) self.assertEqual( Decimal(response_json['cost_summary']['preserved_expenses']), Decimal('0.00'))
def test_state_machine_flow_invoice_disabled(self): currency = PublicsCurrencyFactory() expense_type = PublicsTravelExpenseTypeFactory() business_area = PublicsBusinessAreaFactory() dsa_region = PublicsDSARegionFactory() wbs = PublicsWBSFactory(business_area=business_area) grant = wbs.grants.first() fund = grant.funds.first() workspace = self.unicef_staff.profile.country workspace.business_area_code = business_area.code workspace.save() data = { 'cost_assignments': [{ 'wbs': wbs.id, 'grant': grant.id, 'fund': fund.id, 'share': 100 }], 'deductions': [{ 'date': '2016-11-03', 'breakfast': True, 'lunch': True, 'dinner': False, 'accomodation': True }], 'itinerary': [{ 'origin': 'Berlin', 'destination': 'Budapest', 'departure_date': '2017-04-14T17:06:55.821490', 'arrival_date': '2017-04-15T17:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': [] }, { 'origin': 'Budapest', 'destination': 'Berlin', 'departure_date': '2017-05-20T12:06:55.821490', 'arrival_date': '2017-05-21T12:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': [] }], 'traveler': self.traveler.id, 'ta_required': True, 'currency': currency.id, 'supervisor': self.unicef_staff.id, 'expenses': [{ 'amount': '120', 'type': expense_type.id, 'currency': currency.id, 'document_currency': currency.id }] } response = self.forced_auth_req('post', reverse('t2f:travels:list:index'), data=data, user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['cost_summary']['preserved_expenses'], None) travel_id = response_json['id'] response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'submit_for_approval' }), data=data, user=self.traveler) response_json = json.loads(response.rendered_content) travel = Travel.objects.get(id=travel_id) self.assertIsNotNone(travel.submitted_at) self.assertIsNotNone(travel.first_submission_date) response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'approve' }), data=response_json, user=self.traveler) response_json = json.loads(response.rendered_content) # Go straight to sent for payment when invoicing is disabled. self.assertEqual(response_json['status'], Travel.SENT_FOR_PAYMENT) # No email has been sent regarding SENT_FOR_PAYMENT status when invoicing is disabled. subjects = [x.subject for x in mail.outbox] self.assertNotIn( 'Travel #{} sent for payment.'.format( response_json["reference_number"]), subjects) response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'mark_as_certified' }), data=response_json, user=self.traveler) response_json = json.loads(response.rendered_content) # No pending invoice check when invoicing is disabled. self.assertEqual(response_json['status'], Travel.CERTIFIED) response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'mark_as_completed' }), data=data, user=self.traveler) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['non_field_errors'], ['Field report has to be filled.']) self.assertEqual(travel.report_note, '') # None should be handled as empty string travel.report_note = None # This has to be set explicitly since serializer does not accept None response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'mark_as_completed' }), data=data, user=self.traveler) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['non_field_errors'], ['Field report has to be filled.']) self.assertEqual(travel.report_note, None) data = response_json data['report'] = 'Something' response = self.forced_auth_req('post', reverse( 't2f:travels:details:state_change', kwargs={ 'travel_pk': travel_id, 'transition_name': 'mark_as_completed' }), data=data, user=self.traveler) response_json = json.loads(response.rendered_content) self.assertEqual(response_json['status'], Travel.COMPLETED)