2022-07-25 20:08:40 -04:00
|
|
|
defmodule Memex.NotesTest do
|
|
|
|
use Memex.DataCase
|
2022-11-17 22:38:52 -05:00
|
|
|
import Memex.NotesFixtures
|
|
|
|
alias Memex.{Notes, Notes.Note}
|
|
|
|
@moduletag :notes_test
|
|
|
|
@invalid_attrs %{content: nil, tag: nil, title: nil, visibility: nil}
|
2022-07-25 20:08:40 -04:00
|
|
|
|
|
|
|
describe "notes" do
|
2022-11-17 22:38:52 -05:00
|
|
|
setup do
|
|
|
|
[user: user_fixture()]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "list_notes/1 returns all notes for a user", %{user: user} do
|
|
|
|
note_a = note_fixture(%{title: "a", visibility: :public}, user)
|
|
|
|
note_b = note_fixture(%{title: "b", visibility: :unlisted}, user)
|
|
|
|
note_c = note_fixture(%{title: "c", visibility: :private}, user)
|
|
|
|
assert Notes.list_notes(user) == [note_a, note_b, note_c]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "list_public_notes/0 returns public notes", %{user: user} do
|
|
|
|
public_note = note_fixture(%{visibility: :public}, user)
|
|
|
|
note_fixture(%{visibility: :unlisted}, user)
|
|
|
|
note_fixture(%{visibility: :private}, user)
|
|
|
|
assert Notes.list_public_notes() == [public_note]
|
|
|
|
end
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
test "get_note!/1 returns the note with given id", %{user: user} do
|
|
|
|
note = note_fixture(%{visibility: :public}, user)
|
|
|
|
assert Notes.get_note!(note.id, user) == note
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
note = note_fixture(%{visibility: :unlisted}, user)
|
|
|
|
assert Notes.get_note!(note.id, user) == note
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
note = note_fixture(%{visibility: :private}, user)
|
|
|
|
assert Notes.get_note!(note.id, user) == note
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
test "get_note!/1 only returns unlisted or public notes for other users", %{user: user} do
|
|
|
|
another_user = user_fixture()
|
|
|
|
note = note_fixture(%{visibility: :public}, another_user)
|
|
|
|
assert Notes.get_note!(note.id, user) == note
|
|
|
|
|
|
|
|
note = note_fixture(%{visibility: :unlisted}, another_user)
|
|
|
|
assert Notes.get_note!(note.id, user) == note
|
|
|
|
|
|
|
|
note = note_fixture(%{visibility: :private}, another_user)
|
|
|
|
|
|
|
|
assert_raise Ecto.NoResultsError, fn ->
|
|
|
|
Notes.get_note!(note.id, user)
|
|
|
|
end
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
test "create_note/1 with valid data creates a note", %{user: user} do
|
|
|
|
valid_attrs = %{
|
|
|
|
"content" => "some content",
|
|
|
|
"tags_string" => "tag1,tag2",
|
|
|
|
"title" => "some title",
|
|
|
|
"visibility" => :public
|
|
|
|
}
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
assert {:ok, %Note{} = note} = Notes.create_note(valid_attrs, user)
|
2022-07-25 20:08:40 -04:00
|
|
|
assert note.content == "some content"
|
2022-11-17 22:38:52 -05:00
|
|
|
assert note.tags == ["tag1", "tag2"]
|
2022-07-25 20:08:40 -04:00
|
|
|
assert note.title == "some title"
|
|
|
|
assert note.visibility == :public
|
|
|
|
end
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
test "create_note/1 with invalid data returns error changeset", %{user: user} do
|
|
|
|
assert {:error, %Ecto.Changeset{}} = Notes.create_note(@invalid_attrs, user)
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
test "update_note/2 with valid data updates the note", %{user: user} do
|
|
|
|
note = note_fixture(user)
|
2022-07-25 22:05:54 -04:00
|
|
|
|
|
|
|
update_attrs = %{
|
2022-11-17 22:38:52 -05:00
|
|
|
"content" => "some updated content",
|
|
|
|
"tags_string" => "tag1,tag2",
|
|
|
|
"title" => "some updated title",
|
|
|
|
"visibility" => :private
|
2022-07-25 22:05:54 -04:00
|
|
|
}
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
assert {:ok, %Note{} = note} = Notes.update_note(note, update_attrs, user)
|
2022-07-25 20:08:40 -04:00
|
|
|
assert note.content == "some updated content"
|
2022-11-17 22:38:52 -05:00
|
|
|
assert note.tags == ["tag1", "tag2"]
|
2022-07-25 20:08:40 -04:00
|
|
|
assert note.title == "some updated title"
|
|
|
|
assert note.visibility == :private
|
|
|
|
end
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
test "update_note/2 with invalid data returns error changeset", %{user: user} do
|
|
|
|
note = note_fixture(user)
|
|
|
|
assert {:error, %Ecto.Changeset{}} = Notes.update_note(note, @invalid_attrs, user)
|
|
|
|
assert note == Notes.get_note!(note.id, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delete_note/1 deletes the note", %{user: user} do
|
|
|
|
note = note_fixture(user)
|
|
|
|
assert {:ok, %Note{}} = Notes.delete_note(note, user)
|
|
|
|
assert_raise Ecto.NoResultsError, fn -> Notes.get_note!(note.id, user) end
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
test "delete_note/1 deletes the note for an admin user", %{user: user} do
|
|
|
|
admin_user = admin_fixture()
|
|
|
|
note = note_fixture(user)
|
|
|
|
assert {:ok, %Note{}} = Notes.delete_note(note, admin_user)
|
|
|
|
assert_raise Ecto.NoResultsError, fn -> Notes.get_note!(note.id, user) end
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
test "change_note/1 returns a note changeset", %{user: user} do
|
|
|
|
note = note_fixture(user)
|
|
|
|
assert %Ecto.Changeset{} = Notes.change_note(note, user)
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|