-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify Manifester class to work with MockStub
Add a class to test_manifester.py to enable requests to be sent to a MockStub version of the requests package instead of sending live requests to the API. Modify the Manifester class to use the MockStub-based requests when running unit tests. Add a helper function to generate mock HTTP response codes.
- Loading branch information
Showing
3 changed files
with
117 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from unittest.mock import Mock | ||
|
||
from requests import request | ||
from manifester import Manifester | ||
from manifester.settings import settings | ||
from manifester.helpers import MockStub, fake_http_response_code | ||
import pytest | ||
import random | ||
|
||
def test_empty_init(manifest_category="golden_ticket"): | ||
manifester_inst = Manifester(manifest_category=manifest_category) | ||
assert isinstance(manifester_inst, Manifester) | ||
|
||
class RhsmApiStub(MockStub): | ||
def __init__(self, in_dict=None, **kwargs): | ||
self._good_codes = kwargs.get("good_codes", [200]) | ||
self._bad_codes = kwargs.get("bad_codes", [429, 500, 504]) | ||
self._fail_rate = kwargs.get("fail_rate", 10) | ||
super().__init__(in_dict) | ||
|
||
@property | ||
def status_code(self): | ||
return fake_http_response_code(self._good_codes, self._bad_codes, self._fail_rate) | ||
|
||
def post(*args, **kwargs): | ||
if args[0].endswith("openid-connect/token"): | ||
return MockStub(in_dict={"access_token": "this is a simulated access token"}, status_code=200) | ||
if args[0].endswith("allocations"): | ||
return MockStub(in_dict={"uuid": "1234567890"}) | ||
if args[0].endswith("entitlements"): | ||
return MockStub(status_code=200) | ||
|
||
def get(*args, **kwargs): | ||
if args[0].endswith("pools"): | ||
# question: how to fake > 50 pools to test use of offset parameter? | ||
return MockStub(in_dict={"pool": "this is a simulated list of dictionaries of subscription pool data"}) | ||
if "allocations" in args[0] and not ("export" in args[0] or "pools" in args[0]): | ||
return MockStub(in_dict={"allocation_data": "this allocation data also includes entitlement data"}) | ||
if args[0].endswith("export"): | ||
return MockStub(in_dict={"export_job": "Manifest export job triggered successfully"}) | ||
if "exportJob" in args[0]: | ||
responses = [202, 200] | ||
return MockStub(status_code=random.choice(responses)) | ||
if "export" in args[0] and not args[0].endswith("export"): | ||
return MockStub(in_dict={"content": "this is a simulated manifest"}) | ||
|
||
|
||
def test_create_allocation(): | ||
manifester = Manifester(manifest_category="golden_ticket", requester=RhsmApiStub(in_dict=None, status_code=200)) | ||
allocation_uuid = manifester.create_subscription_allocation() | ||
breakpoint() | ||
assert allocation_uuid == "1234567890" |