|
| 1 | +# Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved. |
| 2 | +# |
| 3 | +# This software is licensed under the Apache License, Version 2.0 (the |
| 4 | +# "License") as published by the Apache Software Foundation. |
| 5 | +# |
| 6 | +# You may not use this file except in compliance with the License. You may |
| 7 | +# obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | +import json |
| 15 | +from typing import Any, Union |
| 16 | + |
| 17 | +from fastapi import FastAPI |
| 18 | +from fastapi.requests import Request |
| 19 | +from fastapi.testclient import TestClient |
| 20 | +from pytest import fixture, mark |
| 21 | +from supertokens_python import InputAppInfo, SupertokensConfig, init |
| 22 | +from supertokens_python.framework.fastapi import get_middleware |
| 23 | +from supertokens_python.recipe import emailpassword, emailverification, session |
| 24 | +from supertokens_python.recipe.session import SessionContainer |
| 25 | +from supertokens_python.recipe.session.asyncio import ( |
| 26 | + create_new_session, |
| 27 | + get_session, |
| 28 | + refresh_session, |
| 29 | +) |
| 30 | +from tests.utils import ( |
| 31 | + setup_function, |
| 32 | + sign_up_request, |
| 33 | + start_st, |
| 34 | + teardown_function, |
| 35 | +) |
| 36 | +from supertokens_python.recipe.emailpassword import InputFormField |
| 37 | + |
| 38 | +_ = setup_function # type: ignore |
| 39 | +_ = teardown_function # type: ignore |
| 40 | + |
| 41 | +pytestmark = mark.asyncio |
| 42 | + |
| 43 | + |
| 44 | +@fixture(scope="function") |
| 45 | +async def driver_config_client(): |
| 46 | + app = FastAPI() |
| 47 | + app.add_middleware(get_middleware()) |
| 48 | + |
| 49 | + @app.get("/login") |
| 50 | + async def login(request: Request): # type: ignore |
| 51 | + user_id = "userId" |
| 52 | + await create_new_session(request, user_id, {}, {}) |
| 53 | + return {"userId": user_id} |
| 54 | + |
| 55 | + @app.post("/refresh") |
| 56 | + async def custom_refresh(request: Request): # type: ignore |
| 57 | + await refresh_session(request) |
| 58 | + return {} # type: ignore |
| 59 | + |
| 60 | + @app.get("/info") |
| 61 | + async def info_get(request: Request): # type: ignore |
| 62 | + await get_session(request, True) |
| 63 | + return {} # type: ignore |
| 64 | + |
| 65 | + @app.get("/custom/info") |
| 66 | + def custom_info(_): # type: ignore |
| 67 | + return {} # type: ignore |
| 68 | + |
| 69 | + @app.options("/custom/handle") |
| 70 | + def custom_handle_options(_): # type: ignore |
| 71 | + return {"method": "option"} |
| 72 | + |
| 73 | + @app.get("/handle") |
| 74 | + async def handle_get(request: Request): # type: ignore |
| 75 | + session: Union[SessionContainer, None] = await get_session(request, True) |
| 76 | + if session is None: |
| 77 | + raise Exception("Should never come here") |
| 78 | + return {"s": session.get_handle()} |
| 79 | + |
| 80 | + @app.post("/logout") |
| 81 | + async def custom_logout(request: Request): # type: ignore |
| 82 | + session: Union[SessionContainer, None] = await get_session(request, True) |
| 83 | + if session is None: |
| 84 | + raise Exception("Should never come here") |
| 85 | + await session.revoke_session() |
| 86 | + return {} # type: ignore |
| 87 | + |
| 88 | + return TestClient(app) |
| 89 | + |
| 90 | + |
| 91 | +async def test_update_email_or_password_with_default_validator( |
| 92 | + driver_config_client: TestClient, |
| 93 | +): |
| 94 | + init( |
| 95 | + supertokens_config=SupertokensConfig("http://localhost:3567"), |
| 96 | + app_info=InputAppInfo( |
| 97 | + app_name="SuperTokens Demo", |
| 98 | + api_domain="http://api.supertokens.io", |
| 99 | + website_domain="http://supertokens.io", |
| 100 | + api_base_path="/auth", |
| 101 | + ), |
| 102 | + framework="fastapi", |
| 103 | + recipe_list=[ |
| 104 | + session.init( |
| 105 | + anti_csrf="VIA_TOKEN", |
| 106 | + get_token_transfer_method=lambda _, __, ___: "cookie", |
| 107 | + ), |
| 108 | + emailverification.init("OPTIONAL"), |
| 109 | + emailpassword.init(), |
| 110 | + ], |
| 111 | + ) |
| 112 | + start_st() |
| 113 | + |
| 114 | + response_1 = sign_up_request( driver_config_client, "[email protected]", "testPass123") |
| 115 | + |
| 116 | + dict_response = json.loads(response_1.text) |
| 117 | + |
| 118 | + user_id = dict_response["user"]["id"] |
| 119 | + |
| 120 | + r = await emailpassword.EmailPasswordRecipe.get_instance().recipe_implementation.update_email_or_password( |
| 121 | + user_id=user_id, |
| 122 | + email=None, |
| 123 | + password="test", |
| 124 | + user_context={}, |
| 125 | + apply_password_policy=None, |
| 126 | + ) |
| 127 | + |
| 128 | + assert ( |
| 129 | + r.failure_reason # type: ignore |
| 130 | + == "Password must contain at least 8 characters, including a number" |
| 131 | + ) |
| 132 | + |
| 133 | + |
| 134 | +async def test_update_email_or_password_with_custom_validator( |
| 135 | + driver_config_client: TestClient, |
| 136 | +): |
| 137 | + async def validate_pass(value: Any): |
| 138 | + # Validation method to make sure that age >= 18 |
| 139 | + if len(value) < 3: |
| 140 | + return "Password should be at least 3 chars long" |
| 141 | + return None # means that there is no error |
| 142 | + |
| 143 | + init( |
| 144 | + supertokens_config=SupertokensConfig("http://localhost:3567"), |
| 145 | + app_info=InputAppInfo( |
| 146 | + app_name="SuperTokens Demo", |
| 147 | + api_domain="http://api.supertokens.io", |
| 148 | + website_domain="http://supertokens.io", |
| 149 | + api_base_path="/auth", |
| 150 | + ), |
| 151 | + framework="fastapi", |
| 152 | + recipe_list=[ |
| 153 | + session.init( |
| 154 | + anti_csrf="VIA_TOKEN", |
| 155 | + get_token_transfer_method=lambda _, __, ___: "cookie", |
| 156 | + ), |
| 157 | + emailverification.init("OPTIONAL"), |
| 158 | + emailpassword.init( |
| 159 | + sign_up_feature=emailpassword.InputSignUpFeature( |
| 160 | + form_fields=[InputFormField(id="password", validate=validate_pass)] |
| 161 | + ) |
| 162 | + ), |
| 163 | + ], |
| 164 | + ) |
| 165 | + start_st() |
| 166 | + |
| 167 | + response_1 = sign_up_request( driver_config_client, "[email protected]", "testPass123") |
| 168 | + |
| 169 | + dict_response = json.loads(response_1.text) |
| 170 | + |
| 171 | + user_id = dict_response["user"]["id"] |
| 172 | + |
| 173 | + r = await emailpassword.EmailPasswordRecipe.get_instance().recipe_implementation.update_email_or_password( |
| 174 | + user_id=user_id, |
| 175 | + email=None, |
| 176 | + password="te", |
| 177 | + user_context={}, |
| 178 | + apply_password_policy=None, |
| 179 | + ) |
| 180 | + assert r.failure_reason == "Password should be at least 3 chars long" # type: ignore |
0 commit comments