Flows and dashboards are created by POST under an issued or superuser-pinned id; PUT only updates; deleted flows leave a tombstone with their key; the controller stores by id and runs by key, hooks live under /hooks/<flow id>; a key with state or queued work behind it is refused until cleared
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
"""Dashboards over HTTP: saving one, which is also how the first one is made."""
|
||||
"""Dashboards over HTTP: creating one under an issued id, and saving it."""
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from fluksio.core.config import settings
|
||||
from fluksio.flow.store import ISSUED_ID
|
||||
from tests.utils.flows import put_dashboard
|
||||
|
||||
PREFIX = f"{settings.API_V1_STR}/dashboards"
|
||||
|
||||
@@ -26,37 +28,78 @@ def a_dashboard(name: str) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def test_a_save_creates_a_dashboard_that_does_not_exist_yet(
|
||||
def test_a_dashboard_is_created_with_an_id_and_saved_by_it(
|
||||
client: TestClient, superuser_token_headers: dict[str, str]
|
||||
) -> None:
|
||||
"""A first draft, not a 404: creating one *is* saving it at version 0."""
|
||||
body = a_dashboard("put_creates")
|
||||
|
||||
response = client.put(
|
||||
f"{PREFIX}/put_creates", headers=superuser_token_headers, json=body
|
||||
"""A first draft under an id the server issues, which every path then names."""
|
||||
created = client.post(
|
||||
f"{PREFIX}/", headers=superuser_token_headers, json={"name": "made_here"}
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.text
|
||||
saved = response.json()
|
||||
assert saved["version"] == 1 and saved["has_draft"] is True
|
||||
assert created.status_code == 201, created.text
|
||||
made = created.json()
|
||||
assert ISSUED_ID.fullmatch(made["id"]) and made["name"] == "made_here"
|
||||
assert made["version"] == 1 and made["has_draft"] is True
|
||||
# A draft alone: nothing was published, so no panel can be shown it.
|
||||
assert (
|
||||
client.get(f"{PREFIX}/put_creates", headers=superuser_token_headers).status_code
|
||||
client.get(
|
||||
f"{PREFIX}/{made['id']}", headers=superuser_token_headers
|
||||
).status_code
|
||||
== 404
|
||||
)
|
||||
body = {**a_dashboard("made_here"), "version": made["version"]}
|
||||
saved = client.put(
|
||||
f"{PREFIX}/{made['id']}", headers=superuser_token_headers, json=body
|
||||
)
|
||||
assert saved.status_code == 200, saved.text
|
||||
draft = client.get(
|
||||
f"{PREFIX}/put_creates?draft=true", headers=superuser_token_headers
|
||||
f"{PREFIX}/{made['id']}?draft=true", headers=superuser_token_headers
|
||||
)
|
||||
assert draft.json()["widgets"] == body["widgets"]
|
||||
(listed,) = client.get(
|
||||
f"{PREFIX}/?name=made_here", headers=superuser_token_headers
|
||||
).json()["data"]
|
||||
assert listed["id"] == made["id"]
|
||||
|
||||
copy = client.post(
|
||||
f"{PREFIX}/{made['id']}/duplicate", headers=superuser_token_headers
|
||||
).json()
|
||||
assert copy["name"] == "made_here_copy" and copy["id"] != made["id"]
|
||||
for dashboard_id in (made["id"], copy["id"]):
|
||||
client.delete(f"{PREFIX}/{dashboard_id}", headers=superuser_token_headers)
|
||||
|
||||
|
||||
def test_a_save_neither_creates_nor_renames(
|
||||
client: TestClient, superuser_token_headers: dict[str, str]
|
||||
) -> None:
|
||||
missing = client.put(
|
||||
f"{PREFIX}/nowhere",
|
||||
headers=superuser_token_headers,
|
||||
json={**a_dashboard("nowhere"), "version": 0},
|
||||
)
|
||||
assert missing.status_code == 404
|
||||
saved = put_dashboard(
|
||||
client, superuser_token_headers, a_dashboard("put_fixed")
|
||||
).json()
|
||||
renamed = client.put(
|
||||
f"{PREFIX}/put_fixed",
|
||||
headers=superuser_token_headers,
|
||||
json={**saved, "name": "renamed"},
|
||||
)
|
||||
assert renamed.status_code == 409
|
||||
elsewhere = client.put(
|
||||
f"{PREFIX}/put_fixed",
|
||||
headers=superuser_token_headers,
|
||||
json={**saved, "id": "elsewhere"},
|
||||
)
|
||||
assert elsewhere.status_code == 422
|
||||
client.delete(f"{PREFIX}/put_fixed", headers=superuser_token_headers)
|
||||
|
||||
|
||||
def test_a_save_of_an_existing_dashboard_keeps_what_it_did_not_change(
|
||||
client: TestClient, superuser_token_headers: dict[str, str]
|
||||
) -> None:
|
||||
seeded = a_dashboard("put_updates")
|
||||
first = client.put(
|
||||
f"{PREFIX}/put_updates", headers=superuser_token_headers, json=seeded
|
||||
).json()
|
||||
first = put_dashboard(client, superuser_token_headers, seeded).json()
|
||||
|
||||
renamed = {**first, "widgets": [{**first["widgets"][0], "title": "Outside"}]}
|
||||
second = client.put(
|
||||
@@ -78,37 +121,38 @@ def test_a_save_based_on_a_version_someone_moved_past_is_refused(
|
||||
client: TestClient, superuser_token_headers: dict[str, str]
|
||||
) -> None:
|
||||
body = a_dashboard("put_conflicts")
|
||||
client.put(f"{PREFIX}/put_conflicts", headers=superuser_token_headers, json=body)
|
||||
put_dashboard(client, superuser_token_headers, body)
|
||||
|
||||
stale = client.put(
|
||||
f"{PREFIX}/put_conflicts", headers=superuser_token_headers, json=body
|
||||
f"{PREFIX}/put_conflicts",
|
||||
headers=superuser_token_headers,
|
||||
json={**body, "version": 1},
|
||||
)
|
||||
|
||||
assert stale.status_code == 409
|
||||
assert stale.json()["detail"]["current_version"] == 1
|
||||
assert stale.json()["detail"]["current_version"] == 2
|
||||
|
||||
|
||||
def test_a_dashboard_is_known_by_an_id_no_save_changes(
|
||||
client: TestClient, superuser_token_headers: dict[str, str]
|
||||
def test_only_a_superuser_pins_a_dashboard_id(
|
||||
client: TestClient,
|
||||
superuser_token_headers: dict[str, str],
|
||||
normal_user_token_headers: dict[str, str],
|
||||
) -> None:
|
||||
saved = client.put(
|
||||
f"{PREFIX}/put_identified",
|
||||
pinned = {"id": "pinned_hall", "name": "pinned_hall"}
|
||||
refused = client.post(f"{PREFIX}/", headers=normal_user_token_headers, json=pinned)
|
||||
assert refused.status_code == 403
|
||||
made = client.post(f"{PREFIX}/", headers=superuser_token_headers, json=pinned)
|
||||
assert made.status_code == 201 and made.json()["id"] == "pinned_hall"
|
||||
again = client.post(
|
||||
f"{PREFIX}/",
|
||||
headers=superuser_token_headers,
|
||||
json=a_dashboard("put_identified"),
|
||||
).json()
|
||||
assert saved["id"] == "put_identified"
|
||||
|
||||
elsewhere = client.put(
|
||||
f"{PREFIX}/put_identified",
|
||||
headers=superuser_token_headers,
|
||||
json={**saved, "id": "elsewhere"},
|
||||
json={"id": "pinned_hall", "name": "other_hall"},
|
||||
)
|
||||
assert elsewhere.status_code == 422
|
||||
renamed = client.post(
|
||||
f"{PREFIX}/put_identified/rename",
|
||||
headers=superuser_token_headers,
|
||||
json={"name": "renamed"},
|
||||
assert again.status_code == 409
|
||||
# Anyone may make one under an id of the server's.
|
||||
issued = client.post(
|
||||
f"{PREFIX}/", headers=normal_user_token_headers, json={"name": "their_hall"}
|
||||
)
|
||||
assert renamed.status_code in (404, 405)
|
||||
|
||||
client.delete(f"{PREFIX}/put_identified", headers=superuser_token_headers)
|
||||
assert issued.status_code == 201
|
||||
for dashboard_id in ("pinned_hall", issued.json()["id"]):
|
||||
client.delete(f"{PREFIX}/{dashboard_id}", headers=superuser_token_headers)
|
||||
|
||||
Reference in New Issue
Block a user