package handler import ( "context" "net/http" "net/http/httptest" "strings" "testing" "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/warmbly/warmbly/internal/app/email" "github.com/warmbly/warmbly/internal/errx" "github.com/warmbly/warmbly/internal/models" ) type vendorConsentService struct { email.EmailService code, state, providerErr string res *email.VendorConsentResult xerr *errx.Error } func (s *vendorConsentService) FinishVendorConsent(_ context.Context, code, state, providerErr string, authorizeFor func(string) email.FinishAuthorizer) (*email.VendorConsentResult, *errx.Error) { s.code, s.state, s.providerErr = code, state, providerErr if authorizeFor == nil { return nil, errx.ErrForbidden } return s.res, s.xerr } func vendorCallback(t *testing.T, svc email.EmailService, query string) *httptest.ResponseRecorder { t.Helper() w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodGet, "/addresses/google/callback?"+query, nil) c.Set("request_id", "req-42") (&Handler{EmailService: svc}).renderOAuthCallback(c, "gmail") return w } func TestVendorConsentCallbackFinishesServerSide(t *testing.T) { svc := &vendorConsentService{res: &email.VendorConsentResult{Account: &models.Email{ID: uuid.New(), Email: "ava@acme.io"}, ActorID: uuid.New()}} w := vendorCallback(t, svc, "code=the-code&state=vc.nonce") body := w.Body.String() if w.Code != http.StatusOK || svc.code != "the-code" || svc.state != "vc.nonce" || !strings.Contains(body, "Connected") { t.Fatalf("callback = %d %s", w.Code, body) } // The code is redeemed here, never handed to a page script or another origin. if strings.Contains(body, "the-code") || strings.Contains(body, "