-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Base test class to handle auth (#153)
* Adding auth tokens * Setup for auth tests * Fixing tests * Cleanup * Spelling * Remove unnecessary commit
- Loading branch information
1 parent
e43bf95
commit 0aea3a3
Showing
5 changed files
with
133 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""Base class for endpoint tests""" | ||
|
||
from typing import List, Tuple | ||
from pytest import param | ||
|
||
def authentication_tests(tests: List[Tuple[str, List[str], List[str]]]) -> List[any]: | ||
"""Transform the format to single authentication tests""" | ||
|
||
single_tests = [] | ||
for test in tests: | ||
endpoint, parameters, methods = test | ||
for method in methods: | ||
single_tests.append(param( | ||
(endpoint, parameters, method), | ||
id = f"{endpoint} {method}" | ||
)) | ||
return single_tests | ||
|
||
def authorization_tests(tests: List[Tuple[str, List[str], str, List[str], List[str]]]) -> List[any]: | ||
"""Transform the format to single authorization tests""" | ||
|
||
single_tests = [] | ||
for test in tests: | ||
endpoint, parameters, method, allowed_tokens, disallowed_tokens = test | ||
for token in (allowed_tokens + disallowed_tokens): | ||
allowed = token in allowed_tokens | ||
single_tests.append(param( | ||
(endpoint, parameters, method, token, allowed), | ||
id = f"{endpoint} {method} {token} {'allowed' if allowed else 'disallowed'}" | ||
)) | ||
return single_tests | ||
|
||
class TestEndpoint: | ||
"""Base class for endpoint tests""" | ||
|
||
def authentication(self, authentication_parameter: Tuple[str, any]): | ||
"""Test if the authentication for the given enpoint works""" | ||
|
||
endpoint, method = authentication_parameter | ||
|
||
response = method(endpoint) | ||
assert response.status_code == 401 | ||
|
||
response = method(endpoint, headers = {"Authorization": "0123456789"}) | ||
assert response.status_code == 401 | ||
|
||
response = method(endpoint, headers = {"Authorization": "login"}) | ||
assert response.status_code != 401 | ||
|
||
def authorization(self, auth_parameter: Tuple[str, any, str, bool]): | ||
"""Test if the authorization for the given endpoint works""" | ||
|
||
endpoint, method, token, allowed = auth_parameter | ||
|
||
response = method(endpoint, headers = {"Authorization": token}) | ||
assert allowed == (response.status_code != 403) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters