Skip to content

Commit

Permalink
Merge branch 'bc-home-redirect' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
macifell committed Sep 23, 2024
2 parents 2aebecc + 1ce16a9 commit 6f8beb5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/recognizer/bigcommerce.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ defmodule Recognizer.BigCommerce do
user
|> Recognizer.Repo.preload(:bigcommerce_user)
|> jwt_claims()
|> Map.put("redirect_to", "/")
|> Token.generate_and_sign(jwt_signer())

token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,42 @@ defmodule RecognizerWeb.Accounts.UserRegistrationControllerTest do

test "redirects to bigcommerce if already logged in", %{conn: conn} do
%{user: user} = insert(:bc_customer_user)
conn = conn |> log_in_user(user) |> get(Routes.user_registration_path(conn, :new, %{"bc" => "true"}))

assert redirected_to(conn) =~ "http://localhost/login/"
redirect =
conn
|> log_in_user(user)
|> get(Routes.user_registration_path(conn, :new, %{"bc" => "true"}))
|> redirected_to()

jwt_payload =
redirect
|> then(&Regex.named_captures(~r/\.(?<payload>.+)\./, &1))
|> then(fn %{"payload" => base64} -> base64 end)
|> Base.decode64!(padding: false)
|> Jason.decode!(keys: :atoms)

assert redirect =~ "http://localhost/login/"
refute :redirect_to in Map.keys(jwt_payload)
end

test "redirects to bigcommerce checkout if already logged in", %{conn: conn} do
%{user: user} = insert(:bc_customer_user)

redirect =
conn
|> log_in_user(user)
|> get(Routes.user_registration_path(conn, :new, %{"bc" => "true", "checkout" => "true"}))
|> redirected_to()

jwt_payload =
redirect
|> then(&Regex.named_captures(~r/\.(?<payload>.+)\./, &1))
|> then(fn %{"payload" => base64} -> base64 end)
|> Base.decode64!(padding: false)
|> Jason.decode!(keys: :atoms)

assert redirect =~ "http://localhost/login/"
assert %{redirect_to: "/checkout"} = jwt_payload
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,39 @@ defmodule RecognizerWeb.Accounts.UserSessionControllerTest do
end

test "redirects to bigcommerce if already logged in", %{conn: conn, user: user} do
conn = conn |> log_in_user(user) |> get(Routes.user_session_path(conn, :new, %{"bc" => "true"}))
assert redirected_to(conn) =~ "http://localhost/login/"
redirect =
conn
|> log_in_user(user)
|> get(Routes.user_session_path(conn, :new, %{"bc" => "true"}))
|> redirected_to()

jwt_payload =
redirect
|> then(&Regex.named_captures(~r/\.(?<payload>.+)\./, &1))
|> then(fn %{"payload" => base64} -> base64 end)
|> Base.decode64!(padding: false)
|> Jason.decode!(keys: :atoms)

assert redirect =~ "http://localhost/login/"
refute :redirect_to in Map.keys(jwt_payload)
end

test "redirects to bigcommerce checkout if already logged in", %{conn: conn, user: user} do
redirect =
conn
|> log_in_user(user)
|> get(Routes.user_session_path(conn, :new, %{"bc" => "true", "checkout" => "true"}))
|> redirected_to()

jwt_payload =
redirect
|> then(&Regex.named_captures(~r/\.(?<payload>.+)\./, &1))
|> then(fn %{"payload" => base64} -> base64 end)
|> Base.decode64!(padding: false)
|> Jason.decode!(keys: :atoms)

assert redirect =~ "http://localhost/login/"
assert %{redirect_to: "/checkout"} = jwt_payload
end
end

Expand Down

0 comments on commit 6f8beb5

Please sign in to comment.