Skip to content

Commit

Permalink
feat: Adding tests to page_controller with mocking Stripe's services. #1
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Dec 28, 2022
1 parent 8209fed commit 4b62332
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
5 changes: 4 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ defmodule App.MixProject do
{:stripity_stripe, "~> 2.17"},

# Storage
{:pockets, "~> 0.1.0"}
{:pockets, "~> 0.1.0"},

# Mocking for testing
{:mock, "~> 0.3.0", only: :test}

]
end
Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
"jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"},
"joken": {:hex, :joken, "2.5.0", "09be497d804b8115eb6f07615cef2e60c2a1008fb89dc0aef0d4c4b4609b99aa", [:mix], [{:jose, "~> 1.11.2", [hex: :jose, repo: "hexpm", optional: false]}], "hexpm", "22b25c89617c5ed8ca7b31026340a25ea0f9ca7160f9706b79be9ed81fdf74e7"},
"jose": {:hex, :jose, "1.11.5", "3bc2d75ffa5e2c941ca93e5696b54978323191988eb8d225c2e663ddfefd515e", [:mix, :rebar3], [], "hexpm", "dcd3b215bafe02ea7c5b23dafd3eb8062a5cd8f2d904fd9caa323d37034ab384"},
"meck": {:hex, :meck, "0.9.2", "85ccbab053f1db86c7ca240e9fc718170ee5bda03810a6292b5306bf31bae5f5", [:rebar3], [], "hexpm", "81344f561357dc40a8344afa53767c32669153355b626ea9fcbc8da6b3045826"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"mint": {:hex, :mint, "1.4.2", "50330223429a6e1260b2ca5415f69b0ab086141bc76dc2fbf34d7c389a6675b2", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "ce75a5bbcc59b4d7d8d70f8b2fc284b1751ffb35c7b6a6302b5192f8ab4ddd80"},
"mock": {:hex, :mock, "0.3.7", "75b3bbf1466d7e486ea2052a73c6e062c6256fb429d6797999ab02fa32f29e03", [:mix], [{:meck, "~> 0.9.2", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm", "4da49a4609e41fd99b7836945c26f373623ea968cfb6282742bcb94440cf7e5c"},
"nimble_options": {:hex, :nimble_options, "0.5.2", "42703307b924880f8c08d97719da7472673391905f528259915782bb346e0a1b", [:mix], [], "hexpm", "4da7f904b915fd71db549bcdc25f8d56f378ef7ae07dc1d372cbe72ba950dce0"},
"nimble_pool": {:hex, :nimble_pool, "0.2.6", "91f2f4c357da4c4a0a548286c84a3a28004f68f05609b4534526871a22053cde", [:mix], [], "hexpm", "1c715055095d3f2705c4e236c18b618420a35490da94149ff8b580a2144f653f"},
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
Expand Down
4 changes: 2 additions & 2 deletions test/app_web/controllers/app_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ defmodule AppWeb.AppControllerTest do
import App.ConnFixtures

test "test dashboard with logged in user", %{conn: conn} do
conn = setup_conn_with_user(conn)
conn = setup_conn_with_user(conn, 2)
conn = get(conn, ~p"/dashboard")
assert redirected_to(conn) == ~p"/"
assert redirected_to(conn, 302) == ~p"/"
end

test "test dashboard with logged in and paid user", %{conn: conn} do
Expand Down
59 changes: 58 additions & 1 deletion test/app_web/controllers/page_controller_test.exs
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
6 changes: 3 additions & 3 deletions test/support/fixtures/conn_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -41,7 +41,7 @@ defmodule App.ConnFixtures do
picture: "this",
auth_provider: "GitHub",
sid: 1,
id: @person_id
id: person_id
})
)

Expand Down

0 comments on commit 4b62332

Please sign in to comment.