-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding tests for app_controller, auth_controller. #1
- Loading branch information
1 parent
584a317
commit 8209fed
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule AppWeb.AppControllerTest do | ||
use AppWeb.ConnCase | ||
import App.ConnFixtures | ||
|
||
test "test dashboard with logged in user", %{conn: conn} do | ||
conn = setup_conn_with_user(conn) | ||
conn = get(conn, ~p"/dashboard") | ||
assert redirected_to(conn) == ~p"/" | ||
end | ||
|
||
test "test dashboard with logged in and paid user", %{conn: conn} do | ||
|
||
UsersTable.create_user(%{stripe_id: 1, person_id: 1, status: true}) | ||
conn = setup_conn_with_user(conn) | ||
|
||
conn = get(conn, ~p"/dashboard") | ||
assert html_response(conn, 200) =~ "nyan cat" | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
|
||
|
||
defmodule AppWeb.AuthControllerTest do | ||
use AppWeb.ConnCase, async: true | ||
|
||
test "Logout link displayed when loggedin", %{conn: conn} do | ||
data = %{ | ||
username: "test_username", | ||
email: "[email protected]", | ||
givenName: "John Doe", | ||
picture: "this", | ||
auth_provider: "GitHub", | ||
sid: 1, | ||
id: 1 | ||
} | ||
|
||
jwt = AuthPlug.Token.generate_jwt!(data) | ||
|
||
conn = get(conn, "/?jwt=#{jwt}") | ||
assert html_response(conn, 200) =~ "logout" | ||
end | ||
|
||
test "get /logout with valid JWT", %{conn: conn} do | ||
data = %{ | ||
email: "[email protected]", | ||
givenName: "Al", | ||
picture: "this", | ||
auth_provider: "GitHub", | ||
sid: 1, | ||
id: 1 | ||
} | ||
|
||
jwt = AuthPlug.Token.generate_jwt!(data) | ||
|
||
conn = | ||
conn | ||
|> put_req_header("authorization", jwt) | ||
|> get("/logout") | ||
|
||
assert "/" = redirected_to(conn, 302) | ||
end | ||
|
||
test "test login link redirect to authdemo.fly.dev", %{conn: conn} do | ||
conn = get(conn, "/login") | ||
assert redirected_to(conn, 302) =~ "authdemo.fly.dev" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule App.ConnFixtures do | ||
@moduledoc """ | ||
This module defines test helpers for creating | ||
conn connections with associated data | ||
""" | ||
|
||
@person_id 1 | ||
|
||
@doc """ | ||
Generate a connection with a logged in user. | ||
""" | ||
def setup_conn_with_user(conn) do | ||
new_assigns = %{ | ||
person: %{ | ||
app_id: 5, | ||
aud: "Joken", | ||
auth_provider: "github", | ||
email: "[email protected]", | ||
exp: 1_702_132_323, | ||
iat: 1_670_595_323, | ||
id: @person_id, | ||
iss: "Joken", | ||
jti: "2snib63q7a9l9sfmdg00117h", | ||
nbf: 1_670_595_323, | ||
sid: 1, | ||
username: "test_username", | ||
picture: "this", | ||
givenName: "John Doe" | ||
}, | ||
loggedin: true | ||
} | ||
|
||
new_assigns = | ||
Map.put( | ||
new_assigns, | ||
:jwt, | ||
AuthPlug.Token.generate_jwt!(%{ | ||
username: "test_username", | ||
email: "[email protected]", | ||
givenName: "John Doe", | ||
picture: "this", | ||
auth_provider: "GitHub", | ||
sid: 1, | ||
id: @person_id | ||
}) | ||
) | ||
|
||
Map.replace(conn, :assigns, new_assigns) | ||
end | ||
end |