def test_get_qid_for_linking(app_test_client): # Given case_id = str(uuid.uuid4()) qid = '1234567890' qid_payload = { 'caseId': case_id, 'questionnaireId': qid, } # Mock the case API response responses.add(responses.GET, f'{TestConfig.CASE_API_URL}/qids/{qid}', body=json.dumps(qid_payload)) # When response = app_test_client.get( f'case-details/link-qid/?qid={qid}&case_id={case_id}') # Then unittest_helper.assertEqual(response.status_code, 200) unittest_helper.assertIn(f'<b>QID:</b> {qid}'.encode(), response.data) unittest_helper.assertIn(f'<b>Case ID:</b> {case_id}'.encode(), response.data) unittest_helper.assertIn( 'Warning: This QID is already linked to a case. If you submit it will be re-linked.' .encode(), response.data) unittest_helper.assertIn('Link QID'.encode(), response.data)
def test_postcode_search_results(app_test_client): # Given postcode = 'AB1 2CD' url_safe_postcode = urllib.parse.quote(postcode) case = { 'organisationName': 'test_org', 'addressLine1': 'Somewhere', 'addressLine2': 'Over The', 'addressLine3': 'Rainbow', 'townName': 'Newport', 'postcode': postcode, 'caseType': 'HH', 'addressLevel': 'U', 'estabType': 'HOUSEHOLD', 'caseRef': '123456789' } # Mock the case API response responses.add(responses.GET, f'{TestConfig.CASE_API_URL}/cases/postcode/{url_safe_postcode}', body=json.dumps([case])) # When response = app_test_client.get(f'/postcode/?postcode={url_safe_postcode}') # Then unittest_helper.assertEqual(response.status_code, 200) unittest_helper.assertIn(f'1 result for postcode: "{postcode}"'.encode(), response.data) # Check the address summary line is there unittest_helper.assertIn( ', '.join([v for k, v in case.items() if k in ADDRESS_SUMMARY_FIELDS]).encode(), response.data) for value in case.values(): unittest_helper.assertIn(value.encode(), response.data)
def test_case_details_results(app_test_client): # Given case_id = str(uuid.uuid4()) url_safe_case_id = urllib.parse.quote(case_id) case_details_payload = { 'id': case_id, 'organisationName': 'test_org', 'addressLine1': 'Somewhere', 'addressLine2': 'Over The', 'addressLine3': 'Rainbow', 'townName': 'Newport', 'postcode': 'XX0 0XX', 'caseType': 'HH', 'addressLevel': 'U', 'estabType': 'HOUSEHOLD', 'caseRef': '123456789', 'events': [ { 'id': '14063759-c608-4f9f-8fa5-988f52260d7f', 'eventType': 'SAMPLE_LOADED', 'rmEventProcessed': '2020-08-26T07:38:47.453158Z', 'eventDescription': 'Create case sample received', 'eventDate': '2020-08-26T07:38:47.453158Z', 'type': 'None', 'channel': 'RM', 'transactionId': 'None', 'eventPayload': '{\"testKey\": \"testValue\"}' }, ] } # Mock the case API response responses.add( responses.GET, f'{TestConfig.CASE_API_URL}/cases/case-details/{url_safe_case_id}', body=json.dumps(case_details_payload)) # When response = app_test_client.get( f'/case-details/?case_id={url_safe_case_id}') # Then case_details_payload['events'][0]['eventPayload'] = json.loads( case_details_payload['events'][0]['eventPayload']) unittest_helper.assertEqual(response.status_code, 200) assert_case_details(case_details_payload, response)
def test_postcode_search(app_test_client): # When response = app_test_client.get('/') # Then unittest_helper.assertEqual(response.status_code, 200) unittest_helper.assertIn(b'Census ROps Case Management', response.data) unittest_helper.assertIn(b'Search cases', response.data) unittest_helper.assertIn(b'Enter a postcode', response.data)
def test_get_case_details_success(): case_id = str(uuid.uuid4()) url_safe_case_id = urllib.parse.quote(case_id) responses.add(responses.GET, f'{TestConfig.CASE_API_URL}/cases/case-details/{url_safe_case_id}', json.dumps(TEST_CASE)) case_details = get_all_case_details(case_id, TestConfig.CASE_API_URL) unittest_helper.assertEqual(case_details, TEST_CASE)
def test_get_qid(app_test_client): # Given responses.add(responses.GET, f'{TestConfig.CASE_API_URL}/qids/{TEST_QID_JSON["questionnaireId"]}', json.dumps(TEST_QID_JSON)) # When with app_test_client.app_context(): qid_response = get_qid(TEST_QID_JSON['questionnaireId'], TestConfig.CASE_API_URL) # Then unittest_helper.assertEqual(qid_response, TEST_QID_JSON)
def test_get_cases_by_postcode_no_matches(): # Given url_safe_postcode = urllib.parse.quote(TEST_CASE['postcode']) responses.add(responses.GET, f'{TestConfig.CASE_API_URL}/cases/postcode/{url_safe_postcode}', status=404) # When cases = get_cases_by_postcode(TEST_CASE['postcode'], TestConfig.CASE_API_URL) # Then unittest_helper.assertEqual(cases, dict())
def test_get_cases_by_postcode_success(): # Given url_safe_postcode = urllib.parse.quote(TEST_CASE['postcode']) responses.add(responses.GET, f'{TestConfig.CASE_API_URL}/cases/postcode/{url_safe_postcode}', json.dumps([TEST_CASE])) # When cases = get_cases_by_postcode(TEST_CASE['postcode'], TestConfig.CASE_API_URL) # Then unittest_helper.assertEqual(cases, [TEST_CASE])
def test_create_app(): # Given app.testing = True os.environ['APP_SETTINGS'] = 'TestConfig' # When test_app = create_app() # Then # Check the app has been initialized with the given test config unittest_helper.assertEqual(test_app.config['CASE_API_URL'], TestConfig.CASE_API_URL)
def test_json_logging(caplog): # Given os.environ['JSON_INDENT_LOGGING'] = '1' logger_initial_config(app.config) logger = wrap_logger(logging.getLogger()) # When logger.error('Test') message_json = json.loads(caplog.records[-1].getMessage()) # Then message_contents = {"event": "Test", "level": "error", "service": "census-rm-ops-ui"} for key, value in message_contents.items(): unittest_helper.assertEqual(message_json[key], value)
def test_get_iap_public_key(): # Given test_id = 'test_id' dummy_key = 'dummy_key' responses.add(responses.GET, 'https://www.gstatic.com/iap/verify/public_key', body=json.dumps({test_id: dummy_key})) # When we get the public key twice get_iap_public_key(test_id) key = get_iap_public_key(test_id) # Then unittest_helper.assertEqual(key, dummy_key)
def test_postcode_search_no_results(app_test_client): # Given no_matches_postcode = 'test' # Mock the case API response responses.add(responses.GET, f'{TestConfig.CASE_API_URL}/cases/postcode/{no_matches_postcode}', status=404) # When response = app_test_client.get(f'/postcode/?postcode={no_matches_postcode}') # Then unittest_helper.assertEqual(response.status_code, 200) unittest_helper.assertIn(f'0 results for postcode: "{no_matches_postcode}"'.encode(), response.data)
def test_submit_qid_link(patch_uuid, app_test_client): # Given patch_uuid.return_value = str(uuid.UUID('77f1df52-4b43-11e9-910f-b8ca3a9b9f3e')) responses.add(responses.PUT, f'{TestConfig.CASE_API_URL}/qids/link') case_id = str(uuid.uuid4()) expected_payload = {'transactionId': patch_uuid.return_value, 'channel': 'ROPS_UI', 'qidLink': {"caseId": case_id, "questionnaireId": "987654323456789"}} # When with app_test_client.app_context(): qid_response = submit_qid_link(TEST_QID_JSON['questionnaireId'], case_id, TestConfig.CASE_API_URL) # Then unittest_helper.assertEqual(qid_response.status_code, 200) unittest_helper.assertEqual(json.dumps(expected_payload).encode(), qid_response.request.body)
def test_get_iap_public_key_cache_refresh(): # Given responses.add(responses.GET, 'https://www.gstatic.com/iap/verify/public_key', body=json.dumps({'key_1': 'dummy_1'})) # We get the first key to cache the result get_iap_public_key('key_1') # Add another key to the response responses.remove(responses.GET, 'https://www.gstatic.com/iap/verify/public_key') responses.add(responses.GET, 'https://www.gstatic.com/iap/verify/public_key', body=json.dumps({ 'key_1': 'dummy_1', 'key_2': 'dummy_2' })) # When we get the second key actual_key_2 = get_iap_public_key('key_2') # Then the cache is refreshed so the second key can be accessed unittest_helper.assertEqual(actual_key_2, 'dummy_2')
def test_submitting_qid_link_to_case_api(app_test_client): # Given case_id = str(uuid.uuid4()) qid = '1234567890' url_safe_case_id = urllib.parse.quote(case_id) qid_case_api_payload = { 'caseId': case_id, 'questionnaireId': qid, } request_qid_payload = { 'case_id': case_id, 'qid': qid, } event_payload = json.dumps({"caseId": case_id, "qid": qid}) case_details_payload = { 'id': case_id, 'organisationName': 'test_org', 'addressLine1': 'Somewhere', 'addressLine2': 'Over The', 'addressLine3': 'Rainbow', 'townName': 'Newport', 'postcode': 'XX0 0XX', 'caseType': 'HH', 'addressLevel': 'U', 'estabType': 'HOUSEHOLD', 'caseRef': '123456789', 'events': [ { 'id': '14063759-c608-4f9f-8fa5-988f52260d7f', 'eventType': 'QUESTIONNAIRE_LINKED', 'rmEventProcessed': '2020-08-26T07:38:47.453158Z', 'eventDescription': 'Questionnaire Linked', 'eventDate': '2020-08-26T07:38:47.453158Z', 'type': 'None', 'channel': 'RM', 'transactionId': 'None', 'eventPayload': event_payload }, ] } responses.add( responses.GET, f'{TestConfig.CASE_API_URL}/cases/case-details/{url_safe_case_id}', body=json.dumps(case_details_payload)) # Mock the case API response responses.add(responses.PUT, f'{TestConfig.CASE_API_URL}/qids/link', json=qid_case_api_payload) # When response = app_test_client.post('case-details/link-qid/submit/', follow_redirects=True, data=request_qid_payload) # Then unittest_helper.assertEqual(response.status_code, 200) unittest_helper.assertIn('QUESTIONNAIRE_LINKED'.encode(), response.data) unittest_helper.assertIn('QID link has been submitted'.encode(), response.data)
def test_get_qid_failed_linking(app_test_client): # Given case_id = str(uuid.uuid4()) qid = '1234567890' url_safe_case_id = urllib.parse.quote(case_id) case_details_payload = { 'id': case_id, 'organisationName': 'test_org', 'addressLine1': 'Somewhere', 'addressLine2': 'Over The', 'addressLine3': 'Rainbow', 'townName': 'Newport', 'postcode': 'XX0 0XX', 'caseType': 'HH', 'addressLevel': 'U', 'estabType': 'HOUSEHOLD', 'caseRef': '123456789', 'events': [ { 'id': '14063759-c608-4f9f-8fa5-988f52260d7f', 'eventType': 'SAMPLE_LOADED', 'rmEventProcessed': '2020-08-26T07:38:47.453158Z', 'eventDescription': 'Create case sample received', 'eventDate': '2020-08-26T07:38:47.453158Z', 'type': 'None', 'channel': 'RM', 'transactionId': 'None', 'eventPayload': '{\"testKey\": \"testValue\"}' }, ] } # Mock the case API response responses.add(responses.GET, f'{TestConfig.CASE_API_URL}/qids/{qid}', status=404) responses.add( responses.GET, f'{TestConfig.CASE_API_URL}/cases/case-details/{url_safe_case_id}', body=json.dumps(case_details_payload)) # When response = app_test_client.get( f'case-details/link-qid/?qid={qid}&case_id={case_id}', follow_redirects=True) # Then unittest_helper.assertEqual(response.status_code, 200) unittest_helper.assertIn('QID does not exist in RM'.encode(), response.data)