1+ import importlib
12import json
3+ import logging
4+ import os
5+ import sys
26from datetime import datetime as real_datetime
37from unittest import TestCase
48from unittest .mock import MagicMock , patch
59
10+ import pytest
11+
12+ from supertokens_python import InputAppInfo , SupertokensConfig , init
613from supertokens_python .constants import VERSION
7- 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+ )
20+ from supertokens_python .recipe import session
21+
22+ from tests .utils import clean_st , reset , setup_st , start_st
823
924
1025class LoggerTests (TestCase ):
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 )
34+ reset ()
35+ clean_st ()
36+ setup_st ()
37+
38+ def teardown_method (self , _ ):
39+ self ._caplog .clear ()
40+ reset ()
41+ clean_st ()
42+
1143 @patch ("supertokens_python.logger.datetime" , wraps = real_datetime )
12- def test_json_msg_format (self , datetime_mock : MagicMock ):
44+ def test_1_json_msg_format (self , datetime_mock : MagicMock ):
45+ enable_debug_logging ()
1346 datetime_mock .utcnow .return_value = real_datetime (2000 , 1 , 1 ) # type: ignore
1447
1548 with self .assertLogs (level = "DEBUG" ) as captured :
@@ -22,12 +55,98 @@ def test_json_msg_format(self, datetime_mock: MagicMock):
2255 "t" : "2000-01-01T00:00Z" ,
2356 "sdkVer" : VERSION ,
2457 "message" : "API replied with status 200" ,
25- "file" : "../tests/test_logger.py:16 " ,
58+ "file" : "../tests/test_logger.py:49 " ,
2659 }
2760
2861 @staticmethod
29- def test_stream_formatter_format ():
62+ def test_2_stream_formatter_format ():
3063 assert (
3164 streamFormatter ._fmt # pylint: disable=protected-access
3265 == "{name} {message}\n "
3366 )
67+
68+ def test_3_logger_config_with_debug_false (self ):
69+ start_st ()
70+ init (
71+ supertokens_config = SupertokensConfig ("http://localhost:3567" ),
72+ app_info = InputAppInfo (
73+ app_name = "SuperTokens Demo" ,
74+ api_domain = "api.supertokens.io" ,
75+ website_domain = "supertokens.io" ,
76+ api_base_path = "/" ,
77+ ),
78+ framework = "fastapi" ,
79+ recipe_list = [session .init (anti_csrf = "VIA_CUSTOM_HEADER" )],
80+ debug = False ,
81+ )
82+
83+ logMsg = "log test - valid log"
84+ log_debug_message (logMsg )
85+
86+ assert logMsg not in self ._caplog .text
87+
88+ def test_4_logger_config_with_debug_true (self ):
89+ start_st ()
90+ init (
91+ supertokens_config = SupertokensConfig ("http://localhost:3567" ),
92+ app_info = InputAppInfo (
93+ app_name = "SuperTokens Demo" ,
94+ api_domain = "api.supertokens.io" ,
95+ website_domain = "supertokens.io" ,
96+ api_base_path = "/" ,
97+ ),
98+ framework = "fastapi" ,
99+ recipe_list = [session .init (anti_csrf = "VIA_CUSTOM_HEADER" )],
100+ debug = True ,
101+ )
102+
103+ logMsg = "log test - valid log"
104+ log_debug_message (logMsg )
105+
106+ assert logMsg in self ._caplog .text
107+
108+ def test_5_logger_config_with_debug_not_set (self ):
109+ start_st ()
110+ init (
111+ supertokens_config = SupertokensConfig ("http://localhost:3567" ),
112+ app_info = InputAppInfo (
113+ app_name = "SuperTokens Demo" ,
114+ api_domain = "api.supertokens.io" ,
115+ website_domain = "supertokens.io" ,
116+ api_base_path = "/" ,
117+ ),
118+ framework = "fastapi" ,
119+ recipe_list = [session .init (anti_csrf = "VIA_CUSTOM_HEADER" )],
120+ )
121+
122+ logMsg = "log test - valid log"
123+ log_debug_message (logMsg )
124+
125+ assert logMsg not in self ._caplog .text
126+
127+ def test_6_logger_config_with_env_var (self ):
128+ os .environ ["SUPERTOKENS_DEBUG" ] = "1"
129+
130+ # environment variable was already set when the logger module was imported
131+ # since its being changed for the test, the module needs to be reloaded.
132+ importlib .reload (sys .modules ["supertokens_python.logger" ])
133+ start_st ()
134+ init (
135+ supertokens_config = SupertokensConfig ("http://localhost:3567" ),
136+ app_info = InputAppInfo (
137+ app_name = "SuperTokens Demo" ,
138+ api_domain = "api.supertokens.io" ,
139+ website_domain = "supertokens.io" ,
140+ api_base_path = "/" ,
141+ ),
142+ framework = "fastapi" ,
143+ recipe_list = [session .init (anti_csrf = "VIA_CUSTOM_HEADER" )],
144+ )
145+
146+ logMsg = "log test - valid log"
147+ log_debug_message (logMsg )
148+
149+ # Unset the environment variable
150+ del os .environ ["SUPERTOKENS_DEBUG" ]
151+
152+ assert logMsg in self ._caplog .text
0 commit comments