Skip to content

Commit

Permalink
tests: Remove dependency on Apiary Mock Server API
Browse files Browse the repository at this point in the history
Instead, use a corresponding self-contained configuration based on the
`responses` library, which intercepts HTTP requests made by the
`requests` library.
  • Loading branch information
amotl committed Aug 25, 2023
1 parent b78de3a commit df3c6eb
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 72 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
51 changes: 0 additions & 51 deletions apiary.apib

This file was deleted.

8 changes: 4 additions & 4 deletions docs/backlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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
<https://pypi.python.org/pypi/pytest-localserver>.
- Additional services
- Legal service
- Makefile: Comma in `echo` statements?
Expand All @@ -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
<https://pypi.python.org/pypi/pytest-localserver>.
11 changes: 3 additions & 8 deletions docs/sandbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://opsv31.docs.apiary.io/>.

```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
Expand All @@ -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
Expand All @@ -90,5 +86,4 @@ pytest -k api
```


[apiary ops]: https://opsv31.docs.apiary.io/
[ops registration]: https://developers.epo.org/user/register
3 changes: 1 addition & 2 deletions example.env
Original file line number Diff line number Diff line change
@@ -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'
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"pytest-cache<2",
"pytest-cov<4.2",
"python-dotenv<0.20",
"responses<0.24",
],
}

Expand Down
3 changes: 1 addition & 2 deletions tests/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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"]
47 changes: 43 additions & 4 deletions tests/test_ops_quota.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest
import responses
from pytest import raises

from epo_ops.exceptions import (
Expand All @@ -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="<error><code>403</code><message>This request has been rejected due to the violation of Fair Use policy</message><moreInfo>http://www.epo.org/searching/free/espacenet/fair-use.html</moreInfo></error>",
)
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="<error><code>403</code><message>This request has been rejected due to the violation of Fair Use policy</message><moreInfo>http://www.epo.org/searching/free/espacenet/fair-use.html</moreInfo></error>",
)
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,
Expand Down

0 comments on commit df3c6eb

Please sign in to comment.