-
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 to page_controller with mocking Stripe's services. #1
- Loading branch information
1 parent
8209fed
commit 4b62332
Showing
5 changed files
with
69 additions
and
7 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
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
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
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 |
---|---|---|
@@ -1,8 +1,65 @@ | ||
defmodule AppWeb.PageControllerTest do | ||
use AppWeb.ConnCase | ||
import App.ConnFixtures | ||
|
||
test "GET /", %{conn: conn} do | ||
import Mock | ||
|
||
test "get homepage", %{conn: conn} do | ||
conn = get(conn, ~p"/") | ||
assert html_response(conn, 200) =~ "This is a small project showcasing how to integrate Stripe in a Phoenix project." | ||
end | ||
|
||
test "get success with loggedin user without session_id", %{conn: conn} do | ||
conn = setup_conn_with_user(conn) | ||
conn = get(conn, ~p"/purchase/success") | ||
assert redirected_to(conn, 303) == ~p"/" | ||
end | ||
|
||
test "get success with loggedin user with session_id", %{conn: conn} do | ||
conn = setup_conn_with_user(conn) | ||
|
||
with_mock Stripe.Session, [retrieve: fn(_session_id) -> {:ok, %{customer: 1}} end] do | ||
|
||
conn = get(conn, ~p"/purchase/success?session_id=123") | ||
|
||
assert html_response(conn, 200) =~ "Thank you for completing your secure online payment." | ||
end | ||
end | ||
|
||
test "get success with loggedin user with session_id but stripe session errors out", %{conn: conn} do | ||
conn = setup_conn_with_user(conn) | ||
|
||
with_mock Stripe.Session, [retrieve: fn(_session_id) -> {:error, nil} end] do | ||
|
||
conn = get(conn, ~p"/purchase/success?session_id=123") | ||
assert redirected_to(conn, 303) == ~p"/" | ||
end | ||
end | ||
|
||
test "get cancel with loggedin user without session_id", %{conn: conn} do | ||
conn = setup_conn_with_user(conn) | ||
conn = get(conn, ~p"/purchase/cancel") | ||
assert redirected_to(conn, 303) == ~p"/" | ||
end | ||
|
||
test "get cancel with loggedin user with session_id", %{conn: conn} do | ||
conn = setup_conn_with_user(conn) | ||
|
||
with_mock Stripe.Session, [retrieve: fn(_session_id) -> {:ok, %{customer: 1}} end] do | ||
|
||
conn = get(conn, ~p"/purchase/cancel?session_id=123") | ||
|
||
assert html_response(conn, 200) =~ "looks like something went wrong" | ||
end | ||
end | ||
|
||
test "get cancel with loggedin user with session_id but stripe session errors out", %{conn: conn} do | ||
conn = setup_conn_with_user(conn) | ||
|
||
with_mock Stripe.Session, [retrieve: fn(_session_id) -> {:error, nil} end] do | ||
|
||
conn = get(conn, ~p"/purchase/cancel?session_id=123") | ||
assert redirected_to(conn, 303) == ~p"/" | ||
end | ||
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 |
---|---|---|
|
@@ -9,7 +9,7 @@ defmodule App.ConnFixtures do | |
@doc """ | ||
Generate a connection with a logged in user. | ||
""" | ||
def setup_conn_with_user(conn) do | ||
def setup_conn_with_user(conn, person_id \\ @person_id) do | ||
new_assigns = %{ | ||
person: %{ | ||
app_id: 5, | ||
|
@@ -18,7 +18,7 @@ defmodule App.ConnFixtures do | |
email: "[email protected]", | ||
exp: 1_702_132_323, | ||
iat: 1_670_595_323, | ||
id: @person_id, | ||
id: person_id, | ||
iss: "Joken", | ||
jti: "2snib63q7a9l9sfmdg00117h", | ||
nbf: 1_670_595_323, | ||
|
@@ -41,7 +41,7 @@ defmodule App.ConnFixtures do | |
picture: "this", | ||
auth_provider: "GitHub", | ||
sid: 1, | ||
id: @person_id | ||
id: person_id | ||
}) | ||
) | ||
|
||
|