|
| 1 | +import pytest |
| 2 | + |
| 3 | +from freezegun import freeze_time |
| 4 | +from requests import ConnectTimeout |
| 5 | +from nypl_py_utils.classes.cloudlibrary_client import ( |
| 6 | + CloudLibraryClient, CloudLibraryClientError) |
| 7 | + |
| 8 | +_API_URL = "https://partner.yourcloudlibrary.com/cirrus/library/" |
| 9 | + |
| 10 | +# catch-all API response since we're not testing actual data |
| 11 | +_TEST_LIBRARY_EVENTS_RESPONSE = """<LibraryEventBatch |
| 12 | +xmlns:xsd="http://www.w3.org/2001/XMLSchema" |
| 13 | +xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
| 14 | +<PublishId>4302fcca-ef99-49bf-bd29-d673e990f765</PublishId> |
| 15 | +<PublishDateTimeInUTC>2024-11-10T17:35:18</PublishDateTimeInUTC> |
| 16 | +<LastEventDateTimeInUTC>2012-11-11T13:58:52.055</LastEventDateTimeInUTC> |
| 17 | +<Events> |
| 18 | +<CloudLibraryEvent> |
| 19 | +<EventId>4302fcca-ef99-49bf-bd29-d673e990f4a7</EventId> |
| 20 | +<EventType>CHECKIN</EventType> |
| 21 | +<EventStartDateTimeInUTC>2024-11-10T05:07:56</EventStartDateTimeInUTC> |
| 22 | +<EventEndDateTimeInUTC>2024-11-10T07:50:59</EventEndDateTimeInUTC> |
| 23 | +<ItemId>edbz9</ItemId> |
| 24 | +<ItemLibraryId>1234</ItemLibraryId> |
| 25 | +<ISBN>9780307238405</ISBN> |
| 26 | +<PatronId>TestUser1</PatronId> |
| 27 | +<PatronLibraryId>1234</PatronLibraryId> |
| 28 | +<EventPublishDateTimeInUTC>2024-11-10T17:35:18</EventPublishDateTimeInUTC> |
| 29 | +</CloudLibraryEvent> |
| 30 | +</Events> |
| 31 | +</LibraryEventBatch> |
| 32 | +""" |
| 33 | + |
| 34 | + |
| 35 | +@freeze_time("2024-11-11 10:00:00") |
| 36 | +class TestCloudLibraryClient: |
| 37 | + @pytest.fixture |
| 38 | + def test_instance(self): |
| 39 | + return CloudLibraryClient( |
| 40 | + "library_id", "account_id", "account_key") |
| 41 | + |
| 42 | + def test_get_library_events_success_no_args( |
| 43 | + self, test_instance, mocker): |
| 44 | + start = "2024-11-10T10:00:00" |
| 45 | + end = "2024-11-11T10:00:00" |
| 46 | + mock_request = mocker.patch( |
| 47 | + "nypl_py_utils.classes.cloudlibrary_client.CloudLibraryClient.request", # noqa |
| 48 | + return_value=_TEST_LIBRARY_EVENTS_RESPONSE) |
| 49 | + response = test_instance.get_library_events() |
| 50 | + |
| 51 | + mock_request.assert_called_once_with( |
| 52 | + path=f"data/cloudevents?startdate={start}&enddate={end}", |
| 53 | + method_type="GET") |
| 54 | + assert response == _TEST_LIBRARY_EVENTS_RESPONSE |
| 55 | + |
| 56 | + def test_get_library_events_success_with_start_and_end_date( |
| 57 | + self, test_instance, mocker): |
| 58 | + start = "2024-11-01T10:00:00" |
| 59 | + end = "2024-11-05T10:00:00" |
| 60 | + mock_request = mocker.patch( |
| 61 | + "nypl_py_utils.classes.cloudlibrary_client.CloudLibraryClient.request", # noqa |
| 62 | + return_value=_TEST_LIBRARY_EVENTS_RESPONSE) |
| 63 | + response = test_instance.get_library_events(start, end) |
| 64 | + |
| 65 | + mock_request.assert_called_once_with( |
| 66 | + path=f"data/cloudevents?startdate={start}&enddate={end}", |
| 67 | + method_type="GET") |
| 68 | + assert response == _TEST_LIBRARY_EVENTS_RESPONSE |
| 69 | + |
| 70 | + def test_get_library_events_success_with_no_end_date( |
| 71 | + self, test_instance, mocker): |
| 72 | + start = "2024-11-01T09:00:00" |
| 73 | + end = "2024-11-11T10:00:00" |
| 74 | + mock_request = mocker.patch( |
| 75 | + "nypl_py_utils.classes.cloudlibrary_client.CloudLibraryClient.request", # noqa |
| 76 | + return_value=_TEST_LIBRARY_EVENTS_RESPONSE) |
| 77 | + response = test_instance.get_library_events(start) |
| 78 | + |
| 79 | + mock_request.assert_called_once_with( |
| 80 | + path=f"data/cloudevents?startdate={start}&enddate={end}", |
| 81 | + method_type="GET") |
| 82 | + assert response == _TEST_LIBRARY_EVENTS_RESPONSE |
| 83 | + |
| 84 | + def test_get_library_events_exception_when_start_date_greater_than_end( |
| 85 | + self, test_instance, caplog): |
| 86 | + start = "2024-11-11T09:00:00" |
| 87 | + end = "2024-11-01T10:00:00" |
| 88 | + |
| 89 | + with pytest.raises(CloudLibraryClientError): |
| 90 | + test_instance.get_library_events(start, end) |
| 91 | + assert (f"Start date {start} greater than end date {end}, " |
| 92 | + f"cannot retrieve library events") in caplog.text |
| 93 | + |
| 94 | + def test_get_library_events_exception_when_connection_timeout( |
| 95 | + self, test_instance, requests_mock, caplog): |
| 96 | + start = "2024-11-10T10:00:00" |
| 97 | + end = "2024-11-11T10:00:00" |
| 98 | + url = f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}" # noqa |
| 99 | + |
| 100 | + # We're making sure that a separate error during a sub-method will |
| 101 | + # still result in CloudLibraryClientError |
| 102 | + requests_mock.get( |
| 103 | + url, exc=ConnectTimeout) |
| 104 | + |
| 105 | + with pytest.raises(CloudLibraryClientError): |
| 106 | + test_instance.get_library_events() |
| 107 | + assert (f"Failed to retrieve response from {url}") in caplog.text |
| 108 | + |
| 109 | + def test_get_request_success(self, test_instance, requests_mock): |
| 110 | + start = "2024-11-10T10:00:00" |
| 111 | + end = "2024-11-11T10:00:00" |
| 112 | + url = f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}" # noqa |
| 113 | + expected_headers = {"3mcl-Datetime": "Mon, 11 Nov 2024 10:00:00 GMT", |
| 114 | + "3mcl-Authorization": "3MCLAUTH account_id:KipNmbVsmsT2xPjP4oHAaR3n00JgcszfF6mQRffBoRk=", # noqa |
| 115 | + "3mcl-APIVersion": "3.0.2", |
| 116 | + "Accept": "application/xml"} |
| 117 | + requests_mock.get( |
| 118 | + url=url, text=_TEST_LIBRARY_EVENTS_RESPONSE) |
| 119 | + |
| 120 | + response = test_instance.request( |
| 121 | + path=f"data/cloudevents?startdate={start}&enddate={end}", |
| 122 | + method_type="GET") |
| 123 | + |
| 124 | + assert response.text == _TEST_LIBRARY_EVENTS_RESPONSE |
| 125 | + assert requests_mock.request_history[0].method == "GET" |
| 126 | + assert requests_mock.request_history[0].url == url |
| 127 | + assert requests_mock.request_history[0].body is None |
| 128 | + assert expected_headers.items() <= dict( |
| 129 | + requests_mock.request_history[0].headers).items() |
| 130 | + |
| 131 | + def test_put_request_success(self, test_instance, requests_mock): |
| 132 | + start = "2024-11-10T10:00:00" |
| 133 | + end = "2024-11-11T10:00:00" |
| 134 | + url = f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}" # noqa |
| 135 | + expected_headers = {"3mcl-Datetime": "Mon, 11 Nov 2024 10:00:00 GMT", |
| 136 | + "3mcl-Authorization": "3MCLAUTH account_id:3M773C6ZVWmB/ISoSjQy9iBp48T4tUWhoNOwXaseMtE=", # noqa |
| 137 | + "3mcl-APIVersion": "3.0.2", |
| 138 | + "Content-Type": "application/xml"} |
| 139 | + requests_mock.put( |
| 140 | + url=url, text=_TEST_LIBRARY_EVENTS_RESPONSE) |
| 141 | + |
| 142 | + response = test_instance.request( |
| 143 | + path=f"data/cloudevents?startdate={start}&enddate={end}", |
| 144 | + method_type="PUT", |
| 145 | + body={"test": "test"}) |
| 146 | + |
| 147 | + assert response.text == _TEST_LIBRARY_EVENTS_RESPONSE |
| 148 | + assert requests_mock.request_history[0].method == "PUT" |
| 149 | + assert requests_mock.request_history[0].url == url |
| 150 | + assert requests_mock.request_history[0].body == "test=test" |
| 151 | + assert expected_headers.items() <= dict( |
| 152 | + requests_mock.request_history[0].headers).items() |
| 153 | + |
| 154 | + def test_post_request_success(self, test_instance, requests_mock): |
| 155 | + start = "2024-11-10T10:00:00" |
| 156 | + end = "2024-11-11T10:00:00" |
| 157 | + url = f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}" # noqa |
| 158 | + expected_headers = {"3mcl-Datetime": "Mon, 11 Nov 2024 10:00:00 GMT", |
| 159 | + "3mcl-Authorization": "3MCLAUTH account_id:vF0zI6ee1w1PbTLQ9EVvtxRly2vpCRxdBdAHb8DZQ4E=", # noqa |
| 160 | + "3mcl-APIVersion": "3.0.2", |
| 161 | + "Content-Type": "application/xml"} |
| 162 | + requests_mock.post( |
| 163 | + url=url, text=_TEST_LIBRARY_EVENTS_RESPONSE) |
| 164 | + |
| 165 | + response = test_instance.request( |
| 166 | + path=f"data/cloudevents?startdate={start}&enddate={end}", |
| 167 | + method_type="POST", |
| 168 | + body={"test": "test"}) |
| 169 | + |
| 170 | + assert response.text == _TEST_LIBRARY_EVENTS_RESPONSE |
| 171 | + assert requests_mock.request_history[0].method == "POST" |
| 172 | + assert requests_mock.request_history[0].url == url |
| 173 | + assert requests_mock.request_history[0].body == "test=test" |
| 174 | + assert expected_headers.items() <= dict( |
| 175 | + requests_mock.request_history[0].headers).items() |
| 176 | + |
| 177 | + def test_request_failure(self, test_instance, |
| 178 | + requests_mock, caplog): |
| 179 | + start = "2024-11-10T10:00:00" |
| 180 | + end = "2024-11-11T10:00:00" |
| 181 | + url = f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}" # noqa |
| 182 | + requests_mock.get( |
| 183 | + url, exc=ConnectTimeout) |
| 184 | + |
| 185 | + with pytest.raises(CloudLibraryClientError): |
| 186 | + test_instance.request( |
| 187 | + path=f"data/cloudevents?startdate={start}&enddate={end}", |
| 188 | + method_type="GET") |
| 189 | + assert (f"Failed to retrieve response from " |
| 190 | + f"{url}: ConnectTimeout()") in caplog.text |
| 191 | + |
| 192 | + def test_create_request_body_success(self, test_instance): |
| 193 | + request_type = "CheckoutRequest" |
| 194 | + item_id = "df45qw" |
| 195 | + patron_id = "215555602845" |
| 196 | + EXPECTED_REQUEST_BODY = (f"<{request_type}><ItemId>{item_id}</ItemId>" |
| 197 | + f"<PatronId>{patron_id}</PatronId>" |
| 198 | + f"</{request_type}>") |
| 199 | + request_body = test_instance.create_request_body( |
| 200 | + request_type, item_id, patron_id) |
| 201 | + |
| 202 | + assert request_body == EXPECTED_REQUEST_BODY |
0 commit comments