92 lines
4.6 KiB
Go
92 lines
4.6 KiB
Go
// account_hierarchy_realname_test.go(原 Day28):上级切换/标星/团队/实名的 API 权限与闭环。
|
|
package v1
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
// TestAPI_HierarchyAndProfile admin 全通 / customer 越权 403 / 非法参数 400。
|
|
func TestAPI_HierarchyAndProfile(t *testing.T) {
|
|
r := newD27Router(t) // admin=1(通配) customer=2
|
|
adminTok := d25Token(t, 1, "admin")
|
|
custTok := d25Token(t, 2, "customer")
|
|
|
|
// 标星(customer 无 account:write → 403;admin → 200)
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/2/star", custTok, "application/json", []byte(`{"starred":true}`)); w.Code != http.StatusForbidden {
|
|
t.Fatalf("customer 标星 = %d, 期望 403", w.Code)
|
|
}
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/2/star", adminTok, "application/json", []byte(`{"starred":true}`)); w.Code != http.StatusOK {
|
|
t.Fatalf("admin 标星 = %d body=%s", w.Code, w.Body.String())
|
|
}
|
|
// 列表置顶(标星的 2 号排首)
|
|
w := doReq(r, http.MethodGet, "/api/v1/accounts?page=1&page_size=10", adminTok)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("列表 = %d", w.Code)
|
|
}
|
|
if b := w.Body.String(); !contains(b, `"is_starred":1`) {
|
|
t.Fatalf("列表应含标星标记: %s", b)
|
|
}
|
|
// 服务团队(admin → 200;超长 → 400)
|
|
if w := doReqBody(r, http.MethodPut, "/api/v1/accounts/2/svc-team", adminTok, "application/json", []byte(`{"svc_team":"华东二组"}`)); w.Code != http.StatusOK {
|
|
t.Fatalf("admin 绑定团队 = %d", w.Code)
|
|
}
|
|
// 上级切换:customer 2 挂到 admin 1 下(admin 操作)
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/2/parent", adminTok, "application/json", []byte(`{"parent_id":1}`)); w.Code != http.StatusOK {
|
|
t.Fatalf("admin 切换上级 = %d body=%s", w.Code, w.Body.String())
|
|
}
|
|
// 防环:1 挂到 2 下(2 已是 1 的子……实际 2 挂 1 下,1→2 成环)→ 400
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/1/parent", adminTok, "application/json", []byte(`{"parent_id":2}`)); w.Code != http.StatusBadRequest {
|
|
t.Fatalf("成环切换 = %d, 期望 400(body=%s)", w.Code, w.Body.String())
|
|
}
|
|
// customer 切换上级 → 403
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/2/parent", custTok, "application/json", []byte(`{"parent_id":1}`)); w.Code != http.StatusForbidden {
|
|
t.Fatalf("customer 切换上级 = %d, 期望 403", w.Code)
|
|
}
|
|
}
|
|
|
|
// TestAPI_RealnameFlow 实名提交(本人)→ 审核(admin)→ 查询脱敏;越权 403。
|
|
func TestAPI_RealnameFlow(t *testing.T) {
|
|
r := newD27Router(t)
|
|
adminTok := d25Token(t, 1, "admin")
|
|
custTok := d25Token(t, 2, "customer")
|
|
body := []byte(`{"real_name":"张三","id_card":"350203199001011234"}`)
|
|
|
|
// customer 提交本人 → 200
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/2/realname", custTok, "application/json", body); w.Code != http.StatusOK {
|
|
t.Fatalf("本人提交实名 = %d body=%s", w.Code, w.Body.String())
|
|
}
|
|
// 越权:customer 给 admin(1) 提交 → 403
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/1/realname", custTok, "application/json", body); w.Code != http.StatusForbidden {
|
|
t.Fatalf("越权提交 = %d, 期望 403", w.Code)
|
|
}
|
|
// 本人查询 → 脱敏(不含完整证件号)
|
|
w := doReq(r, http.MethodGet, "/api/v1/accounts/2/realname", custTok)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("查询实名 = %d", w.Code)
|
|
}
|
|
if b := w.Body.String(); contains(b, "350203199001011234") || !contains(b, "3502") {
|
|
t.Fatalf("证件号应脱敏: %s", b)
|
|
}
|
|
// customer 审核 → 403(account:write)
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/2/realname/review", custTok, "application/json", []byte(`{"approve":true}`)); w.Code != http.StatusForbidden {
|
|
t.Fatalf("customer 审核 = %d, 期望 403", w.Code)
|
|
}
|
|
// admin 审核通过 → 200 状态 1
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/2/realname/review", adminTok, "application/json", []byte(`{"approve":true}`)); w.Code != http.StatusOK || !contains(w.Body.String(), `"status":1`) {
|
|
t.Fatalf("admin 审核 = %d body=%s", w.Code, w.Body.String())
|
|
}
|
|
// 终态再审核 → 400(状态机)
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/2/realname/review", adminTok, "application/json", []byte(`{"approve":true}`)); w.Code != http.StatusBadRequest {
|
|
t.Fatalf("终态再审核 = %d, 期望 400", w.Code)
|
|
}
|
|
// 已通过再提交 → 400
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/2/realname", custTok, "application/json", body); w.Code != http.StatusBadRequest {
|
|
t.Fatalf("已通过再提交 = %d, 期望 400", w.Code)
|
|
}
|
|
// 未认证 → 401
|
|
if w := doReq(r, http.MethodGet, "/api/v1/accounts/2/realname", ""); w.Code != http.StatusUnauthorized {
|
|
t.Fatalf("未认证 = %d, 期望 401", w.Code)
|
|
}
|
|
}
|