diff --git a/packages/shared/friends/sagas.ts b/packages/shared/friends/sagas.ts index 72a97589c..53d5e6905 100644 --- a/packages/shared/friends/sagas.ts +++ b/packages/shared/friends/sagas.ts @@ -2234,6 +2234,11 @@ export async function requestFriendship(request: SendFriendRequestPayload) { return buildFriendRequestErrorResponse(FriendshipErrorCode.FEC_NON_EXISTING_USER) } + // Check if the user is trying to send a friend request to themself + if (getUserIdFromMatrix(ownId) === userId) { + return buildFriendRequestErrorResponse(FriendshipErrorCode.FEC_INVALID_REQUEST) + } + // Check if the users are already friends or if a friend request has already been sent. if (isFriend(store.getState(), userId) || isToPendingRequest(store.getState(), userId)) { return buildFriendRequestErrorResponse(FriendshipErrorCode.FEC_INVALID_REQUEST) diff --git a/test/unit/friends.saga.test.ts b/test/unit/friends.saga.test.ts index a22d237c8..52e50220d 100644 --- a/test/unit/friends.saga.test.ts +++ b/test/unit/friends.saga.test.ts @@ -40,6 +40,8 @@ import { FriendshipStatus, GetFriendshipStatusRequest } from '@dcl/protocol/out-ts/decentraland/renderer/kernel_services/friends_kernel.gen' +import { SendFriendRequestPayload } from '@dcl/protocol/out-ts/decentraland/renderer/kernel_services/friend_request_kernel.gen' +import { FriendshipErrorCode } from '@dcl/protocol/out-ts/decentraland/renderer/common/friend_request_common.gen' function getMockedAvatar(userId: string, name: string): ProfileUserInfo { return { @@ -160,7 +162,8 @@ const stubClient = { return m }, getDomain: () => 'decentraland.org', - setStatus: () => Promise.resolve() + setStatus: () => Promise.resolve(), + getOwnId: () => '0xa2' } as unknown as SocialAPI const friendsFromStore: FriendsState = { @@ -564,7 +567,7 @@ describe('Friends sagas', () => { }) }) - describe('Get Friendship Status', () => { + describe('Get friendship status', () => { beforeEach(() => { const { store } = buildStore(mockStoreCalls()) globalThis.globalStore = store @@ -614,4 +617,33 @@ describe('Friends sagas', () => { }) }) }) + + describe('Send friend request via protocol', () => { + beforeEach(() => { + const { store } = buildStore(mockStoreCalls()) + globalThis.globalStore = store + }) + + afterEach(() => { + sinon.restore() + sinon.reset() + }) + + context('When the user tries to send a friend request to themself', () => { + it('Should return FriendshipStatus.FEC_INVALID_REQUEST', async () => { + const request: SendFriendRequestPayload = { + userId: '0xa2', + messageBody: 'u r so cool' + } + + const expectedResponse = { + reply: undefined, + error: FriendshipErrorCode.FEC_INVALID_REQUEST + } + + const response = await friendsSagas.requestFriendship(request) + assert.match(response, expectedResponse) + }) + }) + }) })