fix: reject friend request to missing user

This commit is contained in:
richjohnrambo
2026-09-15 00:48:36 +08:00
parent 15abc1b6e8
commit ef8091d805
2 changed files with 37 additions and 0 deletions
+34
View File
@@ -176,6 +176,7 @@ func (s *testHTTPInfrastructureStore) MarkKafkaDeadLetterReplay(_ types.TenantID
type testRelationsStore struct {
context *types.RelationshipContext
err error
created *types.FriendRequest
blocked *types.UserBlock
deleted bool
@@ -1348,6 +1349,9 @@ func (s *testRelationsStore) ListFriends(_ types.TenantID, _ types.Uid, _, _, _
}
func (s *testRelationsStore) GetRelationshipContext(_ types.TenantID, userID, peerID types.Uid, topic string) (*types.RelationshipContext, error) {
if s.err != nil {
return nil, s.err
}
if s.context != nil {
return s.context, nil
}
@@ -8020,6 +8024,36 @@ func TestServeIMFriendRequestCreate(t *testing.T) {
}
}
func TestServeIMFriendRequestCreateReturnsNotFoundForMissingTarget(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
_ = expectIMBearer(t, ctrl, "token-1", 7, 1)
oldRelations := store.Relations
oldProfiles := store.Profiles
relations := &testRelationsStore{err: types.ErrNotFound}
store.Relations = relations
store.Profiles = &testProfilesStore{}
defer func() {
store.Relations = oldRelations
store.Profiles = oldProfiles
}()
req := httptest.NewRequest(http.MethodPost, "/v1/im/friend-requests",
bytes.NewBufferString(`{"target_user_id":"`+types.Uid(2).UserId()+`","message":"你好","source":"search"}`))
req.Header.Set("Authorization", "Bearer token-1")
req.Header.Set("Idempotency-Key", "fr-missing")
rec := httptest.NewRecorder()
serveIMFriendRequests(rec, req)
if rec.Code != http.StatusNotFound {
t.Fatalf("status: got %d, want %d; body=%s", rec.Code, http.StatusNotFound, rec.Body.String())
}
if relations.created != nil {
t.Fatalf("friend request should not be created: %#v", relations.created)
}
}
func TestServeIMFriendRequestCreateIgnoresRelationEventFailure(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()