131 lines
3.6 KiB
Elixir
131 lines
3.6 KiB
Elixir
defmodule NullaWeb.NoteControllerTest do
|
|
use NullaWeb.ConnCase
|
|
|
|
import Nulla.ActorsFixtures
|
|
import Nulla.NotesFixtures
|
|
|
|
alias Nulla.Notes.Note
|
|
|
|
@create_attrs %{
|
|
sensitive: true,
|
|
cc: ["option1", "option2"],
|
|
to: ["option1", "option2"],
|
|
url: "some url",
|
|
language: "some language",
|
|
inReplyTo: "some inReplyTo",
|
|
published: ~U[2025-07-01 09:17:00Z],
|
|
visibility: "some visibility",
|
|
content: "some content"
|
|
}
|
|
@update_attrs %{
|
|
sensitive: false,
|
|
cc: ["option1"],
|
|
to: ["option1"],
|
|
url: "some updated url",
|
|
language: "some updated language",
|
|
inReplyTo: "some updated inReplyTo",
|
|
published: ~U[2025-07-02 09:17:00Z],
|
|
visibility: "some updated visibility",
|
|
content: "some updated content"
|
|
}
|
|
@invalid_attrs %{
|
|
sensitive: nil,
|
|
cc: nil,
|
|
to: nil,
|
|
url: nil,
|
|
language: nil,
|
|
inReplyTo: nil,
|
|
published: nil,
|
|
visibility: nil,
|
|
content: nil
|
|
}
|
|
|
|
setup %{conn: conn} do
|
|
{:ok, conn: put_req_header(conn, "accept", "application/json")}
|
|
end
|
|
|
|
describe "index" do
|
|
test "lists all notes", %{conn: conn} do
|
|
conn = get(conn, ~p"/api/notes")
|
|
assert json_response(conn, 200)["data"] == []
|
|
end
|
|
end
|
|
|
|
describe "create note" do
|
|
test "renders note when data is valid", %{conn: conn} do
|
|
actor = actor_fixture()
|
|
|
|
create_attrs = Map.merge(@create_attrs, %{actor_id: actor.id})
|
|
|
|
conn = post(conn, ~p"/api/notes", note: create_attrs)
|
|
assert %{"id" => id} = json_response(conn, 201)["data"]
|
|
|
|
conn = get(conn, ~p"/api/notes/#{id}")
|
|
|
|
assert %{
|
|
"id" => ^id,
|
|
"cc" => ["option1", "option2"],
|
|
"content" => "some content",
|
|
"inReplyTo" => "some inReplyTo",
|
|
"language" => "some language",
|
|
"published" => "2025-07-01T09:17:00Z",
|
|
"sensitive" => true,
|
|
"to" => ["option1", "option2"],
|
|
"url" => "some url",
|
|
"visibility" => "some visibility"
|
|
} = json_response(conn, 200)["data"]
|
|
end
|
|
|
|
test "renders errors when data is invalid", %{conn: conn} do
|
|
conn = post(conn, ~p"/api/notes", note: @invalid_attrs)
|
|
assert json_response(conn, 422)["errors"] != %{}
|
|
end
|
|
end
|
|
|
|
describe "update note" do
|
|
setup [:create_note]
|
|
|
|
test "renders note when data is valid", %{conn: conn, note: %Note{id: id} = note} do
|
|
conn = put(conn, ~p"/api/notes/#{note}", note: @update_attrs)
|
|
assert %{"id" => ^id} = json_response(conn, 200)["data"]
|
|
|
|
conn = get(conn, ~p"/api/notes/#{id}")
|
|
|
|
assert %{
|
|
"id" => ^id,
|
|
"cc" => ["option1"],
|
|
"content" => "some updated content",
|
|
"inReplyTo" => "some updated inReplyTo",
|
|
"language" => "some updated language",
|
|
"published" => "2025-07-02T09:17:00Z",
|
|
"sensitive" => false,
|
|
"to" => ["option1"],
|
|
"url" => "some updated url",
|
|
"visibility" => "some updated visibility"
|
|
} = json_response(conn, 200)["data"]
|
|
end
|
|
|
|
test "renders errors when data is invalid", %{conn: conn, note: note} do
|
|
conn = put(conn, ~p"/api/notes/#{note}", note: @invalid_attrs)
|
|
assert json_response(conn, 422)["errors"] != %{}
|
|
end
|
|
end
|
|
|
|
describe "delete note" do
|
|
setup [:create_note]
|
|
|
|
test "deletes chosen note", %{conn: conn, note: note} do
|
|
conn = delete(conn, ~p"/api/notes/#{note}")
|
|
assert response(conn, 204)
|
|
|
|
assert_error_sent 404, fn ->
|
|
get(conn, ~p"/api/notes/#{note}")
|
|
end
|
|
end
|
|
end
|
|
|
|
defp create_note(_) do
|
|
note = note_fixture()
|
|
%{note: note}
|
|
end
|
|
end
|