pytest-dev / pytest-dev/pytest-django

How to test stripe webhook using pytest-django

Open
#1,039 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

I have a docker-compose based development environment with

services:
    django:
    postgres:
    stripe:
        command: listen --forward-to http://django:8000/stripe/webhook

When the user uses stripe to make a payment, an event is forwarded by stripe to a webhook http://django:800/stripe/webhook, handled by the django instance.

When I use pytest to test the service, the following happens (as far as I understand):

  1. django is still running with access to the regular database
  2. A live_server with --ds=config.settings.test is created with access to the test database
  3. When a payment is made during testing, stripe still forwards the request to django, which accesses the regular database, NOT the test database that is hooked to live_server, and the test payment would fail.

To fix this problem, we may

  1. Fix stripe: Somehow configure stripe to --forward-to live_server.url/stripe/webhoo. This is what is supposed to happen but requires a different stripe cli instance to be created after live_server.url is determined.
  2. Keep --forward-to to the regular django instance, but django (NOT live_server) will access the test database to complete the test. This may be easier but I am not sure how to configure django to access the test database.

Does anyone have any suggestions on how to do this properly?

Edit: A third option maybe running the tests on django directly without firing up a live_server, and using the normal database. The trouble is then how to direct pytest-django to NOT use the test database.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with the docker-compose services and the pytest-django live_server setup described in the report; verify which process receives the Stripe webhook and which database it uses. Done means identifying a reproducible test configuration in which the webhook reaches the test environment and the payment completes, or documenting the project-specific decision needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, docker-compose, python
Domain
backend, testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.