feat: complete remaining Go backend modules
This commit is contained in:
75
backend/internal/httpapi/admin_test.go
Normal file
75
backend/internal/httpapi/admin_test.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.nianxx.cn/wangxuming/NianAIGC/backend/internal/administration"
|
||||
)
|
||||
|
||||
func TestAdminGroupsCompatibilityIsGone(t *testing.T) {
|
||||
authorizer, _ := NewPlatformAuthorizer(AuthState{Required: true}, nil)
|
||||
handler, _ := NewAdminHandler(authorizer, administration.NewService(&adminStoreStub{}))
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/api/admin/accounts/groups", nil))
|
||||
if w.Code != http.StatusGone || !strings.Contains(w.Body.String(), "不再使用外部部门接口") {
|
||||
t.Fatalf("status=%d body=%q", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminAccountsListUsesSecurityProjection(t *testing.T) {
|
||||
store := &adminStoreStub{accounts: []administration.Account{{ID: "u", Phone: "13800138000", DisplayName: "User", Role: administration.RoleUser, OrganizationID: "org", Status: administration.StatusActive, PasswordHash: "must-not-leak", PasswordSalt: "must-not-leak", SessionVersion: 9}}}
|
||||
authorizer, _ := NewPlatformAuthorizer(AuthState{}, nil)
|
||||
handler, _ := NewAdminHandler(authorizer, administration.NewService(store))
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/admin/accounts", nil))
|
||||
if w.Code != http.StatusOK || strings.Contains(w.Body.String(), "must-not-leak") || strings.Contains(w.Body.String(), "sessionVersion") {
|
||||
t.Fatalf("status=%d body=%q", w.Code, w.Body.String())
|
||||
}
|
||||
var body struct {
|
||||
Members []map[string]any `json:"members"`
|
||||
CanManage bool `json:"canManageOrganizations"`
|
||||
}
|
||||
if json.Unmarshal(w.Body.Bytes(), &body) != nil || len(body.Members) != 1 || !body.CanManage {
|
||||
t.Fatalf("body=%q", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
type adminStoreStub struct {
|
||||
accounts []administration.Account
|
||||
organizations []administration.Organization
|
||||
}
|
||||
|
||||
func (s *adminStoreStub) ListAccounts(context.Context, administration.AccountFilters) ([]administration.Account, error) {
|
||||
return s.accounts, nil
|
||||
}
|
||||
func (s *adminStoreStub) GetAccount(context.Context, string) (administration.Account, bool, error) {
|
||||
return administration.Account{}, false, nil
|
||||
}
|
||||
func (s *adminStoreStub) CreateAccount(_ context.Context, a administration.Account) (administration.Account, error) {
|
||||
return a, nil
|
||||
}
|
||||
func (s *adminStoreStub) UpdateAccount(_ context.Context, a administration.Account) (administration.Account, error) {
|
||||
return a, nil
|
||||
}
|
||||
func (s *adminStoreStub) DeleteAccount(context.Context, string, string) error { return nil }
|
||||
func (s *adminStoreStub) ListOrganizations(context.Context, bool) ([]administration.Organization, error) {
|
||||
return s.organizations, nil
|
||||
}
|
||||
func (s *adminStoreStub) GetOrganization(context.Context, string) (administration.Organization, bool, error) {
|
||||
return administration.Organization{}, false, nil
|
||||
}
|
||||
func (s *adminStoreStub) CreateOrganization(_ context.Context, o administration.Organization) (administration.Organization, error) {
|
||||
return o, nil
|
||||
}
|
||||
func (s *adminStoreStub) UpdateOrganization(_ context.Context, o administration.Organization) (administration.Organization, error) {
|
||||
return o, nil
|
||||
}
|
||||
func (s *adminStoreStub) DeleteOrganization(context.Context, string) error { return nil }
|
||||
func (s *adminStoreStub) CountOrganizationMembers(context.Context, string) (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
Reference in New Issue
Block a user