()
| 135 | } |
| 136 | |
| 137 | func (ts *MiddlewareTestSuite) TestVerifyCaptchaInvalid() { |
| 138 | cases := []struct { |
| 139 | desc string |
| 140 | errorCodes []string |
| 141 | expectedCode int |
| 142 | expectedMsg string |
| 143 | }{ |
| 144 | { |
| 145 | "Captcha validation failed", |
| 146 | []string{"not-using-dummy-secret"}, |
| 147 | http.StatusBadRequest, |
| 148 | "captcha protection: request disallowed (not-using-dummy-secret)", |
| 149 | }, |
| 150 | { |
| 151 | "Captcha validation failed", |
| 152 | []string{"invalid-input-secret"}, |
| 153 | http.StatusBadRequest, |
| 154 | "captcha protection: request disallowed (invalid-input-secret)", |
| 155 | }, |
| 156 | } |
| 157 | for _, c := range cases { |
| 158 | ts.Run(c.desc, func() { |
| 159 | ts.Config.Security.Captcha.Enabled = true |
| 160 | ts.Config.Security.Captcha.Provider = "hcaptcha" |
| 161 | ts.Config.Security.Captcha.Secret = "test-secret" |
| 162 | |
| 163 | ts.CaptchaVerifier.Result = &security.VerificationResponse{ |
| 164 | Success: false, |
| 165 | ErrorCodes: c.errorCodes, |
| 166 | } |
| 167 | ts.CaptchaVerifier.Err = nil |
| 168 | |
| 169 | var buffer bytes.Buffer |
| 170 | require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(map[string]interface{}{ |
| 171 | "email": "test@example.com", |
| 172 | "password": "secret", |
| 173 | "gotrue_meta_security": map[string]interface{}{ |
| 174 | "captcha_token": captchaResponse, |
| 175 | }, |
| 176 | })) |
| 177 | req := httptest.NewRequest(http.MethodPost, "http://localhost", &buffer) |
| 178 | req.Header.Set("Content-Type", "application/json") |
| 179 | |
| 180 | req = req.WithContext(context.Background()) |
| 181 | |
| 182 | w := httptest.NewRecorder() |
| 183 | |
| 184 | _, err := ts.API.verifyCaptcha(w, req) |
| 185 | require.Equal(ts.T(), c.expectedCode, err.(*HTTPError).HTTPStatus) |
| 186 | require.Equal(ts.T(), c.expectedMsg, err.(*HTTPError).Message) |
| 187 | }) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | func (ts *MiddlewareTestSuite) TestIsValidExternalHost() { |
| 192 | cases := []struct { |
nothing calls this directly
no test coverage detected