()
| 51 | } |
| 52 | |
| 53 | func (ts *MiddlewareTestSuite) TestVerifyCaptchaValid() { |
| 54 | ts.Config.Security.Captcha.Enabled = true |
| 55 | ts.Config.Security.Captcha.Provider = "hcaptcha" |
| 56 | ts.Config.Security.Captcha.Secret = "test-secret" |
| 57 | |
| 58 | // Configure mock to return success |
| 59 | ts.CaptchaVerifier.Result = &security.VerificationResponse{Success: true} |
| 60 | ts.CaptchaVerifier.Err = nil |
| 61 | |
| 62 | adminClaims := &AccessTokenClaims{ |
| 63 | Role: "supabase_admin", |
| 64 | } |
| 65 | adminJwt, err := jwt.NewWithClaims(jwt.SigningMethodHS256, adminClaims).SignedString([]byte(ts.Config.JWT.Secret)) |
| 66 | require.NoError(ts.T(), err) |
| 67 | cases := []struct { |
| 68 | desc string |
| 69 | adminJwt string |
| 70 | captcha_token string |
| 71 | expectVerify bool |
| 72 | }{ |
| 73 | { |
| 74 | "Valid captcha response", |
| 75 | "", |
| 76 | captchaResponse, |
| 77 | true, |
| 78 | }, |
| 79 | { |
| 80 | "Ignore captcha if admin role is present", |
| 81 | adminJwt, |
| 82 | "", |
| 83 | false, |
| 84 | }, |
| 85 | } |
| 86 | for _, c := range cases { |
| 87 | // Reset mock state between cases |
| 88 | ts.CaptchaVerifier.LastToken = "" |
| 89 | ts.CaptchaVerifier.LastClientIP = "" |
| 90 | |
| 91 | var buffer bytes.Buffer |
| 92 | require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(map[string]interface{}{ |
| 93 | "email": "test@example.com", |
| 94 | "password": "secret", |
| 95 | "gotrue_meta_security": map[string]interface{}{ |
| 96 | "captcha_token": c.captcha_token, |
| 97 | }, |
| 98 | })) |
| 99 | req := httptest.NewRequest(http.MethodPost, "http://localhost", &buffer) |
| 100 | req.Header.Set("Content-Type", "application/json") |
| 101 | if c.adminJwt != "" { |
| 102 | req.Header.Set("Authorization", "Bearer "+c.adminJwt) |
| 103 | } |
| 104 | |
| 105 | beforeCtx := context.Background() |
| 106 | req = req.WithContext(beforeCtx) |
| 107 | |
| 108 | w := httptest.NewRecorder() |
| 109 | |
| 110 | afterCtx, err := ts.API.verifyCaptcha(w, req) |
nothing calls this directly
no test coverage detected