|
1 | 1 | import unittest |
| 2 | +from unittest import mock |
2 | 3 |
|
3 | 4 | from fhirclient.client import FHIRClient |
| 5 | +from fhirclient.server import FHIRNotFoundException, FHIRUnauthorizedException |
| 6 | + |
| 7 | +# Smallest valid-but-fake client state |
| 8 | +MIN_STATE = { |
| 9 | + "server": {"base_uri": "http://example.com/fhir"}, |
| 10 | +} |
4 | 11 |
|
5 | 12 |
|
6 | 13 | class TestClient(unittest.TestCase): |
@@ -39,3 +46,54 @@ def test_load_from_state(self): |
39 | 46 | client.from_state({'app_id': 'NewID', 'server': state['server']}) |
40 | 47 | self.assertEqual('NewID', client.app_id) |
41 | 48 | self.assertEqual('LaunchToken', client.launch_token) |
| 49 | + |
| 50 | + @mock.patch("fhirclient.models.patient.Patient.read") |
| 51 | + def test_patient_property_happy_path(self, mock_read): |
| 52 | + save_func = mock.MagicMock() |
| 53 | + |
| 54 | + # Verify that we gracefully handle no patient_id being given |
| 55 | + client = FHIRClient(state=MIN_STATE, save_func=save_func) |
| 56 | + self.assertIsNone(client.patient) |
| 57 | + self.assertEqual(mock_read.call_count, 0) |
| 58 | + self.assertEqual(save_func.call_count, 0) |
| 59 | + |
| 60 | + # Verify we expose the provided patient ID as a Patient object |
| 61 | + client = FHIRClient(state={"patient_id": "P123", **MIN_STATE}, save_func=save_func) |
| 62 | + self.assertIsNotNone(client.patient) |
| 63 | + self.assertEqual(mock_read.call_count, 1) |
| 64 | + self.assertEqual(mock_read.call_args, mock.call("P123", client.server)) |
| 65 | + self.assertEqual(save_func.call_count, 1) |
| 66 | + |
| 67 | + @mock.patch("fhirclient.models.patient.Patient.read") |
| 68 | + @mock.patch("fhirclient.client.FHIRClient.reauthorize") |
| 69 | + def test_patient_property_unauthorized(self, mock_reauthorize, mock_read): |
| 70 | + """We should attempt to reauthorize and re-request the patient""" |
| 71 | + |
| 72 | + client = FHIRClient(state={"patient_id": "P123", **MIN_STATE}) |
| 73 | + |
| 74 | + # First try with a failed re-authorize |
| 75 | + mock_read.side_effect = FHIRUnauthorizedException("response") |
| 76 | + mock_reauthorize.return_value = False |
| 77 | + self.assertIsNone(client.patient) |
| 78 | + self.assertEqual(mock_read.call_count, 1) |
| 79 | + self.assertEqual(mock_reauthorize.call_count, 1) |
| 80 | + |
| 81 | + # Then with a successful re-authorize |
| 82 | + mock_read.reset_mock() |
| 83 | + mock_read.side_effect = [FHIRUnauthorizedException("response"), mock.MagicMock()] |
| 84 | + mock_reauthorize.reset_mock() |
| 85 | + mock_reauthorize.return_value = True |
| 86 | + self.assertIsNotNone(client.patient) |
| 87 | + self.assertEqual(mock_read.call_count, 2) |
| 88 | + self.assertEqual(mock_reauthorize.call_count, 1) |
| 89 | + |
| 90 | + @mock.patch("fhirclient.models.patient.Patient.read") |
| 91 | + def test_patient_property_not_found(self, mock_read): |
| 92 | + """We should attempt to reauthorize and re-request the patient""" |
| 93 | + mock_read.side_effect = FHIRNotFoundException("response") |
| 94 | + |
| 95 | + client = FHIRClient(state={"patient_id": "P123", **MIN_STATE}) |
| 96 | + self.assertEqual(client.patient_id, "P123") # sanity check before we start |
| 97 | + |
| 98 | + self.assertIsNone(client.patient) |
| 99 | + self.assertIsNone(client.patient_id) # we clear out the patient id |
0 commit comments