- Add 17 missing @spec annotations (layouts, error_json, error_html, skewt_svg) - Move 12+ nested alias/import/require to module top level - Add phx-change/id attributes to 11 raw HTML <form> tags - Remove 4 unused LiveView assigns (:bounds, :data_provider) - Add 3 missing doctest references (HrrrNativeClient, BulkFetch, Accounts) - Break 2 long lines (path_compute.ex:382) - Strengthen weak test assertions (is_binary→byte_size, is_list→!=[]) - Replace Module.concat with Module.safe_concat (2 occurrences) - Replace length/1 > 0 with list != [] (9 occurrences) - Remove no-op assert true, fix no-assertion tests Remaining: 24 socket.assigns introspection warnings (deliberate test pattern for observable behavior testing), 1 formatter-resistant long line, 3 app-code usage warnings.
71 lines
2.3 KiB
Elixir
71 lines
2.3 KiB
Elixir
defmodule MicrowavepropWeb.ApiTokenControllerTest do
|
|
use MicrowavepropWeb.ConnCase, async: true
|
|
|
|
import Microwaveprop.AccountsFixtures
|
|
|
|
alias Microwaveprop.Accounts
|
|
|
|
setup :register_and_log_in_user
|
|
|
|
describe "POST /users/api-tokens" do
|
|
test "creates a token and surfaces the plaintext once via flash", %{conn: conn, user: user} do
|
|
conn =
|
|
post(conn, ~p"/users/api-tokens", %{
|
|
"api_token" => %{"name" => "laptop"}
|
|
})
|
|
|
|
assert redirected_to(conn) == ~p"/users/settings"
|
|
info = Phoenix.Flash.get(conn.assigns.flash, :info)
|
|
assert info =~ "laptop"
|
|
token = Phoenix.Flash.get(conn.assigns.flash, :api_token)
|
|
assert byte_size(token) > 0
|
|
assert String.starts_with?(token, "mwp_")
|
|
|
|
assert [record] = Accounts.list_api_tokens(user)
|
|
assert record.name == "laptop"
|
|
end
|
|
|
|
test "shows an error when name is blank", %{conn: conn, user: user} do
|
|
conn =
|
|
post(conn, ~p"/users/api-tokens", %{
|
|
"api_token" => %{"name" => ""}
|
|
})
|
|
|
|
assert redirected_to(conn) == ~p"/users/settings"
|
|
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "name"
|
|
assert Accounts.list_api_tokens(user) == []
|
|
end
|
|
|
|
test "redirects unauthenticated users to login" do
|
|
conn =
|
|
post(build_conn(), ~p"/users/api-tokens", %{
|
|
"api_token" => %{"name" => "Nope"}
|
|
})
|
|
|
|
assert redirected_to(conn) == ~p"/users/log-in"
|
|
end
|
|
end
|
|
|
|
describe "DELETE /users/api-tokens/:id" do
|
|
test "revokes a token owned by the current user", %{conn: conn, user: user} do
|
|
{:ok, {_plaintext, record}} = Accounts.create_api_token(user, %{"name" => "bye"})
|
|
|
|
conn = delete(conn, ~p"/users/api-tokens/#{record.id}")
|
|
|
|
assert redirected_to(conn) == ~p"/users/settings"
|
|
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "revoked"
|
|
assert Accounts.list_api_tokens(user) == []
|
|
end
|
|
|
|
test "does not revoke a token owned by another user", %{conn: conn} do
|
|
other = user_fixture()
|
|
{:ok, {_plaintext, record}} = Accounts.create_api_token(other, %{"name" => "theirs"})
|
|
|
|
conn = delete(conn, ~p"/users/api-tokens/#{record.id}")
|
|
|
|
assert redirected_to(conn) == ~p"/users/settings"
|
|
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "not found"
|
|
assert [_] = Accounts.list_api_tokens(other)
|
|
end
|
|
end
|
|
end
|