-
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.
- Loading branch information
Showing
6 changed files
with
228 additions
and
20 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,173 @@ | ||
from pathlib import Path | ||
from typing import Iterator | ||
|
||
import pytest | ||
from inline_snapshot import snapshot | ||
|
||
from mreg_cli.tokenfile import TokenFile | ||
|
||
TOKENS_PATH_ORIGINAL = TokenFile.tokens_path | ||
|
||
|
||
TOKEN_FILE_SINGLE = """ | ||
{ | ||
"tokens": [ | ||
{ | ||
"token": "exampletoken123", | ||
"url": "https://example.com", | ||
"username": "exampleuser" | ||
} | ||
] | ||
} | ||
""" | ||
|
||
|
||
TOKEN_FILE_MULTIPLE = """ | ||
{ | ||
"tokens": [ | ||
{ | ||
"token": "exampletoken123", | ||
"url": "https://example.com", | ||
"username": "exampleuser" | ||
}, | ||
{ | ||
"token": "footoken456", | ||
"url": "https://foo.com", | ||
"username": "foouser" | ||
}, | ||
{ | ||
"token": "bartoken789", | ||
"url": "https://bar.com", | ||
"username": "baruser" | ||
} | ||
] | ||
} | ||
""" | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def reset_token_file_path() -> Iterator[None]: | ||
"""Reset the token file path after each test.""" | ||
yield | ||
TokenFile.tokens_path = TOKENS_PATH_ORIGINAL | ||
|
||
|
||
def test_load_tokens_file_nonexistent(tmp_path: Path) -> None: | ||
"""Load from a nonexistent tokens file.""" | ||
tokens_path = tmp_path / "does_not_exist.json" | ||
assert not tokens_path.exists() | ||
TokenFile.tokens_path = str(tokens_path) | ||
tokenfile = TokenFile._load_tokens() | ||
assert tokenfile.tokens == [] | ||
|
||
|
||
def test_load_tokens_file_empty(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: | ||
"""Load from an empty tokens file.""" | ||
tokens_path = tmp_path / "empty.json" | ||
tokens_path.touch() | ||
assert tokens_path.read_text() == "" | ||
TokenFile.tokens_path = str(tokens_path) | ||
tokenfile = TokenFile._load_tokens() | ||
assert tokenfile.tokens == [] | ||
assert "Failed to decode JSON" in capsys.readouterr().err | ||
|
||
|
||
def test_load_tokens_file_invalid(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: | ||
"""Load from a tokens file with invalid JSON.""" | ||
tokens_path = tmp_path / "invalid.json" | ||
tokens_path.write_text("not json") | ||
assert tokens_path.read_text() == "not json" | ||
TokenFile.tokens_path = str(tokens_path) | ||
tokenfile = TokenFile._load_tokens() | ||
assert tokenfile.tokens == [] | ||
assert "Failed to decode JSON" in capsys.readouterr().err | ||
|
||
|
||
def test_load_tokens_file_single(tmp_path: Path) -> None: | ||
"""Load from a tokens file with a single token.""" | ||
tokens_path = tmp_path / "single.json" | ||
tokens_path.write_text(TOKEN_FILE_SINGLE) | ||
TokenFile.tokens_path = str(tokens_path) | ||
tokenfile = TokenFile._load_tokens() | ||
assert len(tokenfile.tokens) == 1 | ||
assert tokenfile.tokens[0].token == snapshot("exampletoken123") | ||
assert tokenfile.tokens[0].url == snapshot("https://example.com") | ||
assert tokenfile.tokens[0].username == snapshot("exampleuser") | ||
|
||
|
||
def test_load_tokens_file_multiple(tmp_path: Path) -> None: | ||
"""Load from a tokens file with multiple tokens.""" | ||
tokens_path = tmp_path / "multiple.json" | ||
tokens_path.write_text(TOKEN_FILE_MULTIPLE) | ||
TokenFile.tokens_path = str(tokens_path) | ||
tokenfile = TokenFile._load_tokens() | ||
assert len(tokenfile.tokens) == snapshot(3) | ||
|
||
|
||
def test_get_entry(tmp_path: Path) -> None: | ||
"""Get a token from the token file.""" | ||
tokens_path = tmp_path / "get_token.json" | ||
tokens_path.write_text(TOKEN_FILE_MULTIPLE) | ||
TokenFile.tokens_path = str(tokens_path) | ||
tokenfile = TokenFile._load_tokens() | ||
|
||
token = tokenfile.get_entry("exampleuser", "https://example.com") | ||
assert token is not None | ||
assert token.token == snapshot("exampletoken123") | ||
assert token.url == snapshot("https://example.com") | ||
assert token.username == snapshot("exampleuser") | ||
|
||
token = tokenfile.get_entry("foouser", "https://foo.com") | ||
assert token is not None | ||
assert token.token == snapshot("footoken456") | ||
assert token.url == snapshot("https://foo.com") | ||
assert token.username == snapshot("foouser") | ||
|
||
token = tokenfile.get_entry("baruser", "https://bar.com") | ||
assert token is not None | ||
assert token.token == snapshot("bartoken789") | ||
assert token.url == snapshot("https://bar.com") | ||
assert token.username == snapshot("baruser") | ||
|
||
token = tokenfile.get_entry("nonexistent", "https://example.com") | ||
assert token is None | ||
|
||
|
||
def test_set_entry_existing(tmp_path: Path) -> None: | ||
"""Set a token in the token file that already exists.""" | ||
tokens_path = tmp_path / "set_existing.json" | ||
tokens_path.write_text(TOKEN_FILE_MULTIPLE) | ||
TokenFile.tokens_path = str(tokens_path) | ||
tokenfile = TokenFile._load_tokens() | ||
|
||
assert len(tokenfile.tokens) == snapshot(3) | ||
tokenfile = tokenfile.set_entry("newuser", "https://new.com", "newtoken123") | ||
assert len(tokenfile.tokens) == snapshot(4) | ||
token = tokenfile.get_entry("newuser", "https://new.com") | ||
assert token is not None | ||
assert token.token == snapshot("newtoken123") | ||
|
||
|
||
@pytest.mark.parametrize("create_before", [True, False], ids=["create_before", "create_after"]) | ||
def test_set_entry_new(tmp_path: Path, create_before: bool) -> None: | ||
"""Set a token in the token file that does not already exist.""" | ||
tokens_path = tmp_path / "set_new.json" | ||
if create_before: | ||
tokens_path.touch() # empty file | ||
TokenFile.tokens_path = str(tokens_path) | ||
tokenfile = TokenFile._load_tokens() | ||
|
||
# Write tokens to the empty file | ||
assert len(tokenfile.tokens) == snapshot(0) | ||
tokenfile = tokenfile.set_entry("newuser", "https://new.com", "newtoken123") | ||
assert len(tokenfile.tokens) == snapshot(1) | ||
token = tokenfile.get_entry("newuser", "https://new.com") | ||
assert token is not None | ||
assert token.token == snapshot("newtoken123") | ||
|
||
# Try to load the tokens from the file again | ||
new_tokenfile = TokenFile._load_tokens() | ||
assert len(new_tokenfile.tokens) == snapshot(1) | ||
token = new_tokenfile.get_entry("newuser", "https://new.com") | ||
assert token is not None | ||
assert token.token == snapshot("newtoken123") |
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,22 @@ | ||
from typing import Any | ||
|
||
import pytest | ||
|
||
from mreg_cli.utilities.api import strip_none | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"inp,expect", | ||
[ | ||
# Empty dict | ||
({}, {}), | ||
# Mixed values | ||
({"foo": "bar", "baz": None}, {"foo": "bar"}), | ||
# Multiple keys with None values | ||
({"foo": None, "bar": None}, {}), | ||
# Nested dicts | ||
({"foo": {"bar": {"baz": None}}}, {}), | ||
], | ||
) | ||
def test_strip_none(inp: dict[str, Any], expect: dict[str, Any]) -> None: | ||
assert strip_none(inp) == expect |
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