Add the auth pipeline for the Link API path.
This commit is contained in:
@@ -2,6 +2,7 @@ defmodule LinkShortenerWeb.Api.V1.LinkControllerTest do
|
||||
use LinkShortenerWeb.ConnCase
|
||||
|
||||
import LinkShortener.LinksFixtures
|
||||
import LinkShortener.AccountsFixtures
|
||||
|
||||
alias LinkShortener.Links.Link
|
||||
alias LinkShortener.Links
|
||||
@@ -23,7 +24,13 @@ defmodule LinkShortenerWeb.Api.V1.LinkControllerTest do
|
||||
}
|
||||
|
||||
setup %{conn: conn} do
|
||||
{:ok, conn: put_req_header(conn, "accept", "application/json")}
|
||||
%{token: token} = create_user_token()
|
||||
|
||||
conn = conn
|
||||
|> put_req_header("accept", "application/json")
|
||||
|> put_req_header("authorization", "Bearer #{token}")
|
||||
|
||||
{:ok, conn: conn}
|
||||
end
|
||||
|
||||
describe "index" do
|
||||
@@ -90,6 +97,11 @@ defmodule LinkShortenerWeb.Api.V1.LinkControllerTest do
|
||||
end
|
||||
end
|
||||
|
||||
defp create_user_token() do
|
||||
token = user_token_fixture()
|
||||
%{token: token}
|
||||
end
|
||||
|
||||
defp create_link(_) do
|
||||
link = link_fixture()
|
||||
%{link: link}
|
||||
|
||||
@@ -4,6 +4,10 @@ defmodule LinkShortener.AccountsFixtures do
|
||||
entities via the `LinkShortener.Accounts` context.
|
||||
"""
|
||||
|
||||
alias LinkShortener.Accounts
|
||||
alias LinkShortener.Accounts.User
|
||||
alias LinkShortenerWeb.Auth.Guardian
|
||||
|
||||
@doc """
|
||||
Generate a unique user email.
|
||||
"""
|
||||
@@ -26,6 +30,18 @@ defmodule LinkShortener.AccountsFixtures do
|
||||
user
|
||||
end
|
||||
|
||||
def user_token_fixture(attrs \\ %{}) do
|
||||
user_params = %{
|
||||
email: "user@mail.com",
|
||||
password: "hello world 12345"
|
||||
}
|
||||
|
||||
{:ok, %User{} = user} = Accounts.register_user(user_params)
|
||||
{:ok, token, _claims} = Guardian.encode_and_sign(user)
|
||||
|
||||
token
|
||||
end
|
||||
|
||||
def extract_user_token(fun) do
|
||||
{:ok, captured_email} = fun.(&"[TOKEN]#{&1}[TOKEN]")
|
||||
[_, token | _] = String.split(captured_email.text_body, "[TOKEN]")
|
||||
|
||||
Reference in New Issue
Block a user