2021-05-13 14:47:30 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationType } from '../../state/ducks/conversations';
|
2023-09-14 17:04:48 +00:00
|
|
|
import { generateAci } from '../../types/ServiceId';
|
|
|
|
import { normalizeAci } from '../../util/normalizeAci';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { ServiceIdString } from '../../types/ServiceId';
|
2023-08-16 20:54:39 +00:00
|
|
|
import { getDefaultConversationWithServiceId } from '../helpers/getDefaultConversation';
|
2021-05-13 14:47:30 +00:00
|
|
|
|
|
|
|
import { getGroupMemberships } from '../../util/getGroupMemberships';
|
|
|
|
|
|
|
|
describe('getGroupMemberships', () => {
|
2023-08-16 20:54:39 +00:00
|
|
|
const normalConversation1 = getDefaultConversationWithServiceId();
|
|
|
|
const normalConversation2 = getDefaultConversationWithServiceId();
|
|
|
|
const unregisteredConversation = getDefaultConversationWithServiceId({
|
2021-05-13 14:47:30 +00:00
|
|
|
discoveredUnregisteredAt: Date.now(),
|
|
|
|
});
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
function getConversationByServiceId(
|
|
|
|
serviceId: ServiceIdString
|
2021-10-26 22:59:08 +00:00
|
|
|
): undefined | ConversationType {
|
2021-05-13 14:47:30 +00:00
|
|
|
return [
|
|
|
|
normalConversation1,
|
|
|
|
normalConversation2,
|
|
|
|
unregisteredConversation,
|
2023-08-16 20:54:39 +00:00
|
|
|
].find(conversation => conversation.serviceId === serviceId);
|
2021-05-13 14:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('memberships', () => {
|
|
|
|
it('returns an empty array if passed undefined', () => {
|
|
|
|
const conversation = {};
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const result = getGroupMemberships(
|
|
|
|
conversation,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId
|
2021-11-11 22:43:05 +00:00
|
|
|
).memberships;
|
2021-05-13 14:47:30 +00:00
|
|
|
|
|
|
|
assert.isEmpty(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns an empty array if passed an empty array', () => {
|
|
|
|
const conversation = { memberships: [] };
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const result = getGroupMemberships(
|
|
|
|
conversation,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId
|
2021-11-11 22:43:05 +00:00
|
|
|
).memberships;
|
2021-05-13 14:47:30 +00:00
|
|
|
|
|
|
|
assert.isEmpty(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("filters out conversation IDs that don't exist", () => {
|
|
|
|
const conversation = {
|
|
|
|
memberships: [
|
|
|
|
{
|
2023-08-16 20:54:39 +00:00
|
|
|
aci: generateAci(),
|
2021-05-13 14:47:30 +00:00
|
|
|
isAdmin: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const result = getGroupMemberships(
|
|
|
|
conversation,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId
|
2021-11-11 22:43:05 +00:00
|
|
|
).memberships;
|
2021-05-13 14:47:30 +00:00
|
|
|
|
|
|
|
assert.isEmpty(result);
|
|
|
|
});
|
|
|
|
|
2021-07-28 18:52:49 +00:00
|
|
|
it('does not filter out unregistered conversations', () => {
|
2021-05-13 14:47:30 +00:00
|
|
|
const conversation = {
|
|
|
|
memberships: [
|
|
|
|
{
|
2023-08-16 20:54:39 +00:00
|
|
|
aci: normalizeAci(unregisteredConversation.serviceId, 'test'),
|
2021-05-13 14:47:30 +00:00
|
|
|
isAdmin: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const result = getGroupMemberships(
|
|
|
|
conversation,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId
|
2021-11-11 22:43:05 +00:00
|
|
|
).memberships;
|
2021-05-13 14:47:30 +00:00
|
|
|
|
2021-07-28 18:52:49 +00:00
|
|
|
assert.lengthOf(result, 1);
|
|
|
|
assert.deepEqual(result[0], {
|
|
|
|
isAdmin: true,
|
|
|
|
member: unregisteredConversation,
|
|
|
|
});
|
2021-05-13 14:47:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('hydrates memberships', () => {
|
|
|
|
const conversation = {
|
|
|
|
memberships: [
|
|
|
|
{
|
2023-08-16 20:54:39 +00:00
|
|
|
aci: normalizeAci(normalConversation2.serviceId, 'test'),
|
2021-05-13 14:47:30 +00:00
|
|
|
isAdmin: false,
|
|
|
|
},
|
|
|
|
{
|
2023-08-16 20:54:39 +00:00
|
|
|
aci: normalizeAci(normalConversation1.serviceId, 'test'),
|
2021-05-13 14:47:30 +00:00
|
|
|
isAdmin: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const result = getGroupMemberships(
|
|
|
|
conversation,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId
|
2021-11-11 22:43:05 +00:00
|
|
|
).memberships;
|
2021-05-13 14:47:30 +00:00
|
|
|
|
|
|
|
assert.lengthOf(result, 2);
|
|
|
|
assert.deepEqual(result[0], {
|
|
|
|
isAdmin: false,
|
|
|
|
member: normalConversation2,
|
|
|
|
});
|
|
|
|
assert.deepEqual(result[1], {
|
|
|
|
isAdmin: true,
|
|
|
|
member: normalConversation1,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('pendingApprovalMemberships', () => {
|
|
|
|
it('returns an empty array if passed undefined', () => {
|
|
|
|
const conversation = {};
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const result = getGroupMemberships(
|
|
|
|
conversation,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId
|
2021-11-11 22:43:05 +00:00
|
|
|
).pendingApprovalMemberships;
|
2021-05-13 14:47:30 +00:00
|
|
|
|
|
|
|
assert.isEmpty(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns an empty array if passed an empty array', () => {
|
|
|
|
const conversation = { pendingApprovalMemberships: [] };
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const result = getGroupMemberships(
|
|
|
|
conversation,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId
|
2021-11-11 22:43:05 +00:00
|
|
|
).pendingApprovalMemberships;
|
2021-05-13 14:47:30 +00:00
|
|
|
|
|
|
|
assert.isEmpty(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("filters out conversation IDs that don't exist", () => {
|
|
|
|
const conversation = {
|
2023-08-16 20:54:39 +00:00
|
|
|
pendingApprovalMemberships: [{ aci: generateAci() }],
|
2021-05-13 14:47:30 +00:00
|
|
|
};
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const result = getGroupMemberships(
|
|
|
|
conversation,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId
|
2021-11-11 22:43:05 +00:00
|
|
|
).pendingApprovalMemberships;
|
2021-05-13 14:47:30 +00:00
|
|
|
|
|
|
|
assert.isEmpty(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('filters out unregistered conversations', () => {
|
|
|
|
const conversation = {
|
2023-08-10 16:43:33 +00:00
|
|
|
pendingApprovalMemberships: [
|
2023-08-16 20:54:39 +00:00
|
|
|
{ aci: normalizeAci(unregisteredConversation.serviceId, 'test') },
|
2023-08-10 16:43:33 +00:00
|
|
|
],
|
2021-05-13 14:47:30 +00:00
|
|
|
};
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const result = getGroupMemberships(
|
|
|
|
conversation,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId
|
2021-11-11 22:43:05 +00:00
|
|
|
).pendingApprovalMemberships;
|
2021-05-13 14:47:30 +00:00
|
|
|
|
|
|
|
assert.isEmpty(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('hydrates pending-approval memberships', () => {
|
|
|
|
const conversation = {
|
|
|
|
pendingApprovalMemberships: [
|
2023-08-16 20:54:39 +00:00
|
|
|
{ aci: normalizeAci(normalConversation2.serviceId, 'test') },
|
|
|
|
{ aci: normalizeAci(normalConversation1.serviceId, 'test') },
|
2021-05-13 14:47:30 +00:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const result = getGroupMemberships(
|
|
|
|
conversation,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId
|
2021-11-11 22:43:05 +00:00
|
|
|
).pendingApprovalMemberships;
|
2021-05-13 14:47:30 +00:00
|
|
|
|
|
|
|
assert.lengthOf(result, 2);
|
|
|
|
assert.deepEqual(result[0], { member: normalConversation2 });
|
|
|
|
assert.deepEqual(result[1], { member: normalConversation1 });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('pendingMemberships', () => {
|
|
|
|
it('returns an empty array if passed undefined', () => {
|
|
|
|
const conversation = {};
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const result = getGroupMemberships(
|
|
|
|
conversation,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId
|
2021-11-11 22:43:05 +00:00
|
|
|
).pendingMemberships;
|
2021-05-13 14:47:30 +00:00
|
|
|
|
|
|
|
assert.isEmpty(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns an empty array if passed an empty array', () => {
|
|
|
|
const conversation = { pendingMemberships: [] };
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const result = getGroupMemberships(
|
|
|
|
conversation,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId
|
2021-11-11 22:43:05 +00:00
|
|
|
).pendingMemberships;
|
2021-05-13 14:47:30 +00:00
|
|
|
|
|
|
|
assert.isEmpty(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("filters out conversation IDs that don't exist", () => {
|
|
|
|
const conversation = {
|
|
|
|
pendingMemberships: [
|
2021-10-26 22:59:08 +00:00
|
|
|
{
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId: generateAci(),
|
|
|
|
addedByUserId: normalizeAci(normalConversation1.serviceId, 'test'),
|
2021-10-26 22:59:08 +00:00
|
|
|
},
|
2021-05-13 14:47:30 +00:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const result = getGroupMemberships(
|
|
|
|
conversation,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId
|
2021-11-11 22:43:05 +00:00
|
|
|
).pendingMemberships;
|
2021-05-13 14:47:30 +00:00
|
|
|
|
|
|
|
assert.isEmpty(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('filters out unregistered conversations', () => {
|
|
|
|
const conversation = {
|
|
|
|
pendingMemberships: [
|
|
|
|
{
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId: unregisteredConversation.serviceId,
|
|
|
|
addedByUserId: normalizeAci(normalConversation1.serviceId, 'test'),
|
2021-05-13 14:47:30 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const result = getGroupMemberships(
|
|
|
|
conversation,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId
|
2021-11-11 22:43:05 +00:00
|
|
|
).pendingMemberships;
|
2021-05-13 14:47:30 +00:00
|
|
|
|
|
|
|
assert.isEmpty(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('hydrates pending memberships', () => {
|
2023-08-10 16:43:33 +00:00
|
|
|
const abc = generateAci();
|
|
|
|
const xyz = generateAci();
|
2021-10-26 22:59:08 +00:00
|
|
|
|
2021-05-13 14:47:30 +00:00
|
|
|
const conversation = {
|
|
|
|
pendingMemberships: [
|
2023-08-16 20:54:39 +00:00
|
|
|
{ serviceId: normalConversation2.serviceId, addedByUserId: abc },
|
|
|
|
{ serviceId: normalConversation1.serviceId, addedByUserId: xyz },
|
2021-05-13 14:47:30 +00:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const result = getGroupMemberships(
|
|
|
|
conversation,
|
2023-08-10 16:43:33 +00:00
|
|
|
getConversationByServiceId
|
2021-11-11 22:43:05 +00:00
|
|
|
).pendingMemberships;
|
2021-05-13 14:47:30 +00:00
|
|
|
|
|
|
|
assert.lengthOf(result, 2);
|
|
|
|
assert.deepEqual(result[0], {
|
|
|
|
member: normalConversation2,
|
2021-10-26 22:59:08 +00:00
|
|
|
metadata: { addedByUserId: abc },
|
2021-05-13 14:47:30 +00:00
|
|
|
});
|
|
|
|
assert.deepEqual(result[1], {
|
|
|
|
member: normalConversation1,
|
2021-10-26 22:59:08 +00:00
|
|
|
metadata: { addedByUserId: xyz },
|
2021-05-13 14:47:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|