Skip to content

Commit 8769507

Browse files
committed
error on multiple ownership
1 parent 450c707 commit 8769507

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

lambdas/backend-api/src/__tests__/templates/infra/template-repository.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,72 @@ describe('templateRepository', () => {
16281628
});
16291629
});
16301630

1631+
describe('getUserTemplateOwner', () => {
1632+
test('gets owner field based on templateId', async () => {
1633+
const { templateRepository, mocks } = setup();
1634+
1635+
mocks.ddbDocClient.on(QueryCommand).resolves({
1636+
Items: [
1637+
{
1638+
owner: ownerWithClientPrefix,
1639+
},
1640+
],
1641+
});
1642+
1643+
const owner = await templateRepository.getUserTemplateOwner(
1644+
user,
1645+
templateId
1646+
);
1647+
1648+
expect(owner).toEqual(ownerWithClientPrefix);
1649+
1650+
expect(mocks.ddbDocClient).toHaveReceivedCommandWith(QueryCommand, {
1651+
ExpressionAttributeValues: { ':id': templateId },
1652+
IndexName: 'QueryById',
1653+
KeyConditionExpression: 'id = :id',
1654+
TableName: templatesTableName,
1655+
});
1656+
});
1657+
1658+
test('returns undefined if one template exists, but does not match user', async () => {
1659+
const { templateRepository, mocks } = setup();
1660+
1661+
mocks.ddbDocClient.on(QueryCommand).resolves({
1662+
Items: [
1663+
{
1664+
owner: 'someone-else',
1665+
},
1666+
],
1667+
});
1668+
1669+
const owner = await templateRepository.getUserTemplateOwner(
1670+
user,
1671+
'template-id'
1672+
);
1673+
1674+
expect(owner).toBeUndefined();
1675+
});
1676+
1677+
test('throws if more than one owner matches the user', async () => {
1678+
const { templateRepository, mocks } = setup();
1679+
1680+
mocks.ddbDocClient.on(QueryCommand).resolves({
1681+
Items: [
1682+
{
1683+
owner: ownerWithClientPrefix,
1684+
},
1685+
{
1686+
owner: user.userId,
1687+
},
1688+
],
1689+
});
1690+
1691+
await expect(
1692+
templateRepository.getUserTemplateOwner(user, 'template-id')
1693+
).rejects.toThrow('Unexpectedly found more than one template owner');
1694+
});
1695+
});
1696+
16311697
describe('setLetterFileVirusScanStatusForUpload', () => {
16321698
it('updates the virusScanStatus on the pdfTemplate field when the status is PASSED', async () => {
16331699
const { templateRepository, mocks } = setup();

lambdas/backend-api/src/templates/infra/template-repository.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ export class TemplateRepository {
740740
}
741741
}
742742

743-
private async getUserTemplateOwner(
743+
async getUserTemplateOwner(
744744
user: User,
745745
templateId: string
746746
): Promise<string | undefined> {
@@ -755,11 +755,17 @@ export class TemplateRepository {
755755
})
756756
);
757757

758-
return dbResponse.Items?.find(
758+
const items = dbResponse.Items?.filter(
759759
(item) =>
760760
item.owner === user.userId ||
761761
(user.clientId && item.owner === this.clientOwnerKey(user.clientId))
762-
)?.owner;
762+
);
763+
764+
if (items && items?.length > 1) {
765+
throw new Error('Unexpectedly found more than one template owner');
766+
}
767+
768+
return items?.[0]?.owner;
763769
}
764770

765771
private toDatabaseKey(templateKey: TemplateKey) {

0 commit comments

Comments
 (0)