blob: e6cf0e6f2f42b05328009c2c088801bc6a25f42d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
defmodule SilmataivasWeb.AuthTest do
use SilmataivasWeb.ConnCase
import Silmataivas.UsersFixtures
alias SilmataivasWeb.Plugs.Auth
describe "auth plug" do
test "authenticates user with valid token", %{conn: conn} do
# Create a user
user = user_fixture()
# Set up the connection with a valid token
conn =
conn
|> put_req_header("authorization", "Bearer #{user.user_id}")
|> Auth.call(%{})
# Verify the user is authenticated
assert conn.assigns.current_user.id == user.id
refute conn.halted
end
test "rejects request with invalid token format", %{conn: conn} do
# Set up the connection with an invalid token format
conn =
conn
|> put_req_header("authorization", "Invalid #{Ecto.UUID.generate()}")
|> Auth.call(%{})
# Verify the connection is halted
assert conn.halted
assert conn.status == 401
assert conn.resp_body == "Unauthorized"
end
test "rejects request with non-existent user token", %{conn: conn} do
# Set up the connection with a non-existent user token
conn =
conn
|> put_req_header("authorization", "Bearer #{Ecto.UUID.generate()}")
|> Auth.call(%{})
# Verify the connection is halted
assert conn.halted
assert conn.status == 401
assert conn.resp_body == "Unauthorized"
end
test "rejects request without authorization header", %{conn: conn} do
# Set up the connection without an authorization header
conn = Auth.call(conn, %{})
# Verify the connection is halted
assert conn.halted
assert conn.status == 401
assert conn.resp_body == "Unauthorized"
end
end
end
|