|
| 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 | + |
| 15 | +from typing import List |
| 16 | + |
| 17 | +from pytest import mark |
| 18 | +from tests.utils import clean_st, reset, setup_st, start_st |
| 19 | + |
| 20 | +from supertokens_python import InputAppInfo, SupertokensConfig |
| 21 | +from supertokens_python import asyncio as st_asyncio |
| 22 | +from supertokens_python import init |
| 23 | +from supertokens_python.recipe import emailpassword, session |
| 24 | +from supertokens_python.recipe.emailpassword import asyncio as ep_asyncio |
| 25 | +from supertokens_python.recipe.emailpassword.interfaces import SignUpOkResult |
| 26 | + |
| 27 | + |
| 28 | +def setup_function(_): |
| 29 | + reset() |
| 30 | + clean_st() |
| 31 | + setup_st() |
| 32 | + |
| 33 | + |
| 34 | +def teardown_function(_): |
| 35 | + reset() |
| 36 | + clean_st() |
| 37 | + |
| 38 | + |
| 39 | +@mark.asyncio |
| 40 | +async def test_supertokens_functions(): |
| 41 | + init( |
| 42 | + supertokens_config=SupertokensConfig('http://localhost:3567'), |
| 43 | + app_info=InputAppInfo( |
| 44 | + app_name="ST", |
| 45 | + api_domain="http://api.supertokens.io", |
| 46 | + website_domain="http://supertokens.io", |
| 47 | + api_base_path="/auth" |
| 48 | + ), |
| 49 | + framework='fastapi', |
| 50 | + recipe_list=[emailpassword.init(), session.init()] |
| 51 | + ) |
| 52 | + start_st() |
| 53 | + |
| 54 | + emails = [f"{u}@example.com" for u in ["foo", "bar", "baz"]] |
| 55 | + user_ids: List[str] = [] |
| 56 | + for e in emails: |
| 57 | + signup_resp = await ep_asyncio.sign_up(e, "secret_pass") |
| 58 | + assert isinstance(signup_resp, SignUpOkResult) |
| 59 | + user_ids.append(signup_resp.user.user_id) |
| 60 | + |
| 61 | + # Get user count |
| 62 | + assert await st_asyncio.get_user_count() == len(emails) |
| 63 | + |
| 64 | + # Get users in ascending order by joining time |
| 65 | + users_asc = (await st_asyncio.get_users_oldest_first(limit=10)).users |
| 66 | + emails_asc = [user.email for user in users_asc] |
| 67 | + assert emails_asc == emails |
| 68 | + |
| 69 | + # Get users in descending order by joining time |
| 70 | + users_desc = (await st_asyncio.get_users_newest_first(limit=10)).users |
| 71 | + emails_desc = [user.email for user in users_desc] |
| 72 | + assert emails_desc == emails[::-1] |
| 73 | + |
| 74 | + # Delete the 2nd user (bar@example.com) |
| 75 | + await st_asyncio.delete_user(user_ids[1]) |
| 76 | + |
| 77 | + # Again, get users in ascending order by joining time |
| 78 | + # We expect that the 2nd user (bar@example.com) must be absent. |
| 79 | + users_asc = (await st_asyncio.get_users_oldest_first(limit=10)).users |
| 80 | + emails_asc = [user.email for user in users_asc] |
| 81 | + assert emails[1] not in emails_asc # The 2nd user must be deleted now. |
0 commit comments