package wristband import ( "context" "net/http" "net/http/httptest" "slices" "strings" "testing" ) // TestConsentPathSmoke covers the configurable MCP authorize handoff: the // default options hand a valid request off to {issuer}/connect?request= // exactly as the PHP server does, a ConsentPath replaces /connect on the // same issuer, and DefaultScopes (filtered to the accepted set) is what a // request naming no data scope gets instead of read. func TestConsentPathSmoke(t *testing.T) { const issuer = "https://issuer.example.test" _, challenge := s256Pair(t) newServer := func(mutate func(*Options)) (*Server, *memoryBackend) { backend := newMemoryBackend() opts := DefaultOptions() opts.Issuer = issuer opts.Resource = issuer + "/mcp" if mutate != nil { mutate(&opts) } srv := NewServer(opts) srv.SetBackend(backend) insertAuthorizeTestClient(backend, "cli", []string{authorizeTestRedirect}, nil) return srv, backend } authorize := func(srv *Server, scope string) string { params := map[string]string{ "client_id": "cli", "redirect_uri": authorizeTestRedirect, "response_type": "code", "code_challenge": challenge, "code_challenge_method": "S256", } if scope != "" { params["scope"] = scope } rec := httptest.NewRecorder() srv.Authorize(rec, authorizeRequest(params)) if rec.Code != http.StatusFound { t.Fatalf("status = %d, want 302", rec.Code) } return rec.Header().Get("Location") } requestOf := func(t *testing.T, location, prefix string) string { t.Helper() if !strings.HasPrefix(location, prefix) { t.Fatalf("Location = %q, want prefix %q", location, prefix) } id := strings.TrimPrefix(location, prefix) if id == "" || strings.ContainsAny(id, "?&#/") { t.Fatalf("Location = %q carries no plain request id", location) } return id } t.Run("default options hand off to /connect", func(t *testing.T) { srv, _ := newServer(nil) requestOf(t, authorize(srv, "read"), issuer+"/connect?request=") }) t.Run("a consent path replaces /connect on the issuer", func(t *testing.T) { srv, _ := newServer(func(o *Options) { o.ConsentPath = "/admin/connect" }) requestOf(t, authorize(srv, "read"), issuer+"/admin/connect?request=") }) t.Run("default scopes fill a request without a data scope", func(t *testing.T) { srv, _ := newServer(func(o *Options) { o.ScopesSupported = append(o.ScopesSupported, "acme") o.DefaultScopes = []string{"acme", "not-accepted"} }) for _, scope := range []string{"", "openid profile"} { id := requestOf(t, authorize(srv, scope), issuer+"/connect?request=") view, err := srv.PendingRequest(context.Background(), id, 1) if err != nil { t.Fatal(err) } if !slices.Equal(view.ScopesRequested, []string{"acme"}) { t.Fatalf("scope %q stored %v, want [acme]", scope, view.ScopesRequested) } } }) t.Run("without default scopes the PHP read fallback stays", func(t *testing.T) { srv, _ := newServer(nil) id := requestOf(t, authorize(srv, ""), issuer+"/connect?request=") view, err := srv.PendingRequest(context.Background(), id, 1) if err != nil { t.Fatal(err) } if !slices.Equal(view.ScopesRequested, []string{"read"}) { t.Fatalf("stored %v, want [read]", view.ScopesRequested) } }) t.Run("default scopes outside the accepted set fall back to read", func(t *testing.T) { srv, _ := newServer(func(o *Options) { o.DefaultScopes = []string{"acme"} }) id := requestOf(t, authorize(srv, ""), issuer+"/connect?request=") view, err := srv.PendingRequest(context.Background(), id, 1) if err != nil { t.Fatal(err) } if !slices.Equal(view.ScopesRequested, []string{"read"}) { t.Fatalf("stored %v, want [read]", view.ScopesRequested) } }) }