memEx/test/memex/notes_test.exs

216 lines
7.6 KiB
Elixir
Raw Permalink Normal View History

2022-07-25 20:08:40 -04:00
defmodule Memex.NotesTest do
2023-04-14 23:50:16 -04:00
use Memex.DataCase, async: true
2023-03-22 22:08:37 -04:00
import Memex.Fixtures
2022-11-17 22:38:52 -05:00
alias Memex.{Notes, Notes.Note}
@moduletag :notes_test
2022-11-26 14:51:18 -05:00
@invalid_attrs %{content: nil, tag: nil, slug: 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
2022-11-26 14:51:18 -05:00
note_a = note_fixture(%{slug: "a", visibility: :public}, user)
note_b = note_fixture(%{slug: "b", visibility: :unlisted}, user)
note_c = note_fixture(%{slug: "c", visibility: :private}, user)
2022-11-27 21:10:54 -05:00
_shouldnt_return = note_fixture(%{visibility: :private}, user_fixture())
2022-11-17 22:38:52 -05:00
assert Notes.list_notes(user) == [note_a, note_b, note_c]
end
2022-11-27 21:10:54 -05:00
test "list_notes/2 returns relevant notes for a user", %{user: user} do
note_a = note_fixture(%{slug: "dogs", content: "has some treats in it"}, user)
note_b = note_fixture(%{slug: "cats", tags: ["home"]}, user)
note_c =
%{slug: "chickens", content: "bananas stuff", tags: ["life", "decisions"]}
|> note_fixture(user)
_shouldnt_return =
%{slug: "dog", content: "banana treat stuff", visibility: :private}
|> note_fixture(user_fixture())
# slug
assert Notes.list_notes("dog", user) == [note_a]
assert Notes.list_notes("dogs", user) == [note_a]
assert Notes.list_notes("cat", user) == [note_b]
assert Notes.list_notes("chicken", user) == [note_c]
# content
assert Notes.list_notes("treat", user) == [note_a]
assert Notes.list_notes("banana", user) == [note_c]
assert Notes.list_notes("stuff", user) == [note_c]
# tag
assert Notes.list_notes("home", user) == [note_b]
assert Notes.list_notes("life", user) == [note_c]
assert Notes.list_notes("decision", user) == [note_c]
assert Notes.list_notes("decisions", user) == [note_c]
end
2022-11-17 22:38:52 -05:00
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-27 21:10:54 -05:00
test "list_public_notes/1 returns relevant notes for a user", %{user: user} do
note_a =
%{slug: "dogs", content: "has some treats in it", visibility: :public}
|> note_fixture(user)
note_b =
%{slug: "cats", tags: ["home"], visibility: :public}
|> note_fixture(user)
note_c =
%{
slug: "chickens",
content: "bananas stuff",
tags: ["life", "decisions"],
visibility: :public
}
|> note_fixture(user)
_shouldnt_return =
%{
slug: "dog",
content: "treats bananas stuff",
tags: ["home", "life", "decisions"],
visibility: :private
}
|> note_fixture(user)
# slug
assert Notes.list_public_notes("dog") == [note_a]
assert Notes.list_public_notes("dogs") == [note_a]
assert Notes.list_public_notes("cat") == [note_b]
assert Notes.list_public_notes("chicken") == [note_c]
# content
assert Notes.list_public_notes("treat") == [note_a]
assert Notes.list_public_notes("banana") == [note_c]
assert Notes.list_public_notes("stuff") == [note_c]
# tag
assert Notes.list_public_notes("home") == [note_b]
assert Notes.list_public_notes("life") == [note_c]
assert Notes.list_public_notes("decision") == [note_c]
assert Notes.list_public_notes("decisions") == [note_c]
end
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-26 14:51:18 -05:00
test "get_note_by_slug/1 returns the note with given id", %{user: user} do
note = note_fixture(%{slug: "a", visibility: :public}, user)
assert Notes.get_note_by_slug("a", user) == note
note = note_fixture(%{slug: "b", visibility: :unlisted}, user)
assert Notes.get_note_by_slug("b", user) == note
note = note_fixture(%{slug: "c", visibility: :private}, user)
assert Notes.get_note_by_slug("c", user) == note
end
test "get_note_by_slug/1 only returns unlisted or public notes for other users", %{
user: user
} do
another_user = user_fixture()
note = note_fixture(%{slug: "a", visibility: :public}, another_user)
assert Notes.get_note_by_slug("a", user) == note
note = note_fixture(%{slug: "b", visibility: :unlisted}, another_user)
assert Notes.get_note_by_slug("b", user) == note
note_fixture(%{slug: "c", visibility: :private}, another_user)
assert Notes.get_note_by_slug("c", user) |> is_nil()
end
2022-11-17 22:38:52 -05:00
test "create_note/1 with valid data creates a note", %{user: user} do
valid_attrs = %{
2023-03-22 22:08:37 -04:00
content: "some content",
tags_string: "tag1,tag2",
slug: "some-slug",
visibility: :public
2022-11-17 22:38:52 -05:00
}
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-11-26 14:51:18 -05:00
assert note.slug == "some-slug"
2022-07-25 20:08:40 -04:00
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 = %{
2023-03-22 22:08:37 -04:00
content: "some updated content",
tags_string: "tag1,tag2",
slug: "some-updated-slug",
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-11-26 14:51:18 -05:00
assert note.slug == "some-updated-slug"
2022-07-25 20:08:40 -04:00
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