This commit is contained in:
Mirai Kumiko 2025-07-04 10:25:40 +02:00
parent b35e18cd20
commit 82f55f7afe
Signed by: miraikumiko
GPG key ID: 3F178B1B5E0CB278
80 changed files with 6687 additions and 5 deletions

View file

@ -0,0 +1,107 @@
defmodule NullaWeb.ActivityControllerTest do
use NullaWeb.ConnCase
import Nulla.ActivitiesFixtures
alias Nulla.Activities.Activity
@create_attrs %{
type: "some type",
cc: ["option1", "option2"],
to: ["option1", "option2"],
ap_id: "some ap_id",
actor: "some actor",
object: "some object"
}
@update_attrs %{
type: "some updated type",
cc: ["option1"],
to: ["option1"],
ap_id: "some updated ap_id",
actor: "some updated actor",
object: "some updated object"
}
@invalid_attrs %{type: nil, cc: nil, to: nil, ap_id: nil, actor: nil, object: nil}
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "index" do
test "lists all activities", %{conn: conn} do
conn = get(conn, ~p"/api/activities")
assert json_response(conn, 200)["data"] == []
end
end
describe "create activity" do
test "renders activity when data is valid", %{conn: conn} do
conn = post(conn, ~p"/api/activities", activity: @create_attrs)
assert %{"id" => id} = json_response(conn, 201)["data"]
conn = get(conn, ~p"/api/activities/#{id}")
assert %{
"id" => ^id,
"actor" => "some actor",
"ap_id" => "some ap_id",
"cc" => ["option1", "option2"],
"object" => "some object",
"to" => ["option1", "option2"],
"type" => "some type"
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, ~p"/api/activities", activity: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "update activity" do
setup [:create_activity]
test "renders activity when data is valid", %{
conn: conn,
activity: %Activity{id: id} = activity
} do
conn = put(conn, ~p"/api/activities/#{activity}", activity: @update_attrs)
assert %{"id" => ^id} = json_response(conn, 200)["data"]
conn = get(conn, ~p"/api/activities/#{id}")
assert %{
"id" => ^id,
"actor" => "some updated actor",
"ap_id" => "some updated ap_id",
"cc" => ["option1"],
"object" => "some updated object",
"to" => ["option1"],
"type" => "some updated type"
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn, activity: activity} do
conn = put(conn, ~p"/api/activities/#{activity}", activity: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "delete activity" do
setup [:create_activity]
test "deletes chosen activity", %{conn: conn, activity: activity} do
conn = delete(conn, ~p"/api/activities/#{activity}")
assert response(conn, 204)
assert_error_sent 404, fn ->
get(conn, ~p"/api/activities/#{activity}")
end
end
end
defp create_activity(_) do
activity = activity_fixture()
%{activity: activity}
end
end

View file

@ -0,0 +1,211 @@
defmodule NullaWeb.ActorControllerTest do
use NullaWeb.ConnCase
import Nulla.ActorsFixtures
alias Nulla.Actors.Actor
@create_attrs %{
name: "some name",
tag: [],
type: "some type",
image: %{},
url: "some url",
acct: "some acct",
ap_id: "some ap_id",
following: "some following",
followers: "some followers",
inbox: "some inbox",
outbox: "some outbox",
featured: "some featured",
featuredTags: "some featuredTags",
preferredUsername: "some preferredUsername",
summary: "some summary",
manuallyApprovesFollowers: true,
discoverable: true,
indexable: true,
published: ~U[2025-06-30 13:31:00Z],
memorial: true,
publicKey: %{},
attachment: [],
endpoints: %{},
icon: %{},
vcard_bday: ~D[2025-06-30],
vcard_Address: "some vcard_Address"
}
@update_attrs %{
name: "some updated name",
tag: [],
type: "some updated type",
image: %{},
url: "some updated url",
acct: "some updated acct",
ap_id: "some updated ap_id",
following: "some updated following",
followers: "some updated followers",
inbox: "some updated inbox",
outbox: "some updated outbox",
featured: "some updated featured",
featuredTags: "some updated featuredTags",
preferredUsername: "some updated preferredUsername",
summary: "some updated summary",
manuallyApprovesFollowers: false,
discoverable: false,
indexable: false,
published: ~U[2025-07-01 13:31:00Z],
memorial: false,
publicKey: %{},
attachment: [],
endpoints: %{},
icon: %{},
vcard_bday: ~D[2025-07-01],
vcard_Address: "some updated vcard_Address"
}
@invalid_attrs %{
name: nil,
tag: nil,
type: nil,
image: nil,
url: nil,
acct: nil,
ap_id: nil,
following: nil,
followers: nil,
inbox: nil,
outbox: nil,
featured: nil,
featuredTags: nil,
preferredUsername: nil,
summary: nil,
manuallyApprovesFollowers: nil,
discoverable: nil,
indexable: nil,
published: nil,
memorial: nil,
publicKey: nil,
attachment: nil,
endpoints: nil,
icon: nil,
vcard_bday: nil,
vcard_Address: nil
}
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "index" do
test "lists all actors", %{conn: conn} do
conn = get(conn, ~p"/api/actors")
assert json_response(conn, 200)["data"] == []
end
end
describe "create actor" do
test "renders actor when data is valid", %{conn: conn} do
conn = post(conn, ~p"/api/actors", actor: @create_attrs)
assert %{"id" => id} = json_response(conn, 201)["data"]
conn = get(conn, ~p"/api/actors/#{id}")
assert %{
"id" => ^id,
"acct" => "some acct",
"ap_id" => "some ap_id",
"attachment" => [],
"discoverable" => true,
"endpoints" => %{},
"featured" => "some featured",
"featuredTags" => "some featuredTags",
"followers" => "some followers",
"following" => "some following",
"icon" => %{},
"image" => %{},
"inbox" => "some inbox",
"indexable" => true,
"manuallyApprovesFollowers" => true,
"memorial" => true,
"name" => "some name",
"outbox" => "some outbox",
"preferredUsername" => "some preferredUsername",
"publicKey" => %{},
"published" => "2025-06-30T13:31:00Z",
"summary" => "some summary",
"tag" => [],
"type" => "some type",
"url" => "some url",
"vcard_Address" => "some vcard_Address",
"vcard_bday" => "2025-06-30"
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, ~p"/api/actors", actor: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "update actor" do
setup [:create_actor]
test "renders actor when data is valid", %{conn: conn, actor: %Actor{id: id} = actor} do
conn = put(conn, ~p"/api/actors/#{actor}", actor: @update_attrs)
assert %{"id" => ^id} = json_response(conn, 200)["data"]
conn = get(conn, ~p"/api/actors/#{id}")
assert %{
"id" => ^id,
"acct" => "some updated acct",
"ap_id" => "some updated ap_id",
"attachment" => [],
"discoverable" => false,
"endpoints" => %{},
"featured" => "some updated featured",
"featuredTags" => "some updated featuredTags",
"followers" => "some updated followers",
"following" => "some updated following",
"icon" => %{},
"image" => %{},
"inbox" => "some updated inbox",
"indexable" => false,
"manuallyApprovesFollowers" => false,
"memorial" => false,
"name" => "some updated name",
"outbox" => "some updated outbox",
"preferredUsername" => "some updated preferredUsername",
"publicKey" => %{},
"published" => "2025-07-01T13:31:00Z",
"summary" => "some updated summary",
"tag" => [],
"type" => "some updated type",
"url" => "some updated url",
"vcard_Address" => "some updated vcard_Address",
"vcard_bday" => "2025-07-01"
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn, actor: actor} do
conn = put(conn, ~p"/api/actors/#{actor}", actor: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "delete actor" do
setup [:create_actor]
test "deletes chosen actor", %{conn: conn, actor: actor} do
conn = delete(conn, ~p"/api/actors/#{actor}")
assert response(conn, 204)
assert_error_sent 404, fn ->
get(conn, ~p"/api/actors/#{actor}")
end
end
end
defp create_actor(_) do
actor = actor_fixture()
%{actor: actor}
end
end

View file

@ -0,0 +1,118 @@
defmodule NullaWeb.MediaAttachmentControllerTest do
use NullaWeb.ConnCase
import Nulla.NotesFixtures
import Nulla.MediaAttachmentsFixtures
alias Nulla.MediaAttachments.MediaAttachment
@create_attrs %{
name: "some name",
type: "some type",
width: 42,
url: "some url",
mediaType: "some mediaType",
height: 42
}
@update_attrs %{
name: "some updated name",
type: "some updated type",
width: 43,
url: "some updated url",
mediaType: "some updated mediaType",
height: 43
}
@invalid_attrs %{name: nil, type: nil, width: nil, url: nil, mediaType: nil, height: nil}
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "index" do
test "lists all media_attachments", %{conn: conn} do
conn = get(conn, ~p"/api/media_attachments")
assert json_response(conn, 200)["data"] == []
end
end
describe "create media_attachment" do
test "renders media_attachment when data is valid", %{conn: conn} do
note = note_fixture()
create_attrs = Map.merge(@create_attrs, %{note_id: note.id})
conn = post(conn, ~p"/api/media_attachments", media_attachment: create_attrs)
assert %{"id" => id} = json_response(conn, 201)["data"]
conn = get(conn, ~p"/api/media_attachments/#{id}")
assert %{
"id" => ^id,
"height" => 42,
"mediaType" => "some mediaType",
"name" => "some name",
"type" => "some type",
"url" => "some url",
"width" => 42
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, ~p"/api/media_attachments", media_attachment: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "update media_attachment" do
setup [:create_media_attachment]
test "renders media_attachment when data is valid", %{
conn: conn,
media_attachment: %MediaAttachment{id: id} = media_attachment
} do
conn =
put(conn, ~p"/api/media_attachments/#{media_attachment}", media_attachment: @update_attrs)
assert %{"id" => ^id} = json_response(conn, 200)["data"]
conn = get(conn, ~p"/api/media_attachments/#{id}")
assert %{
"id" => ^id,
"height" => 43,
"mediaType" => "some updated mediaType",
"name" => "some updated name",
"type" => "some updated type",
"url" => "some updated url",
"width" => 43
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn, media_attachment: media_attachment} do
conn =
put(conn, ~p"/api/media_attachments/#{media_attachment}",
media_attachment: @invalid_attrs
)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "delete media_attachment" do
setup [:create_media_attachment]
test "deletes chosen media_attachment", %{conn: conn, media_attachment: media_attachment} do
conn = delete(conn, ~p"/api/media_attachments/#{media_attachment}")
assert response(conn, 204)
assert_error_sent 404, fn ->
get(conn, ~p"/api/media_attachments/#{media_attachment}")
end
end
end
defp create_media_attachment(_) do
media_attachment = media_attachment_fixture()
%{media_attachment: media_attachment}
end
end

View file

@ -0,0 +1,131 @@
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

View file

@ -0,0 +1,154 @@
defmodule NullaWeb.RelationControllerTest do
use NullaWeb.ConnCase
import Nulla.ActorsFixtures
import Nulla.RelationsFixtures
alias Nulla.Relations.Relation
@create_attrs %{
following: true,
followed_by: true,
showing_replies: true,
showings_reblogs: true,
notifying: true,
muting: true,
muting_notifications: true,
blocking: true,
blocked_by: true,
domain_blocking: true,
requested: true,
note: "some note"
}
@update_attrs %{
following: false,
followed_by: false,
showing_replies: false,
showings_reblogs: false,
notifying: false,
muting: false,
muting_notifications: false,
blocking: false,
blocked_by: false,
domain_blocking: false,
requested: false,
note: "some updated note"
}
@invalid_attrs %{
following: nil,
followed_by: nil,
showing_replies: nil,
showings_reblogs: nil,
notifying: nil,
muting: nil,
muting_notifications: nil,
blocking: nil,
blocked_by: nil,
domain_blocking: nil,
requested: nil,
note: nil
}
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "index" do
test "lists all relations", %{conn: conn} do
conn = get(conn, ~p"/api/relations")
assert json_response(conn, 200)["data"] == []
end
end
describe "create relation" do
test "renders relation when data is valid", %{conn: conn} do
local_actor = actor_fixture()
remote_actor = actor_fixture()
create_attrs =
Map.merge(@create_attrs, %{
local_actor_id: local_actor.id,
remote_actor_id: remote_actor.id
})
conn = post(conn, ~p"/api/relations", relation: create_attrs)
assert %{"id" => id} = json_response(conn, 201)["data"]
conn = get(conn, ~p"/api/relations/#{id}")
assert %{
"id" => ^id,
"blocked_by" => true,
"blocking" => true,
"domain_blocking" => true,
"followed_by" => true,
"following" => true,
"muting" => true,
"muting_notifications" => true,
"note" => "some note",
"notifying" => true,
"requested" => true,
"showing_replies" => true,
"showings_reblogs" => true
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, ~p"/api/relations", relation: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "update relation" do
setup [:create_relation]
test "renders relation when data is valid", %{
conn: conn,
relation: %Relation{id: id} = relation
} do
conn = put(conn, ~p"/api/relations/#{relation}", relation: @update_attrs)
assert %{"id" => ^id} = json_response(conn, 200)["data"]
conn = get(conn, ~p"/api/relations/#{id}")
assert %{
"id" => ^id,
"blocked_by" => false,
"blocking" => false,
"domain_blocking" => false,
"followed_by" => false,
"following" => false,
"muting" => false,
"muting_notifications" => false,
"note" => "some updated note",
"notifying" => false,
"requested" => false,
"showing_replies" => false,
"showings_reblogs" => false
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn, relation: relation} do
conn = put(conn, ~p"/api/relations/#{relation}", relation: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "delete relation" do
setup [:create_relation]
test "deletes chosen relation", %{conn: conn, relation: relation} do
conn = delete(conn, ~p"/api/relations/#{relation}")
assert response(conn, 204)
assert_error_sent 404, fn ->
get(conn, ~p"/api/relations/#{relation}")
end
end
end
defp create_relation(_) do
relation = relation_fixture()
%{relation: relation}
end
end

View file

@ -0,0 +1,113 @@
defmodule NullaWeb.UserSessionControllerTest do
use NullaWeb.ConnCase, async: true
import Nulla.AccountsFixtures
setup do
%{user: user_fixture()}
end
describe "POST /users/log_in" do
test "logs the user in", %{conn: conn, user: user} do
conn =
post(conn, ~p"/users/log_in", %{
"user" => %{"email" => user.email, "password" => valid_user_password()}
})
assert get_session(conn, :user_token)
assert redirected_to(conn) == ~p"/"
# Now do a logged in request and assert on the menu
conn = get(conn, ~p"/")
response = html_response(conn, 200)
assert response =~ user.email
assert response =~ ~p"/users/settings"
assert response =~ ~p"/users/log_out"
end
test "logs the user in with remember me", %{conn: conn, user: user} do
conn =
post(conn, ~p"/users/log_in", %{
"user" => %{
"email" => user.email,
"password" => valid_user_password(),
"remember_me" => "true"
}
})
assert conn.resp_cookies["_nulla_web_user_remember_me"]
assert redirected_to(conn) == ~p"/"
end
test "logs the user in with return to", %{conn: conn, user: user} do
conn =
conn
|> init_test_session(user_return_to: "/foo/bar")
|> post(~p"/users/log_in", %{
"user" => %{
"email" => user.email,
"password" => valid_user_password()
}
})
assert redirected_to(conn) == "/foo/bar"
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Welcome back!"
end
test "login following registration", %{conn: conn, user: user} do
conn =
conn
|> post(~p"/users/log_in", %{
"_action" => "registered",
"user" => %{
"email" => user.email,
"password" => valid_user_password()
}
})
assert redirected_to(conn) == ~p"/"
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Account created successfully"
end
test "login following password update", %{conn: conn, user: user} do
conn =
conn
|> post(~p"/users/log_in", %{
"_action" => "password_updated",
"user" => %{
"email" => user.email,
"password" => valid_user_password()
}
})
assert redirected_to(conn) == ~p"/users/settings"
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Password updated successfully"
end
test "redirects to login page with invalid credentials", %{conn: conn} do
conn =
post(conn, ~p"/users/log_in", %{
"user" => %{"email" => "invalid@email.com", "password" => "invalid_password"}
})
assert Phoenix.Flash.get(conn.assigns.flash, :error) == "Invalid email or password"
assert redirected_to(conn) == ~p"/users/log_in"
end
end
describe "DELETE /users/log_out" do
test "logs the user out", %{conn: conn, user: user} do
conn = conn |> log_in_user(user) |> delete(~p"/users/log_out")
assert redirected_to(conn) == ~p"/"
refute get_session(conn, :user_token)
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Logged out successfully"
end
test "succeeds even if the user is not logged in", %{conn: conn} do
conn = delete(conn, ~p"/users/log_out")
assert redirected_to(conn) == ~p"/"
refute get_session(conn, :user_token)
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Logged out successfully"
end
end
end

View file

@ -0,0 +1,67 @@
defmodule NullaWeb.UserConfirmationInstructionsLiveTest do
use NullaWeb.ConnCase, async: true
import Phoenix.LiveViewTest
import Nulla.AccountsFixtures
alias Nulla.Accounts
alias Nulla.Repo
setup do
%{user: user_fixture()}
end
describe "Resend confirmation" do
test "renders the resend confirmation page", %{conn: conn} do
{:ok, _lv, html} = live(conn, ~p"/users/confirm")
assert html =~ "Resend confirmation instructions"
end
test "sends a new confirmation token", %{conn: conn, user: user} do
{:ok, lv, _html} = live(conn, ~p"/users/confirm")
{:ok, conn} =
lv
|> form("#resend_confirmation_form", user: %{email: user.email})
|> render_submit()
|> follow_redirect(conn, ~p"/")
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~
"If your email is in our system"
assert Repo.get_by!(Accounts.UserToken, user_id: user.id).context == "confirm"
end
test "does not send confirmation token if user is confirmed", %{conn: conn, user: user} do
Repo.update!(Accounts.User.confirm_changeset(user))
{:ok, lv, _html} = live(conn, ~p"/users/confirm")
{:ok, conn} =
lv
|> form("#resend_confirmation_form", user: %{email: user.email})
|> render_submit()
|> follow_redirect(conn, ~p"/")
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~
"If your email is in our system"
refute Repo.get_by(Accounts.UserToken, user_id: user.id)
end
test "does not send confirmation token if email is invalid", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/users/confirm")
{:ok, conn} =
lv
|> form("#resend_confirmation_form", user: %{email: "unknown@example.com"})
|> render_submit()
|> follow_redirect(conn, ~p"/")
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~
"If your email is in our system"
assert Repo.all(Accounts.UserToken) == []
end
end
end

View file

@ -0,0 +1,89 @@
defmodule NullaWeb.UserConfirmationLiveTest do
use NullaWeb.ConnCase, async: true
import Phoenix.LiveViewTest
import Nulla.AccountsFixtures
alias Nulla.Accounts
alias Nulla.Repo
setup do
%{user: user_fixture()}
end
describe "Confirm user" do
test "renders confirmation page", %{conn: conn} do
{:ok, _lv, html} = live(conn, ~p"/users/confirm/some-token")
assert html =~ "Confirm Account"
end
test "confirms the given token once", %{conn: conn, user: user} do
token =
extract_user_token(fn url ->
Accounts.deliver_user_confirmation_instructions(user, url)
end)
{:ok, lv, _html} = live(conn, ~p"/users/confirm/#{token}")
result =
lv
|> form("#confirmation_form")
|> render_submit()
|> follow_redirect(conn, "/")
assert {:ok, conn} = result
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~
"User confirmed successfully"
assert Accounts.get_user!(user.id).confirmed_at
refute get_session(conn, :user_token)
assert Repo.all(Accounts.UserToken) == []
# when not logged in
{:ok, lv, _html} = live(conn, ~p"/users/confirm/#{token}")
result =
lv
|> form("#confirmation_form")
|> render_submit()
|> follow_redirect(conn, "/")
assert {:ok, conn} = result
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~
"User confirmation link is invalid or it has expired"
# when logged in
conn =
build_conn()
|> log_in_user(user)
{:ok, lv, _html} = live(conn, ~p"/users/confirm/#{token}")
result =
lv
|> form("#confirmation_form")
|> render_submit()
|> follow_redirect(conn, "/")
assert {:ok, conn} = result
refute Phoenix.Flash.get(conn.assigns.flash, :error)
end
test "does not confirm email with invalid token", %{conn: conn, user: user} do
{:ok, lv, _html} = live(conn, ~p"/users/confirm/invalid-token")
{:ok, conn} =
lv
|> form("#confirmation_form")
|> render_submit()
|> follow_redirect(conn, ~p"/")
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~
"User confirmation link is invalid or it has expired"
refute Accounts.get_user!(user.id).confirmed_at
end
end
end

View file

@ -0,0 +1,63 @@
defmodule NullaWeb.UserForgotPasswordLiveTest do
use NullaWeb.ConnCase, async: true
import Phoenix.LiveViewTest
import Nulla.AccountsFixtures
alias Nulla.Accounts
alias Nulla.Repo
describe "Forgot password page" do
test "renders email page", %{conn: conn} do
{:ok, lv, html} = live(conn, ~p"/users/reset_password")
assert html =~ "Forgot your password?"
assert has_element?(lv, ~s|a[href="#{~p"/users/register"}"]|, "Register")
assert has_element?(lv, ~s|a[href="#{~p"/users/log_in"}"]|, "Log in")
end
test "redirects if already logged in", %{conn: conn} do
result =
conn
|> log_in_user(user_fixture())
|> live(~p"/users/reset_password")
|> follow_redirect(conn, ~p"/")
assert {:ok, _conn} = result
end
end
describe "Reset link" do
setup do
%{user: user_fixture()}
end
test "sends a new reset password token", %{conn: conn, user: user} do
{:ok, lv, _html} = live(conn, ~p"/users/reset_password")
{:ok, conn} =
lv
|> form("#reset_password_form", user: %{"email" => user.email})
|> render_submit()
|> follow_redirect(conn, "/")
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "If your email is in our system"
assert Repo.get_by!(Accounts.UserToken, user_id: user.id).context ==
"reset_password"
end
test "does not send reset password token if email is invalid", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/users/reset_password")
{:ok, conn} =
lv
|> form("#reset_password_form", user: %{"email" => "unknown@example.com"})
|> render_submit()
|> follow_redirect(conn, "/")
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "If your email is in our system"
assert Repo.all(Accounts.UserToken) == []
end
end
end

View file

@ -0,0 +1,87 @@
defmodule NullaWeb.UserLoginLiveTest do
use NullaWeb.ConnCase, async: true
import Phoenix.LiveViewTest
import Nulla.AccountsFixtures
describe "Log in page" do
test "renders log in page", %{conn: conn} do
{:ok, _lv, html} = live(conn, ~p"/users/log_in")
assert html =~ "Log in"
assert html =~ "Register"
assert html =~ "Forgot your password?"
end
test "redirects if already logged in", %{conn: conn} do
result =
conn
|> log_in_user(user_fixture())
|> live(~p"/users/log_in")
|> follow_redirect(conn, "/")
assert {:ok, _conn} = result
end
end
describe "user login" do
test "redirects if user login with valid credentials", %{conn: conn} do
password = "123456789abcd"
user = user_fixture(%{password: password})
{:ok, lv, _html} = live(conn, ~p"/users/log_in")
form =
form(lv, "#login_form", user: %{email: user.email, password: password, remember_me: true})
conn = submit_form(form, conn)
assert redirected_to(conn) == ~p"/"
end
test "redirects to login page with a flash error if there are no valid credentials", %{
conn: conn
} do
{:ok, lv, _html} = live(conn, ~p"/users/log_in")
form =
form(lv, "#login_form",
user: %{email: "test@email.com", password: "123456", remember_me: true}
)
conn = submit_form(form, conn)
assert Phoenix.Flash.get(conn.assigns.flash, :error) == "Invalid email or password"
assert redirected_to(conn) == "/users/log_in"
end
end
describe "login navigation" do
test "redirects to registration page when the Register button is clicked", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/users/log_in")
{:ok, _login_live, login_html} =
lv
|> element(~s|main a:fl-contains("Sign up")|)
|> render_click()
|> follow_redirect(conn, ~p"/users/register")
assert login_html =~ "Register"
end
test "redirects to forgot password page when the Forgot Password button is clicked", %{
conn: conn
} do
{:ok, lv, _html} = live(conn, ~p"/users/log_in")
{:ok, conn} =
lv
|> element(~s|main a:fl-contains("Forgot your password?")|)
|> render_click()
|> follow_redirect(conn, ~p"/users/reset_password")
assert conn.resp_body =~ "Forgot your password?"
end
end
end

View file

@ -0,0 +1,87 @@
defmodule NullaWeb.UserRegistrationLiveTest do
use NullaWeb.ConnCase, async: true
import Phoenix.LiveViewTest
import Nulla.AccountsFixtures
describe "Registration page" do
test "renders registration page", %{conn: conn} do
{:ok, _lv, html} = live(conn, ~p"/users/register")
assert html =~ "Register"
assert html =~ "Log in"
end
test "redirects if already logged in", %{conn: conn} do
result =
conn
|> log_in_user(user_fixture())
|> live(~p"/users/register")
|> follow_redirect(conn, "/")
assert {:ok, _conn} = result
end
test "renders errors for invalid data", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/users/register")
result =
lv
|> element("#registration_form")
|> render_change(user: %{"email" => "with spaces", "password" => "too short"})
assert result =~ "Register"
assert result =~ "must have the @ sign and no spaces"
assert result =~ "should be at least 12 character"
end
end
describe "register user" do
test "creates account and logs the user in", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/users/register")
email = unique_user_email()
form = form(lv, "#registration_form", user: valid_user_attributes(email: email))
render_submit(form)
conn = follow_trigger_action(form, conn)
assert redirected_to(conn) == ~p"/"
# Now do a logged in request and assert on the menu
conn = get(conn, "/")
response = html_response(conn, 200)
assert response =~ email
assert response =~ "Settings"
assert response =~ "Log out"
end
test "renders errors for duplicated email", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/users/register")
user = user_fixture(%{email: "test@email.com"})
result =
lv
|> form("#registration_form",
user: %{"email" => user.email, "password" => "valid_password"}
)
|> render_submit()
assert result =~ "has already been taken"
end
end
describe "registration navigation" do
test "redirects to login page when the Log in button is clicked", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/users/register")
{:ok, _login_live, login_html} =
lv
|> element(~s|main a:fl-contains("Log in")|)
|> render_click()
|> follow_redirect(conn, ~p"/users/log_in")
assert login_html =~ "Log in"
end
end
end

View file

@ -0,0 +1,118 @@
defmodule NullaWeb.UserResetPasswordLiveTest do
use NullaWeb.ConnCase, async: true
import Phoenix.LiveViewTest
import Nulla.AccountsFixtures
alias Nulla.Accounts
setup do
user = user_fixture()
token =
extract_user_token(fn url ->
Accounts.deliver_user_reset_password_instructions(user, url)
end)
%{token: token, user: user}
end
describe "Reset password page" do
test "renders reset password with valid token", %{conn: conn, token: token} do
{:ok, _lv, html} = live(conn, ~p"/users/reset_password/#{token}")
assert html =~ "Reset Password"
end
test "does not render reset password with invalid token", %{conn: conn} do
{:error, {:redirect, to}} = live(conn, ~p"/users/reset_password/invalid")
assert to == %{
flash: %{"error" => "Reset password link is invalid or it has expired."},
to: ~p"/"
}
end
test "renders errors for invalid data", %{conn: conn, token: token} do
{:ok, lv, _html} = live(conn, ~p"/users/reset_password/#{token}")
result =
lv
|> element("#reset_password_form")
|> render_change(
user: %{"password" => "secret12", "password_confirmation" => "secret123456"}
)
assert result =~ "should be at least 12 character"
assert result =~ "does not match password"
end
end
describe "Reset Password" do
test "resets password once", %{conn: conn, token: token, user: user} do
{:ok, lv, _html} = live(conn, ~p"/users/reset_password/#{token}")
{:ok, conn} =
lv
|> form("#reset_password_form",
user: %{
"password" => "new valid password",
"password_confirmation" => "new valid password"
}
)
|> render_submit()
|> follow_redirect(conn, ~p"/users/log_in")
refute get_session(conn, :user_token)
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Password reset successfully"
assert Accounts.get_user_by_email_and_password(user.email, "new valid password")
end
test "does not reset password on invalid data", %{conn: conn, token: token} do
{:ok, lv, _html} = live(conn, ~p"/users/reset_password/#{token}")
result =
lv
|> form("#reset_password_form",
user: %{
"password" => "too short",
"password_confirmation" => "does not match"
}
)
|> render_submit()
assert result =~ "Reset Password"
assert result =~ "should be at least 12 character(s)"
assert result =~ "does not match password"
end
end
describe "Reset password navigation" do
test "redirects to login page when the Log in button is clicked", %{conn: conn, token: token} do
{:ok, lv, _html} = live(conn, ~p"/users/reset_password/#{token}")
{:ok, conn} =
lv
|> element(~s|main a:fl-contains("Log in")|)
|> render_click()
|> follow_redirect(conn, ~p"/users/log_in")
assert conn.resp_body =~ "Log in"
end
test "redirects to registration page when the Register button is clicked", %{
conn: conn,
token: token
} do
{:ok, lv, _html} = live(conn, ~p"/users/reset_password/#{token}")
{:ok, conn} =
lv
|> element(~s|main a:fl-contains("Register")|)
|> render_click()
|> follow_redirect(conn, ~p"/users/register")
assert conn.resp_body =~ "Register"
end
end
end

View file

@ -0,0 +1,210 @@
defmodule NullaWeb.UserSettingsLiveTest do
use NullaWeb.ConnCase, async: true
alias Nulla.Accounts
import Phoenix.LiveViewTest
import Nulla.AccountsFixtures
describe "Settings page" do
test "renders settings page", %{conn: conn} do
{:ok, _lv, html} =
conn
|> log_in_user(user_fixture())
|> live(~p"/users/settings")
assert html =~ "Change Email"
assert html =~ "Change Password"
end
test "redirects if user is not logged in", %{conn: conn} do
assert {:error, redirect} = live(conn, ~p"/users/settings")
assert {:redirect, %{to: path, flash: flash}} = redirect
assert path == ~p"/users/log_in"
assert %{"error" => "You must log in to access this page."} = flash
end
end
describe "update email form" do
setup %{conn: conn} do
password = valid_user_password()
user = user_fixture(%{password: password})
%{conn: log_in_user(conn, user), user: user, password: password}
end
test "updates the user email", %{conn: conn, password: password, user: user} do
new_email = unique_user_email()
{:ok, lv, _html} = live(conn, ~p"/users/settings")
result =
lv
|> form("#email_form", %{
"current_password" => password,
"user" => %{"email" => new_email}
})
|> render_submit()
assert result =~ "A link to confirm your email"
assert Accounts.get_user_by_email(user.email)
end
test "renders errors with invalid data (phx-change)", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/users/settings")
result =
lv
|> element("#email_form")
|> render_change(%{
"action" => "update_email",
"current_password" => "invalid",
"user" => %{"email" => "with spaces"}
})
assert result =~ "Change Email"
assert result =~ "must have the @ sign and no spaces"
end
test "renders errors with invalid data (phx-submit)", %{conn: conn, user: user} do
{:ok, lv, _html} = live(conn, ~p"/users/settings")
result =
lv
|> form("#email_form", %{
"current_password" => "invalid",
"user" => %{"email" => user.email}
})
|> render_submit()
assert result =~ "Change Email"
assert result =~ "did not change"
assert result =~ "is not valid"
end
end
describe "update password form" do
setup %{conn: conn} do
password = valid_user_password()
user = user_fixture(%{password: password})
%{conn: log_in_user(conn, user), user: user, password: password}
end
test "updates the user password", %{conn: conn, user: user, password: password} do
new_password = valid_user_password()
{:ok, lv, _html} = live(conn, ~p"/users/settings")
form =
form(lv, "#password_form", %{
"current_password" => password,
"user" => %{
"email" => user.email,
"password" => new_password,
"password_confirmation" => new_password
}
})
render_submit(form)
new_password_conn = follow_trigger_action(form, conn)
assert redirected_to(new_password_conn) == ~p"/users/settings"
assert get_session(new_password_conn, :user_token) != get_session(conn, :user_token)
assert Phoenix.Flash.get(new_password_conn.assigns.flash, :info) =~
"Password updated successfully"
assert Accounts.get_user_by_email_and_password(user.email, new_password)
end
test "renders errors with invalid data (phx-change)", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/users/settings")
result =
lv
|> element("#password_form")
|> render_change(%{
"current_password" => "invalid",
"user" => %{
"password" => "too short",
"password_confirmation" => "does not match"
}
})
assert result =~ "Change Password"
assert result =~ "should be at least 12 character(s)"
assert result =~ "does not match password"
end
test "renders errors with invalid data (phx-submit)", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/users/settings")
result =
lv
|> form("#password_form", %{
"current_password" => "invalid",
"user" => %{
"password" => "too short",
"password_confirmation" => "does not match"
}
})
|> render_submit()
assert result =~ "Change Password"
assert result =~ "should be at least 12 character(s)"
assert result =~ "does not match password"
assert result =~ "is not valid"
end
end
describe "confirm email" do
setup %{conn: conn} do
user = user_fixture()
email = unique_user_email()
token =
extract_user_token(fn url ->
Accounts.deliver_user_update_email_instructions(%{user | email: email}, user.email, url)
end)
%{conn: log_in_user(conn, user), token: token, email: email, user: user}
end
test "updates the user email once", %{conn: conn, user: user, token: token, email: email} do
{:error, redirect} = live(conn, ~p"/users/settings/confirm_email/#{token}")
assert {:live_redirect, %{to: path, flash: flash}} = redirect
assert path == ~p"/users/settings"
assert %{"info" => message} = flash
assert message == "Email changed successfully."
refute Accounts.get_user_by_email(user.email)
assert Accounts.get_user_by_email(email)
# use confirm token again
{:error, redirect} = live(conn, ~p"/users/settings/confirm_email/#{token}")
assert {:live_redirect, %{to: path, flash: flash}} = redirect
assert path == ~p"/users/settings"
assert %{"error" => message} = flash
assert message == "Email change link is invalid or it has expired."
end
test "does not update email with invalid token", %{conn: conn, user: user} do
{:error, redirect} = live(conn, ~p"/users/settings/confirm_email/oops")
assert {:live_redirect, %{to: path, flash: flash}} = redirect
assert path == ~p"/users/settings"
assert %{"error" => message} = flash
assert message == "Email change link is invalid or it has expired."
assert Accounts.get_user_by_email(user.email)
end
test "redirects if user is not logged in", %{token: token} do
conn = build_conn()
{:error, redirect} = live(conn, ~p"/users/settings/confirm_email/#{token}")
assert {:redirect, %{to: path, flash: flash}} = redirect
assert path == ~p"/users/log_in"
assert %{"error" => message} = flash
assert message == "You must log in to access this page."
end
end
end

View file

@ -0,0 +1,272 @@
defmodule NullaWeb.UserAuthTest do
use NullaWeb.ConnCase, async: true
alias Phoenix.LiveView
alias Nulla.Accounts
alias NullaWeb.UserAuth
import Nulla.AccountsFixtures
@remember_me_cookie "_nulla_web_user_remember_me"
setup %{conn: conn} do
conn =
conn
|> Map.replace!(:secret_key_base, NullaWeb.Endpoint.config(:secret_key_base))
|> init_test_session(%{})
%{user: user_fixture(), conn: conn}
end
describe "log_in_user/3" do
test "stores the user token in the session", %{conn: conn, user: user} do
conn = UserAuth.log_in_user(conn, user)
assert token = get_session(conn, :user_token)
assert get_session(conn, :live_socket_id) == "users_sessions:#{Base.url_encode64(token)}"
assert redirected_to(conn) == ~p"/"
assert Accounts.get_user_by_session_token(token)
end
test "clears everything previously stored in the session", %{conn: conn, user: user} do
conn = conn |> put_session(:to_be_removed, "value") |> UserAuth.log_in_user(user)
refute get_session(conn, :to_be_removed)
end
test "redirects to the configured path", %{conn: conn, user: user} do
conn = conn |> put_session(:user_return_to, "/hello") |> UserAuth.log_in_user(user)
assert redirected_to(conn) == "/hello"
end
test "writes a cookie if remember_me is configured", %{conn: conn, user: user} do
conn = conn |> fetch_cookies() |> UserAuth.log_in_user(user, %{"remember_me" => "true"})
assert get_session(conn, :user_token) == conn.cookies[@remember_me_cookie]
assert %{value: signed_token, max_age: max_age} = conn.resp_cookies[@remember_me_cookie]
assert signed_token != get_session(conn, :user_token)
assert max_age == 5_184_000
end
end
describe "logout_user/1" do
test "erases session and cookies", %{conn: conn, user: user} do
user_token = Accounts.generate_user_session_token(user)
conn =
conn
|> put_session(:user_token, user_token)
|> put_req_cookie(@remember_me_cookie, user_token)
|> fetch_cookies()
|> UserAuth.log_out_user()
refute get_session(conn, :user_token)
refute conn.cookies[@remember_me_cookie]
assert %{max_age: 0} = conn.resp_cookies[@remember_me_cookie]
assert redirected_to(conn) == ~p"/"
refute Accounts.get_user_by_session_token(user_token)
end
test "broadcasts to the given live_socket_id", %{conn: conn} do
live_socket_id = "users_sessions:abcdef-token"
NullaWeb.Endpoint.subscribe(live_socket_id)
conn
|> put_session(:live_socket_id, live_socket_id)
|> UserAuth.log_out_user()
assert_receive %Phoenix.Socket.Broadcast{event: "disconnect", topic: ^live_socket_id}
end
test "works even if user is already logged out", %{conn: conn} do
conn = conn |> fetch_cookies() |> UserAuth.log_out_user()
refute get_session(conn, :user_token)
assert %{max_age: 0} = conn.resp_cookies[@remember_me_cookie]
assert redirected_to(conn) == ~p"/"
end
end
describe "fetch_current_user/2" do
test "authenticates user from session", %{conn: conn, user: user} do
user_token = Accounts.generate_user_session_token(user)
conn = conn |> put_session(:user_token, user_token) |> UserAuth.fetch_current_user([])
assert conn.assigns.current_user.id == user.id
end
test "authenticates user from cookies", %{conn: conn, user: user} do
logged_in_conn =
conn |> fetch_cookies() |> UserAuth.log_in_user(user, %{"remember_me" => "true"})
user_token = logged_in_conn.cookies[@remember_me_cookie]
%{value: signed_token} = logged_in_conn.resp_cookies[@remember_me_cookie]
conn =
conn
|> put_req_cookie(@remember_me_cookie, signed_token)
|> UserAuth.fetch_current_user([])
assert conn.assigns.current_user.id == user.id
assert get_session(conn, :user_token) == user_token
assert get_session(conn, :live_socket_id) ==
"users_sessions:#{Base.url_encode64(user_token)}"
end
test "does not authenticate if data is missing", %{conn: conn, user: user} do
_ = Accounts.generate_user_session_token(user)
conn = UserAuth.fetch_current_user(conn, [])
refute get_session(conn, :user_token)
refute conn.assigns.current_user
end
end
describe "on_mount :mount_current_user" do
test "assigns current_user based on a valid user_token", %{conn: conn, user: user} do
user_token = Accounts.generate_user_session_token(user)
session = conn |> put_session(:user_token, user_token) |> get_session()
{:cont, updated_socket} =
UserAuth.on_mount(:mount_current_user, %{}, session, %LiveView.Socket{})
assert updated_socket.assigns.current_user.id == user.id
end
test "assigns nil to current_user assign if there isn't a valid user_token", %{conn: conn} do
user_token = "invalid_token"
session = conn |> put_session(:user_token, user_token) |> get_session()
{:cont, updated_socket} =
UserAuth.on_mount(:mount_current_user, %{}, session, %LiveView.Socket{})
assert updated_socket.assigns.current_user == nil
end
test "assigns nil to current_user assign if there isn't a user_token", %{conn: conn} do
session = conn |> get_session()
{:cont, updated_socket} =
UserAuth.on_mount(:mount_current_user, %{}, session, %LiveView.Socket{})
assert updated_socket.assigns.current_user == nil
end
end
describe "on_mount :ensure_authenticated" do
test "authenticates current_user based on a valid user_token", %{conn: conn, user: user} do
user_token = Accounts.generate_user_session_token(user)
session = conn |> put_session(:user_token, user_token) |> get_session()
{:cont, updated_socket} =
UserAuth.on_mount(:ensure_authenticated, %{}, session, %LiveView.Socket{})
assert updated_socket.assigns.current_user.id == user.id
end
test "redirects to login page if there isn't a valid user_token", %{conn: conn} do
user_token = "invalid_token"
session = conn |> put_session(:user_token, user_token) |> get_session()
socket = %LiveView.Socket{
endpoint: NullaWeb.Endpoint,
assigns: %{__changed__: %{}, flash: %{}}
}
{:halt, updated_socket} = UserAuth.on_mount(:ensure_authenticated, %{}, session, socket)
assert updated_socket.assigns.current_user == nil
end
test "redirects to login page if there isn't a user_token", %{conn: conn} do
session = conn |> get_session()
socket = %LiveView.Socket{
endpoint: NullaWeb.Endpoint,
assigns: %{__changed__: %{}, flash: %{}}
}
{:halt, updated_socket} = UserAuth.on_mount(:ensure_authenticated, %{}, session, socket)
assert updated_socket.assigns.current_user == nil
end
end
describe "on_mount :redirect_if_user_is_authenticated" do
test "redirects if there is an authenticated user ", %{conn: conn, user: user} do
user_token = Accounts.generate_user_session_token(user)
session = conn |> put_session(:user_token, user_token) |> get_session()
assert {:halt, _updated_socket} =
UserAuth.on_mount(
:redirect_if_user_is_authenticated,
%{},
session,
%LiveView.Socket{}
)
end
test "doesn't redirect if there is no authenticated user", %{conn: conn} do
session = conn |> get_session()
assert {:cont, _updated_socket} =
UserAuth.on_mount(
:redirect_if_user_is_authenticated,
%{},
session,
%LiveView.Socket{}
)
end
end
describe "redirect_if_user_is_authenticated/2" do
test "redirects if user is authenticated", %{conn: conn, user: user} do
conn = conn |> assign(:current_user, user) |> UserAuth.redirect_if_user_is_authenticated([])
assert conn.halted
assert redirected_to(conn) == ~p"/"
end
test "does not redirect if user is not authenticated", %{conn: conn} do
conn = UserAuth.redirect_if_user_is_authenticated(conn, [])
refute conn.halted
refute conn.status
end
end
describe "require_authenticated_user/2" do
test "redirects if user is not authenticated", %{conn: conn} do
conn = conn |> fetch_flash() |> UserAuth.require_authenticated_user([])
assert conn.halted
assert redirected_to(conn) == ~p"/users/log_in"
assert Phoenix.Flash.get(conn.assigns.flash, :error) ==
"You must log in to access this page."
end
test "stores the path to redirect to on GET", %{conn: conn} do
halted_conn =
%{conn | path_info: ["foo"], query_string: ""}
|> fetch_flash()
|> UserAuth.require_authenticated_user([])
assert halted_conn.halted
assert get_session(halted_conn, :user_return_to) == "/foo"
halted_conn =
%{conn | path_info: ["foo"], query_string: "bar=baz"}
|> fetch_flash()
|> UserAuth.require_authenticated_user([])
assert halted_conn.halted
assert get_session(halted_conn, :user_return_to) == "/foo?bar=baz"
halted_conn =
%{conn | path_info: ["foo"], query_string: "bar", method: "POST"}
|> fetch_flash()
|> UserAuth.require_authenticated_user([])
assert halted_conn.halted
refute get_session(halted_conn, :user_return_to)
end
test "does not redirect if user is authenticated", %{conn: conn, user: user} do
conn = conn |> assign(:current_user, user) |> UserAuth.require_authenticated_user([])
refute conn.halted
refute conn.status
end
end
end