use slugs

This commit is contained in:
2022-11-26 14:51:18 -05:00
parent e9360fb3d5
commit 264f13e523
48 changed files with 536 additions and 280 deletions

View File

@ -3,7 +3,7 @@ defmodule Memex.ContextsTest do
import Memex.ContextsFixtures
alias Memex.{Contexts, Contexts.Context}
@moduletag :contexts_test
@invalid_attrs %{content: nil, tag: nil, title: nil, visibility: nil}
@invalid_attrs %{content: nil, tag: nil, slug: nil, visibility: nil}
describe "contexts" do
setup do
@ -11,9 +11,9 @@ defmodule Memex.ContextsTest do
end
test "list_contexts/1 returns all contexts for a user", %{user: user} do
context_a = context_fixture(%{title: "a", visibility: :public}, user)
context_b = context_fixture(%{title: "b", visibility: :unlisted}, user)
context_c = context_fixture(%{title: "c", visibility: :private}, user)
context_a = context_fixture(%{slug: "a", visibility: :public}, user)
context_b = context_fixture(%{slug: "b", visibility: :unlisted}, user)
context_c = context_fixture(%{slug: "c", visibility: :private}, user)
assert Contexts.list_contexts(user) == [context_a, context_b, context_c]
end
@ -50,18 +50,43 @@ defmodule Memex.ContextsTest do
end
end
test "get_context_by_slug/1 returns the context with given id", %{user: user} do
context = context_fixture(%{slug: "a", visibility: :public}, user)
assert Contexts.get_context_by_slug("a", user) == context
context = context_fixture(%{slug: "b", visibility: :unlisted}, user)
assert Contexts.get_context_by_slug("b", user) == context
context = context_fixture(%{slug: "c", visibility: :private}, user)
assert Contexts.get_context_by_slug("c", user) == context
end
test "get_context_by_slug/1 only returns unlisted or public contexts for other users", %{
user: user
} do
another_user = user_fixture()
context = context_fixture(%{slug: "a", visibility: :public}, another_user)
assert Contexts.get_context_by_slug("a", user) == context
context = context_fixture(%{slug: "b", visibility: :unlisted}, another_user)
assert Contexts.get_context_by_slug("b", user) == context
context_fixture(%{slug: "c", visibility: :private}, another_user)
assert Contexts.get_context_by_slug("c", user) |> is_nil()
end
test "create_context/1 with valid data creates a context", %{user: user} do
valid_attrs = %{
"content" => "some content",
"tags_string" => "tag1,tag2",
"title" => "some title",
"slug" => "some-slug",
"visibility" => :public
}
assert {:ok, %Context{} = context} = Contexts.create_context(valid_attrs, user)
assert context.content == "some content"
assert context.tags == ["tag1", "tag2"]
assert context.title == "some title"
assert context.slug == "some-slug"
assert context.visibility == :public
end
@ -75,14 +100,14 @@ defmodule Memex.ContextsTest do
update_attrs = %{
"content" => "some updated content",
"tags_string" => "tag1,tag2",
"title" => "some updated title",
"slug" => "some-updated-slug",
"visibility" => :private
}
assert {:ok, %Context{} = context} = Contexts.update_context(context, update_attrs, user)
assert context.content == "some updated content"
assert context.tags == ["tag1", "tag2"]
assert context.title == "some updated title"
assert context.slug == "some-updated-slug"
assert context.visibility == :private
end

View File

@ -3,7 +3,7 @@ defmodule Memex.NotesTest do
import Memex.NotesFixtures
alias Memex.{Notes, Notes.Note}
@moduletag :notes_test
@invalid_attrs %{content: nil, tag: nil, title: nil, visibility: nil}
@invalid_attrs %{content: nil, tag: nil, slug: nil, visibility: nil}
describe "notes" do
setup do
@ -11,9 +11,9 @@ defmodule Memex.NotesTest do
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)
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)
assert Notes.list_notes(user) == [note_a, note_b, note_c]
end
@ -50,18 +50,43 @@ defmodule Memex.NotesTest do
end
end
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
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",
"slug" => "some-slug",
"visibility" => :public
}
assert {:ok, %Note{} = note} = Notes.create_note(valid_attrs, user)
assert note.content == "some content"
assert note.tags == ["tag1", "tag2"]
assert note.title == "some title"
assert note.slug == "some-slug"
assert note.visibility == :public
end
@ -75,14 +100,14 @@ defmodule Memex.NotesTest do
update_attrs = %{
"content" => "some updated content",
"tags_string" => "tag1,tag2",
"title" => "some updated title",
"slug" => "some-updated-slug",
"visibility" => :private
}
assert {:ok, %Note{} = note} = Notes.update_note(note, update_attrs, user)
assert note.content == "some updated content"
assert note.tags == ["tag1", "tag2"]
assert note.title == "some updated title"
assert note.slug == "some-updated-slug"
assert note.visibility == :private
end

