Skip to content

Commit

Permalink
Refactor user controller test
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed May 13, 2024
1 parent c1004c3 commit 48fc3a1
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions test/controllers/user_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Asciinema.UserControllerTest do
alias Asciinema.Accounts

describe "sign-up" do
test "successful sign-up", %{conn: conn} do
test "success", %{conn: conn} do
conn = get conn, ~p"/users/new", t: Accounts.signup_token("test@example.com")
assert redirected_to(conn, 302) == ~p"/users/new"

Expand All @@ -16,7 +16,7 @@ defmodule Asciinema.UserControllerTest do
assert flash(conn, :info) =~ ~r/welcome/i
end

test "failed sign-up due to email taken", %{conn: conn} do
test "failure - email taken", %{conn: conn} do
insert(:user, email: "test@example.com")

conn = get conn, ~p"/users/new", t: Accounts.signup_token("test@example.com")
Expand All @@ -30,7 +30,7 @@ defmodule Asciinema.UserControllerTest do
assert flash(conn, :error) =~ ~r/already/i
end

test "failed sign-up due to invalid token", %{conn: conn} do
test "failure - invalid token", %{conn: conn} do
conn = get conn, ~p"/users/new", t: "nope"
assert redirected_to(conn, 302) == ~p"/users/new"

Expand All @@ -43,64 +43,77 @@ defmodule Asciinema.UserControllerTest do
end
end

describe "profile page" do
test "via ID based path", %{conn: conn} do
describe "show" do
test "by ID", %{conn: conn} do
user = insert(:user, username: "dracula3000")
conn = log_in(conn, user)

conn = get(conn, ~p"/u/#{user}")

assert html_response(conn, 200) =~ "dracula3000"
end

test "via username based path", %{conn: conn} do
test "by username", %{conn: conn} do
user = insert(:user, username: "dracula3000")
conn = log_in(conn, user)

conn = get(conn, ~p"/~dracula3000")

assert html_response(conn, 200) =~ "dracula3000"
end

test "asciicast visibility" do
test "asciicast visibility, as guest", %{conn: conn} do
user = insert(:user, username: "dracula3000")
insert(:asciicast, user: user, private: false, title: "Public stuff")
insert(:asciicast, user: user, private: true, title: "Private stuff")

# as guest

conn = get(build_conn(), ~p"/~dracula3000")
conn = get(conn, ~p"/~dracula3000")

html = html_response(conn, 200)
assert html =~ "1 public"
assert html =~ "Public stuff"
refute html =~ "Private stuff"
end

# as himself

conn = log_in(build_conn(), user)
test "asciicast visibility, as owner", %{conn: conn} do
user = insert(:user, username: "dracula3000")
insert(:asciicast, user: user, private: false, title: "Public stuff")
insert(:asciicast, user: user, private: true, title: "Private stuff")
conn = log_in(conn, user)

conn = get(conn, ~p"/~dracula3000")

html = html_response(conn, 200)
assert html =~ "2 recordings"
assert html =~ "Public stuff"
assert html =~ "Private stuff"
end
end

describe "account editing" do
describe "edit" do
test "requires logged in user", %{conn: conn} do
conn = get(conn, ~p"/user/edit")

assert redirected_to(conn, 302) == ~p"/login/new"
end

test "displays form", %{conn: conn} do
user = insert(:user)
conn = log_in(conn, user)

conn = get(conn, ~p"/user/edit")

assert html_response(conn, 200) =~ "Update"
end
end

test "update name", %{conn: conn} do
describe "update" do
test "updates name", %{conn: conn} do
user = insert(:user)
conn = log_in(conn, user)

conn = put conn, ~p"/user", %{user: %{name: "Rick"}}

location = List.first(get_resp_header(conn, "location"))
assert flash(conn, :info) =~ ~r/updated/i
assert response(conn, 302)
Expand All @@ -110,7 +123,9 @@ defmodule Asciinema.UserControllerTest do
test "displays error when invalid input", %{conn: conn} do
user = insert(:user)
conn = log_in(conn, user)

conn = put conn, ~p"/user", %{user: %{username: "R"}}

assert html_response(conn, 200) =~ "at least 2"
end
end
Expand Down

0 comments on commit 48fc3a1

Please sign in to comment.