Skip to content

Commit

Permalink
Refactor ApiTokenController test
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Jun 17, 2024
1 parent 7f42a10 commit 7a0b7aa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
62 changes: 27 additions & 35 deletions test/controllers/api_token_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
defmodule Asciinema.ApiTokenControllerTest do
use AsciinemaWeb.ConnCase
import Asciinema.Factory
alias Asciinema.Accounts

@revoked_token "eb927b31-9ca3-4a6a-8a0c-dfba318e2e84"
@regular_user_token "c4ecd96a-9a16-464d-be6a-bc1f3c50c4ae"
@other_regular_user_token "b26c2fe0-603b-4b10-b0fa-f6ec85628831"
@tmp_user_token "863f6ae5-3f32-4ffc-8d47-284222d6225f"

setup %{conn: conn} do
{:ok, _} = Accounts.get_user_with_api_token(@revoked_token, "revoked")
@revoked_token |> Accounts.get_api_token!() |> Accounts.revoke_api_token!()
user = insert(:user, username: "test")
{:ok, _} = Accounts.create_api_token(user, @regular_user_token)
other_regular_user = insert(:user)
{:ok, _} = Accounts.create_api_token(other_regular_user, @other_regular_user_token)
{:ok, tmp_user} = Accounts.get_user_with_api_token(@tmp_user_token, "tmp")

{:ok, conn: conn, user: user, tmp_user: tmp_user}
end

describe "register" do
test "as a guest redirects to login page", %{conn: conn} do
Expand All @@ -28,7 +10,8 @@ defmodule Asciinema.ApiTokenControllerTest do
assert flash(conn, :info)
end

test "with invalid token shows error", %{conn: conn, user: user} do
test "with invalid token shows error", %{conn: conn} do
user = insert(:user)
conn = log_in(conn, user)

conn = get(conn, ~p"/connect/nopenope")
Expand All @@ -37,40 +20,46 @@ defmodule Asciinema.ApiTokenControllerTest do
assert flash(conn, :error) =~ ~r/invalid/i
end

test "with revoked token shows error", %{conn: conn, user: user} do
test "with revoked token shows error", %{conn: conn} do
user = insert(:user)
api_token = insert(:revoked_api_token, user: user)
conn = log_in(conn, user)

conn = get(conn, ~p"/connect/#{@revoked_token}")
conn = get(conn, ~p"/connect/#{api_token.token}")

assert redirected_to(conn, 302) == "/"
assert flash(conn, :error) =~ ~r/been revoked/i
end

test "with tmp user token shows notice, redirects to profile page", %{conn: conn, user: user} do
test "with tmp user token shows notice, redirects to profile page", %{conn: conn} do
user = insert(:user, username: "test")
tmp_user = insert(:temporary_user)
api_token = insert(:api_token, user: tmp_user)
conn = log_in(conn, user)

conn = get(conn, ~p"/connect/#{@tmp_user_token}")
conn = get(conn, ~p"/connect/#{api_token.token}")

assert redirected_to(conn, 302) == ~p"/~test"
assert flash(conn, :info) =~ ~r/successfully/
end

test "with their own token shows notice, redirects to profile page", %{conn: conn, user: user} do
test "with their own token shows notice, redirects to profile page", %{conn: conn} do
user = insert(:user, username: "test")
api_token = insert(:api_token, user: user)
conn = log_in(conn, user)

conn = get(conn, ~p"/connect/#{@regular_user_token}")
conn = get(conn, ~p"/connect/#{api_token.token}")

assert redirected_to(conn, 302) == ~p"/~test"
assert flash(conn, :info) =~ ~r/successfully/
end

test "with other user's token shows error, redirects to profile page", %{
conn: conn,
user: user
} do
test "with other user's token shows error, redirects to profile page", %{conn: conn} do
user = insert(:user, username: "test")
api_token = insert(:api_token)
conn = log_in(conn, user)

conn = get(conn, ~p"/connect/#{@other_regular_user_token}")
conn = get(conn, ~p"/connect/#{api_token.token}")

assert redirected_to(conn, 302) == ~p"/~test"
assert flash(conn, :error) =~ ~r/different/
Expand All @@ -85,27 +74,30 @@ defmodule Asciinema.ApiTokenControllerTest do
assert flash(conn, :info)
end

test "with user's own token shows notice, redirects to settings", %{conn: conn, user: user} do
conn = log_in(conn, user)
test "with user's own token shows notice, redirects to settings", %{conn: conn} do
user = insert(:user)
api_token = insert(:api_token, user: user)
conn = log_in(conn, user)

conn = delete(conn, ~p"/api_tokens/#{api_token.id}")

assert redirected_to(conn, 302) == ~p"/user/edit"
assert flash(conn, :info) =~ ~r/revoked/
end

test "with other user's token shows error, redirects to settings", %{conn: conn, user: user} do
conn = log_in(conn, user)
test "with other user's token shows error, redirects to settings", %{conn: conn} do
user = insert(:user)
api_token = insert(:api_token)
conn = log_in(conn, user)

conn = delete(conn, ~p"/api_tokens/#{api_token.id}")

assert redirected_to(conn, 302) == ~p"/user/edit"
assert flash(conn, :error) =~ ~r/not found/
end

test "with invalid token shows error, redirects to settings", %{conn: conn, user: user} do
test "with invalid token shows error, redirects to settings", %{conn: conn} do
user = insert(:user)
conn = log_in(conn, user)

conn = delete(conn, ~p"/api_tokens/123456789")
Expand Down
8 changes: 8 additions & 0 deletions test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ defmodule Asciinema.Factory do
}
end

def revoked_api_token_factory do
%ApiToken{
user: build(:user),
token: sequence(:token, &"token-#{&1}"),
revoked_at: Timex.now()
}
end

def asciicast_factory do
build(:asciicast_v2)
end
Expand Down

0 comments on commit 7a0b7aa

Please sign in to comment.