View File

@ -3,7 +3,7 @@ defmodule Memex.PipelinesTest do
import Memex.PipelinesFixtures
alias Memex.{Pipelines, Pipelines.Pipeline}
@moduletag :pipelines_test
@invalid_attrs %{description: nil, tag: nil, title: nil, visibility: nil}
@invalid_attrs %{description: nil, tag: nil, slug: nil, visibility: nil}
describe "pipelines" do
setup do
@ -11,9 +11,9 @@ defmodule Memex.PipelinesTest do
end
test "list_pipelines/1 returns all pipelines for a user", %{user: user} do
pipeline_a = pipeline_fixture(%{title: "a", visibility: :public}, user)
pipeline_b = pipeline_fixture(%{title: "b", visibility: :unlisted}, user)
pipeline_c = pipeline_fixture(%{title: "c", visibility: :private}, user)
pipeline_a = pipeline_fixture(%{slug: "a", visibility: :public}, user)
pipeline_b = pipeline_fixture(%{slug: "b", visibility: :unlisted}, user)
pipeline_c = pipeline_fixture(%{slug: "c", visibility: :private}, user)
assert Pipelines.list_pipelines(user) == [pipeline_a, pipeline_b, pipeline_c]
end
@ -52,18 +52,43 @@ defmodule Memex.PipelinesTest do
end
end
test "get_pipeline_by_slug/1 returns the pipeline with given id", %{user: user} do
pipeline = pipeline_fixture(%{slug: "a", visibility: :public}, user)
assert Pipelines.get_pipeline_by_slug("a", user) == pipeline
pipeline = pipeline_fixture(%{slug: "b", visibility: :unlisted}, user)
assert Pipelines.get_pipeline_by_slug("b", user) == pipeline
pipeline = pipeline_fixture(%{slug: "c", visibility: :private}, user)
assert Pipelines.get_pipeline_by_slug("c", user) == pipeline
end
test "get_pipeline_by_slug/1 only returns unlisted or public pipelines for other users", %{
user: user
} do
another_user = user_fixture()
pipeline = pipeline_fixture(%{slug: "a", visibility: :public}, another_user)
assert Pipelines.get_pipeline_by_slug("a", user) == pipeline
pipeline = pipeline_fixture(%{slug: "b", visibility: :unlisted}, another_user)
assert Pipelines.get_pipeline_by_slug("b", user) == pipeline
pipeline_fixture(%{slug: "c", visibility: :private}, another_user)
assert Pipelines.get_pipeline_by_slug("c", user) |> is_nil()
end
test "create_pipeline/1 with valid data creates a pipeline", %{user: user} do
valid_attrs = %{
"description" => "some description",
"tags_string" => "tag1,tag2",
"title" => "some title",
"slug" => "some-slug",
"visibility" => :public
}
assert {:ok, %Pipeline{} = pipeline} = Pipelines.create_pipeline(valid_attrs, user)
assert pipeline.description == "some description"
assert pipeline.tags == ["tag1", "tag2"]
assert pipeline.title == "some title"
assert pipeline.slug == "some-slug"
assert pipeline.visibility == :public
end
@ -77,7 +102,7 @@ defmodule Memex.PipelinesTest do
update_attrs = %{
"description" => "some updated description",
"tags_string" => "tag1,tag2",
"title" => "some updated title",
"slug" => "some-updated-slug",
"visibility" => :private
}
@ -86,7 +111,7 @@ defmodule Memex.PipelinesTest do
assert pipeline.description == "some updated description"
assert pipeline.tags == ["tag1", "tag2"]
assert pipeline.title == "some updated title"
assert pipeline.slug == "some-updated-slug"
assert pipeline.visibility == :private
end

View File

@ -7,19 +7,19 @@ defmodule MemexWeb.ContextLiveTest do
@create_attrs %{
"content" => "some content",
"tags_string" => "tag1",
"title" => "some title",
"slug" => "some-slug",
"visibility" => :public
}
@update_attrs %{
"content" => "some updated content",
"tags_string" => "tag1,tag2",
"title" => "some updated title",
"slug" => "some-updated-slug",
"visibility" => :private
}
@invalid_attrs %{
"content" => nil,
"tags_string" => "",
"title" => nil,
"slug" => nil,
"visibility" => nil
}
@ -55,7 +55,7 @@ defmodule MemexWeb.ContextLiveTest do
|> render_submit()
|> follow_redirect(conn, Routes.context_index_path(conn, :index))
assert html =~ "#{@create_attrs |> Map.get("title")} created"
assert html =~ "#{@create_attrs |> Map.get("slug")} created"
assert html =~ "some content"
end
@ -65,7 +65,7 @@ defmodule MemexWeb.ContextLiveTest do
assert index_live |> element("[data-qa=\"context-edit-#{context.id}\"]") |> render_click() =~
"edit"
assert_patch(index_live, Routes.context_index_path(conn, :edit, context))
assert_patch(index_live, Routes.context_index_path(conn, :edit, context.slug))
assert index_live
|> form("#context-form", context: @invalid_attrs)
@ -77,7 +77,7 @@ defmodule MemexWeb.ContextLiveTest do
|> render_submit()
|> follow_redirect(conn, Routes.context_index_path(conn, :index))
assert html =~ "#{@update_attrs |> Map.get("title")} saved"
assert html =~ "#{@update_attrs |> Map.get("slug")} saved"
assert html =~ "some updated content"
end
@ -93,18 +93,18 @@ defmodule MemexWeb.ContextLiveTest do
setup [:register_and_log_in_user, :create_context]
test "displays context", %{conn: conn, context: context} do
{:ok, _show_live, html} = live(conn, Routes.context_show_path(conn, :show, context))
{:ok, _show_live, html} = live(conn, Routes.context_show_path(conn, :show, context.slug))
assert html =~ "context"
assert html =~ context.content
end
test "updates context within modal", %{conn: conn, context: context} do
{:ok, show_live, _html} = live(conn, Routes.context_show_path(conn, :show, context))
{:ok, show_live, _html} = live(conn, Routes.context_show_path(conn, :show, context.slug))
assert show_live |> element("a", "edit") |> render_click() =~ "edit"
assert_patch(show_live, Routes.context_show_path(conn, :edit, context))
assert_patch(show_live, Routes.context_show_path(conn, :edit, context.slug))
assert show_live
|> form("#context-form", context: @invalid_attrs)
@ -112,16 +112,16 @@ defmodule MemexWeb.ContextLiveTest do
{:ok, _, html} =
show_live
|> form("#context-form", context: @update_attrs)
|> form("#context-form", context: Map.put(@update_attrs, "slug", context.slug))
|> render_submit()
|> follow_redirect(conn, Routes.context_show_path(conn, :show, context))
|> follow_redirect(conn, Routes.context_show_path(conn, :show, context.slug))
assert html =~ "#{@update_attrs |> Map.get("title")} saved"
assert html =~ "#{context.slug} saved"
assert html =~ "some updated content"
end
test "deletes context", %{conn: conn, context: context} do
{:ok, show_live, _html} = live(conn, Routes.context_show_path(conn, :show, context))
{:ok, show_live, _html} = live(conn, Routes.context_show_path(conn, :show, context.slug))
{:ok, index_live, _html} =
show_live

View File

