forked from retoor/devplacepy
feat: add test suites for backup schedules, service config, auth token, bookmarks, content permissions, devii adopt, and devrant auth
Add comprehensive test coverage for seven new API areas: - Backup schedules CRUD operations in admin panel - Service configuration saving and retrieval for news AI model - Token authentication with email/username and JSON body support - Bookmarking for project and news targets with invalid type validation - Content permissions testing with member and admin session fixtures - Devii adopt endpoint redirect behavior and session cookie handling - Devrant authentication flow with user registration and token retrieval
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import time
|
||||
import pytest
|
||||
import requests
|
||||
from tests.conftest import BASE_URL
|
||||
from devplacepy.database import get_table, refresh_snapshot, set_setting, get_setting
|
||||
|
||||
JSON = {"Accept": "application/json"}
|
||||
_counter_cfg = [0]
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def _settings(app_server):
|
||||
set_setting("rate_limit_per_minute", "1000000")
|
||||
set_setting("registration_open", "1")
|
||||
yield
|
||||
|
||||
|
||||
def _admin(seeded_db):
|
||||
refresh_snapshot()
|
||||
key = get_table("users").find_one(username="alice_test")["api_key"]
|
||||
s = requests.Session()
|
||||
s.headers.update({"X-API-KEY": key, **JSON})
|
||||
return s
|
||||
|
||||
|
||||
def _member():
|
||||
_counter_cfg[0] += 1
|
||||
name = f"cfgmem{int(time.time() * 1000)}{_counter_cfg[0]}"
|
||||
requests.post(
|
||||
f"{BASE_URL}/auth/signup",
|
||||
data={
|
||||
"username": name,
|
||||
"email": f"{name}@t.dev",
|
||||
"password": "secret123",
|
||||
"confirm_password": "secret123",
|
||||
},
|
||||
allow_redirects=True,
|
||||
)
|
||||
refresh_snapshot()
|
||||
s = requests.Session()
|
||||
s.headers.update(
|
||||
{"X-API-KEY": get_table("users").find_one(username=name)["api_key"], **JSON}
|
||||
)
|
||||
return s
|
||||
|
||||
|
||||
def test_admin_saves_service_config(seeded_db):
|
||||
admin = _admin(seeded_db)
|
||||
new_model = f"test-model-{int(time.time())}"
|
||||
original = get_setting("news_ai_model", "")
|
||||
try:
|
||||
r = admin.post(
|
||||
f"{BASE_URL}/admin/services/news/config",
|
||||
data={"news_ai_model": new_model},
|
||||
)
|
||||
assert r.status_code == 200, r.text[:300]
|
||||
assert r.json().get("ok") is True
|
||||
refresh_snapshot()
|
||||
assert get_setting("news_ai_model", "") == new_model
|
||||
finally:
|
||||
if original:
|
||||
set_setting("news_ai_model", original)
|
||||
|
||||
|
||||
def test_unknown_service_config_404(seeded_db):
|
||||
admin = _admin(seeded_db)
|
||||
r = admin.post(
|
||||
f"{BASE_URL}/admin/services/no_such_service/config",
|
||||
data={"foo": "bar"},
|
||||
)
|
||||
assert r.status_code == 404
|
||||
assert r.json().get("ok") is False
|
||||
|
||||
|
||||
def test_member_cannot_save_config(app_server):
|
||||
member = _member()
|
||||
r = member.post(
|
||||
f"{BASE_URL}/admin/services/news/config",
|
||||
data={"news_ai_model": "x"},
|
||||
allow_redirects=False,
|
||||
)
|
||||
assert r.status_code in (302, 303, 403)
|
||||
|
||||
|
||||
def test_guest_cannot_save_config(app_server):
|
||||
r = requests.post(
|
||||
f"{BASE_URL}/admin/services/news/config",
|
||||
data={"news_ai_model": "x"},
|
||||
allow_redirects=False,
|
||||
)
|
||||
assert r.status_code in (302, 303, 401)
|
||||
Reference in New Issue
Block a user