Giter Site home page Giter Site logo

Test mode about ueberauth HOT 6 OPEN

ueberauth avatar ueberauth commented on April 27, 2024
Test mode

from ueberauth.

Comments (6)

yordis avatar yordis commented on April 27, 2024 1

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.

from ueberauth.

yordis avatar yordis commented on April 27, 2024 1

@axelson done

from ueberauth.

elvanja avatar elvanja commented on April 27, 2024 1

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: "[email protected]"}})

    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

from ueberauth.

doomspork avatar doomspork commented on April 27, 2024

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.

from ueberauth.

Papipo avatar Papipo commented on April 27, 2024

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

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

from ueberauth.

axelson avatar axelson commented on April 27, 2024

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

from ueberauth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.