1
1
import importlib
2
2
import json
3
+ import logging
3
4
import os
4
5
import sys
5
6
from datetime import datetime as real_datetime
10
11
11
12
from supertokens_python import InputAppInfo , SupertokensConfig , init
12
13
from supertokens_python .constants import VERSION
13
- from supertokens_python .logger import log_debug_message , streamFormatter
14
+ from supertokens_python .logger import (
15
+ log_debug_message ,
16
+ streamFormatter ,
17
+ NAMESPACE ,
18
+ enable_debug_logging ,
19
+ )
14
20
from supertokens_python .recipe import session
15
21
16
22
from tests .utils import clean_st , reset , setup_st , start_st
17
23
18
24
19
25
class LoggerTests (TestCase ):
20
- def setup_method (self , _ ):
21
- self ._caplog .clear ()
26
+ @pytest .fixture (autouse = True )
27
+ def inject_fixtures (self , caplog : pytest .LogCaptureFixture ):
28
+ # caplog is the pytest fixture to capture all logs
29
+ self ._caplog = caplog # pylint: disable=attribute-defined-outside-init
30
+
31
+ def setup_method (self , _ ): # pylint: disable=no-self-use
32
+ # Setting the log level to a higher level so debug logs are not printed
33
+ logging .getLogger (NAMESPACE ).setLevel (logging .ERROR )
22
34
reset ()
23
35
clean_st ()
24
36
setup_st ()
@@ -28,13 +40,9 @@ def teardown_method(self, _):
28
40
reset ()
29
41
clean_st ()
30
42
31
- @pytest .fixture (autouse = True )
32
- def inject_fixtures (self , caplog : pytest .LogCaptureFixture ):
33
- # caplog is the pytest fixture to capture all logs
34
- self ._caplog = caplog # pylint: disable=attribute-defined-outside-init
35
-
36
43
@patch ("supertokens_python.logger.datetime" , wraps = real_datetime )
37
- def test_json_msg_format (self , datetime_mock : MagicMock ):
44
+ def test_1_json_msg_format (self , datetime_mock : MagicMock ):
45
+ enable_debug_logging ()
38
46
datetime_mock .utcnow .return_value = real_datetime (2000 , 1 , 1 ) # type: ignore
39
47
40
48
with self .assertLogs (level = "DEBUG" ) as captured :
@@ -47,17 +55,17 @@ def test_json_msg_format(self, datetime_mock: MagicMock):
47
55
"t" : "2000-01-01T00:00Z" ,
48
56
"sdkVer" : VERSION ,
49
57
"message" : "API replied with status 200" ,
50
- "file" : "../tests/test_logger.py:40 " ,
58
+ "file" : "../tests/test_logger.py:49 " ,
51
59
}
52
60
53
61
@staticmethod
54
- def test_stream_formatter_format ():
62
+ def test_2_stream_formatter_format ():
55
63
assert (
56
64
streamFormatter ._fmt # pylint: disable=protected-access
57
65
== "{name} {message}\n "
58
66
)
59
67
60
- def test_logger_config_with_debug_true (self ):
68
+ def test_3_logger_config_with_debug_false (self ):
61
69
start_st ()
62
70
init (
63
71
supertokens_config = SupertokensConfig ("http://localhost:3567" ),
@@ -69,15 +77,15 @@ def test_logger_config_with_debug_true(self):
69
77
),
70
78
framework = "fastapi" ,
71
79
recipe_list = [session .init (anti_csrf = "VIA_CUSTOM_HEADER" )],
72
- debug = True ,
80
+ debug = False ,
73
81
)
74
82
75
83
logMsg = "log test - valid log"
76
84
log_debug_message (logMsg )
77
85
78
- assert logMsg in self ._caplog .text
86
+ assert logMsg not in self ._caplog .text
79
87
80
- def test_logger_config_with_debug_false (self ):
88
+ def test_4_logger_config_with_debug_true (self ):
81
89
start_st ()
82
90
init (
83
91
supertokens_config = SupertokensConfig ("http://localhost:3567" ),
@@ -89,15 +97,15 @@ def test_logger_config_with_debug_false(self):
89
97
),
90
98
framework = "fastapi" ,
91
99
recipe_list = [session .init (anti_csrf = "VIA_CUSTOM_HEADER" )],
92
- debug = False ,
100
+ debug = True ,
93
101
)
94
102
95
103
logMsg = "log test - valid log"
96
104
log_debug_message (logMsg )
97
105
98
- assert logMsg not in self ._caplog .text
106
+ assert logMsg in self ._caplog .text
99
107
100
- def test_logger_config_with_debug_not_set (self ):
108
+ def test_5_logger_config_with_debug_not_set (self ):
101
109
start_st ()
102
110
init (
103
111
supertokens_config = SupertokensConfig ("http://localhost:3567" ),
@@ -116,7 +124,7 @@ def test_logger_config_with_debug_not_set(self):
116
124
117
125
assert logMsg not in self ._caplog .text
118
126
119
- def test_logger_config_with_env_var (self ):
127
+ def test_6_logger_config_with_env_var (self ):
120
128
os .environ ["SUPERTOKENS_DEBUG" ] = "1"
121
129
122
130
# environment variable was already set when the logger module was imported
0 commit comments