test: cover string user ids in workbench payloads

This commit is contained in:
richjohnrambo
2026-09-16 18:05:26 +08:00
parent 903a77f2c5
commit fc246c3086
3 changed files with 85 additions and 13 deletions
+81
View File
@@ -0,0 +1,81 @@
package httpapi
import (
"strings"
"testing"
"time"
"git.118im.com/im_team/im_workbench/internal/store"
)
func TestPayloadUserIDsAreStrings(t *testing.T) {
now := time.Date(2026, 9, 16, 9, 30, 0, 0, time.UTC)
userA, err := parseUserID("usrag7w9xJ6hto")
if err != nil {
t.Fatal(err)
}
userB, err := parseUserID("usr1RqTFgHSenM")
if err != nil {
t.Fatal(err)
}
payloads := []map[string]any{
organizationMemberPayload(store.OrganizationMember{UserID: userA}),
attendanceRecordPayload(&store.AttendanceRecord{RecordID: "att_1", UserID: userA, Date: now, CreatedAt: now, UpdatedAt: now}),
attendanceAppealPayload(&store.AttendanceAppeal{AppealID: "atta_1", UserID: userA, SubmittedAt: now, CreatedAt: now, UpdatedAt: now}),
meetingPayload(store.Meeting{
MeetingID: "mtg_1",
OrganizerID: userA,
RecorderID: userB,
StartAt: now,
EndAt: now.Add(time.Hour),
CreatedAt: now,
UpdatedAt: now,
Participants: []store.MeetingParticipant{
{UserID: userA, Response: "accepted"},
{UserID: userB, Response: "pending"},
},
}, userA),
meetingNoteFilePayload(store.MeetingNoteFile{NoteFileID: "mnote_1", UploaderID: userA, CreatedAt: now, UpdatedAt: now}, userA),
dailyReportPayload(store.DailyReport{ReportID: "dr_1", AuthorID: userA, ReportDate: now, CreatedAt: now, UpdatedAt: now}),
monthlyReportPayload(store.MonthlyReport{ReportID: "mr_1", AuthorID: userA, CreatedAt: now, UpdatedAt: now}),
mailboxSubmissionPayload(store.MailboxSubmission{SubmissionID: "mb_1", SenderID: userA, SubmittedAt: now}),
}
for _, payload := range payloads {
assertNoNumericUserIDs(t, payload)
}
}
func assertNoNumericUserIDs(t *testing.T, value any) {
t.Helper()
switch typed := value.(type) {
case map[string]any:
for key, item := range typed {
if isUserIDKey(key) {
if _, ok := item.(string); !ok {
t.Fatalf("%s is %T, want string", key, item)
}
}
assertNoNumericUserIDs(t, item)
}
case []map[string]any:
for _, item := range typed {
assertNoNumericUserIDs(t, item)
}
case []any:
for _, item := range typed {
assertNoNumericUserIDs(t, item)
}
}
}
func isUserIDKey(key string) bool {
return key == "user_id" ||
key == "organizer_id" ||
key == "recorder_user_id" ||
key == "uploader_user_id" ||
key == "author_id" ||
key == "sender_id" ||
strings.HasSuffix(key, "_user_id")
}