goth
goth copied to clipboard
Setup test environment with Goth
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, 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 could you share this implementation? I think this PR will improve the library DX.
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
@petermueller would be lovely to see how you are doing it 🙏
Hi folks! I would like to know how to handle the Goth in test env 👀
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
I just created a fake http_client which returns a static token, and configure Goth for that in tests.