Migration and data access functions for stories

This commit is contained in:
Scott Nonnenberg 2021-12-08 11:52:46 -08:00 committed by GitHub
parent 9f4a01c535
commit fdc9885baa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 3428 additions and 202 deletions

View file

@ -0,0 +1,126 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import dataInterface from '../../sql/Client';
import { getRandomBytes } from '../../Crypto';
import { UUID } from '../../types/UUID';
import type { UUIDStringType } from '../../types/UUID';
import type { StoryDistributionWithMembersType } from '../../sql/Interface';
const {
_deleteAllStoryDistributions,
_getAllStoryDistributionMembers,
_getAllStoryDistributions,
createNewStoryDistribution,
deleteStoryDistribution,
getAllStoryDistributionsWithMembers,
modifyStoryDistributionMembers,
} = dataInterface;
function getUuid(): UUIDStringType {
return UUID.generate().toString();
}
describe('sql/storyDistribution', () => {
beforeEach(async () => {
await _deleteAllStoryDistributions();
});
it('roundtrips with create/fetch/delete', async () => {
const list: StoryDistributionWithMembersType = {
id: getUuid(),
name: 'My Story',
avatarUrlPath: getUuid(),
avatarKey: getRandomBytes(128),
members: [getUuid(), getUuid()],
senderKeyInfo: {
createdAtDate: Date.now(),
distributionId: getUuid(),
memberDevices: [],
},
};
await createNewStoryDistribution(list);
assert.lengthOf(await _getAllStoryDistributions(), 1);
assert.lengthOf(await _getAllStoryDistributionMembers(), 2);
const allHydratedLists = await getAllStoryDistributionsWithMembers();
assert.lengthOf(allHydratedLists, 1);
assert.deepEqual(allHydratedLists[0], list);
await deleteStoryDistribution(list.id);
assert.lengthOf(await _getAllStoryDistributions(), 0);
assert.lengthOf(await _getAllStoryDistributionMembers(), 0);
assert.lengthOf(await getAllStoryDistributionsWithMembers(), 0);
});
it('adds and removes with modifyStoryDistributionMembers', async () => {
const UUID_1 = getUuid();
const UUID_2 = getUuid();
const UUID_3 = getUuid();
const UUID_4 = getUuid();
const list: StoryDistributionWithMembersType = {
id: getUuid(),
name: 'My Story',
avatarUrlPath: getUuid(),
avatarKey: getRandomBytes(128),
members: [UUID_1, UUID_2],
senderKeyInfo: {
createdAtDate: Date.now(),
distributionId: getUuid(),
memberDevices: [],
},
};
await createNewStoryDistribution(list);
assert.lengthOf(await _getAllStoryDistributions(), 1);
assert.lengthOf(await _getAllStoryDistributionMembers(), 2);
await modifyStoryDistributionMembers(list.id, {
toAdd: [UUID_3, UUID_4],
toRemove: [UUID_1],
});
assert.lengthOf(await _getAllStoryDistributions(), 1);
assert.lengthOf(await _getAllStoryDistributionMembers(), 3);
const allHydratedLists = await getAllStoryDistributionsWithMembers();
assert.lengthOf(allHydratedLists, 1);
assert.deepEqual(allHydratedLists[0], {
...list,
members: [UUID_2, UUID_3, UUID_4],
});
});
it('eliminates duplicates without complaint in createNewStoryDistribution', async () => {
const UUID_1 = getUuid();
const UUID_2 = getUuid();
const list: StoryDistributionWithMembersType = {
id: getUuid(),
name: 'My Story',
avatarUrlPath: getUuid(),
avatarKey: getRandomBytes(128),
members: [UUID_1, UUID_1, UUID_2],
senderKeyInfo: {
createdAtDate: Date.now(),
distributionId: getUuid(),
memberDevices: [],
},
};
await createNewStoryDistribution(list);
assert.lengthOf(await _getAllStoryDistributions(), 1);
assert.lengthOf(await _getAllStoryDistributionMembers(), 2);
const allHydratedLists = await getAllStoryDistributionsWithMembers();
assert.lengthOf(allHydratedLists, 1);
assert.deepEqual(allHydratedLists[0].members, [UUID_1, UUID_2]);
});
});