Skip to content

Commit fc242ac

Browse files
committed
HTTP mixin: warn if only username or password is set
and add a testcase for the warning
1 parent 676f384 commit fc242ac

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

intelmq/lib/mixins/http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def setup(self):
6868
# auth settings. username or password must exist (if only one, then this is likely an error, but we should try without auth) and not be an empty string
6969
if self.http_username and self.http_password:
7070
self.__auth = (self.http_username, self.http_password)
71-
elif self.http_username and self.http_password:
71+
elif self.http_username or self.http_password:
7272
# only one, but not both are given
7373
self.logger.warning("Either 'http_username' or 'http_password' are given, but for HTTP Authentication, both must be set.")
7474
self.__session.auth = self.__auth

intelmq/tests/bots/collectors/http/test_collector.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,23 @@ def check_authorization_header(request):
225225
additional_matcher=check_authorization_header)
226226
self.run_bot()
227227

228+
def test_auth_only_username_or_password(self, mocker):
229+
"""
230+
Test that no Authorization is set only the username is given and password is missing
231+
"""
232+
def check_authorization_header(request):
233+
print(f'check_authorization_header: {request.headers}') # show the erroneos headers when the test fails
234+
return 'Authorization' not in request.headers
235+
# generates a mock address if the Authorization header is empty, otherwise the test fails
236+
captured = mocker.register_uri('GET', self.sysconfig['http_url'],
237+
text='Foo Bar',
238+
additional_matcher=check_authorization_header)
239+
log_line = "Either 'http_username' or 'http_password' are given, but for HTTP Authentication, both must be set\."
240+
self.run_bot(parameters={'http_username': 'username'}, allowed_warning_count=1)
241+
self.assertLogMatches(log_line, 'WARNING')
242+
self.run_bot(parameters={'http_password': 'password'}, allowed_warning_count=1)
243+
self.assertLogMatches(log_line, 'WARNING')
244+
228245
def test_auth(self, mocker):
229246
"""
230247
Test the Authorization header when username and password are set.

0 commit comments

Comments
 (0)