Skip to content

Commit

Permalink
fix: removed unused variable
Browse files Browse the repository at this point in the history
fix: ordered tests andd reset logger after each test
  • Loading branch information
IamMayankThakur committed Nov 3, 2023
1 parent 8f902a0 commit c1bdfae
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
1 change: 0 additions & 1 deletion supertokens_python/supertokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ def __init__(
mode,
)
self.supertokens_config = supertokens_config
self.debug = debug
if debug is True:
enable_debug_logging()
self._telemetry_status: str = "NONE"
Expand Down
46 changes: 27 additions & 19 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import importlib
import json
import logging
import os
import sys
from datetime import datetime as real_datetime
Expand All @@ -10,15 +11,26 @@

from supertokens_python import InputAppInfo, SupertokensConfig, init
from supertokens_python.constants import VERSION
from supertokens_python.logger import log_debug_message, streamFormatter
from supertokens_python.logger import (
log_debug_message,
streamFormatter,
NAMESPACE,
enable_debug_logging,
)
from supertokens_python.recipe import session

from tests.utils import clean_st, reset, setup_st, start_st


class LoggerTests(TestCase):
def setup_method(self, _):
self._caplog.clear()
@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog: pytest.LogCaptureFixture):
# caplog is the pytest fixture to capture all logs
self._caplog = caplog # pylint: disable=attribute-defined-outside-init

def setup_method(self, _): # pylint: disable=no-self-use
# Setting the log level to a higher level so debug logs are not printed
logging.getLogger(NAMESPACE).setLevel(logging.ERROR)
reset()
clean_st()
setup_st()
Expand All @@ -28,13 +40,9 @@ def teardown_method(self, _):
reset()
clean_st()

@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog: pytest.LogCaptureFixture):
# caplog is the pytest fixture to capture all logs
self._caplog = caplog # pylint: disable=attribute-defined-outside-init

@patch("supertokens_python.logger.datetime", wraps=real_datetime)
def test_json_msg_format(self, datetime_mock: MagicMock):
def test_1_json_msg_format(self, datetime_mock: MagicMock):
enable_debug_logging()
datetime_mock.utcnow.return_value = real_datetime(2000, 1, 1) # type: ignore

with self.assertLogs(level="DEBUG") as captured:
Expand All @@ -47,17 +55,17 @@ def test_json_msg_format(self, datetime_mock: MagicMock):
"t": "2000-01-01T00:00Z",
"sdkVer": VERSION,
"message": "API replied with status 200",
"file": "../tests/test_logger.py:40",
"file": "../tests/test_logger.py:49",
}

@staticmethod
def test_stream_formatter_format():
def test_2_stream_formatter_format():
assert (
streamFormatter._fmt # pylint: disable=protected-access
== "{name} {message}\n"
)

def test_logger_config_with_debug_true(self):
def test_3_logger_config_with_debug_false(self):
start_st()
init(
supertokens_config=SupertokensConfig("http://localhost:3567"),
Expand All @@ -69,15 +77,15 @@ def test_logger_config_with_debug_true(self):
),
framework="fastapi",
recipe_list=[session.init(anti_csrf="VIA_CUSTOM_HEADER")],
debug=True,
debug=False,
)

logMsg = "log test - valid log"
log_debug_message(logMsg)

assert logMsg in self._caplog.text
assert logMsg not in self._caplog.text

def test_logger_config_with_debug_false(self):
def test_4_logger_config_with_debug_true(self):
start_st()
init(
supertokens_config=SupertokensConfig("http://localhost:3567"),
Expand All @@ -89,15 +97,15 @@ def test_logger_config_with_debug_false(self):
),
framework="fastapi",
recipe_list=[session.init(anti_csrf="VIA_CUSTOM_HEADER")],
debug=False,
debug=True,
)

logMsg = "log test - valid log"
log_debug_message(logMsg)

assert logMsg not in self._caplog.text
assert logMsg in self._caplog.text

def test_logger_config_with_debug_not_set(self):
def test_5_logger_config_with_debug_not_set(self):
start_st()
init(
supertokens_config=SupertokensConfig("http://localhost:3567"),
Expand All @@ -116,7 +124,7 @@ def test_logger_config_with_debug_not_set(self):

assert logMsg not in self._caplog.text

def test_logger_config_with_env_var(self):
def test_6_logger_config_with_env_var(self):
os.environ["SUPERTOKENS_DEBUG"] = "1"

# environment variable was already set when the logger module was imported
Expand Down

0 comments on commit c1bdfae

Please sign in to comment.