package main import ( "errors" "net/http/httptest" "reflect" "strings" "testing" "github.com/tinode/chat/server/store/types" ) func TestLoginAuditAccountDoesNotTreatTokenAsAccount(t *testing.T) { kind, account := loginAuditAccount("token", "secret-token", "") if kind != "" || account != "" { t.Fatalf("token must not be persisted as an account: kind=%q account=%q", kind, account) } kind, account = loginAuditAccount("basic", "13800138000", "") if kind != "phone" || account != "13800138000" { t.Fatalf("unexpected basic account classification: kind=%q account=%q", kind, account) } } func TestHashLoginAccount(t *testing.T) { hash := hashLoginAccount(" Alice ") if len(hash) != 64 || hash != hashLoginAccount("alice") || strings.Contains(hash, "alice") { t.Fatalf("unexpected account hash %q", hash) } } func TestClassifyLoginFailure(t *testing.T) { tests := []struct { scheme string err error want string }{ {"basic", types.ErrFailed, "password_error"}, {"sms_code", types.ErrFailed, "code_error"}, {"basic", types.ErrUserNotFound, "account_not_found"}, {"basic", types.ErrPermissionDenied, "account_disabled"}, {"basic", types.ErrPolicy, "risk_blocked"}, {"basic", errors.New("too many attempts"), "rate_limited"}, } for _, tc := range tests { got, _, _, _ := classifyLoginFailure(tc.scheme, tc.err) if got != tc.want { t.Errorf("classifyLoginFailure(%q,%v)=%q want %q", tc.scheme, tc.err, got, tc.want) } } } func TestMessageQuerySpecsExposeAdminFields(t *testing.T) { privateSpec := privateMessagesInternalQuerySpec() groupSpec := groupMessagesInternalQuerySpec() for label, spec := range map[string]*internalQuerySpec{"private": privateSpec, "group": groupSpec} { joined := strings.ToLower(spec.Select + " " + spec.From) for _, field := range []string{"from_user_id", "content_type", "content_excerpt", "status", "im_message_admin_actions"} { if !strings.Contains(joined, field) { t.Errorf("%s query missing %s", label, field) } } } if strings.Contains(strings.ToLower(privateSpec.Select), "m.content as content") || strings.Contains(strings.ToLower(groupSpec.Select), "m.content as content") { t.Fatal("message list query must not expose full message content") } if !strings.Contains(privateSpec.Select, "CAST(m.`from` AS CHAR) AS from_user_id") || !strings.Contains(privateSpec.Select, "CAST(rs.userid AS CHAR) AS to_user_id") { t.Fatal("private message query must expose 64-bit user IDs as strings") } if !strings.Contains(groupSpec.Select, "CAST(m.from_user AS CHAR) AS from_user_id") { t.Fatal("group message query must expose 64-bit user IDs as strings") } } func TestGroupMessageQuerySupportsOperationalFilters(t *testing.T) { req := httptest.NewRequest("GET", "/?seq_id=42&sender_username=alice&sender_nickname=%E5%B0%8F%E6%98%8E&group_name=%E6%B5%8B%E8%AF%95%E7%BE%A4", nil) query, _, args, _, _, err := buildInternalAdminListQuery(req, groupMessagesInternalQuerySpec(), "", types.TenantID(1)) if err != nil { t.Fatal(err) } for _, clause := range []string{"m.seqid = ?", "u.username LIKE ?", "p.nickname LIKE ?", "gs.name LIKE ?"} { if !strings.Contains(query, clause) { t.Errorf("group message query missing %q: %s", clause, query) } } want := []any{int64(1), int64(42), "%alice%", "%小明%", "%测试群%"} if !reflect.DeepEqual(args, want) { t.Fatalf("unexpected filter arguments: got %#v, want %#v", args, want) } } func TestLegacyGroupMessagesUseShardDirectory(t *testing.T) { query := strings.ToLower(internalLegacyGroupMessagesSelect(types.TenantID(9))) if strings.Contains(query, "im_group_message_topic_shards") { t.Fatalf("legacy group message query must not use removed topic shard routes: %s", query) } if !strings.Contains(query, "im_group_message_shards") { t.Fatalf("legacy group message query must use group message shard directory: %s", query) } if !strings.Contains(query, "legacy.created_at>=route.shard_start") || !strings.Contains(query, "legacy.created_at