def test_generate_eq_url_missing_mandatory_event_date(self): # Given a mandatory event date does not exist collex_events_dates = [{ 'id': 'e82e7ec9-b14e-412c-813e-edfd2e03e773', 'collectionExerciseId': '8d926ae3-fb3c-4c25-9f0f-356ded7d1ac0', 'tag': 'return_by', 'timestamp': '2018-03-27T01:00:00.000+01:00' }, { 'id': '8a24731e-3d79-4f3c-b6eb-3b199f53694f', 'collectionExerciseId': '8d926ae3-fb3c-4c25-9f0f-356ded7d1ac0', 'tag': 'reminder', 'timestamp': '2018-04-03T01:00:00.000+01:00' }] # When find_event_date_by_tag is called with a search param # Then an InvalidEqPayLoad is raised with self.assertRaises(InvalidEqPayLoad) as e: EqPayload()._find_event_date_by_tag('return by', collex_events_dates, '123', True) self.assertEqual( e.exception.error, 'Mandatory event not found for collection 123 for search param return by' )
def test_generate_eq_url_iso8601_date_format(self): # Given an invalid date date = '2007-01-25T12:00:00Z' # When format_string_long_date_time_to_short_date is called # Then an InvalidEqPayLoad is raised result = EqPayload()._format_string_long_date_time_to_short_date(date) self.assertEqual(result, '2007-01-25')
def test_generate_eq_url_incorrect_date_format(self): # Given an invalid date date = 'invalid' # When format_string_long_date_time_to_short_date is called # Then an InvalidEqPayLoad is raised with self.assertRaises(InvalidEqPayLoad) as e: EqPayload()._format_string_long_date_time_to_short_date(date) self.assertEqual(e.exception.error, 'Unable to format invalid')
def test_generate_eq_url_non_mandatory_event_date_is_none(self): # Given a non mandatory event date does not exist collex_events_dates = [] # When find_event_date_by_tag is called with a search param # Then a None response is returned and no exception is raised response = EqPayload()._find_event_date_by_tag('employment', collex_events_dates, '123', False) self.assertEqual(response, None)
def test_generate_eq_url_no_eq_id(self, mock_request): # Given all external services are mocked and we have an EQ collection instrument without an EQ ID with open( 'tests/test_data/collection_instrument/collection_instrument_eq_no_eq_id.json' ) as json_data: collection_instrument_eq_no_eq_id = json.load(json_data) mock_request.get(url_get_ci, json=collection_instrument_eq_no_eq_id) # When create_payload is called # Then an InvalidEqPayLoad is raised with self.assertRaises(InvalidEqPayLoad) as e: EqPayload().create_payload(case) self.assertEqual( e.exception.error, 'Collection instrument 68ad4018-2ddd-4894-89e7-33f0135887a2 ' 'classifiers are incorrect or missing')
def test_generate_eq_url_seft(self, mock_request): # Given all external services are mocked and we have seft collection instrument mock_request.get(url_get_collection_exercise, json=collection_exercise) mock_request.get(url_get_collection_exercise_events, json=collection_exercise_events) mock_request.get(url_get_business_party, json=business_party) mock_request.get(url_get_survey, json=survey) mock_request.get(url_get_ci, json=collection_instrument_seft) # When create_payload is called # Then an InvalidEqPayLoad is raised with self.assertRaises(InvalidEqPayLoad) as e: EqPayload().create_payload(case) self.assertEqual( e.exception.error, 'Collection instrument 68ad4018-2ddd-4894-89e7-33f0135887a2 type is not EQ' )
def test_generate_eq_url_non_mandatory_event_date_is_returned(self): # Given a non mandatory event date exists collex_events_dates = [{ 'id': 'e82e7ec9-b14e-412c-813e-edfd2e03e773', 'collectionExerciseId': '8d926ae3-fb3c-4c25-9f0f-356ded7d1ac0', 'tag': 'return_by', 'timestamp': '2018-03-27T01:00:00.000+01:00' }, { 'id': '8a24731e-3d79-4f3c-b6eb-3b199f53694f', 'collectionExerciseId': '8d926ae3-fb3c-4c25-9f0f-356ded7d1ac0', 'tag': 'employment', 'timestamp': '2018-04-03T01:00:00.000+01:00' }] # When find_event_date_by_tag is called with a search param # Then the formatted date is returned response = EqPayload()._find_event_date_by_tag('employment', collex_events_dates, '123', False) self.assertEqual(response, '2018-04-03')
def get(): """ Generate EQ URL with JWT by creating payload from services and then encrypting. :return EQ URL """ case_id = request.args['case_id'] party_id = request.args['party_id'] logger.info('Generating EQ URL', case_id=case_id, party_id=party_id) case = case_controller.get_case_by_case_id(case_id) if case['caseGroup']['caseGroupStatus'] == 'COMPLETE': logger.info( 'The case group status is complete, opening an EQ is forbidden', case_id=case_id, party_id=party_id) return Response(status=403) case_controller.check_case_permissions(party_id, case['partyId'], case_id=case_id) payload = EqPayload().create_payload(case) json_secret_keys = current_app.config['JSON_SECRET_KEYS'] encrypter = Encrypter(json_secret_keys) token = encrypter.encrypt(payload) eq_url = current_app.config['EQ_URL'] + token category = 'EQ_LAUNCH' case_controller.post_case_event( case_id, party_id=party_id, category=category, description= f"Instrument {case['collectionInstrumentId']} launched by {party_id} for case {case_id}" ) return make_response(jsonify(eq_url=eq_url), 200)