Best Python code snippet using grail_python
test_log_sanitizer.py
Source:test_log_sanitizer.py
1import logging2from typing import Any, NamedTuple3import pytest4from ujcatapi.libs.log_sanitizer import sanitize_formatters5@pytest.mark.parametrize(6 "text, replaced",7 [8 # Case: leaves nearby text intact9 (10 "stuff='123', password='12345', etc",11 "stuff='123', password='**** [hidden for privacy] ****', etc",12 ),13 # Cases: redacts email addresses, may include plus in local14 ("email='alice@admin.sammybridge.com'", "email='ali**@adm**.sam********.c**'"),15 ("email='bob@user.sammybridge.com'", "email='b**@us**.sam********.c**'"),16 ("email='bob-with-hyphens@bob.com'", "email='b**-wi**-hyp****@b**.c**'"),17 ("email='me@me.com'", "email='**@**.c**'"),18 ("email='a@a.com'", "email='*@*.c**'"),19 ("email='alice.has.lots.of.pii+1@a.com'", "email='ali**.h**.lo**.**.p**+*@*.c**'"),20 # Case: emails don't need to be matched to "email" keyword21 ("...,alice@admin.sammybridge.com,...", "...,ali**@adm**.sam********.c**,..."),22 # Case: preserves "s23 ('password="password"', 'password="**** [hidden for privacy] ****"'),24 # Case: preserves 's25 ("password='password'", "password='**** [hidden for privacy] ****'"),26 # Case: value may contain "s or 's27 ("password='p@^&*(\\')\"xyz'", "password='**** [hidden for privacy] ****'"),28 # Case: partial request params with stack strace29 (30 "{'jwt': '123.123.123456\nTraceback:",31 "{'jwt': '**** [hidden for privacy] ****\nTraceback:",32 ),33 ],34)35def test_sanitizes_string(text: str, replaced: str, caplog: Any) -> None:36 caplog.set_level(logging.INFO)37 logger = logging.getLogger()38 sanitize_formatters(logger.handlers)39 logger.info(text)40 assert replaced in caplog.text41def test_sanitizes_object(caplog: Any) -> None:42 caplog.set_level(logging.INFO)43 # mypy has a cache-related issue with defining classes inside functions,44 # workaround is to use inline syntax. cf. https://github.com/python/mypy/issues/728145 DummyClass = NamedTuple(46 "DummyClass",47 [("test_field", str), ("password", str), ("password_reset_token", str), ("email", str)],48 )49 test_instance = DummyClass(50 test_field="test",51 password_reset_token="my_token",52 password="test_password\"'xyziuoahsodf123123",53 email="alice@admin.sammybridge.com",54 )55 logger = logging.getLogger()56 sanitize_formatters(logger.handlers)57 logger.info(str(test_instance))58 assert (59 "DummyClass(test_field='test', "60 "password='**** [hidden for privacy] ****', "61 "password_reset_token='**** [hidden for privacy] ****', "62 "email='ali**@adm**.sam********.c**')"63 ) in caplog.text64def test_sanitizes_dicts(caplog: Any) -> None:65 caplog.set_level(logging.INFO)66 test_dict_params = {67 "jwt": "abcd.efg.hijk",68 "access_token": "456",69 "email": "alice@admin.sammybridge.com",70 "credentials": {"password": "password"},71 }72 logger = logging.getLogger()73 sanitize_formatters(logger.handlers)74 logger.info(str(test_dict_params))75 assert (76 "{'jwt': '**** [hidden for privacy] ****', "77 "'access_token': '**** [hidden for privacy] ****', "78 "'email': 'ali**@adm**.sam********.c**', 'credentials': {'password': "79 "'**** [hidden for privacy] ****'}}"...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!