Skip to content
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

Test mode #70

Open
Papipo opened this issue Mar 23, 2018 · 6 comments
Open

Test mode #70

Papipo opened this issue Mar 23, 2018 · 6 comments

Comments

@Papipo
Copy link

Papipo commented Mar 23, 2018

Hi!

I've been lost for two days trying to find a proper way of doing testing like you would do with Omniauth.
As a workaround I've set up Bypass, but I find the process clumsy and verbose, since I need to configure each Strategy host to map to different Bypass ports, etc.

So I am willing to tackle the feature but I would like some pointers about how to implement it so we can get a mergeable pull requests.

Thanks.

@doomspork
Copy link
Member

Howdy @Papipo! This is definitely something everyone on the @ueberauth/developers team has wanted though I fear we've never come to consensus on the best way to do so.

If you're interested in pursuing this feature we can use this issue to discuss the different approaches and hopefully decide on one to take.

@doomspork doomspork mentioned this issue Apr 25, 2018
@Papipo
Copy link
Author

Papipo commented Apr 25, 2018

#69 is great. I copy a couple of feature requests I put there:

  • Adding mocks at runtime.
  • Forcing an authentication failure.

@yordis yordis added this to the v1.1 milestone Dec 24, 2018
@yordis
Copy link
Member

yordis commented Dec 24, 2018

We should allow swapping the implementation based on the environment. We shouldn't mock or come out with an implementation that leaks abstraction.

In my opinion, the issue is that Ueberauth needs to embrace an adapter pattern.

@axelson
Copy link

axelson commented Sep 13, 2021

Can we re-open this since it isn't actually fixed? (as far as I can tell)

@yordis yordis reopened this Sep 13, 2021
@yordis
Copy link
Member

yordis commented Sep 13, 2021

@axelson done

@elvanja
Copy link

elvanja commented Jul 19, 2023

If it helps anyone, I used this idea to test various paths. It is not generally applicable to every situation, but it is easily adaptable (pun intended 😄):

# in test/support/test_ueberauth_strategy.ex
defmodule Experiment.TestUeberauthStrategy do
  alias Ueberauth.Auth
  alias Ueberauth.Failure

  use Ueberauth.Strategy,
    uid_field: :id,
    ignores_csrf_attack: true

  def handle_request!(conn), do: aliased_strategy(conn).handle_request!(conn)

  def handle_callback!(%{assigns: %{ueberauth_auth: %Auth{}}} = conn), do: conn
  def handle_callback!(%{assigns: %{ueberauth_failure: %Failure{}}} = conn), do: conn
  def handle_callback!(conn), do: aliased_strategy(conn).handle_callback!(conn)

  defp aliased_strategy(%{private: %{ueberauth_request_options: %{options: opts}}} = conn) do
    Keyword.fetch!(opts, :aliased_strategy)
  end
end

# in config/test.exs
config :ueberauth, Ueberauth,
  providers: [microsoft: {Experiment.TestUeberauthStrategy, [aliased_strategy: Ueberauth.Strategy.Microsoft]}]

# in some test, e.g. test/experiment_web/controllers/auth_controller_test.exs
defmodule ExperimentWeb.AuthControllerTest do
  alias Ueberauth.Auth

  test "uses mock", %{conn: conn} do
    # request login
    conn = get(conn, ~p"/auth/microsoft")
    assert redirected_to(conn, 302) =~ "https://login.microsoftonline.com"

    # callback from provider with successfully logged in user
    conn =
      conn
      |> recycle()
      |> assign(:ueberauth_auth, %Auth{provider: :microsoft, info: %{email: "some.email@domain.com"}})

    conn =
      get(conn, ~p"/auth/microsoft/callback", %{
        code: UUID.generate(),
        session_state: UUID.generate(),
        state: UUID.generate()
      })

    # whatever further assertions you might need
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants