markbates / markbates/goth

Testing auth handler with apitest

Open
#403 0 comments 0 reactions 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.