|
| 1 | +import pytest |
| 2 | +from starlette import status |
| 3 | +from fastapi.testclient import TestClient |
| 4 | +from sqlalchemy.orm import Session |
| 5 | +from auth_backend.models.db import AuthMethod |
| 6 | + |
| 7 | + |
| 8 | +url = "/email/reset/password/" |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.skip() |
| 12 | +def test_unprocessable_jsons_no_token(client: TestClient, dbsession: Session, user_id: int): |
| 13 | + token = dbsession.query(AuthMethod).filter(AuthMethod.user_id == user_id, |
| 14 | + AuthMethod.param == "confirmation_token", |
| 15 | + AuthMethod.auth_method == "email").one() |
| 16 | + response = client.get(f"/email/approve?token={token.value}") |
| 17 | + assert response.status_code == status.HTTP_200_OK |
| 18 | + |
| 19 | + response = client.post(f"{url}{user_id}/request") |
| 20 | + assert response.status_code == status.HTTP_200_OK |
| 21 | + reset_token = dbsession.query(AuthMethod).filter(AuthMethod.auth_method == "email", |
| 22 | + AuthMethod.param == "reset_token", AuthMethod.user_id == user_id).one() |
| 23 | + assert reset_token |
| 24 | + |
| 25 | + response = client.post(f"{url}{user_id}", json={"reset_token": reset_token, "new_password": ""}) |
| 26 | + assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY |
| 27 | + |
| 28 | + response = client.post(f"{url}{user_id}", json={"reset_token": "", "new_password": ""}) |
| 29 | + assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY |
| 30 | + |
| 31 | + response = client.post(f"{url}{user_id}", json={"reset_token": "", "new_password": "changedstring3"}) |
| 32 | + assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.skip() |
| 36 | +def test_unprocessable_jsons_with_token(client: TestClient, dbsession: Session, user): |
| 37 | + user_id, body, response = user["user_id"], user["body"], user["login_json"] |
| 38 | + auth_token = response["token"] |
| 39 | + |
| 40 | + response = client.post(f"{url}{user_id}/request", json={"token": auth_token, "password": ""}) |
| 41 | + assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY |
| 42 | + |
| 43 | + response = client.post(f"{url}{user_id}/request", json={"token": "", "password": ""}) |
| 44 | + assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY |
| 45 | + |
| 46 | + response = client.post(f"{url}{user_id}/request", json={"token": "", "password": "string"}) |
| 47 | + assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY |
| 48 | + |
| 49 | + response = client.post(f"{url}{user_id}/request", json={"token": auth_token, "password": "string"}) |
| 50 | + assert response.status_code == status.HTTP_200_OK |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.skip() |
| 54 | +def test_no_token(client: TestClient, dbsession: Session, user_id: str): |
| 55 | + token = dbsession.query(AuthMethod).filter(AuthMethod.user_id == user_id, |
| 56 | + AuthMethod.param == "confirmation_token", |
| 57 | + AuthMethod.auth_method == "email").one() |
| 58 | + response = client.post(f"{url}{user_id}/request") |
| 59 | + assert response.status_code == status.HTTP_403_FORBIDDEN |
| 60 | + |
| 61 | + response = client.get(f"/email/approve?token={token.value}") |
| 62 | + assert response.status_code == status.HTTP_200_OK |
| 63 | + |
| 64 | + response = client.post(f"{url}{user_id}/request") |
| 65 | + assert response.status_code == status.HTTP_200_OK |
| 66 | + reset_token = dbsession.query(AuthMethod).filter(AuthMethod.auth_method == "email", AuthMethod.param == "reset_token", AuthMethod.user_id == user_id).one() |
| 67 | + assert reset_token |
| 68 | + |
| 69 | + response = client.post(f"{url}{user_id}", json={"reset_token": reset_token, "new_password": "changedstring"}) |
| 70 | + assert response.status_code == status.HTTP_200_OK |
| 71 | + |
| 72 | + response = client.post(f"{url}{user_id}", json={"reset_token": reset_token, "new_password": "changedstring2"}) |
| 73 | + assert response.status_code == status.HTTP_403_FORBIDDEN |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.skip() |
| 77 | +def test_with_token(client: TestClient, dbsession: Session, user): |
| 78 | + user_id, body, response = user["user_id"], user["body"], user["login_json"] |
| 79 | + auth_token = response["token"] |
| 80 | + |
| 81 | + response = client.post(f"{url}{user_id}/request", json={"token": auth_token, "password": "wrong"}) |
| 82 | + assert response.status_code == status.HTTP_403_FORBIDDEN |
| 83 | + |
| 84 | + response = client.post(f"{url}{user_id}/request", json={"token": auth_token, "password": "string"}) |
| 85 | + assert response.status_code == status.HTTP_200_OK |
| 86 | + reset_token = dbsession.query(AuthMethod).filter(AuthMethod.auth_method == "email", |
| 87 | + AuthMethod.param == "reset_token", AuthMethod.user_id == user_id).one() |
| 88 | + assert reset_token |
| 89 | + |
| 90 | + response = client.post(f"{url}{user_id}", json={"reset_token": reset_token, "new_password": "changedstring"}) |
| 91 | + assert response.status_code == status.HTTP_200_OK |
| 92 | + |
| 93 | + response = client.post(f"{url}{user_id}", json={"reset_token": reset_token, "new_password": "changedstring2"}) |
| 94 | + assert response.status_code == status.HTTP_403_FORBIDDEN |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
0 commit comments