@ -7,19 +7,19 @@ defmodule MemexWeb.NoteLiveTest do
@create_attrs %{
"content" => "some content",
"tags_string" => "tag1",
"title" => "some title",
"slug" => "some-slug",
"visibility" => :public
}
@update_attrs %{
"content" => "some updated content",
"tags_string" => "tag1,tag2",
"title" => "some updated title",
"slug" => "some-updated-slug",
"visibility" => :private
}
@invalid_attrs %{
"content" => nil,
"tags_string" => "",
"title" => nil,
"slug" => nil,
"visibility" => nil
}
@ -55,7 +55,7 @@ defmodule MemexWeb.NoteLiveTest do
|> render_submit()
|> follow_redirect(conn, Routes.note_index_path(conn, :index))
assert html =~ "#{@create_attrs |> Map.get("title")} created"
assert html =~ "#{@create_attrs |> Map.get("slug")} created"
assert html =~ "some content"
end
@ -65,7 +65,7 @@ defmodule MemexWeb.NoteLiveTest do
assert index_live |> element("[data-qa=\"note-edit-#{note.id}\"]") |> render_click() =~
"edit"
assert_patch(index_live, Routes.note_index_path(conn, :edit, note))
assert_patch(index_live, Routes.note_index_path(conn, :edit, note.slug))
assert index_live
|> form("#note-form", note: @invalid_attrs)
@ -77,7 +77,7 @@ defmodule MemexWeb.NoteLiveTest do
|> render_submit()
|> follow_redirect(conn, Routes.note_index_path(conn, :index))
assert html =~ "#{@update_attrs |> Map.get("title")} saved"
assert html =~ "#{@update_attrs |> Map.get("slug")} saved"
assert html =~ "some updated content"
end
@ -93,18 +93,18 @@ defmodule MemexWeb.NoteLiveTest do
setup [:register_and_log_in_user, :create_note]
test "displays note", %{conn: conn, note: note} do
{:ok, _show_live, html} = live(conn, Routes.note_show_path(conn, :show, note))
{:ok, _show_live, html} = live(conn, Routes.note_show_path(conn, :show, note.slug))
assert html =~ "note"
assert html =~ note.content
end
test "updates note within modal", %{conn: conn, note: note} do
{:ok, show_live, _html} = live(conn, Routes.note_show_path(conn, :show, note))
{:ok, show_live, _html} = live(conn, Routes.note_show_path(conn, :show, note.slug))
assert show_live |> element("a", "edit") |> render_click() =~ "edit"
assert_patch(show_live, Routes.note_show_path(conn, :edit, note))
assert_patch(show_live, Routes.note_show_path(conn, :edit, note.slug))
assert show_live
|> form("#note-form", note: @invalid_attrs)
@ -112,16 +112,16 @@ defmodule MemexWeb.NoteLiveTest do
{:ok, _, html} =
show_live
|> form("#note-form", note: @update_attrs)
|> form("#note-form", note: Map.put(@update_attrs, "slug", note.slug))
|> render_submit()
|> follow_redirect(conn, Routes.note_show_path(conn, :show, note))
|> follow_redirect(conn, Routes.note_show_path(conn, :show, note.slug))
assert html =~ "#{@update_attrs |> Map.get("title")} saved"
assert html =~ "#{note.slug} saved"
assert html =~ "some updated content"
end
test "deletes note", %{conn: conn, note: note} do
{:ok, show_live, _html} = live(conn, Routes.note_show_path(conn, :show, note))
{:ok, show_live, _html} = live(conn, Routes.note_show_path(conn, :show, note.slug))
{:ok, index_live, _html} =
show_live

View File

@ -7,19 +7,19 @@ defmodule MemexWeb.PipelineLiveTest do
@create_attrs %{
"description" => "some description",
"tags_string" => "tag1",
"title" => "some title",
"slug" => "some-slug",
"visibility" => :public
}
@update_attrs %{
"description" => "some updated description",
"tags_string" => "tag1,tag2",
"title" => "some updated title",
"slug" => "some-updated-slug",
"visibility" => :private
}
@invalid_attrs %{
"description" => nil,
"tags_string" => "",
"title" => nil,
"slug" => nil,
"visibility" => nil
}
@ -55,7 +55,7 @@ defmodule MemexWeb.PipelineLiveTest do
|> render_submit()
|> follow_redirect(conn, Routes.pipeline_index_path(conn, :index))
assert html =~ "#{@create_attrs |> Map.get("title")} created"
assert html =~ "#{@create_attrs |> Map.get("slug")} created"
assert html =~ "some description"
end
@ -65,7 +65,7 @@ defmodule MemexWeb.PipelineLiveTest do
assert index_live |> element("[data-qa=\"pipeline-edit-#{pipeline.id}\"]") |> render_click() =~
"edit"
assert_patch(index_live, Routes.pipeline_index_path(conn, :edit, pipeline))
assert_patch(index_live, Routes.pipeline_index_path(conn, :edit, pipeline.slug))
assert index_live
|> form("#pipeline-form", pipeline: @invalid_attrs)
@ -77,7 +77,7 @@ defmodule MemexWeb.PipelineLiveTest do
|> render_submit()
|> follow_redirect(conn, Routes.pipeline_index_path(conn, :index))
assert html =~ "#{@update_attrs |> Map.get("title")} saved"
assert html =~ "#{@update_attrs |> Map.get("slug")} saved"
assert html =~ "some updated description"
end
@ -96,18 +96,18 @@ defmodule MemexWeb.PipelineLiveTest do
setup [:register_and_log_in_user, :create_pipeline]
test "displays pipeline", %{conn: conn, pipeline: pipeline} do
{:ok, _show_live, html} = live(conn, Routes.pipeline_show_path(conn, :show, pipeline))
{:ok, _show_live, html} = live(conn, Routes.pipeline_show_path(conn, :show, pipeline.slug))
assert html =~ "pipeline"
assert html =~ pipeline.description
end
test "updates pipeline within modal", %{conn: conn, pipeline: pipeline} do
{:ok, show_live, _html} = live(conn, Routes.pipeline_show_path(conn, :show, pipeline))
{:ok, show_live, _html} = live(conn, Routes.pipeline_show_path(conn, :show, pipeline.slug))
assert show_live |> element("a", "edit") |> render_click() =~ "edit"
assert_patch(show_live, Routes.pipeline_show_path(conn, :edit, pipeline))
assert_patch(show_live, Routes.pipeline_show_path(conn, :edit, pipeline.slug))
assert show_live
|> form("#pipeline-form", pipeline: @invalid_attrs)
@ -115,16 +115,16 @@ defmodule MemexWeb.PipelineLiveTest do
{:ok, _, html} =
show_live
|> form("#pipeline-form", pipeline: @update_attrs)
|> form("#pipeline-form", pipeline: Map.put(@update_attrs, "slug", pipeline.slug))
|> render_submit()
|> follow_redirect(conn, Routes.pipeline_show_path(conn, :show, pipeline))
|> follow_redirect(conn, Routes.pipeline_show_path(conn, :show, pipeline.slug))
assert html =~ "#{@update_attrs |> Map.get("title")} saved"
assert html =~ "#{pipeline.slug} saved"
assert html =~ "some updated description"
end
test "deletes pipeline", %{conn: conn, pipeline: pipeline} do
{:ok, show_live, _html} = live(conn, Routes.pipeline_show_path(conn, :show, pipeline))
{:ok, show_live, _html} = live(conn, Routes.pipeline_show_path(conn, :show, pipeline.slug))
{:ok, index_live, _html} =
show_live

