1+ import os
12import threading
3+ from contextlib import ExitStack
24from typing import Any , Dict , List , Union
5+ from unittest .mock import patch
36
4- import pytest
7+ from pytest import mark , param , raises
58from supertokens_python .utils import (
69 RWMutex ,
710 get_top_level_domain_for_same_site_resolution ,
811 humanize_time ,
912 is_version_gte ,
1013)
1114
12- from tests .utils import is_subset
15+ from tests .utils import is_subset , outputs
1316
1417
15- @pytest . mark .parametrize (
18+ @mark .parametrize (
1619 "version,min_minor_version,is_gte" ,
1720 [
1821 (
@@ -72,7 +75,7 @@ def test_util_is_version_gte(version: str, min_minor_version: str, is_gte: bool)
7275HOUR = 60 * MINUTE
7376
7477
75- @pytest . mark .parametrize (
78+ @mark .parametrize (
7679 "ms,out" ,
7780 [
7881 (1 * SECOND , "1 second" ),
@@ -91,7 +94,7 @@ def test_humanize_time(ms: int, out: str):
9194 assert humanize_time (ms ) == out
9295
9396
94- @pytest . mark .parametrize (
97+ @mark .parametrize (
9598 "d1,d2,result" ,
9699 [
97100 ({"a" : {"b" : [1 , 2 ]}, "c" : 1 }, {"c" : 1 }, True ),
@@ -176,7 +179,7 @@ def balance_is_valid():
176179 assert actual_balance == expected_balance , "Incorrect account balance"
177180
178181
179- @pytest . mark .parametrize (
182+ @mark .parametrize (
180183 "url,res" ,
181184 [
182185 ("http://localhost:3001" , "localhost" ),
@@ -196,3 +199,41 @@ def balance_is_valid():
196199)
197200def test_tld_for_same_site (url : str , res : str ):
198201 assert get_top_level_domain_for_same_site_resolution (url ) == res
202+
203+
204+ @mark .parametrize (
205+ ["internet_disabled" , "env_val" , "expectation" ],
206+ [
207+ param (True , "False" , raises (RuntimeError ), id = "Internet disabled, flag unset" ),
208+ param (True , "True" , outputs ("google.com" ), id = "Internet disabled, flag set" ),
209+ param (False , "False" , outputs ("google.com" ), id = "Internet enabled, flag unset" ),
210+ param (False , "True" , outputs ("google.com" ), id = "Internet enabled, flag set" ),
211+ ],
212+ )
213+ def test_tldextract_http_toggle (
214+ internet_disabled : bool ,
215+ env_val : str ,
216+ expectation : Any ,
217+ # pyfakefs fixture, mocks the filesystem
218+ # Mocking `tldextract`'s cache path does not work in repeated tests
219+ fs : Any ,
220+ ):
221+ import socket
222+
223+ # Disable sockets, will raise errors on HTTP calls
224+ socket_patch = patch .object (socket , "socket" , side_effect = RuntimeError )
225+ environ_patch = patch .dict (
226+ os .environ ,
227+ {"SUPERTOKENS_TLDEXTRACT_DISABLE_HTTP" : env_val },
228+ )
229+
230+ stack = ExitStack ()
231+ stack .enter_context (environ_patch )
232+ if internet_disabled :
233+ stack .enter_context (socket_patch )
234+
235+ # if `expectation` is raises, checks for raise
236+ # if `outputs`, value used in `assert` statement
237+ with stack , expectation as expected_output :
238+ output = get_top_level_domain_for_same_site_resolution ("https://google.com" )
239+ assert output == expected_output
0 commit comments