Files
lotherk 1b490485ff refactor: import Gotea from Gitea v28.4.0
Rename the main module to gotea.dev and the binary to gotea, and update the application name and English locale. Keep the external gitea.dev/sdk and gitea.dev/actionslib module imports unchanged.

Assisted-by: OpenCode:DeepSeek-V4.1-Flash
2026-11-22 12:13:09 +00:00

54 lines
1.5 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package mcaptcha
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"gotea.dev/modules/json"
"gotea.dev/modules/setting"
"gotea.dev/modules/test"
"github.com/stretchr/testify/assert"
)
func TestMCaptchaVerify(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/api/v1/pow/siteverify":
var req map[string]string
_ = json.NewDecoder(r.Body).Decode(&req)
if req["key"] == "test-site-key" {
w.WriteHeader(http.StatusOK)
assert.Equal(t, "test-secret", req["secret"])
resp := map[string]bool{"valid": req["token"] == "token-valid"}
_ = json.NewEncoder(w).Encode(resp)
} else {
w.WriteHeader(http.StatusBadRequest)
}
}
}))
defer srv.Close()
defer test.MockVariableValue(&setting.Service)()
setting.Service.McaptchaURL = strings.TrimSuffix(srv.URL, "/") + "/"
setting.Service.McaptchaSitekey = "test-site-key"
setting.Service.McaptchaSecret = "test-secret"
valid, err := Verify(t.Context(), "token-valid")
assert.NoError(t, err)
assert.True(t, valid)
valid, err = Verify(t.Context(), "token-invalid")
assert.NoError(t, err)
assert.False(t, valid)
setting.Service.McaptchaSitekey = "test-site-key-invalid"
valid, err = Verify(t.Context(), "token-invalid")
assert.Error(t, err)
assert.False(t, valid)
}