Files
yhy-mager/backend/internal/httpapi/admin_accounts_test.go
xiewenjun 54c318c615
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled
feat: implement license platform business flows and delivery tooling
Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation.

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
2026-10-09 15:33:22 +08:00

48 lines
1.5 KiB
Go

package httpapi
import (
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
)
func TestAdminCredentialValidation(t *testing.T) {
for _, name := range []string{"admin", "admin-1", "ops_team", strings.Repeat("a", 32)} {
if !validAdminUsername(name) {
t.Fatalf("valid username rejected: %s", name)
}
}
for _, name := range []string{"", "ab", "name with space", "root@example.test", "管理员", "ADMIN", strings.Repeat("a", 33)} {
if validAdminUsername(name) {
t.Fatalf("invalid username accepted: %s", name)
}
}
if normalizeAdminUsername(" TEAM_1 ") != "team_1" {
t.Fatal("normalization")
}
for _, password := range []string{"x", strings.Repeat("a", 72), strings.Repeat("中", 24)} {
if !validAdminPassword(password) {
t.Fatal("valid password rejected")
}
}
for _, password := range []string{"", strings.Repeat("a", 73), strings.Repeat("中", 25)} {
if validAdminPassword(password) {
t.Fatal("invalid password accepted")
}
}
}
func TestAdminCannotDisableSelf(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest("POST", "/admins/1/active", strings.NewReader(`{"active":false}`))
c.Request.Header.Set("Content-Type", "application/json")
c.Params = gin.Params{{Key: "id", Value: "1"}}
c.Set("admin_id", uint64(1))
(&BusinessAPI{}).setAdminActive(c)
if w.Code != 400 || !strings.Contains(w.Body.String(), "不能停用当前登录的管理员") {
t.Fatalf("%d %s", w.Code, w.Body)
}
}