adrienverge / localstripe

A fake but stateful Stripe server that you can run locally, for testing purposes.
GNU General Public License v3.0
192 stars 59 forks source link

Optional use as a local library? #189

Closed meequz closed 3 years ago

meequz commented 3 years ago

The description says the project is not a local library but a server. However I believe it would be useful to many projects to use it as a local lib.

For example, I have a Django project that uses official stripe-python package to communicate with Stripe. If localstripe could seamlessly replace stripe object when doing import stripe in Django views, simulating Stripe responses but not doing any real requests, it would make possible to auto test these Django views.

Is it possible to use localstripe as a local lib? If not, how hard it would be to implement such a feature?

(also I'll be glad to hear any thoughts on how to autotest business logic that uses Stripe. Options that comes to mind: unittest.mock, official stripe-mock, this project as a server; however local library seems to be the fastest and most neat option).

adrienverge commented 3 years ago

Hello,

I understand the use case, it would be handy to easily set-up tests.

However, localstripe cannot easily be used as a "mock" like you describe, mainly because it is stateful: it does need a server to run and store information. You can compare it to a database like MySQL or MongoDB: they provide libs to connect to a server, but not libs that store information, because data from many clients would overlap; should the lib start when the OS boots? When should it clean its data? When should the lib wipe-out its data? If this is done by the lib callee, could it cause problems to other programs using the lib? When the lib is called, it needs to secretly open a port on the system for localstripe to listen: is this a security problem?

Also, I guess you're talking about a mock library for Python, but there is no reason for not doing it for other languages too.

There is no easy way to turn this project into a lib, but I guess it would be easy to write a wrapper that suits your own use: spawn a localstripe instance and set stripe.api_url.

I'll close this issue but feel free to reopen if needed!

meequz commented 3 years ago

Thank you for the detailed answer!

As of storing data, I imagine that this potential localstripe-lib could use RAM and just have a method to clear all the data. That way the lib user can call that method in the beginning of each test run to ensure clean Stripe state. Something similar to what Django does by default with in-memory SQLite state while running tests.