-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(federation): manage invitations through the store
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
- Loading branch information
Showing
5 changed files
with
529 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -267,3 +267,10 @@ export const AVATAR = { | |
FULL: 512, | ||
}, | ||
} | ||
|
||
export const FEDERATION = { | ||
STATE: { | ||
PENDING: 0, | ||
ACCEPTED: 1, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* @copyright Copyright (c) 2024 Maksim Sukharev <antreesy.web@gmail.com> | ||
* | ||
* @author Maksim Sukharev <antreesy.web@gmail.com> | ||
* | ||
* @license AGPL-3.0-or-later | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
import axios from '@nextcloud/axios' | ||
import { generateOcsUrl } from '@nextcloud/router' | ||
|
||
/** | ||
* Fetches list of shares for a current user | ||
* | ||
* @param {object} [options] options; | ||
*/ | ||
const getShares = async function(options) { | ||
return axios.get(generateOcsUrl('apps/spreed/api/v1/federation/invitation', undefined, options), options) | ||
} | ||
|
||
/** | ||
* Accept an invitation by provided id. | ||
* | ||
* @param {number} id invitation id; | ||
* @param {object} [options] options; | ||
*/ | ||
const acceptShare = async function(id, options) { | ||
return axios.post(generateOcsUrl('apps/spreed/api/v1/federation/invitation/{id}', { id }, options), {}, options) | ||
} | ||
|
||
/** | ||
* Reject an invitation by provided id. | ||
* | ||
* @param {number} id invitation id; | ||
* @param {object} [options] options; | ||
*/ | ||
const rejectShare = async function(id, options) { | ||
return axios.delete(generateOcsUrl('apps/spreed/api/v1/federation/invitation/{id}', { id }, options), options) | ||
} | ||
|
||
export { | ||
getShares, | ||
acceptShare, | ||
rejectShare, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
import { setActivePinia, createPinia } from 'pinia' | ||
|
||
import { getShares, acceptShare, rejectShare } from '../../services/federationService.js' | ||
import { generateOCSErrorResponse, generateOCSResponse } from '../../test-helpers.js' | ||
import { useFederationStore } from '../federation.js' | ||
|
||
jest.mock('../../services/federationService', () => ({ | ||
getShares: jest.fn(), | ||
acceptShare: jest.fn(), | ||
rejectShare: jest.fn(), | ||
})) | ||
|
||
describe('federationStore', () => { | ||
const invites = [ | ||
{ | ||
id: 2, | ||
userId: 'user0', | ||
state: 0, | ||
localRoomId: 10, | ||
accessToken: 'ACCESS_TOKEN', | ||
remoteServerUrl: 'remote.nextcloud.com', | ||
remoteToken: 'TOKEN_2', | ||
remoteAttendeeId: 11, | ||
inviterCloudId: 'user2@remote.nextcloud.com', | ||
inviterDisplayName: 'User Two', | ||
roomName: 'Federation room 2' | ||
}, | ||
{ | ||
id: 1, | ||
userId: 'user0', | ||
state: 1, | ||
localRoomId: 9, | ||
accessToken: 'ACCESS_TOKEN', | ||
remoteServerUrl: 'remote.nextcloud.com', | ||
remoteToken: 'TOKEN_1', | ||
remoteAttendeeId: 11, | ||
inviterCloudId: 'user1@remote.nextcloud.com', | ||
inviterDisplayName: 'User One', | ||
roomName: 'Federation room 1' | ||
}, | ||
] | ||
const notifications = [ | ||
{ | ||
notificationId: 122, | ||
app: 'spreed', | ||
user: 'user0', | ||
objectType: 'remote_talk_share', | ||
objectId: '2', | ||
messageRichParameters: { | ||
user1: { | ||
type: 'user', | ||
id: 'user2', | ||
name: 'User Two', | ||
server: 'remote.nextcloud.com' | ||
}, | ||
roomName: { | ||
type: 'highlight', | ||
id: 'remote.nextcloud.com::TOKEN_2', | ||
name: 'Federation room 2' | ||
}, | ||
}, | ||
}, | ||
{ | ||
notificationId: 123, | ||
app: 'spreed', | ||
user: 'user0', | ||
objectType: 'remote_talk_share', | ||
objectId: '3', | ||
messageRichParameters: { | ||
user1: { | ||
type: 'user', | ||
id: 'user3', | ||
name: 'User Three', | ||
server: 'remote.nextcloud.com' | ||
}, | ||
roomName: { | ||
type: 'highlight', | ||
id: 'remote.nextcloud.com::TOKEN_3', | ||
name: 'Federation room 3' | ||
}, | ||
}, | ||
} | ||
] | ||
let federationStore | ||
|
||
beforeEach(async () => { | ||
setActivePinia(createPinia()) | ||
federationStore = useFederationStore() | ||
}) | ||
|
||
afterEach(async () => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('returns an empty objects when invitations are not loaded yet', async () => { | ||
// Assert: check initial state of the store | ||
expect(federationStore.pendingShares).toStrictEqual({}) | ||
expect(federationStore.acceptedShares).toStrictEqual({}) | ||
}) | ||
|
||
it('does not handle accepted invitations when missing in the store', async () => { | ||
// Act: accept invite from notification | ||
await federationStore.markInvitationAccepted(invites[0].id, {}) | ||
|
||
// Assert: check initial state of the store | ||
expect(federationStore.pendingShares).toStrictEqual({}) | ||
expect(federationStore.acceptedShares).toStrictEqual({}) | ||
}) | ||
|
||
it('processes a response from server and stores invites', async () => { | ||
// Arrange | ||
const response = generateOCSResponse({ payload: invites }) | ||
getShares.mockResolvedValueOnce(response) | ||
|
||
// Act: load invites from server | ||
await federationStore.getShares() | ||
|
||
// Assert | ||
expect(getShares).toHaveBeenCalled() | ||
expect(federationStore.pendingShares).toMatchObject({ [invites[0].id]: invites[0] }) | ||
expect(federationStore.acceptedShares).toMatchObject({ [invites[1].id]: invites[1] }) | ||
}) | ||
|
||
it('handles error in server request for getShares', async () => { | ||
// Arrange | ||
const response = generateOCSErrorResponse({ status: 404, payload: [] }) | ||
getShares.mockRejectedValueOnce(response) | ||
console.error = jest.fn() | ||
|
||
// Act | ||
await federationStore.getShares() | ||
|
||
// Assert: store hasn't changed | ||
expect(federationStore.pendingShares).toStrictEqual({}) | ||
expect(federationStore.acceptedShares).toStrictEqual({}) | ||
}) | ||
|
||
it('updates invites in the store after receiving a notification', async () => { | ||
// Arrange | ||
const response = generateOCSResponse({ payload: invites }) | ||
getShares.mockResolvedValueOnce(response) | ||
await federationStore.getShares() | ||
|
||
// Act: trigger notification handling | ||
notifications.forEach(federationStore.addInvitationFromNotification) | ||
|
||
// Assert | ||
expect(federationStore.pendingShares).toMatchObject({ | ||
[invites[0].id]: invites[0], | ||
[notifications[1].objectId]: { id: notifications[1].objectId }, | ||
}) | ||
expect(federationStore.acceptedShares).toMatchObject({ [invites[1].id]: invites[1] }) | ||
}) | ||
|
||
it('accepts invitation and modify store', async () => { | ||
// Arrange | ||
const response = generateOCSResponse({ payload: invites }) | ||
getShares.mockResolvedValueOnce(response) | ||
await federationStore.getShares() | ||
|
||
const room = { | ||
id: 10, | ||
remoteAccessToken: 'ACCESS_TOKEN', | ||
} | ||
const acceptResponse = generateOCSResponse({ payload: room }) | ||
acceptShare.mockResolvedValueOnce(acceptResponse) | ||
|
||
// Act: accept invite | ||
const conversation = await federationStore.acceptShare(invites[0].id) | ||
|
||
// Assert | ||
expect(acceptShare).toHaveBeenCalledWith(invites[0].id) | ||
expect(conversation).toMatchObject(room) | ||
expect(federationStore.pendingShares).toStrictEqual({}) | ||
expect(federationStore.acceptedShares).toMatchObject({ | ||
[invites[0].id]: { ...invites[0], state: 1 }, | ||
[invites[1].id]: invites[1], | ||
}) | ||
}) | ||
|
||
it('skip already accepted invitations', async () => { | ||
// Arrange | ||
const response = generateOCSResponse({ payload: invites }) | ||
getShares.mockResolvedValueOnce(response) | ||
await federationStore.getShares() | ||
|
||
// Act: accept invite | ||
await federationStore.acceptShare(invites[1].id) | ||
|
||
// Assert | ||
expect(acceptShare).not.toHaveBeenCalled() | ||
expect(federationStore.pendingShares).toMatchObject({ [invites[0].id]: invites[0] }) | ||
expect(federationStore.acceptedShares).toMatchObject({ [invites[1].id]: invites[1] }) | ||
}) | ||
|
||
it('handles error in server request for acceptShare', async () => { | ||
// Arrange | ||
const response = generateOCSResponse({ payload: invites }) | ||
getShares.mockResolvedValueOnce(response) | ||
await federationStore.getShares() | ||
const errorResponse = generateOCSErrorResponse({ status: 404, payload: [] }) | ||
acceptShare.mockRejectedValueOnce(errorResponse) | ||
console.error = jest.fn() | ||
|
||
// Act | ||
await federationStore.acceptShare(invites[0].id) | ||
|
||
// Assert: store hasn't changed | ||
expect(federationStore.pendingShares).toMatchObject({ [invites[0].id]: invites[0] }) | ||
expect(federationStore.acceptedShares).toMatchObject({ [invites[1].id]: invites[1] }) | ||
}) | ||
|
||
it('rejects invitation and modify store', async () => { | ||
// Arrange | ||
const response = generateOCSResponse({ payload: invites }) | ||
getShares.mockResolvedValueOnce(response) | ||
await federationStore.getShares() | ||
|
||
const rejectResponse = generateOCSResponse({ payload: [] }) | ||
rejectShare.mockResolvedValueOnce(rejectResponse) | ||
|
||
// Act: reject invite | ||
await federationStore.rejectShare(invites[0].id) | ||
|
||
// Assert | ||
expect(rejectShare).toHaveBeenCalledWith(invites[0].id) | ||
expect(federationStore.pendingShares).toStrictEqual({}) | ||
expect(federationStore.acceptedShares).toMatchObject({ [invites[1].id]: invites[1] }) | ||
}) | ||
|
||
it('skip already rejected invitations', async () => { | ||
// Arrange | ||
const response = generateOCSResponse({ payload: invites }) | ||
getShares.mockResolvedValueOnce(response) | ||
await federationStore.getShares() | ||
|
||
// Act: reject invite | ||
await federationStore.rejectShare(invites[1].id) | ||
|
||
// Assert | ||
expect(rejectShare).not.toHaveBeenCalled() | ||
expect(federationStore.pendingShares).toMatchObject({ [invites[0].id]: invites[0] }) | ||
expect(federationStore.acceptedShares).toMatchObject({ [invites[1].id]: invites[1] }) | ||
}) | ||
|
||
it('handles error in server request for rejectShare', async () => { | ||
// Arrange | ||
const response = generateOCSResponse({ payload: invites }) | ||
getShares.mockResolvedValueOnce(response) | ||
await federationStore.getShares() | ||
const errorResponse = generateOCSErrorResponse({ status: 404, payload: [] }) | ||
rejectShare.mockRejectedValueOnce(errorResponse) | ||
console.error = jest.fn() | ||
|
||
// Act | ||
await federationStore.rejectShare(invites[0].id) | ||
|
||
// Assert: store hasn't changed | ||
expect(federationStore.pendingShares).toMatchObject({ [invites[0].id]: invites[0] }) | ||
expect(federationStore.acceptedShares).toMatchObject({ [invites[1].id]: invites[1] }) | ||
}) | ||
}) |
Oops, something went wrong.