fix: reject friend request to missing user
This commit is contained in:
@@ -176,6 +176,7 @@ func (s *testHTTPInfrastructureStore) MarkKafkaDeadLetterReplay(_ types.TenantID
|
|||||||
|
|
||||||
type testRelationsStore struct {
|
type testRelationsStore struct {
|
||||||
context *types.RelationshipContext
|
context *types.RelationshipContext
|
||||||
|
err error
|
||||||
created *types.FriendRequest
|
created *types.FriendRequest
|
||||||
blocked *types.UserBlock
|
blocked *types.UserBlock
|
||||||
deleted bool
|
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) {
|
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 {
|
if s.context != nil {
|
||||||
return 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) {
|
func TestServeIMFriendRequestCreateIgnoresRelationEventFailure(t *testing.T) {
|
||||||
ctrl := gomock.NewController(t)
|
ctrl := gomock.NewController(t)
|
||||||
defer ctrl.Finish()
|
defer ctrl.Finish()
|
||||||
|
|||||||
@@ -3436,6 +3436,9 @@ func relationshipContext(tenantAdp adapter.TenantBusinessAdapter, tenantID types
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if peer == nil {
|
||||||
|
return nil, types.ErrNotFound
|
||||||
|
}
|
||||||
ctx.Peer = peer
|
ctx.Peer = peer
|
||||||
if cachedBlock, err := cachedBlockContains(tenantID, userID, peerID); err != nil {
|
if cachedBlock, err := cachedBlockContains(tenantID, userID, peerID); err != nil {
|
||||||
logs.Warn.Printf("cache block[%s:%s]: read failed: %v", userID.UserId(), peerID.UserId(), err)
|
logs.Warn.Printf("cache block[%s:%s]: read failed: %v", userID.UserId(), peerID.UserId(), err)
|
||||||
|
|||||||
Reference in New Issue
Block a user