signal-desktop/ts/test-both/util/getGroupMemberships_test.ts

280 lines
7.3 KiB
TypeScript
Raw Normal View History

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import type { ConversationType } from '../../state/ducks/conversations';
import { generateAci, normalizeAci } from '../../types/ServiceId';
import type { ServiceIdString } from '../../types/ServiceId';
2021-10-26 22:59:08 +00:00
import { getDefaultConversationWithUuid } from '../helpers/getDefaultConversation';
import { getGroupMemberships } from '../../util/getGroupMemberships';
describe('getGroupMemberships', () => {
2021-10-26 22:59:08 +00:00
const normalConversation1 = getDefaultConversationWithUuid();
const normalConversation2 = getDefaultConversationWithUuid();
const unregisteredConversation = getDefaultConversationWithUuid({
discoveredUnregisteredAt: Date.now(),
});
function getConversationByServiceId(
serviceId: ServiceIdString
2021-10-26 22:59:08 +00:00
): undefined | ConversationType {
return [
normalConversation1,
normalConversation2,
unregisteredConversation,
].find(conversation => conversation.uuid === serviceId);
}
describe('memberships', () => {
it('returns an empty array if passed undefined', () => {
const conversation = {};
2021-11-11 22:43:05 +00:00
const result = getGroupMemberships(
conversation,
getConversationByServiceId
2021-11-11 22:43:05 +00:00
).memberships;
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,
getConversationByServiceId
2021-11-11 22:43:05 +00:00
).memberships;
assert.isEmpty(result);
});
it("filters out conversation IDs that don't exist", () => {
const conversation = {
memberships: [
{
uuid: generateAci(),
isAdmin: true,
},
],
};
2021-11-11 22:43:05 +00:00
const result = getGroupMemberships(
conversation,
getConversationByServiceId
2021-11-11 22:43:05 +00:00
).memberships;
assert.isEmpty(result);
});
it('does not filter out unregistered conversations', () => {
const conversation = {
memberships: [
{
uuid: normalizeAci(unregisteredConversation.uuid, 'test'),
isAdmin: true,
},
],
};
2021-11-11 22:43:05 +00:00
const result = getGroupMemberships(
conversation,
getConversationByServiceId
2021-11-11 22:43:05 +00:00
).memberships;
assert.lengthOf(result, 1);
assert.deepEqual(result[0], {
isAdmin: true,
member: unregisteredConversation,
});
});
it('hydrates memberships', () => {
const conversation = {
memberships: [
{
uuid: normalizeAci(normalConversation2.uuid, 'test'),
isAdmin: false,
},
{
uuid: normalizeAci(normalConversation1.uuid, 'test'),
isAdmin: true,
},
],
};
2021-11-11 22:43:05 +00:00
const result = getGroupMemberships(
conversation,
getConversationByServiceId
2021-11-11 22:43:05 +00:00
).memberships;
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,
getConversationByServiceId
2021-11-11 22:43:05 +00:00
).pendingApprovalMemberships;
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,
getConversationByServiceId
2021-11-11 22:43:05 +00:00
).pendingApprovalMemberships;
assert.isEmpty(result);
});
it("filters out conversation IDs that don't exist", () => {
const conversation = {
pendingApprovalMemberships: [{ uuid: generateAci() }],
};
2021-11-11 22:43:05 +00:00
const result = getGroupMemberships(
conversation,
getConversationByServiceId
2021-11-11 22:43:05 +00:00
).pendingApprovalMemberships;
assert.isEmpty(result);
});
it('filters out unregistered conversations', () => {
const conversation = {
pendingApprovalMemberships: [
{ uuid: normalizeAci(unregisteredConversation.uuid, 'test') },
],
};
2021-11-11 22:43:05 +00:00
const result = getGroupMemberships(
conversation,
getConversationByServiceId
2021-11-11 22:43:05 +00:00
).pendingApprovalMemberships;
assert.isEmpty(result);
});
it('hydrates pending-approval memberships', () => {
const conversation = {
pendingApprovalMemberships: [
{ uuid: normalizeAci(normalConversation2.uuid, 'test') },
{ uuid: normalizeAci(normalConversation1.uuid, 'test') },
],
};
2021-11-11 22:43:05 +00:00
const result = getGroupMemberships(
conversation,
getConversationByServiceId
2021-11-11 22:43:05 +00:00
).pendingApprovalMemberships;
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,
getConversationByServiceId
2021-11-11 22:43:05 +00:00
).pendingMemberships;
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,
getConversationByServiceId
2021-11-11 22:43:05 +00:00
).pendingMemberships;
assert.isEmpty(result);
});
it("filters out conversation IDs that don't exist", () => {
const conversation = {
pendingMemberships: [
2021-10-26 22:59:08 +00:00
{
uuid: generateAci(),
addedByUserId: normalizeAci(normalConversation1.uuid, 'test'),
2021-10-26 22:59:08 +00:00
},
],
};
2021-11-11 22:43:05 +00:00
const result = getGroupMemberships(
conversation,
getConversationByServiceId
2021-11-11 22:43:05 +00:00
).pendingMemberships;
assert.isEmpty(result);
});
it('filters out unregistered conversations', () => {
const conversation = {
pendingMemberships: [
{
2021-10-26 22:59:08 +00:00
uuid: unregisteredConversation.uuid,
addedByUserId: normalizeAci(normalConversation1.uuid, 'test'),
},
],
};
2021-11-11 22:43:05 +00:00
const result = getGroupMemberships(
conversation,
getConversationByServiceId
2021-11-11 22:43:05 +00:00
).pendingMemberships;
assert.isEmpty(result);
});
it('hydrates pending memberships', () => {
const abc = generateAci();
const xyz = generateAci();
2021-10-26 22:59:08 +00:00
const conversation = {
pendingMemberships: [
2021-10-26 22:59:08 +00:00
{ uuid: normalConversation2.uuid, addedByUserId: abc },
{ uuid: normalConversation1.uuid, addedByUserId: xyz },
],
};
2021-11-11 22:43:05 +00:00
const result = getGroupMemberships(
conversation,
getConversationByServiceId
2021-11-11 22:43:05 +00:00
).pendingMemberships;
assert.lengthOf(result, 2);
assert.deepEqual(result[0], {
member: normalConversation2,
2021-10-26 22:59:08 +00:00
metadata: { addedByUserId: abc },
});
assert.deepEqual(result[1], {
member: normalConversation1,
2021-10-26 22:59:08 +00:00
metadata: { addedByUserId: xyz },
});
});
});
});