1+ import importlib
12import json
3+ import os
4+ import sys
25from datetime import datetime as real_datetime
36from unittest import TestCase
47from unittest .mock import MagicMock , patch
58
9+ import pytest
10+
11+ from supertokens_python import InputAppInfo , SupertokensConfig , init
612from supertokens_python .constants import VERSION
713from supertokens_python .logger import log_debug_message , streamFormatter
14+ from supertokens_python .recipe import session
15+
16+ from tests .utils import clean_st , reset , setup_st , start_st
817
918
1019class LoggerTests (TestCase ):
20+ def setup_method (self , _ ):
21+ self ._caplog .clear ()
22+ reset ()
23+ clean_st ()
24+ setup_st ()
25+
26+ def teardown_method (self , _ ):
27+ self ._caplog .clear ()
28+ reset ()
29+ clean_st ()
30+
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+
1136 @patch ("supertokens_python.logger.datetime" , wraps = real_datetime )
1237 def test_json_msg_format (self , datetime_mock : MagicMock ):
1338 datetime_mock .utcnow .return_value = real_datetime (2000 , 1 , 1 ) # type: ignore
@@ -22,7 +47,7 @@ def test_json_msg_format(self, datetime_mock: MagicMock):
2247 "t" : "2000-01-01T00:00Z" ,
2348 "sdkVer" : VERSION ,
2449 "message" : "API replied with status 200" ,
25- "file" : "../tests/test_logger.py:16 " ,
50+ "file" : "../tests/test_logger.py:40 " ,
2651 }
2752
2853 @staticmethod
@@ -31,3 +56,89 @@ def test_stream_formatter_format():
3156 streamFormatter ._fmt # pylint: disable=protected-access
3257 == "{name} {message}\n "
3358 )
59+
60+ def test_logger_config_with_debug_true (self ):
61+ start_st ()
62+ init (
63+ supertokens_config = SupertokensConfig ("http://localhost:3567" ),
64+ app_info = InputAppInfo (
65+ app_name = "SuperTokens Demo" ,
66+ api_domain = "api.supertokens.io" ,
67+ website_domain = "supertokens.io" ,
68+ api_base_path = "/" ,
69+ ),
70+ framework = "fastapi" ,
71+ recipe_list = [session .init (anti_csrf = "VIA_CUSTOM_HEADER" )],
72+ debug = True ,
73+ )
74+
75+ logMsg = "log test - valid log"
76+ log_debug_message (logMsg )
77+
78+ assert logMsg in self ._caplog .text
79+
80+ def test_logger_config_with_debug_false (self ):
81+ start_st ()
82+ init (
83+ supertokens_config = SupertokensConfig ("http://localhost:3567" ),
84+ app_info = InputAppInfo (
85+ app_name = "SuperTokens Demo" ,
86+ api_domain = "api.supertokens.io" ,
87+ website_domain = "supertokens.io" ,
88+ api_base_path = "/" ,
89+ ),
90+ framework = "fastapi" ,
91+ recipe_list = [session .init (anti_csrf = "VIA_CUSTOM_HEADER" )],
92+ debug = False ,
93+ )
94+
95+ logMsg = "log test - valid log"
96+ log_debug_message (logMsg )
97+
98+ assert logMsg not in self ._caplog .text
99+
100+ def test_logger_config_with_debug_not_set (self ):
101+ start_st ()
102+ init (
103+ supertokens_config = SupertokensConfig ("http://localhost:3567" ),
104+ app_info = InputAppInfo (
105+ app_name = "SuperTokens Demo" ,
106+ api_domain = "api.supertokens.io" ,
107+ website_domain = "supertokens.io" ,
108+ api_base_path = "/" ,
109+ ),
110+ framework = "fastapi" ,
111+ recipe_list = [session .init (anti_csrf = "VIA_CUSTOM_HEADER" )],
112+ )
113+
114+ logMsg = "log test - valid log"
115+ log_debug_message (logMsg )
116+
117+ assert logMsg not in self ._caplog .text
118+
119+ def test_logger_config_with_env_var (self ):
120+ os .environ ["SUPERTOKENS_DEBUG" ] = "1"
121+
122+ # environment variable was already set when the logger module was imported
123+ # since its being changed for the test, the module needs to be reloaded.
124+ importlib .reload (sys .modules ["supertokens_python.logger" ])
125+ start_st ()
126+ init (
127+ supertokens_config = SupertokensConfig ("http://localhost:3567" ),
128+ app_info = InputAppInfo (
129+ app_name = "SuperTokens Demo" ,
130+ api_domain = "api.supertokens.io" ,
131+ website_domain = "supertokens.io" ,
132+ api_base_path = "/" ,
133+ ),
134+ framework = "fastapi" ,
135+ recipe_list = [session .init (anti_csrf = "VIA_CUSTOM_HEADER" )],
136+ )
137+
138+ logMsg = "log test - valid log"
139+ log_debug_message (logMsg )
140+
141+ # Unset the environment variable
142+ del os .environ ["SUPERTOKENS_DEBUG" ]
143+
144+ assert logMsg in self ._caplog .text
0 commit comments