Implement GET /api/v1/announcements/:id

This commit is contained in:
Tusooa Zhu 2022-03-08 16:19:35 -05:00
parent 5169ad8f14
commit aa1fff279e
No known key found for this signature in database
GPG Key ID: 7B467EDE43A08224
2 changed files with 67 additions and 2 deletions

View File

@ -55,7 +55,20 @@ def mark_read(_conn, _params) do
end
@doc "POST /api/v1/announcements/:id"
def show(_conn, _params) do
{:error, :not_found}
def show(%{assigns: %{user: user}} = conn, %{id: id} = _params) do
render_announcement_by_id(conn, id, user)
end
def show(conn, %{id: id} = _params) do
render_announcement_by_id(conn, id)
end
def render_announcement_by_id(conn, id, user \\ nil) do
with announcement when not is_nil(announcement) <- Announcement.get_by_id(id) do
render(conn, "show.json", announcement: announcement, user: user)
else
_ ->
{:error, :not_found}
end
end
end

View File

@ -51,4 +51,56 @@ test "when authenticated and announcement is read by user" do
assert [%{"id" => ^id, "read" => true}] = response
end
end
describe "GET /api/v1/announcements/:id" do
test "it shows one announcement" do
%{id: id} = insert(:announcement)
response =
build_conn()
|> get("/api/v1/announcements/#{id}")
|> json_response_and_validate_schema(:ok)
assert %{"id" => ^id} = response
refute Map.has_key?(response, "read")
end
test "it gives 404 for non-existent announcements" do
%{id: id} = insert(:announcement)
_response =
build_conn()
|> get("/api/v1/announcements/#{id}xxx")
|> json_response_and_validate_schema(:not_found)
end
test "when authenticated, also expose read property" do
%{id: id} = insert(:announcement)
%{conn: conn} = oauth_access(["read:accounts"])
response =
conn
|> get("/api/v1/announcements/#{id}")
|> json_response_and_validate_schema(:ok)
assert %{"id" => ^id, "read" => false} = response
end
test "when authenticated and announcement is read by user" do
%{id: id} = announcement = insert(:announcement)
user = insert(:user)
AnnouncementReadRelationship.mark_read(user, announcement)
%{conn: conn} = oauth_access(["read:accounts"], user: user)
response =
conn
|> get("/api/v1/announcements/#{id}")
|> json_response_and_validate_schema(:ok)
assert %{"id" => ^id, "read" => true} = response
end
end
end