Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
feat: check if the user tries to send a friend request to themself (#836
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Julieta11 authored Jan 10, 2023
1 parent baf7a4e commit 03066cf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/shared/friends/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 34 additions & 2 deletions test/unit/friends.saga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -564,7 +567,7 @@ describe('Friends sagas', () => {
})
})

describe('Get Friendship Status', () => {
describe('Get friendship status', () => {
beforeEach(() => {
const { store } = buildStore(mockStoreCalls())
globalThis.globalStore = store
Expand Down Expand Up @@ -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)
})
})
})
})

0 comments on commit 03066cf

Please sign in to comment.