Skip to content

Commit 8209fed

Browse files
committed
feat: Adding tests for app_controller, auth_controller. #1
1 parent 584a317 commit 8209fed

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
defmodule AppWeb.AppControllerTest do
2+
use AppWeb.ConnCase
3+
import App.ConnFixtures
4+
5+
test "test dashboard with logged in user", %{conn: conn} do
6+
conn = setup_conn_with_user(conn)
7+
conn = get(conn, ~p"/dashboard")
8+
assert redirected_to(conn) == ~p"/"
9+
end
10+
11+
test "test dashboard with logged in and paid user", %{conn: conn} do
12+
13+
UsersTable.create_user(%{stripe_id: 1, person_id: 1, status: true})
14+
conn = setup_conn_with_user(conn)
15+
16+
conn = get(conn, ~p"/dashboard")
17+
assert html_response(conn, 200) =~ "nyan cat"
18+
end
19+
20+
end
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
3+
defmodule AppWeb.AuthControllerTest do
4+
use AppWeb.ConnCase, async: true
5+
6+
test "Logout link displayed when loggedin", %{conn: conn} do
7+
data = %{
8+
username: "test_username",
9+
10+
givenName: "John Doe",
11+
picture: "this",
12+
auth_provider: "GitHub",
13+
sid: 1,
14+
id: 1
15+
}
16+
17+
jwt = AuthPlug.Token.generate_jwt!(data)
18+
19+
conn = get(conn, "/?jwt=#{jwt}")
20+
assert html_response(conn, 200) =~ "logout"
21+
end
22+
23+
test "get /logout with valid JWT", %{conn: conn} do
24+
data = %{
25+
26+
givenName: "Al",
27+
picture: "this",
28+
auth_provider: "GitHub",
29+
sid: 1,
30+
id: 1
31+
}
32+
33+
jwt = AuthPlug.Token.generate_jwt!(data)
34+
35+
conn =
36+
conn
37+
|> put_req_header("authorization", jwt)
38+
|> get("/logout")
39+
40+
assert "/" = redirected_to(conn, 302)
41+
end
42+
43+
test "test login link redirect to authdemo.fly.dev", %{conn: conn} do
44+
conn = get(conn, "/login")
45+
assert redirected_to(conn, 302) =~ "authdemo.fly.dev"
46+
end
47+
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
defmodule App.ConnFixtures do
2+
@moduledoc """
3+
This module defines test helpers for creating
4+
conn connections with associated data
5+
"""
6+
7+
@person_id 1
8+
9+
@doc """
10+
Generate a connection with a logged in user.
11+
"""
12+
def setup_conn_with_user(conn) do
13+
new_assigns = %{
14+
person: %{
15+
app_id: 5,
16+
aud: "Joken",
17+
auth_provider: "github",
18+
19+
exp: 1_702_132_323,
20+
iat: 1_670_595_323,
21+
id: @person_id,
22+
iss: "Joken",
23+
jti: "2snib63q7a9l9sfmdg00117h",
24+
nbf: 1_670_595_323,
25+
sid: 1,
26+
username: "test_username",
27+
picture: "this",
28+
givenName: "John Doe"
29+
},
30+
loggedin: true
31+
}
32+
33+
new_assigns =
34+
Map.put(
35+
new_assigns,
36+
:jwt,
37+
AuthPlug.Token.generate_jwt!(%{
38+
username: "test_username",
39+
40+
givenName: "John Doe",
41+
picture: "this",
42+
auth_provider: "GitHub",
43+
sid: 1,
44+
id: @person_id
45+
})
46+
)
47+
48+
Map.replace(conn, :assigns, new_assigns)
49+
end
50+
end

0 commit comments

Comments
 (0)