View File

@ -13,11 +13,11 @@ defmodule MemexWeb.ErrorViewTest do
test "renders 404.html" do
assert render_to_string(MemexWeb.ErrorView, "404.html", []) =~
dgettext("errors", "Not found")
dgettext("errors", "not found")
end
test "renders 500.html" do
assert render_to_string(MemexWeb.ErrorView, "500.html", []) =~
dgettext("errors", "Internal Server Error")
dgettext("errors", "internal server error")
end
end

View File

@ -2,7 +2,6 @@ defmodule Memex.Fixtures do
@moduledoc """
This module defines test helpers for creating entities
"""
alias Memex.{Accounts, Accounts.User, Email, Repo}
def unique_user_email, do: "user#{System.unique_integer()}@example.com"
@ -57,5 +56,14 @@ defmodule Memex.Fixtures do
})
end
def random_slug(length \\ 20) do
symbols = '0123456789abcdef-'
symbol_count = Enum.count(symbols)
for _ <- Range.new(1, length),
into: "",
do: <<Enum.at(symbols, :rand.uniform(symbol_count - 1))>>
end
defp unwrap_ok_tuple({:ok, value}), do: value
end

View File

@ -3,6 +3,7 @@ defmodule Memex.ContextsFixtures do
This module defines test helpers for creating
entities via the `Memex.Contexts` context.
"""
import Memex.Fixtures
alias Memex.{Accounts.User, Contexts, Contexts.Context}
@doc """
@ -16,7 +17,7 @@ defmodule Memex.ContextsFixtures do
|> Enum.into(%{
content: "some content",
tag: [],
title: "some title",
slug: random_slug(),
visibility: :private
})
|> Contexts.create_context(user)

View File

@ -3,6 +3,7 @@ defmodule Memex.NotesFixtures do
This module defines test helpers for creating
entities via the `Memex.Notes` context.
"""
import Memex.Fixtures
alias Memex.{Accounts.User, Notes, Notes.Note}
@doc """
@ -16,7 +17,7 @@ defmodule Memex.NotesFixtures do
|> Enum.into(%{
content: "some content",
tag: [],
title: "some title",
slug: random_slug(),
visibility: :private
})
|> Notes.create_note(user)

View File

@ -3,6 +3,7 @@ defmodule Memex.PipelinesFixtures do
This module defines test helpers for creating
entities via the `Memex.Pipelines` context.
"""
import Memex.Fixtures
alias Memex.{Accounts.User, Pipelines, Pipelines.Pipeline}
@doc """
@ -16,7 +17,7 @@ defmodule Memex.PipelinesFixtures do
|> Enum.into(%{
description: "some description",
tag: [],
title: "some title",
slug: random_slug(),
visibility: :private
})
|> Pipelines.create_pipeline(user)