goth icon indicating copy to clipboard operation
goth copied to clipboard

Setup test environment with Goth

Open RodolfoSilva opened this issue 1 year ago • 7 comments

Is there an easy way to mock Goth.fetch!(MyApp.Goth) when running tests? For instance, I want to use the same dummy token when this is used in test mode.

RodolfoSilva avatar Aug 10 '24 11:08 RodolfoSilva

@RodolfoSilva, or maintainers, I can make a PR. I've had to solve this a few times and there's a decent pattern that could live as a test implementation or as documentation, or as an igniter module, depending on people's preference

petermueller avatar Sep 10 '24 22:09 petermueller

@petermueller could you share this implementation? I think this PR will improve the library DX.

RodolfoSilva avatar Sep 11 '24 00:09 RodolfoSilva

Is there an easy way to mock Goth.fetch!(MyApp.Goth) when running tests? For instance, I want to use the same dummy token when this is used in test mode.

You can use Mock to custom your token response, like:

test "fetch token" do
    with_mock Goth, [fetch!: fn _ -> %Goth.Token{token: "XXXXX", type: "X", ...} end] do
      assert %Goth.Token{token: "XXXXX", type: "X", ...} == Goth.fetch!(MyApp.Goth)
    end
end

Cyytrus avatar Oct 10 '24 10:10 Cyytrus

@petermueller would be lovely to see how you are doing it 🙏

gmile avatar Nov 21 '24 10:11 gmile

Hi folks! I would like to know how to handle the Goth in test env 👀

mopp avatar Jan 15 '25 04:01 mopp

Hey guys!

I want to share a way that is working for me:

1.- Delete the processes 2.- Creating a new one with the new config using the url from your custom server 3.- Handle the callback as you want with this body

  setup do
    goth_bypass = Bypass.open()

    Supervisor.terminate_child(MyApp.Supervisor, Goth)
    Supervisor.delete_child(MyApp.Supervisor, Goth)

    Supervisor.start_child(
      MyApp.Supervisor,
      {Goth,
       name: MyApp.Goth,
       source: {:default, url: "http://localhost:#{goth_bypass.port}/oauth2/v4/token"}}
    )

    Bypass.stub(goth_bypass, "POST", "/oauth2/v4/token", fn conn ->
      Plug.Conn.resp(
        conn,
        200,
        Jason.encode!(%{
          "access_token" => "dummy_token",
          "token_type" => "Bearer",
          "expires_in" => 3600
        })
      )
    end)

    %{goth_bypass: goth_bypass}
  end

axelrizo avatar Aug 04 '25 16:08 axelrizo

I just created a fake http_client which returns a static token, and configure Goth for that in tests.

Linuus avatar Sep 02 '25 06:09 Linuus