diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6593db5..9a9bcb9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -31,7 +31,6 @@ jobs: env: OS: ${{ matrix.os }} PYTHON: ${{ matrix.python-version }} - APIARY_URL: ${{ secrets.OPS_APIARY_URL }} OPS_KEY: ${{ secrets.OPS_API_CONSUMER_KEY }} OPS_SECRET: ${{ secrets.OPS_API_CONSUMER_SECRET }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 406f5d2..2a32d6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Add support for Python 3.10 and 3.11 - Configure HTTP client to use a network timeout of 10 seconds - Use `versioningit` for versioning +- Tests: Remove dependency on Apiary Mock Server API ## 4.0.0 (2021-09-19) diff --git a/apiary.apib b/apiary.apib deleted file mode 100644 index 7ea0039..0000000 --- a/apiary.apib +++ /dev/null @@ -1,51 +0,0 @@ -FORMAT: 1A -HOST: https://ops.epo.org - -# OPS v3.2 Test Server -Mock of EPO OPS v3.2 for testing purposes. These end points are based on v -1.3.1 of the reference guide. - -# Group Service Quota Errors -Test end points when user or app reaches OPS quota. - -## IndividualQuotaPerHour exceeded [/individual-per-hour-exceeded/publication/docdb/biblio] -Each registered user account is allowed to use 450MB per hour. - -### POST -+ Request - - ``` - (Forbidden).(Quota).(exceeded) - ``` -+ Response 403 (application/xml) - + Headers - - ``` - X-Rejection-Reason: IndividualQuotaPerHour exceeded - ``` - - + Body - ``` - 403This request has been rejected due to the violation of Fair Use policyhttp://www.epo.org/searching/free/espacenet/fair-use.html - ``` - -## RegisteredQuotaPerWeek exceeded [/registered-per-week-exceeded/publication/docdb/biblio] -Each free registered user account is allowed to use 2.5GB per week. - -### POST -+ Request - - ``` - (Forbidden).(Quota).(exceeded) - ``` -+ Response 403 (application/xml) - + Headers - - ``` - X-Rejection-Reason: RegisteredQuotaPerWeek - ``` - - + Body - ``` - 403This request has been rejected due to the violation of Fair Use policyhttp://www.epo.org/searching/free/espacenet/fair-use.html - ``` diff --git a/docs/backlog.md b/docs/backlog.md index 9aa580f..f546c2f 100644 --- a/docs/backlog.md +++ b/docs/backlog.md @@ -3,8 +3,6 @@ ## Iteration +1 -- Replace Apiary Mock server - https://github.com/ip-tools/python-epo-ops-client/issues/65 - Improve and clean up README - Set up project documentation on Read the Docs - Switch to `pyproject.toml` @@ -18,8 +16,6 @@ This is the content of the original `TODOS.md` file. - Dogpile caching - Generate the key based on a SortedDict of some kind to make sure the exact same request with different argument order are still a hit -- Testing: Replace Apiary tests with monkeypatch? Or - . - Additional services - Legal service - Makefile: Comma in `echo` statements? @@ -33,3 +29,7 @@ This is the content of the original `TODOS.md` file. - Add support for Python 3.10 and 3.11 - Use `ruff` linter, dissolve `flake8` and `isort` - Use `versioningit` for versioning, dissolve `bumpversion` +- Replace Apiary Mock server + https://github.com/ip-tools/python-epo-ops-client/issues/65 +- Testing: Replace Apiary tests with monkeypatch? Or + . diff --git a/docs/sandbox.md b/docs/sandbox.md index ef61124..1decada 100644 --- a/docs/sandbox.md +++ b/docs/sandbox.md @@ -42,23 +42,19 @@ Running the software tests require a working OPS account. ### Prerequisites -Before running the software tests, you will need to define the `APIARY_URL`, +Before running the software tests, you will need to define the `OPS_KEY`, and `OPS_SECRET` environment variables. You can either define them interactively using `export VARNAME=VALUE`, or store them into an `.env` file within the same directory you are running the tests from. See `example.env` for a blueprint. -The Apiary Mock Server API blueprint is available at . - ```shell -export APIARY_URL=https://private-[SECRET]-opsv31.apiary-mock.com/ export OPS_KEY=NKdGMmedZBGLRxTrUwCZMQCYp7Ak5a0u export OPS_SECRET=v3vARPu7DFPEDB8i ``` -_Note that the Apiary URL and the OPS credentials are just for demonstration -purposes, and will not work._ +_Note that the OPS credentials have been invalidated for demonstration purposes._ ### Basics @@ -68,7 +64,7 @@ make check ``` ⚠️ Note that the software tests need a working internet connection, in order to -access both the OPS and the [OPS Apiary Mock Services][apiary ops] services. +access the OPS service. ### Advanced Usage @@ -90,5 +86,4 @@ pytest -k api ``` -[apiary ops]: https://opsv31.docs.apiary.io/ [ops registration]: https://developers.epo.org/user/register diff --git a/example.env b/example.env index 0d42d25..e70eb59 100644 --- a/example.env +++ b/example.env @@ -1,4 +1,3 @@ -# Environment variables sourced during testing -APIARY_URL='URL_TO_MOCK_SERVER' +# Environment variables sourced during testing. OPS_KEY='Consumer Key' OPS_SECRET='Consumer Secret Key' diff --git a/setup.py b/setup.py index a4b8d8a..cad2912 100755 --- a/setup.py +++ b/setup.py @@ -36,6 +36,7 @@ "pytest-cache<2", "pytest-cov<4.2", "python-dotenv<0.20", + "responses<0.24", ], } diff --git a/tests/secrets.py b/tests/secrets.py index 1e47540..bf13873 100644 --- a/tests/secrets.py +++ b/tests/secrets.py @@ -12,7 +12,7 @@ from dotenv import load_dotenv # Prune environment variables. -for k in ("APIARY_URL", "OPS_KEY", "OPS_SECRET"): +for k in ("OPS_KEY", "OPS_SECRET"): if k in os.environ and not os.environ[k]: del os.environ[k] @@ -21,6 +21,5 @@ load_dotenv(dotenv_path) # Set environment variables as constants. -APIARY_URL = os.environ["APIARY_URL"] OPS_KEY = os.environ["OPS_KEY"] OPS_SECRET = os.environ["OPS_SECRET"] diff --git a/tests/test_ops_quota.py b/tests/test_ops_quota.py index 3270912..5cab530 100644 --- a/tests/test_ops_quota.py +++ b/tests/test_ops_quota.py @@ -1,3 +1,5 @@ +import pytest +import responses from pytest import raises from epo_ops.exceptions import ( @@ -6,17 +8,54 @@ ) from epo_ops.models import Docdb -from .secrets import APIARY_URL + +@pytest.fixture +def ops_backend_with_quota_exceeded(): + """ + Emulate an OPS backend, which returns responses on the corresponding + endpoints that the per-hour and per-week service quotas have been exceeded. + """ + token = responses.Response( + responses.POST, + url="https://ops.epo.org/3.2/auth/accesstoken", + status=200, + json={ + "access_token": "foo", + "expires_in": 42, + }, + ) + per_hour = responses.Response( + responses.POST, + url="https://ops.epo.org/3.2/rest-services/individual-per-hour-exceeded/publication/docdb/biblio", + status=403, + headers={ + "X-Rejection-Reason": "IndividualQuotaPerHour exceeded", + "Content-Type": "application/xml", + }, + body="403This request has been rejected due to the violation of Fair Use policyhttp://www.epo.org/searching/free/espacenet/fair-use.html", + ) + per_week = responses.Response( + responses.POST, + url="https://ops.epo.org/3.2/rest-services/registered-per-week-exceeded/publication/docdb/biblio", + status=403, + headers={ + "X-Rejection-Reason": "RegisteredQuotaPerWeek exceeded", + "Content-Type": "application/xml", + }, + body="403This request has been rejected due to the violation of Fair Use policyhttp://www.epo.org/searching/free/espacenet/fair-use.html", + ) + for response in [token, per_hour, per_week]: + responses.add(response) # Helpers def issue_request(client): - return client.published_data("publication", Docdb("Quota", "Forbidden", "exceeded")) + return client.published_data("publication", Docdb("1000000", "EP", "A1")) # Tests -def test_mock_quota_exceeded(all_clients, monkeypatch): - monkeypatch.setattr(all_clients, "__service_url_prefix__", APIARY_URL) +@responses.activate +def test_mock_quota_exceeded(ops_backend_with_quota_exceeded, all_clients, monkeypatch): errors = { "individual-per-hour-exceeded": IndividualQuotaPerHourExceeded, "registered-per-week-exceeded": RegisteredQuotaPerWeekExceeded,