Skip to content

Commit 11230b6

Browse files
authored
Merge pull request #9 from scrapy-plugins/fix/linting
Fix/linting
2 parents 87b05e7 + a1a8a84 commit 11230b6

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

.git-blame-ignore-revs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
d2d60666e425eeda24b7735702978b7dbef509f9
2+
eabf7df0eba1a7edf25712c3272d171d5aeb11ea

src/scrapy_settings_log/__init__.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
logger = logging.getLogger(__name__)
1111

1212
DEFAULT_REGEXES = [
13-
".*(?i)(api[\W_]*key).*", # apikey and variations e.g: shub_apikey or SC_APIKEY
14-
".*(?i)(AWS[\W_]*(SECRET[\W_]*)?(ACCESS)?[\W_]*(KEY|ACCESS[\W_]*KEY))", # AWS_SECRET_ACCESS_KEY and variations
15-
".*(?i)([\W_]*password[\W_]*).*" # password word
13+
r"(?i).*(api[\W_]*key).*", # apikey and variations e.g: shub_apikey or SC_APIKEY
14+
r"(?i).*(AWS[\W_]*(SECRET[\W_]*)?(ACCESS)?[\W_]*(KEY|ACCESS[\W_]*KEY))", # AWS_SECRET_ACCESS_KEY and variations
15+
r"(?i).*([\W_]*password[\W_]*).*", # password word
1616
]
1717

1818

@@ -58,9 +58,15 @@ def spider_closed(self, spider):
5858
if regex is not None:
5959
settings = {k: v for k, v in settings.items() if re.search(regex, k)}
6060
if spider.settings.getbool("MASKED_SENSITIVE_SETTINGS_ENABLED", True):
61-
regex_list = spider.settings.getlist("MASKED_SENSITIVE_SETTINGS_REGEX_LIST", DEFAULT_REGEXES)
61+
regex_list = spider.settings.getlist(
62+
"MASKED_SENSITIVE_SETTINGS_REGEX_LIST", DEFAULT_REGEXES
63+
)
6264
for reg in regex_list:
63-
updated_settings = {k: '**********' if v else v for k, v in settings.items() if re.match(reg, k)}
65+
updated_settings = {
66+
k: "**********" if v else v
67+
for k, v in settings.items()
68+
if re.match(reg, k)
69+
}
6470
settings = {**settings, **updated_settings}
6571

6672
self.output_settings(settings, spider)

tests/test_code.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ class CustomClass:
108108
def test_log_all_should_not_return_apikey_value_by_default(caplog):
109109
settings = {
110110
"SETTINGS_LOGGING_ENABLED": True,
111-
"SHUB_APIKEY": 'apikey_value1',
112-
"shub_apikey": 'apikey_value2',
113-
"api_key": 'apikey_value3',
111+
"SHUB_APIKEY": "apikey_value1",
112+
"shub_apikey": "apikey_value2",
113+
"api_key": "apikey_value3",
114114
}
115115

116116
spider = MockSpider(settings)
@@ -121,13 +121,15 @@ def test_log_all_should_not_return_apikey_value_by_default(caplog):
121121
assert '"SHUB_APIKEY": "**********"' in caplog.text
122122
assert '"shub_apikey": "**********"' in caplog.text
123123
assert '"api_key": "**********"' in caplog.text
124-
assert 'apikey_value' not in caplog.text
124+
assert "apikey_value" not in caplog.text
125125

126126

127-
def test_log_all_should_return_apikey_value_if_MASKED_SENSITIVE_SETTINGS_ENABLED_is_false(caplog):
127+
def test_log_all_should_return_apikey_value_if_MASKED_SENSITIVE_SETTINGS_ENABLED_is_false(
128+
caplog,
129+
):
128130
settings = {
129131
"SETTINGS_LOGGING_ENABLED": True,
130-
"APIKEY": 'apikey_value',
132+
"APIKEY": "apikey_value",
131133
"MASKED_SENSITIVE_SETTINGS_ENABLED": False,
132134
}
133135

@@ -142,11 +144,11 @@ def test_log_all_should_return_apikey_value_if_MASKED_SENSITIVE_SETTINGS_ENABLED
142144
def test_log_all_should_not_return_aws_secret_key_value_by_default(caplog):
143145
settings = {
144146
"SETTINGS_LOGGING_ENABLED": True,
145-
"AWS_SECRET_ACCESS_KEY": 'secret_value1',
146-
"aws_secret_access_key": 'secret_value2',
147-
"aws_access_key": 'secret_value2',
148-
"AWS_SECRET_KEY": 'secret_value2',
149-
"aws_secret_key": 'secret_value2',
147+
"AWS_SECRET_ACCESS_KEY": "secret_value1",
148+
"aws_secret_access_key": "secret_value2",
149+
"aws_access_key": "secret_value2",
150+
"AWS_SECRET_KEY": "secret_value2",
151+
"aws_secret_key": "secret_value2",
150152
}
151153

152154
spider = MockSpider(settings)
@@ -159,14 +161,14 @@ def test_log_all_should_not_return_aws_secret_key_value_by_default(caplog):
159161
assert '"aws_access_key": "**********"' in caplog.text
160162
assert '"AWS_SECRET_KEY": "**********"' in caplog.text
161163
assert '"aws_secret_key": "**********"' in caplog.text
162-
assert 'secret_value' not in caplog.text
164+
assert "secret_value" not in caplog.text
163165

164166

165167
def test_log_all_should_not_return_password_value_by_default(caplog):
166168
settings = {
167169
"SETTINGS_LOGGING_ENABLED": True,
168-
"test_password": 'secret_value1',
169-
"PASSWORD_TEST": 'secret_value2',
170+
"test_password": "secret_value1",
171+
"PASSWORD_TEST": "secret_value2",
170172
}
171173

172174
spider = MockSpider(settings)
@@ -176,22 +178,24 @@ def test_log_all_should_not_return_password_value_by_default(caplog):
176178

177179
assert '"test_password": "**********"' in caplog.text
178180
assert '"PASSWORD_TEST": "**********"' in caplog.text
179-
assert 'secret_value' not in caplog.text
181+
assert "secret_value" not in caplog.text
180182

181183

182-
def test_log_all_should_return_only_the_custom_regex_data_masked_if_MASKED_SENSITIVE_SETTINGS_REGEX_LIST_configured(caplog):
184+
def test_log_all_should_return_only_the_custom_regex_data_masked_if_MASKED_SENSITIVE_SETTINGS_REGEX_LIST_configured(
185+
caplog,
186+
):
183187
settings = {
184188
"SETTINGS_LOGGING_ENABLED": True,
185189
"MASKED_SENSITIVE_SETTINGS_REGEX_LIST": ["apppppppikey"],
186-
"APIKEY": 'apikey_value1',
187-
"apppppppikey": 'some_random_value',
190+
"APIKEY": "apikey_value1",
191+
"apppppppikey": "some_random_value",
188192
}
189193

190194
spider = MockSpider(settings)
191195
logger = SpiderSettingsLogging()
192196
with caplog.at_level(logging.INFO):
193197
logger.spider_closed(spider)
194198

195-
assert 'apikey_value1' in caplog.text
199+
assert "apikey_value1" in caplog.text
196200
assert '"apppppppikey": "**********"' in caplog.text
197-
assert 'some_random_value' not in caplog.text
201+
assert "some_random_value" not in caplog.text

0 commit comments

Comments
 (0)