Skip to content

Commit

Permalink
Merge pull request #169 from jsmolar/cache
Browse files Browse the repository at this point in the history
Simplify Mockserver expectation
  • Loading branch information
pehala authored Feb 6, 2023
2 parents 4f666d0 + ba15b3f commit 9089cab
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 29 deletions.
82 changes: 64 additions & 18 deletions testsuite/mockserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,63 @@


class Mockserver:
"""Mockserver deployed in Openshift (located in Tools or self-managed)"""
"""
Mockserver deployed in Openshift (located in Tools or self-managed)
All existing expectations are stored in `self.expectations: dict[expectation_id, expectation_path]`
"""

def __init__(self, url):
self.url = url
self.expectations = {}

def _expectation(self, expectation_id, response_data):
"""
Creates an Expectation with given response_data.
Expectation is accessible on the `mockserver.url/expectation_id` url.
"""
json_data = {
"id": expectation_id,
"httpRequest": {
"path": f"/{expectation_id}"
}
}
json_data.update(response_data)

def create_expectation(self, expectation_id, path, body,
content_type: Union[ContentType, str] = ContentType.PLAIN_TEXT):
"""Creates an Expectation - response with given body"""
response = httpx.put(
urljoin(self.url, "/mockserver/expectation"), verify=False, timeout=5, json={
"id": expectation_id,
"httpRequest": {
"path": path
},
"httpResponse": {
"headers": {
"Content-Type": [str(content_type)]
},
"body": body
}
}
)
urljoin(self.url, "/mockserver/expectation"), verify=False, timeout=5, json=json_data)
response.raise_for_status()
return self.url + path
self.expectations[expectation_id] = f"{self.url}/{expectation_id}"
return self.expectations[expectation_id]

def create_expectation(
self,
expectation_id,
body,
content_type: Union[ContentType, str] = ContentType.PLAIN_TEXT,
):
"""Creates an Expectation - response with given body"""
json_data = {
"httpResponse": {
"headers": {
"Content-Type": [str(content_type)]
},
"body": body
}
}
return self._expectation(expectation_id, json_data)

def create_template_expectation(self, expectation_id, template):
"""
Creates template expectation in Mustache format.
https://www.mock-server.com/mock_server/response_templates.html
"""
json_data = {
"httpResponseTemplate": {
"templateType": "MUSTACHE",
"template": template
}
}
return self._expectation(expectation_id, json_data)

def clear_expectation(self, expectation_id):
"""Clears Expectation with specific ID"""
Expand All @@ -40,3 +73,16 @@ def clear_expectation(self, expectation_id):
"id": expectation_id
}
).raise_for_status()
del self.expectations[expectation_id]

def verify_expectation(self, path):
"""Verify a request has been received a specific number of times for specific expectation"""
return httpx.put(
urljoin(self.url, "/mockserver/retrieve"), params="type=REQUESTS&format=JSON",
verify=False, timeout=5, json={
"path": path
})

def get_expectation_endpoint(self, expectation_id):
"""Returns endpoint for expectation"""
return f"{self.url}/{expectation_id}"
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def header():
def opa_policy_expectation(request, mockserver, module_label, header):
"""Creates Mockserver Expectation that returns Rego query and returns its endpoint"""
request.addfinalizer(lambda: mockserver.clear_expectation(module_label))
return mockserver.create_expectation(module_label, f"/{module_label}/opa", rego_allow_header(*header))
return mockserver.create_expectation(module_label, rego_allow_header(*header))


@pytest.fixture(scope="module")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def updated_header():
@pytest.fixture(scope="module", autouse=True)
def update_external_opa(mockserver, module_label, updated_header):
"""Updates Expectation with updated header"""
mockserver.create_expectation(module_label, f"/{module_label}/opa", rego_allow_header(*updated_header))
mockserver.create_expectation(module_label, rego_allow_header(*updated_header))
# Sleeps for 1 second to compensate auto-refresh cycle `authorization.opa.externalRegistry.ttl = 1`
time.sleep(1)

Expand Down
22 changes: 15 additions & 7 deletions testsuite/tests/kuadrant/authorino/dinosaur/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ def terms_and_conditions(request, mockserver, module_label):
"""Creates Mockserver Expectation that returns whether terms are required and returns its endpoint"""

def _terms_and_conditions(value):
return mockserver.create_expectation(f"{module_label}-terms", f"/{module_label}/terms-and-conditions",
{"terms_required": value}, ContentType.APPLICATION_JSON)
return mockserver.create_expectation(
f"{module_label}-terms",
{"terms_required": value},
ContentType.APPLICATION_JSON,
)

request.addfinalizer(lambda: mockserver.clear_expectation(f"{module_label}-terms"))
return _terms_and_conditions
Expand All @@ -52,8 +55,11 @@ def cluster_info(request, mockserver, module_label):
"""Creates Mockserver Expectation that returns client ID and returns its endpoint"""

def _cluster_info(value):
return mockserver.create_expectation(f"{module_label}-cluster", f"/{module_label}/cluster-info",
{"client_id": value}, ContentType.APPLICATION_JSON)
return mockserver.create_expectation(
f"{module_label}-cluster",
{"client_id": value},
ContentType.APPLICATION_JSON
)

request.addfinalizer(lambda: mockserver.clear_expectation(f"{module_label}-cluster"))
return _cluster_info
Expand All @@ -64,9 +70,11 @@ def resource_info(request, mockserver, module_label):
"""Creates Mockserver Expectation that returns info about resource and returns its endpoint"""

def _resource_info(org_id, owner):
return mockserver.create_expectation(f"{module_label}-resource", f"/{module_label}/resource-info",
{"org_id": org_id, "owner": owner},
ContentType.APPLICATION_JSON)
return mockserver.create_expectation(
f"{module_label}-resource",
{"org_id": org_id, "owner": owner},
ContentType.APPLICATION_JSON,
)

request.addfinalizer(lambda: mockserver.clear_expectation(f"{module_label}-resource"))
return _resource_info
Expand Down
3 changes: 1 addition & 2 deletions testsuite/tests/kuadrant/authorino/metadata/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
def country_mock_expectation(request, mockserver, module_label):
"""Creates Mockserver Expectation which returns simple JSON that contains `allowed_countries`"""
request.addfinalizer(lambda: mockserver.clear_expectation(module_label))
return mockserver.create_expectation(
module_label, f"/{module_label}/opa", ALLOWED_COUNTRY, ContentType.APPLICATION_JSON)
return mockserver.create_expectation(module_label, ALLOWED_COUNTRY, ContentType.APPLICATION_JSON)


@pytest.fixture(scope="module")
Expand Down

0 comments on commit 9089cab

Please sign in to comment.