-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor TokenFile, add tests * Add api.py tests * Add errorbuilder.py tests * Remove invalid assertions * Test tokenfile class methods on class In the app, we use these methods on the class itself, not instances of it. * Add unit test workflow job * Add fault tolerance for invalid token file * Refactor TokenFile.validate_tokens --------- Co-authored-by: Terje Kvernes <terjekv@users.noreply.github.com>
- Loading branch information
Showing
9 changed files
with
621 additions
and
34 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
Empty file.
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,37 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
from typing import Iterator | ||
|
||
import pytest | ||
from pytest_httpserver import HTTPServer | ||
|
||
from mreg_cli.config import MregCliConfig | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def set_url_env(httpserver: HTTPServer) -> Iterator[None]: | ||
"""Set the config URL to the test HTTP server URL.""" | ||
conf = MregCliConfig() | ||
pre_override_conf = conf._config_cmd.copy() # pyright: ignore[reportPrivateUsage] | ||
conf._config_cmd["url"] = httpserver.url_for("/") # pyright: ignore[reportPrivateUsage] | ||
yield | ||
conf._config_cmd = pre_override_conf # pyright: ignore[reportPrivateUsage] | ||
|
||
|
||
@pytest.fixture(autouse=True if os.environ.get("PYTEST_HTTPSERVER_STRICT") else False) | ||
def check_assertions(httpserver: HTTPServer) -> Iterator[None]: | ||
"""Ensure all HTTP server assertions are checked after the test.""" | ||
# If the HTTP server raises errors or has failed assertions in its handlers | ||
# themselves, we want to raise an exception to fail the test. | ||
# | ||
# The `check_assertions` method will raise an exception if there are | ||
# if any tests have HTTP test server errors. | ||
# See: https://pytest-httpserver.readthedocs.io/en/latest/tutorial.html#handling-test-errors | ||
# https://pytest-httpserver.readthedocs.io/en/latest/howto.html#using-custom-request-handler | ||
# | ||
# If a test has an assertion or handler error that is expected, it should | ||
# call `httpserver.clear_assertions()` and/or `httpserver.clear_handler_errors()` as needed. | ||
yield | ||
httpserver.check_assertions() | ||
httpserver.check_handler_errors() |
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 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from mreg_cli.errorbuilder import ( | ||
ErrorBuilder, | ||
FallbackErrorBuilder, | ||
FilterErrorBuilder, | ||
build_error_message, | ||
get_builder, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"command, exc_or_str, expected", | ||
[ | ||
( | ||
r"permission label_add 192.168.0.0/24 oracle-group ^(db|cman)ora.*\.example\.com$ oracle", | ||
"failed to compile regex", | ||
FilterErrorBuilder, | ||
), | ||
( | ||
r"permission label_add other_error", | ||
"Other error message", | ||
FallbackErrorBuilder, | ||
), | ||
], | ||
) | ||
def test_get_builder(command: str, exc_or_str: str, expected: type[ErrorBuilder]) -> None: | ||
builder = get_builder(command, exc_or_str) | ||
assert builder.__class__ == expected | ||
assert builder.get_underline(0, 0) == "" | ||
assert builder.get_underline(0, 10) == "^^^^^^^^^^" | ||
assert builder.get_underline(5, 10) == " ^^^^^" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"command, exc_or_str, expected", | ||
[ | ||
( | ||
r"permission label_add 192.168.0.0/24 oracle-group ^(db|cman)ora.*\.example\.com$ oracle", | ||
r"Unable to compile regex 'cman)ora.*\.example\.com$ oracle'", | ||
r"""Unable to compile regex 'cman)ora.*\.example\.com$ oracle' | ||
permission label_add 192.168.0.0/24 oracle-group ^(db|cman)ora.*\.example\.com$ oracle | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
└ Consider enclosing this part in quotes.""", | ||
), | ||
( | ||
r"permission label_add other_error", | ||
"Other error message", | ||
"Other error message", | ||
), | ||
], | ||
) | ||
def test_build_error_message(command: str, exc_or_str: str, expected: str) -> None: | ||
assert build_error_message(command, exc_or_str) == expected |
Oops, something went wrong.