# retoor JSON = {"Accept": "application/json"} def _create(admin, **overrides): payload = {"title": "A real note", "body": "Text with **emphasis** and `code`.", "topic": "roll", "status": "draft"} payload.update(overrides) response = admin.post("/admin/posts", json=payload, headers=JSON) assert response.status_code == 200, response.text return response.json()["data"] def test_create_draft_is_hidden_then_published(admin, anon): data = _create(admin, title="Draft that goes live later") assert anon.get(f"/posts/{data['slug']}").status_code == 404 assert admin.post(f"/admin/posts/{data['uid']}/publish", headers=JSON).status_code == 200 page = anon.get(f"/posts/{data['slug']}") assert page.status_code == 200 assert "emphasis" in page.text def test_edit_post(admin, anon): data = _create(admin, title="Note to edit", status="published") response = admin.post(f"/admin/posts/{data['uid']}", json={"title": "Edited note", "body": "New text right here.", "topic": "standard", "status": "published"}, headers=JSON) assert response.status_code == 200, response.text posts = anon.get("/standard", headers=JSON).json()["posts"] assert any(post["title"] == "Edited note" for post in posts) def test_delete_goes_to_trash(admin, anon): data = _create(admin, title="Gone with it note", status="published") assert admin.post(f"/admin/posts/{data['uid']}/delete", headers=JSON).status_code == 200 assert anon.get(f"/posts/{data['slug']}").status_code == 404 trash = admin.get("/admin/trash", headers=JSON).json()["items"] assert any(item["uid"] == data["uid"] for item in trash) def test_validation_error(admin): response = admin.post("/admin/posts", json={"title": "x"}, headers=JSON) assert response.status_code == 422 def test_html_form_post(admin): response = admin.post("/admin/posts", data={"title": "Via the form", "body": "Form text right here.", "topic": "roll", "status": "draft"}) assert response.status_code in (302, 303) def test_member_cannot_write(member): response = member.post("/admin/posts", json={"title": "Not allowed", "body": "Text right here.", "topic": "roll"}, headers=JSON) assert response.status_code == 403