Testing auth handler with apitest
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 6.6k
- Forks
- 631
- PR merge metrics
- No merged PRs in 30d
Description
I am trying to test the auth handler (gorilla mux):
func (env *Env) handleAuth(w http.ResponseWriter, r *http.Request) {
log.Debug().Msg("auth login")
_, err := gothic.CompleteUserAuth(w, r)
if err != nil {
gothic.BeginAuthHandler(w, r)
}
}
I mean, it is simple enough that I probably do not need a test for it but nonetheless, I want one for reasons… The test code is
func Test_handleAuth(t *testing.T) {
goth.UseProviders(
google.New(
os.Getenv("GOOGLE_KEY"),
os.Getenv("GOOGLE_SECRET"),
"http://localhost:8088/api/v1/auth/google/callback"),
)
env := Env{}
apitest.New(). // configuration
Report(apitest.SequenceDiagram()).
HandlerFunc(env.handleAuth).
Get("/api/v1/auth/google").
Expect(t).
Body(`ook`).
Status(http.StatusOK).
End()
}
This fails with the "you must select a provider" message. I thought the callback URL might be wrong so I changed it to 127.0.0.1 but got the same error.
What am I doing wrong?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with Test_handleAuth, the handleAuth entry point, and the goth.UseProviders setup; compare the requested /api/v1/auth/google path and callback configuration with how the provider is selected. Done means the test has a defined expected response and no longer fails with “you must select a provider,” or the required setup is documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- authentication, testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100