[Python] How to Test Password Reset Endpoint Using a Token That Doesn’t Exist in the Database...

Discussão em 'Python' iniciado por Stack, Junho 4, 2025.

  1. Stack

    Stack Membro Participativo

    I'm building a backend system using FastAPI, and I'm currently working on implementing unit tests for the password reset functionality that involves using tokens.

    Here’s the snippet of the code I'm testing:

    def test_web_reset_password():
    payload = {
    "token": "cozy_token", # Token that doesn’t exist in the database
    "new_password": "NewSecret123!"
    }
    response = client.post(f"{WEB_ENDPOINT}/reset-password", json=payload)
    assert response.status_code == 200
    data = response.json()
    assert "status" in data


    Since the token "cozy_token" doesn't exist in the database, I want to know how to handle this scenario in a unit test. Should I simulate the creation of a valid token during the test, or mock the token validation?

    Continue reading...

Compartilhe esta Página