-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to test callback action? #82
Comments
Great question @fuchsberger 💭 |
Actually I figured it out. The trick was to use the Mox Library. Here is what i did:
defmodule DsaWeb.ElixirAuthGoogle do
@callback get_token(String.t, Plug.Conn.t) :: {:ok, map()}
@callback get_user_profile(String.t) :: {:ok, map()}
def get_token(code, conn), do: impl().get_token(code, conn)
def get_user_profile(access_token), do: impl().get_user_profile(access_token)
defp impl, do: Application.get_env(:dsa, :elixir_auth_google, ElixirAuthGoogle)
end
defmodule DsaWeb.StubElixirAuthGoogle do
import Dsa.AccountsFixtures, only: [valid_user_attrs: 1]
@behaviour DsaWeb.ElixirAuthGoogle
def get_token(code, _conn) do
if code == "valid_code" do
{:ok, %{access_token: "valid access token"}}
else
{:ok, %{error: "invalid_grant", error_description: "Malformed auth code."}}
end
end
def get_user_profile(access_token) do
if access_token == "valid access token" do
{:ok, valid_user_attrs(given_name: "John", family_name: "Doe")}
else
{:ok, %{error: "invalid_request", error_description: "Invalid Credentials"}}
end
end
end
Mox.defmock(DsaWeb.MockElixirAuthGoogle, for: DsaWeb.ElixirAuthGoogle)
Application.put_env(:dsa, :elixir_auth_google, DsaWeb.MockElixirAuthGoogle)
ExUnit.start()
# ...
setup _ do
Mox.stub_with(DsaWeb.MockElixirAuthGoogle, DsaWeb.StubElixirAuthGoogle)
:ok
end
defmodule DsaWeb.ElixirAuthGoogleTest do
use DsaWeb.ConnCase, async: true
import Mox
import DsaWeb.ElixirAuthGoogle, only: [get_token: 2, get_user_profile: 1]
describe "get_token/2" do
test "does return access_code if valid code", %{conn: conn} do
assert {:ok, %{access_token: "access token"} == get_token("valid_code", conn)}
end
test "does return error if invalid code", %{conn: conn} do
assert {:ok, %{error: "invalid_grant", error_description: "Malformed auth code."}} == get_token("invalid code", conn)
end
end
describe "get_user_profile/1" do
test "does return access_code if valid access token" do
assert {:ok, %{given_name: "John", family_name: "Doe"}} = get_user_profile("valid access token")
end
test "does return error if invalid access token" do
assert {:ok, %{error: "invalid_request", error_description: "Invalid Credentials"}} = get_user_profile("invalid access token")
end
end
end |
@fuchsberger thanks for the reply. |
yes behavior and stub should ideally be handled via the package not by the user leaving the user with just two lines to add for testing. |
@fuchsberger apologies for not being more helpful on my previous replies. The documentation for it is not great. Sorry. 🤦♂️ |
I am struggling to find a way to properly test the default controller action from the documentation for 100 % project test coverage.
The problem is that I can't expect a valid code from the Google callback redirect leaving me only able to test an invalid code.
Any advice would be appreciated!
The text was updated successfully, but these errors were encountered: