Added a new backend settings system
This can be used for saving runtime-configurable settings in the database.
This commit is contained in:
1
tests_integration/tests/setting/__init__.py
Normal file
1
tests_integration/tests/setting/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for the setting API."""
|
||||
44
tests_integration/tests/setting/test_get.py
Normal file
44
tests_integration/tests/setting/test_get.py
Normal file
@@ -0,0 +1,44 @@
|
||||
"""Integration tests for the Vendor API endpoint."""
|
||||
|
||||
import httpx
|
||||
|
||||
URL = "http://spoolman:8000"
|
||||
|
||||
|
||||
def test_get_currency():
|
||||
"""Test getting the currency setting."""
|
||||
# Execute
|
||||
result = httpx.get(f"{URL}/api/v1/setting/currency")
|
||||
result.raise_for_status()
|
||||
|
||||
# Verify
|
||||
setting = result.json()
|
||||
assert setting == {
|
||||
"value": '"EUR"',
|
||||
"is_set": False,
|
||||
"type": "string",
|
||||
}
|
||||
|
||||
|
||||
def test_get_unknown():
|
||||
"""Test getting an unknown setting."""
|
||||
# Execute
|
||||
result = httpx.get(f"{URL}/api/v1/setting/unknown")
|
||||
assert result.status_code == 404
|
||||
|
||||
|
||||
def test_get_all():
|
||||
"""Test getting all settings."""
|
||||
# Execute
|
||||
result = httpx.get(f"{URL}/api/v1/setting/")
|
||||
result.raise_for_status()
|
||||
|
||||
# Verify
|
||||
settings = result.json()
|
||||
assert settings == {
|
||||
"currency": {
|
||||
"value": '"EUR"',
|
||||
"is_set": False,
|
||||
"type": "string",
|
||||
},
|
||||
}
|
||||
115
tests_integration/tests/setting/test_set.py
Normal file
115
tests_integration/tests/setting/test_set.py
Normal file
@@ -0,0 +1,115 @@
|
||||
"""Integration tests for the Vendor API endpoint."""
|
||||
|
||||
import json
|
||||
|
||||
import httpx
|
||||
|
||||
URL = "http://spoolman:8000"
|
||||
|
||||
|
||||
def test_set_currency():
|
||||
"""Test setting the currency setting."""
|
||||
# Execute
|
||||
result = httpx.post(
|
||||
f"{URL}/api/v1/setting/currency",
|
||||
json='"SEK"',
|
||||
)
|
||||
result.raise_for_status()
|
||||
|
||||
# Verify
|
||||
setting = result.json()
|
||||
assert setting == {
|
||||
"value": '"SEK"',
|
||||
"is_set": True,
|
||||
"type": "string",
|
||||
}
|
||||
|
||||
# Cleanup
|
||||
result = httpx.post(
|
||||
f"{URL}/api/v1/setting/currency",
|
||||
json="",
|
||||
)
|
||||
result.raise_for_status()
|
||||
|
||||
|
||||
def test_unset_currency():
|
||||
"""Test un-setting the currency setting."""
|
||||
# Execute set
|
||||
result = httpx.post(
|
||||
f"{URL}/api/v1/setting/currency",
|
||||
json='"SEK"',
|
||||
)
|
||||
result.raise_for_status()
|
||||
|
||||
# Verify set
|
||||
setting = result.json()
|
||||
assert setting == {
|
||||
"value": '"SEK"',
|
||||
"is_set": True,
|
||||
"type": "string",
|
||||
}
|
||||
|
||||
# Execute unset
|
||||
result = httpx.post(
|
||||
f"{URL}/api/v1/setting/currency",
|
||||
json="",
|
||||
)
|
||||
result.raise_for_status()
|
||||
|
||||
# Verify unset
|
||||
setting = result.json()
|
||||
assert setting == {
|
||||
"value": '"EUR"',
|
||||
"is_set": False,
|
||||
"type": "string",
|
||||
}
|
||||
|
||||
|
||||
def test_set_unknown():
|
||||
"""Test setting an invalid setting."""
|
||||
# Execute
|
||||
result = httpx.post(
|
||||
f"{URL}/api/v1/setting/not-a-setting",
|
||||
json='"SEK"',
|
||||
)
|
||||
assert result.status_code == 404
|
||||
|
||||
|
||||
def test_set_currency_wrong_type():
|
||||
"""Test setting the currency setting with the wrong type."""
|
||||
# Execute
|
||||
result = httpx.post(
|
||||
f"{URL}/api/v1/setting/currency",
|
||||
json=123,
|
||||
)
|
||||
assert result.status_code == 400
|
||||
|
||||
|
||||
def test_set_big_value():
|
||||
"""Test setting a setting to a long string which should be saved correctly."""
|
||||
long_string = "a" * (2**16 - 1 - 2) # Backend guarantees that it can handle strings up to 65535 characters long.
|
||||
# Remove 2 characters to account for the quotes.
|
||||
|
||||
# Execute
|
||||
result = httpx.post(
|
||||
f"{URL}/api/v1/setting/currency",
|
||||
json=json.dumps(long_string),
|
||||
)
|
||||
result.raise_for_status()
|
||||
|
||||
# Verify
|
||||
result = httpx.get(f"{URL}/api/v1/setting/currency")
|
||||
result.raise_for_status()
|
||||
setting = result.json()
|
||||
assert setting == {
|
||||
"value": json.dumps(long_string),
|
||||
"is_set": True,
|
||||
"type": "string",
|
||||
}
|
||||
|
||||
# Cleanup
|
||||
result = httpx.post(
|
||||
f"{URL}/api/v1/setting/currency",
|
||||
json="",
|
||||
)
|
||||
result.raise_for_status()
|
||||
Reference in New Issue
Block a user