Support stubbing AppStoreServerAPIClient for unit tests
WFT opened this issue · comments
Feature request: I'd like the ability to create mock responses to API client calls which will be returned by the next call to a specific client method.
- For reference, boto3 (the AWS library) has a totally fine approach to this: https://botocore.amazonaws.com/v1/documentation/api/latest/reference/stubber.html
- Should allow both successful responses and error codes
- Should allow asserting that the responses were consumed
Use case
I would like to do unit testing for my server's use of the library. I want to test the following features of my server:
- Customer service requests to extend subscription renewals
- Requests to get the latest subscription status of a customer (e.g. for testing my handling of older clients with ReceiptUtility)
Example code
Here's an example test one could write:
from appstoreserverlibrary import testing as appstore
replace_server_api_client(appstore.TestingAPIClient(...))
def test_customer_service_extension(client):
def callback(original_transaction_id: str, extend_renewal_date_request: ExtendRenewalDateRequest) -> ExtendRenewalDateResponse:
assert stuff_about_the_request(original_transaction_id, extend_renewal_date_request)
return ExtendRenewalDateResponse(...)
with appstore.client_stubber as stubber: # Asserts on leaving the `with` that the expected responses were consumed
stubber.extend_subscription_renewal_date.add_response(callback)
response = client.post('/api/customer-service/app-store/extend')
assert response.code == 200
# Check that our server is properly handling saving the effectiveDate to the database
response = client.get('/api/app-store/renewal-date')
assert response.code == 200
assert has_been_extended(response)
@WFT currently how the unit tests are constructed, either directly or through subclassing you can override the direct method used to make the HTTP call, do any validation required on the inputs, and then return any arbitrary response
app-store-server-library-python/tests/test_api_client.py
Lines 381 to 405 in 2d11b06
@alexanderjordanbaker Yeah I think that makes sense. In my codebase I've just been using a general-purpose HTTP mocking library (https://github.com/gabrielfalcao/HTTPretty -- wish it were a little more maintained). I think that to make a really great testing experience though, it'd be nice if we could get it into this library.