Skip to content

Commit

Permalink
feat: Adding tests for app_controller, auth_controller. #1
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Dec 28, 2022
1 parent 584a317 commit 8209fed
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/app_web/controllers/app_controller_test.exs
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
47 changes: 47 additions & 0 deletions test/app_web/controllers/auth_controller_test.exs
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
50 changes: 50 additions & 0 deletions test/support/fixtures/conn_fixtures.ex
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

0 comments on commit 8209fed

Please sign in to comment.