feat: add test suites for backup schedules, service config, auth token, bookmarks, content permissions, devii adopt, and devrant auth
DevPlace CI / test (push) Successful in 35m0s
DevPlace CI / test (push) Successful in 35m0s
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,134 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import time
|
||||
from datetime import datetime, timezone, timedelta
|
||||
|
||||
import requests
|
||||
|
||||
from devplacepy.database import get_table, refresh_snapshot
|
||||
from devplacepy.services.game import store
|
||||
from tests.conftest import BASE_URL
|
||||
|
||||
JSON = {"Accept": "application/json"}
|
||||
_counter_gm = [0]
|
||||
|
||||
|
||||
def _signup():
|
||||
_counter_gm[0] += 1
|
||||
name = f"gm{int(time.time() * 1000)}{_counter_gm[0]}"
|
||||
session = requests.Session()
|
||||
session.post(
|
||||
f"{BASE_URL}/auth/signup",
|
||||
data={
|
||||
"username": name,
|
||||
"email": f"{name}@t.dev",
|
||||
"password": "secret123",
|
||||
"confirm_password": "secret123",
|
||||
},
|
||||
allow_redirects=True,
|
||||
)
|
||||
return session, name
|
||||
|
||||
|
||||
def _reset_farm(username, coins=100000):
|
||||
refresh_snapshot()
|
||||
user = get_table("users").find_one(username=username)
|
||||
farm = store.get_farm(user["uid"])
|
||||
if farm:
|
||||
get_table("game_plots").delete(farm_uid=farm["uid"])
|
||||
get_table("game_farms").delete(uid=farm["uid"])
|
||||
farm = store.ensure_farm(user["uid"])
|
||||
get_table("game_farms").update({"uid": farm["uid"], "coins": coins}, ["uid"])
|
||||
refresh_snapshot()
|
||||
return user, farm
|
||||
|
||||
|
||||
def test_fertilize_requires_auth(app_server, seeded_db):
|
||||
r = requests.post(f"{BASE_URL}/game/fertilize", data={"slot": 0}, headers=JSON)
|
||||
assert r.status_code in (401, 303)
|
||||
|
||||
|
||||
def test_fertilize_empty_plot_returns_400(app_server, seeded_db):
|
||||
session, name = _signup()
|
||||
_reset_farm(name)
|
||||
r = session.post(f"{BASE_URL}/game/fertilize", data={"slot": 0}, headers=JSON)
|
||||
assert r.status_code == 400
|
||||
|
||||
|
||||
def test_claim_quest_requires_auth(app_server, seeded_db):
|
||||
r = requests.post(f"{BASE_URL}/game/quests/claim", data={"quest": "first_harvest"}, headers=JSON)
|
||||
assert r.status_code in (401, 303)
|
||||
|
||||
|
||||
def test_claim_unearned_quest_returns_400(app_server, seeded_db):
|
||||
session, name = _signup()
|
||||
_reset_farm(name)
|
||||
r = session.post(
|
||||
f"{BASE_URL}/game/quests/claim", data={"quest": "first_harvest"}, headers=JSON
|
||||
)
|
||||
assert r.status_code == 400
|
||||
|
||||
|
||||
def test_steal_requires_auth(app_server, seeded_db):
|
||||
r = requests.post(
|
||||
f"{BASE_URL}/game/farm/alice_test/steal", data={"slot": 0}, headers=JSON
|
||||
)
|
||||
assert r.status_code in (401, 303)
|
||||
|
||||
|
||||
def test_steal_unknown_farm_404(app_server, seeded_db):
|
||||
session, name = _signup()
|
||||
r = session.post(
|
||||
f"{BASE_URL}/game/farm/nobody_xyz_zzz/steal", data={"slot": 0}, headers=JSON
|
||||
)
|
||||
assert r.status_code == 404
|
||||
|
||||
|
||||
def test_steal_own_farm_returns_400(app_server, seeded_db):
|
||||
session, name = _signup()
|
||||
_reset_farm(name)
|
||||
r = session.post(
|
||||
f"{BASE_URL}/game/farm/{name}/steal", data={"slot": 0}, headers=JSON
|
||||
)
|
||||
assert r.status_code == 400
|
||||
|
||||
|
||||
def test_steal_empty_plot_returns_400(app_server, seeded_db):
|
||||
owner_session, owner = _signup()
|
||||
_reset_farm(owner)
|
||||
thief_session, thief = _signup()
|
||||
r = thief_session.post(
|
||||
f"{BASE_URL}/game/farm/{owner}/steal", data={"slot": 0}, headers=JSON
|
||||
)
|
||||
assert r.status_code == 400
|
||||
|
||||
|
||||
def test_steal_ready_unprotected_crop_transfers_coins(app_server, seeded_db):
|
||||
owner_session, owner = _signup()
|
||||
_reset_farm(owner, coins=100000)
|
||||
plant = owner_session.post(
|
||||
f"{BASE_URL}/game/plant", data={"slot": 0, "crop": "shell"}, headers=JSON
|
||||
)
|
||||
assert plant.status_code == 200, plant.text[:200]
|
||||
refresh_snapshot()
|
||||
owner_user = get_table("users").find_one(username=owner)
|
||||
farm = store.get_farm(owner_user["uid"])
|
||||
plot = get_table("game_plots").find_one(farm_uid=farm["uid"], slot_index=0)
|
||||
assert plot is not None
|
||||
past = (datetime.now(timezone.utc) - timedelta(days=1)).isoformat()
|
||||
get_table("game_plots").update(
|
||||
{"uid": plot["uid"], "planted_at": past, "ready_at": past}, ["uid"]
|
||||
)
|
||||
refresh_snapshot()
|
||||
|
||||
thief_session, thief = _signup()
|
||||
_reset_farm(thief, coins=0)
|
||||
r = thief_session.post(
|
||||
f"{BASE_URL}/game/farm/{owner}/steal", data={"slot": 0}, headers=JSON
|
||||
)
|
||||
assert r.status_code == 200, r.text[:200]
|
||||
refresh_snapshot()
|
||||
thief_user = get_table("users").find_one(username=thief)
|
||||
thief_farm = store.get_farm(thief_user["uid"])
|
||||
assert int(thief_farm.get("coins", 0)) > 0
|
||||
assert (get_table("game_plots").find_one(uid=plot["uid"]).get("crop_key") or "") == ""
|
||||
Reference in New Issue
Block a user