|
13 | 13 | # under the License. |
14 | 14 | from pytest import mark |
15 | 15 | from unittest.mock import MagicMock |
16 | | -from supertokens_python import InputAppInfo, SupertokensConfig, init |
| 16 | +from supertokens_python import InputAppInfo, SupertokensConfig, init, Supertokens |
17 | 17 | from supertokens_python.normalised_url_domain import NormalisedURLDomain |
18 | 18 | from supertokens_python.normalised_url_path import NormalisedURLPath |
19 | 19 | from supertokens_python.recipe import session |
20 | 20 | from supertokens_python.recipe.session import SessionRecipe |
21 | 21 | from supertokens_python.recipe.session.asyncio import create_new_session |
| 22 | +from typing import Optional, Dict, Any |
| 23 | +from supertokens_python.framework import BaseRequest |
22 | 24 |
|
23 | 25 | from tests.utils import clean_st, reset, setup_st, start_st |
24 | 26 |
|
@@ -814,3 +816,139 @@ async def test_cookie_samesite_with_ec2_public_url(): |
814 | 816 | assert SessionRecipe.get_instance().config.cookie_domain is None |
815 | 817 | assert SessionRecipe.get_instance().config.get_cookie_same_site(None, {}) == "lax" |
816 | 818 | assert SessionRecipe.get_instance().config.cookie_secure is False |
| 819 | + |
| 820 | + |
| 821 | +@mark.asyncio |
| 822 | +async def test_samesite_explicit_config(): |
| 823 | + init( |
| 824 | + supertokens_config=SupertokensConfig("http://localhost:3567"), |
| 825 | + app_info=InputAppInfo( |
| 826 | + app_name="SuperTokens Demo", |
| 827 | + origin="http://localhost:3000", |
| 828 | + api_domain="http://localhost:3001", |
| 829 | + ), |
| 830 | + framework="fastapi", |
| 831 | + recipe_list=[ |
| 832 | + session.init( |
| 833 | + cookie_same_site="strict", |
| 834 | + ) |
| 835 | + ], |
| 836 | + ) |
| 837 | + assert ( |
| 838 | + SessionRecipe.get_instance().config.get_cookie_same_site(None, {}) == "strict" |
| 839 | + ) |
| 840 | + |
| 841 | + |
| 842 | +@mark.asyncio |
| 843 | +async def test_that_exception_is_thrown_if_website_domain_and_origin_are_not_passed(): |
| 844 | + try: |
| 845 | + init( |
| 846 | + supertokens_config=SupertokensConfig("http://localhost:3567"), |
| 847 | + app_info=InputAppInfo( |
| 848 | + app_name="SuperTokens Demo", |
| 849 | + api_domain="http://localhost:3001", |
| 850 | + ), |
| 851 | + framework="fastapi", |
| 852 | + recipe_list=[session.init()], |
| 853 | + ) |
| 854 | + except Exception as e: |
| 855 | + assert str(e) == "Please provide at least one of website_domain or origin" |
| 856 | + else: |
| 857 | + assert False, "Exception not thrown" |
| 858 | + |
| 859 | + |
| 860 | +@mark.asyncio |
| 861 | +async def test_that_init_works_fine_when_using_origin_string(): |
| 862 | + init( |
| 863 | + supertokens_config=SupertokensConfig("http://localhost:3567"), |
| 864 | + app_info=InputAppInfo( |
| 865 | + app_name="SuperTokens Demo", |
| 866 | + api_domain="http://localhost:3001", |
| 867 | + origin="localhost:3000", |
| 868 | + ), |
| 869 | + framework="fastapi", |
| 870 | + recipe_list=[session.init()], |
| 871 | + ) |
| 872 | + |
| 873 | + assert ( |
| 874 | + Supertokens.get_instance() |
| 875 | + .app_info.get_origin(None, {}) |
| 876 | + .get_as_string_dangerous() |
| 877 | + == "http://localhost:3000" |
| 878 | + ) |
| 879 | + |
| 880 | + |
| 881 | +@mark.asyncio |
| 882 | +async def test_that_init_works_fine_when_using_website_domain_string(): |
| 883 | + init( |
| 884 | + supertokens_config=SupertokensConfig("http://localhost:3567"), |
| 885 | + app_info=InputAppInfo( |
| 886 | + app_name="SuperTokens Demo", |
| 887 | + api_domain="http://localhost:3001", |
| 888 | + website_domain="localhost:3000", |
| 889 | + ), |
| 890 | + framework="fastapi", |
| 891 | + recipe_list=[session.init()], |
| 892 | + ) |
| 893 | + |
| 894 | + assert ( |
| 895 | + Supertokens.get_instance() |
| 896 | + .app_info.get_origin(None, {}) |
| 897 | + .get_as_string_dangerous() |
| 898 | + == "http://localhost:3000" |
| 899 | + ) |
| 900 | + |
| 901 | + |
| 902 | +@mark.asyncio |
| 903 | +async def test_that_init_works_fine_when_using_origin_function(): |
| 904 | + def get_origin(_: Optional[BaseRequest], user_context: Dict[str, Any]) -> str: |
| 905 | + if "input" in user_context: |
| 906 | + return user_context["input"] |
| 907 | + return "localhost:3000" |
| 908 | + |
| 909 | + init( |
| 910 | + supertokens_config=SupertokensConfig("http://localhost:3567"), |
| 911 | + app_info=InputAppInfo( |
| 912 | + app_name="SuperTokens Demo", |
| 913 | + api_domain="http://localhost:3001", |
| 914 | + origin=get_origin, |
| 915 | + ), |
| 916 | + framework="fastapi", |
| 917 | + recipe_list=[session.init()], |
| 918 | + ) |
| 919 | + |
| 920 | + assert ( |
| 921 | + Supertokens.get_instance() |
| 922 | + .app_info.get_origin(None, {"input": "localhost:1000"}) |
| 923 | + .get_as_string_dangerous() |
| 924 | + == "http://localhost:1000" |
| 925 | + ) |
| 926 | + |
| 927 | + assert ( |
| 928 | + Supertokens.get_instance() |
| 929 | + .app_info.get_origin(None, {}) |
| 930 | + .get_as_string_dangerous() |
| 931 | + == "http://localhost:3000" |
| 932 | + ) |
| 933 | + |
| 934 | + |
| 935 | +@mark.asyncio |
| 936 | +async def test_that_init_chooses_origin_over_website_domain(): |
| 937 | + init( |
| 938 | + supertokens_config=SupertokensConfig("http://localhost:3567"), |
| 939 | + app_info=InputAppInfo( |
| 940 | + app_name="SuperTokens Demo", |
| 941 | + api_domain="http://localhost:3001", |
| 942 | + website_domain="localhost:3000", |
| 943 | + origin="supertokens.io", |
| 944 | + ), |
| 945 | + framework="fastapi", |
| 946 | + recipe_list=[session.init()], |
| 947 | + ) |
| 948 | + |
| 949 | + assert ( |
| 950 | + Supertokens.get_instance() |
| 951 | + .app_info.get_origin(None, {}) |
| 952 | + .get_as_string_dangerous() |
| 953 | + == "https://supertokens.io" |
| 954 | + ) |
0 commit comments