Stripe Authorization In Open Event API Server
The Open Event System supports payments through stripe. Stripe is a suite of APIs that powers commerce for businesses of all sizes. This blogpost covers testing of Stripe Authorization Schema and endpoints in the API Server.
The Stripe Authorization class provides the following endpoints:
'/stripe-authorization' '/stripe-authorization/<int:id>' '/events/<int:event_id>/stripe-authorization' '/events/<event_identifier>/stripe-authorization'
In the pull request made for adding documentation and tests, these two endpoints were removed:
'stripe_authorization_list', '/events/<int:event_id>/stripe-authorization', '/events/<event_identifier>/stripe-authorization'
This is because each event can have only one stripe authorization, so there can not exist a list of stripe authorization objects related to an event.
The ‘stripe_authorization_list’ endpoint is made POST only. This is because Open Event does not allow individual resources’ list to be accessible. Since, there is no endpoint which returns a list of Stripe Authorizations the StripeAuthorizationList(ResourceList) is removed.
The ResourceDetail class was modified to add a query to support results from ‘/events/<int:event_id>/stripe-authorization’ endpoint. suThe view_kwargs for the detail endpoint has to contain the resource id, so event_id from view_kwags is used to get the id for stripe authorization.
stripe_authorization = self.session.query(StripeAuthorization).filter_by(event_id=view_kwargs['event_id']).one() view_kwargs['id'] = stripe_authorization.id
Writing Test for Documentation
(Tests for the /events/1/stripe-authorization is described here, for others please refer to links in additional references.)
To test the /events/1/stripe-authorization endpoint for GET, we first insert a Stripe Authorization object into the database which will then be retrieved by the GET request and then compared with the expected response body from the documentation file.
Since stripe-auth has a required relationship with event class, an event must also exist for strie auth object to be created. The event is also required because the endpoint ‘events/’ expects an event object to exist. The StripeAuthorizationFactory takes care of this with event as a RelatedFactory. So when a StripeAuthorization object is inserted, an event is created first and passed as the required relationship to stripe_list_post endpoint.
The event is related to the stripe object by setting event_id = 1 in the factory.
Adding the pre-test hook for GET:
@hooks.before("StripeAuthorization > Stripe Authorization for an Event > Get Stripe Authorization Details of an Event") def event_stripe_authorization_get_detail(transaction): """ GET /events/1/stripe-authorization :param transaction: :return: """ with stash['app'].app_context(): stripe = StripeAuthorizationFactory() db.session.add(stripe) db.session.commit()
The expected response for this request can be found here.
- Stripe API Reference – Official Stripe Documentation
- Adding payment to your Web applications with Stripe – Hitch, [blog]
- Writing Dredd Hooks In Python – Python Hooks in Dredd Docs
- Add stripe-auth related endpoints docs and dredd tests – enigmaeth, [commit]