Skip to content

Commit

Permalink
Add tests for collections (#91)
Browse files Browse the repository at this point in the history
* adding test cases for version=1 spocs requests

* separating collections test, since v2 will do different things based on the kevel response, whereas v1 will return the same shape for both kinds of kevel responses

* removing version_two example; this had a misnamed key but the version 2 request was getting generated from the default values

* now with working filter

* tests for collections should mock a collections response from kevel
  • Loading branch information
dmueller authored Jan 10, 2024
1 parent d1f18fa commit 58d9ee7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
11 changes: 10 additions & 1 deletion openapi/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/SpocRequest'
examples:
version_one:
summary: Request from client that does not support collections, FireFox version <= 74
value:
version: 1
consumer_key: "40249-e88c401e1b1f2242d9e441c4"
pocket_id: "{12345678-8901-2345-aaaa-bbbbbbcccccc}"
responses:
'200':
description: Responds with settings and a list of spocs.
Expand Down Expand Up @@ -69,7 +76,6 @@ components:
format: int32
minimum: 1
maximum: 2
default: 2
example: 2
consumer_key:
type: string
Expand Down Expand Up @@ -228,6 +234,9 @@ components:
sponsor:
type: string
example: NextAdvisor
context:
type: string
example: "Sponsored by NextAdvisor"
items:
type: array
items:
Expand Down
42 changes: 39 additions & 3 deletions tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from aioresponses import aioresponses

from tests.fixtures.mock_factory import get_mocked_geolocation_factory
from tests.fixtures.mock_decision import mock_decision_2
from tests.fixtures.mock_decision import mock_decision_2, mock_collection_response

__FACTORY = get_mocked_geolocation_factory()
with patch("app.geolocation.factory.Factory.get_instance", return_value=__FACTORY):
Expand All @@ -23,16 +23,52 @@
}
}

MOCK_DECISIONS_COLLECTION_RESPONSE = {
"user": {
"key": "{12345678-8901-2345-aaaa-bbbbbbcccccc}",
},
"decisions": {
"spocs": [mock_collection_response]
}
}


@responses.activate
@schema.parametrize()
def test_api(case: schemathesis.Case) -> None:
@schema.parametrize(endpoint="/user")
def test_delete_api(case: schemathesis.Case) -> None:
# Mock call to forget API
responses.delete("https://e-10250.adzerk.net/udb/10250/")

# Call Pocket Proxy and validate response
response = case.call_asgi()
case.validate_response(response)


@responses.activate
@schema.parametrize(endpoint="/spocs")
def test_spocs_api(case: schemathesis.Case) -> None:
with aioresponses() as m:
# Mock call to decisions API
m.post("https://e-10250.adzerk.net/api/v2", payload=MOCK_DECISIONS_RESPONSE)

# Call Pocket Proxy and validate response
response = case.call_asgi()
case.validate_response(response)


def version_two(context, body):
return body.get("version") == 2


# clients pass in version=2 if they support collections
@responses.activate
@schema.hooks.apply(version_two, name="filter_body")
@schema.parametrize(endpoint="/spocs")
def test_spocs_collection_api(case: schemathesis.Case) -> None:
with aioresponses() as m:
# Mock call to decisions API
m.post("https://e-10250.adzerk.net/api/v2", payload=MOCK_DECISIONS_COLLECTION_RESPONSE)

# Call Pocket Proxy and validate response
response = case.call_asgi()
case.validate_response(response)

0 comments on commit 58d9ee7

Please sign in to comment.