Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw if rejecting an invite that's already being accepted #528

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/invite-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ export class InviteApi extends TypedEmitter {
const pendingInvite = this.#pendingInvites.getByInviteId(inviteId)
assert(!!pendingInvite, `Cannot find invite ${inviteId}`)

const { peerId, invite } = pendingInvite
const { peerId, invite, isAccepting } = pendingInvite

assert(!isAccepting, `Cannot reject ${inviteIdString}`)

this.rpc
.sendInviteResponse(peerId, {
Expand Down
51 changes: 51 additions & 0 deletions tests/invite-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,57 @@ test('throws when quickly double-accepting the same invite', async (t) => {
t.is(inviteResponseCount, 1, 'only sent one invite response')
})

test('throws when quickly accepting and then rejecting an invite', async (t) => {
const {
rpc,
invitorPeerId,
invite,
inviteExternal,
projectKey,
encryptionKeys,
} = setup()

/** @type {Array<Buffer>} */
const projectKeysFound = []
const inviteApi = new InviteApi({
rpc,
queries: {
isMember: () => false,
addProject: async ({ projectKey }) => {
projectKeysFound.push(projectKey)
},
},
})

const inviteReceivedPromise = once(inviteApi, 'invite-received')

// Invitor: send the invite

rpc.emit('invite', invitorPeerId, invite)

// Invitee: receive the invite

await inviteReceivedPromise

// Invitor: prepare to share project join details upon acceptance

rpc.on('invite-response', () => {
rpc.emit('got-project-details', invitorPeerId, {
inviteId: invite.inviteId,
projectKey,
encryptionKeys,
})
})

// Invitee: accept then reject

const acceptPromise = inviteApi.accept(inviteExternal)

t.exception(() => inviteApi.reject(inviteExternal), 'reject fails')

await acceptPromise
})

test('throws when quickly accepting two invites for the same project', async (t) => {
const {
rpc,
Expand Down
Loading