Files
czl231 3ef33b531d 已完成用户APP首期功能开发
交付用户端首期页面、配套接口、后台资源及测试文档。用户APP构建、静态分析和三个管理后台构建通过;完整测试仍有2项失败,后端模型注释检查未通过,详见交付记录。
2026-09-13 00:57:32 +08:00

51 lines
1.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 功能描述:验证资料更新保留头像、拒绝跨账户图片及空昵称。
// 版本1.0.0。
package user
import (
"git.apinb.com/bsm-sdk/core/types"
"github.com/DATA-DOG/go-sqlmock"
"github.com/gin-gonic/gin"
"net/http/httptest"
"strings"
"testing"
)
// TestProfileUpdateBoundaries 对数据库读写使用 SQL Mock不连接远程数据。
func TestProfileUpdateBoundaries(t *testing.T) {
for _, tc := range []struct {
name, body string
valid bool
}{
{"昵称修改保留头像", `{"name":"新昵称"}`, true},
{"允许保留旧头像", `{"name":"新昵称","avatar":"/uploads/avatars/old.png"}`, true},
{"空昵称拒绝", `{"name":" "}`, false},
{"他人头像拒绝", `{"name":"新昵称","avatar":"/uploads/avatars/other.png"}`, false},
} {
t.Run(tc.name, func(t *testing.T) {
mock := primaryTestDB(t)
mock.ExpectQuery(`SELECT .* FROM "user_account"`).WillReturnRows(sqlmock.NewRows([]string{"id", "identity", "name", "avatar"}).AddRow(1, "alice", "旧昵称", "/uploads/avatars/old.png"))
if tc.valid {
if strings.Contains(tc.body, "avatar") {
mock.ExpectExec(`UPDATE "user_account" SET "avatar"=.*"name"=`).WillReturnResult(sqlmock.NewResult(0, 1))
} else {
mock.ExpectExec(`UPDATE "user_account" SET "name"=`).WillReturnResult(sqlmock.NewResult(0, 1))
}
}
response := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(response)
ctx.Set("Auth", &types.JwtClaims{Client: "user_app", Identity: "alice"})
ctx.Request = httptest.NewRequest("PUT", "/auth/profile", strings.NewReader(tc.body))
ctx.Request.Header.Set("Content-Type", "application/json")
UpdateProfile(ctx)
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatal(err)
}
success := strings.Contains(response.Body.String(), `"code":0`)
if success != tc.valid {
t.Fatalf("更新状态错误: %s", response.Body.String())
}
})
}
}