verae-mcp-timestamping-serv.../internal/tools/oauth_test.go
George Lambert 2eb9ec72c6
Some checks failed
test-and-deploy / test (push) Failing after 10m46s
test-and-deploy / deploy-138 (push) Has been cancelled
feat(mcp): oauth_start / oauth_complete for social login
Browser OAuth via customer.verae.ai portal (authentik at auth.verae.ai).
oauth_start returns authorize_url+state; oauth_complete polls until xVerae-
access_token. Open without bearer on public MCP; welcome/register copy updated.
2026-10-20 16:40:50 -04:00

71 lines
2 KiB
Go

package tools
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestOauthStartAndComplete(t *testing.T) {
var startHits, resultHits int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == "/portal/api/oauth/start":
startHits++
_ = json.NewEncoder(w).Encode(map[string]any{
"authorize_url": "https://auth.verae.ai/application/o/authorize/?x=1",
"state": "st-test",
"expires_at": "2099-01-01T00:00:00Z",
"provider": "google",
})
case r.URL.Path == "/portal/api/oauth/result":
resultHits++
if resultHits < 2 {
w.WriteHeader(http.StatusAccepted)
_ = json.NewEncoder(w).Encode(map[string]string{"status": "pending"})
return
}
_ = json.NewEncoder(w).Encode(map[string]any{
"ok": true,
"token": "portal-session",
"apiKey": "xVerae-test-key",
"created": true,
"email": "user@example.com",
"customerId": "cust_abc",
})
default:
http.NotFound(w, r)
}
}))
t.Cleanup(srv.Close)
t.Setenv("VERAE_OAUTH_PORTAL_URL", srv.URL)
d := &Deps{Public: true}
_, start, err := d.oauthStart(context.Background(), nil, OauthStartArgs{Provider: "google", AcceptTos: true})
if err != nil {
t.Fatal(err)
}
if start.State != "st-test" || start.AuthorizeURL == "" {
t.Fatalf("%+v", start)
}
_, done, err := d.oauthComplete(context.Background(), nil, OauthCompleteArgs{State: start.State, WaitSec: 5})
if err != nil {
t.Fatal(err)
}
if done.AccessToken != "xVerae-test-key" || done.Username != "user@example.com" || !done.Created {
t.Fatalf("%+v", done)
}
if startHits != 1 || resultHits < 2 {
t.Fatalf("hits start=%d result=%d", startHits, resultHits)
}
}
func TestOauthStartRejectsBadProvider(t *testing.T) {
d := &Deps{Public: true}
_, _, err := d.oauthStart(context.Background(), nil, OauthStartArgs{Provider: "facebook"})
if err == nil {
t.Fatal("expected error")
}
}