From e29b5d35893aeb01af114dd34f46c8d044112244 Mon Sep 17 00:00:00 2001 From: richjohnrambo Date: Tue, 15 Sep 2026 16:02:36 +0800 Subject: [PATCH] test: cover blocked profile visibility --- server/im_http_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/server/im_http_test.go b/server/im_http_test.go index fd51cdc..646c4a6 100644 --- a/server/im_http_test.go +++ b/server/im_http_test.go @@ -8767,6 +8767,69 @@ func TestServeIMUserProfile(t *testing.T) { } } +func TestServeIMUserProfileBlockedByMeReturnsMinimalProfile(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + expectIMBearer(t, ctrl, "token-1", 7, 1) + oldUsers := store.Users + oldRelations := store.Relations + oldProfiles := store.Profiles + users := mock_store.NewMockUsersPersistenceInterface(ctrl) + peer := types.Uid(2) + users.EXPECT().Get(types.TenantID(7), peer).Return(&types.User{ + ObjHeader: types.ObjHeader{Id: peer.String()}, + TenantID: 7, + Public: map[string]any{ + "display_name": "Alice", + "nickname": "Alice", + "avatar_file_id": "file-1", + "signature": "hello", + "gender": "female", + "region": "Shanghai", + "public_account": "alice001", + }, + }, nil) + store.Users = users + store.Relations = &testRelationsStore{context: &types.RelationshipContext{ + TenantID: 7, + UserID: 1, + PeerID: peer, + Topic: types.Uid(1).P2PName(peer), + Relationship: types.RelationshipBlockedByMe, + Remark: "老同事", + }} + store.Profiles = &testProfilesStore{} + defer func() { + store.Users = oldUsers + store.Relations = oldRelations + store.Profiles = oldProfiles + }() + + req := httptest.NewRequest(http.MethodGet, "/v1/im/users/"+peer.UserId()+"/profile", nil) + req.Header.Set("Authorization", "Bearer token-1") + rec := httptest.NewRecorder() + + serveIMUserResource(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status: got %d, want %d; body=%s", rec.Code, http.StatusOK, rec.Body.String()) + } + data := decodeIMHTTPResponse(t, rec).Data.(map[string]any) + if data["relationship"] != types.RelationshipBlockedByMe || data["display_name"] != "老同事" || + data["avatar_file_id"] != "file-1" || data["topic"] == "" { + t.Fatalf("unexpected blocked profile response: %#v", data) + } + capabilities := data["capabilities"].(map[string]any) + if capabilities["can_send"] != false || capabilities["can_call"] != false || capabilities["can_add_friend"] != false { + t.Fatalf("blocked profile should disable actions: %#v", data) + } + for _, field := range []string{"signature", "gender", "region", "public_account"} { + if _, ok := data[field]; ok { + t.Fatalf("blocked profile should omit private field %q: %#v", field, data) + } + } +} + func TestServeIMUserQRCodes(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish()