2023-05-17 19:07:17 +02:00
|
|
|
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
2024-03-20 23:53:07 +01:00
|
|
|
import { BulkIdErrorReason } from 'src/dtos/asset-ids.response.dto';
|
|
|
|
import { PersonResponseDto, mapFaces, mapPerson } from 'src/dtos/person.dto';
|
2024-03-20 22:02:51 +01:00
|
|
|
import { AssetFaceEntity } from 'src/entities/asset-face.entity';
|
|
|
|
import { Colorspace, SystemConfigKey } from 'src/entities/system-config.entity';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IAssetRepository, WithoutProperty } from 'src/interfaces/asset.interface';
|
|
|
|
import { ICryptoRepository } from 'src/interfaces/crypto.interface';
|
|
|
|
import { IJobRepository, JobName, JobStatus } from 'src/interfaces/job.interface';
|
2024-04-16 23:30:31 +02:00
|
|
|
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IMachineLearningRepository } from 'src/interfaces/machine-learning.interface';
|
|
|
|
import { IMediaRepository } from 'src/interfaces/media.interface';
|
|
|
|
import { IMoveRepository } from 'src/interfaces/move.interface';
|
|
|
|
import { IPersonRepository } from 'src/interfaces/person.interface';
|
|
|
|
import { FaceSearchResult, ISearchRepository } from 'src/interfaces/search.interface';
|
|
|
|
import { IStorageRepository } from 'src/interfaces/storage.interface';
|
|
|
|
import { ISystemConfigRepository } from 'src/interfaces/system-config.interface';
|
2024-03-21 00:07:30 +01:00
|
|
|
import { PersonService } from 'src/services/person.service';
|
2024-03-21 04:15:09 +01:00
|
|
|
import { CacheControl, ImmichFileResponse } from 'src/utils/file';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { assetStub } from 'test/fixtures/asset.stub';
|
|
|
|
import { authStub } from 'test/fixtures/auth.stub';
|
|
|
|
import { faceStub } from 'test/fixtures/face.stub';
|
|
|
|
import { personStub } from 'test/fixtures/person.stub';
|
|
|
|
import { IAccessRepositoryMock, newAccessRepositoryMock } from 'test/repositories/access.repository.mock';
|
|
|
|
import { newAssetRepositoryMock } from 'test/repositories/asset.repository.mock';
|
|
|
|
import { newCryptoRepositoryMock } from 'test/repositories/crypto.repository.mock';
|
|
|
|
import { newJobRepositoryMock } from 'test/repositories/job.repository.mock';
|
2024-04-16 23:30:31 +02:00
|
|
|
import { newLoggerRepositoryMock } from 'test/repositories/logger.repository.mock';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { newMachineLearningRepositoryMock } from 'test/repositories/machine-learning.repository.mock';
|
|
|
|
import { newMediaRepositoryMock } from 'test/repositories/media.repository.mock';
|
|
|
|
import { newMoveRepositoryMock } from 'test/repositories/move.repository.mock';
|
|
|
|
import { newPersonRepositoryMock } from 'test/repositories/person.repository.mock';
|
|
|
|
import { newSearchRepositoryMock } from 'test/repositories/search.repository.mock';
|
|
|
|
import { newStorageRepositoryMock } from 'test/repositories/storage.repository.mock';
|
|
|
|
import { newSystemConfigRepositoryMock } from 'test/repositories/system-config.repository.mock';
|
2024-01-18 06:08:48 +01:00
|
|
|
import { IsNull } from 'typeorm';
|
2024-04-16 16:44:45 +02:00
|
|
|
import { Mocked } from 'vitest';
|
2023-05-17 19:07:17 +02:00
|
|
|
|
|
|
|
const responseDto: PersonResponseDto = {
|
|
|
|
id: 'person-1',
|
|
|
|
name: 'Person 1',
|
2023-08-18 22:10:29 +02:00
|
|
|
birthDate: null,
|
2023-07-10 19:56:45 +02:00
|
|
|
thumbnailPath: '/path/to/thumbnail.jpg',
|
2023-07-18 20:09:43 +02:00
|
|
|
isHidden: false,
|
2023-05-17 19:07:17 +02:00
|
|
|
};
|
|
|
|
|
2023-10-24 17:53:49 +02:00
|
|
|
const statistics = { assets: 3 };
|
|
|
|
|
2023-09-27 22:46:46 +02:00
|
|
|
const croppedFace = Buffer.from('Cropped Face');
|
|
|
|
|
|
|
|
const detectFaceMock = {
|
|
|
|
assetId: 'asset-1',
|
|
|
|
personId: 'person-1',
|
|
|
|
boundingBox: {
|
|
|
|
x1: 100,
|
|
|
|
y1: 100,
|
|
|
|
x2: 200,
|
|
|
|
y2: 200,
|
|
|
|
},
|
|
|
|
imageHeight: 500,
|
|
|
|
imageWidth: 400,
|
|
|
|
embedding: [1, 2, 3, 4],
|
|
|
|
score: 0.2,
|
|
|
|
};
|
|
|
|
|
2023-05-17 19:07:17 +02:00
|
|
|
describe(PersonService.name, () => {
|
2023-09-18 23:22:44 +02:00
|
|
|
let accessMock: IAccessRepositoryMock;
|
2024-04-16 16:44:45 +02:00
|
|
|
let assetMock: Mocked<IAssetRepository>;
|
|
|
|
let configMock: Mocked<ISystemConfigRepository>;
|
|
|
|
let jobMock: Mocked<IJobRepository>;
|
|
|
|
let machineLearningMock: Mocked<IMachineLearningRepository>;
|
|
|
|
let mediaMock: Mocked<IMediaRepository>;
|
|
|
|
let moveMock: Mocked<IMoveRepository>;
|
|
|
|
let personMock: Mocked<IPersonRepository>;
|
|
|
|
let storageMock: Mocked<IStorageRepository>;
|
|
|
|
let searchMock: Mocked<ISearchRepository>;
|
|
|
|
let cryptoMock: Mocked<ICryptoRepository>;
|
2024-04-16 23:30:31 +02:00
|
|
|
let loggerMock: Mocked<ILoggerRepository>;
|
2023-09-18 23:22:44 +02:00
|
|
|
let sut: PersonService;
|
2023-05-17 19:07:17 +02:00
|
|
|
|
2024-03-05 23:23:06 +01:00
|
|
|
beforeEach(() => {
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock = newAccessRepositoryMock();
|
2023-09-27 22:46:46 +02:00
|
|
|
assetMock = newAssetRepositoryMock();
|
2023-09-18 06:05:35 +02:00
|
|
|
configMock = newSystemConfigRepositoryMock();
|
2023-05-17 19:07:17 +02:00
|
|
|
jobMock = newJobRepositoryMock();
|
2023-09-27 22:46:46 +02:00
|
|
|
machineLearningMock = newMachineLearningRepositoryMock();
|
2023-10-11 04:14:44 +02:00
|
|
|
moveMock = newMoveRepositoryMock();
|
2023-09-27 22:46:46 +02:00
|
|
|
mediaMock = newMediaRepositoryMock();
|
2023-09-25 17:07:21 +02:00
|
|
|
personMock = newPersonRepositoryMock();
|
|
|
|
storageMock = newStorageRepositoryMock();
|
2024-02-13 02:50:47 +01:00
|
|
|
searchMock = newSearchRepositoryMock();
|
2023-12-29 19:41:33 +01:00
|
|
|
cryptoMock = newCryptoRepositoryMock();
|
2024-04-16 23:30:31 +02:00
|
|
|
loggerMock = newLoggerRepositoryMock();
|
2023-09-27 22:46:46 +02:00
|
|
|
sut = new PersonService(
|
|
|
|
accessMock,
|
|
|
|
assetMock,
|
|
|
|
machineLearningMock,
|
2023-10-11 04:14:44 +02:00
|
|
|
moveMock,
|
2023-09-27 22:46:46 +02:00
|
|
|
mediaMock,
|
|
|
|
personMock,
|
|
|
|
configMock,
|
|
|
|
storageMock,
|
|
|
|
jobMock,
|
2024-02-13 02:50:47 +01:00
|
|
|
searchMock,
|
2023-12-29 19:41:33 +01:00
|
|
|
cryptoMock,
|
2024-04-16 23:30:31 +02:00
|
|
|
loggerMock,
|
2023-09-27 22:46:46 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
mediaMock.crop.mockResolvedValue(croppedFace);
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be defined', () => {
|
|
|
|
expect(sut).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getAll', () => {
|
2023-07-18 20:09:43 +02:00
|
|
|
it('should get all hidden and visible people with thumbnails', async () => {
|
2023-09-08 08:49:43 +02:00
|
|
|
personMock.getAllForUser.mockResolvedValue([personStub.withName, personStub.hidden]);
|
2024-02-21 23:03:45 +01:00
|
|
|
personMock.getNumberOfPeople.mockResolvedValue({ total: 2, hidden: 1 });
|
2023-07-18 20:09:43 +02:00
|
|
|
await expect(sut.getAll(authStub.admin, { withHidden: true })).resolves.toEqual({
|
|
|
|
total: 2,
|
2024-02-21 23:03:45 +01:00
|
|
|
hidden: 1,
|
2023-07-18 20:09:43 +02:00
|
|
|
people: [
|
|
|
|
responseDto,
|
|
|
|
{
|
|
|
|
id: 'person-1',
|
|
|
|
name: '',
|
2023-08-18 22:10:29 +02:00
|
|
|
birthDate: null,
|
2023-07-18 20:09:43 +02:00
|
|
|
thumbnailPath: '/path/to/thumbnail.jpg',
|
|
|
|
isHidden: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(personMock.getAllForUser).toHaveBeenCalledWith(authStub.admin.user.id, {
|
2024-01-18 06:08:48 +01:00
|
|
|
minimumFaceCount: 3,
|
2023-09-08 08:49:43 +02:00
|
|
|
withHidden: true,
|
|
|
|
});
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getById', () => {
|
2023-09-18 23:22:44 +02:00
|
|
|
it('should require person.read permission', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.withName);
|
|
|
|
await expect(sut.getById(authStub.admin, 'person-1')).rejects.toBeInstanceOf(BadRequestException);
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-09-18 23:22:44 +02:00
|
|
|
});
|
|
|
|
|
2023-05-17 19:07:17 +02:00
|
|
|
it('should throw a bad request when person is not found', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(null);
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set(['person-1']));
|
2023-05-17 19:07:17 +02:00
|
|
|
await expect(sut.getById(authStub.admin, 'person-1')).rejects.toBeInstanceOf(BadRequestException);
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should get a person by id', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.withName);
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set(['person-1']));
|
2023-05-17 19:07:17 +02:00
|
|
|
await expect(sut.getById(authStub.admin, 'person-1')).resolves.toEqual(responseDto);
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(personMock.getById).toHaveBeenCalledWith('person-1');
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getThumbnail', () => {
|
2023-09-18 23:22:44 +02:00
|
|
|
it('should require person.read permission', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.noName);
|
|
|
|
await expect(sut.getThumbnail(authStub.admin, 'person-1')).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
expect(storageMock.createReadStream).not.toHaveBeenCalled();
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-09-18 23:22:44 +02:00
|
|
|
});
|
|
|
|
|
2023-05-17 19:07:17 +02:00
|
|
|
it('should throw an error when personId is invalid', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(null);
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set(['person-1']));
|
2023-05-17 19:07:17 +02:00
|
|
|
await expect(sut.getThumbnail(authStub.admin, 'person-1')).rejects.toBeInstanceOf(NotFoundException);
|
|
|
|
expect(storageMock.createReadStream).not.toHaveBeenCalled();
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when person has no thumbnail', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.noThumbnail);
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set(['person-1']));
|
2023-05-17 19:07:17 +02:00
|
|
|
await expect(sut.getThumbnail(authStub.admin, 'person-1')).rejects.toBeInstanceOf(NotFoundException);
|
|
|
|
expect(storageMock.createReadStream).not.toHaveBeenCalled();
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should serve the thumbnail', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.noName);
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set(['person-1']));
|
2023-12-12 15:58:25 +01:00
|
|
|
await expect(sut.getThumbnail(authStub.admin, 'person-1')).resolves.toEqual(
|
|
|
|
new ImmichFileResponse({
|
|
|
|
path: '/path/to/thumbnail.jpg',
|
|
|
|
contentType: 'image/jpeg',
|
2023-12-18 17:33:46 +01:00
|
|
|
cacheControl: CacheControl.PRIVATE_WITHOUT_CACHE,
|
2023-12-12 15:58:25 +01:00
|
|
|
}),
|
|
|
|
);
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getAssets', () => {
|
2023-09-18 23:22:44 +02:00
|
|
|
it('should require person.read permission', async () => {
|
|
|
|
personMock.getAssets.mockResolvedValue([assetStub.image, assetStub.video]);
|
|
|
|
await expect(sut.getAssets(authStub.admin, 'person-1')).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
expect(personMock.getAssets).not.toHaveBeenCalled();
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-09-18 23:22:44 +02:00
|
|
|
});
|
|
|
|
|
2023-05-17 19:07:17 +02:00
|
|
|
it("should return a person's assets", async () => {
|
2023-08-01 03:28:07 +02:00
|
|
|
personMock.getAssets.mockResolvedValue([assetStub.image, assetStub.video]);
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set(['person-1']));
|
2023-05-17 19:07:17 +02:00
|
|
|
await sut.getAssets(authStub.admin, 'person-1');
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(personMock.getAssets).toHaveBeenCalledWith('person-1');
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('update', () => {
|
2023-09-18 23:22:44 +02:00
|
|
|
it('should require person.write permission', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.noName);
|
|
|
|
await expect(sut.update(authStub.admin, 'person-1', { name: 'Person 1' })).rejects.toBeInstanceOf(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
expect(personMock.update).not.toHaveBeenCalled();
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-09-18 23:22:44 +02:00
|
|
|
});
|
|
|
|
|
2023-05-17 19:07:17 +02:00
|
|
|
it('should throw an error when personId is invalid', async () => {
|
2024-03-07 21:34:57 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set());
|
2023-05-17 19:07:17 +02:00
|
|
|
await expect(sut.update(authStub.admin, 'person-1', { name: 'Person 1' })).rejects.toBeInstanceOf(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
expect(personMock.update).not.toHaveBeenCalled();
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should update a person's name", async () => {
|
|
|
|
personMock.update.mockResolvedValue(personStub.withName);
|
2023-08-01 03:28:07 +02:00
|
|
|
personMock.getAssets.mockResolvedValue([assetStub.image]);
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set(['person-1']));
|
2023-05-17 19:07:17 +02:00
|
|
|
|
|
|
|
await expect(sut.update(authStub.admin, 'person-1', { name: 'Person 1' })).resolves.toEqual(responseDto);
|
|
|
|
|
|
|
|
expect(personMock.update).toHaveBeenCalledWith({ id: 'person-1', name: 'Person 1' });
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
2023-07-03 00:46:20 +02:00
|
|
|
|
2023-08-18 22:10:29 +02:00
|
|
|
it("should update a person's date of birth", async () => {
|
|
|
|
personMock.update.mockResolvedValue(personStub.withBirthDate);
|
|
|
|
personMock.getAssets.mockResolvedValue([assetStub.image]);
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set(['person-1']));
|
2023-08-18 22:10:29 +02:00
|
|
|
|
|
|
|
await expect(sut.update(authStub.admin, 'person-1', { birthDate: new Date('1976-06-30') })).resolves.toEqual({
|
|
|
|
id: 'person-1',
|
|
|
|
name: 'Person 1',
|
|
|
|
birthDate: new Date('1976-06-30'),
|
|
|
|
thumbnailPath: '/path/to/thumbnail.jpg',
|
|
|
|
isHidden: false,
|
|
|
|
});
|
|
|
|
expect(personMock.update).toHaveBeenCalledWith({ id: 'person-1', birthDate: new Date('1976-06-30') });
|
|
|
|
expect(jobMock.queue).not.toHaveBeenCalled();
|
2024-01-01 21:45:42 +01:00
|
|
|
expect(jobMock.queueAll).not.toHaveBeenCalled();
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-08-18 22:10:29 +02:00
|
|
|
});
|
|
|
|
|
2023-07-18 20:09:43 +02:00
|
|
|
it('should update a person visibility', async () => {
|
|
|
|
personMock.update.mockResolvedValue(personStub.withName);
|
2023-08-01 03:28:07 +02:00
|
|
|
personMock.getAssets.mockResolvedValue([assetStub.image]);
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set(['person-1']));
|
2023-07-18 20:09:43 +02:00
|
|
|
|
|
|
|
await expect(sut.update(authStub.admin, 'person-1', { isHidden: false })).resolves.toEqual(responseDto);
|
|
|
|
|
|
|
|
expect(personMock.update).toHaveBeenCalledWith({ id: 'person-1', isHidden: false });
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-07-18 20:09:43 +02:00
|
|
|
});
|
|
|
|
|
2023-07-03 00:46:20 +02:00
|
|
|
it("should update a person's thumbnailPath", async () => {
|
2023-09-26 09:03:22 +02:00
|
|
|
personMock.update.mockResolvedValue(personStub.withName);
|
2023-09-27 22:46:46 +02:00
|
|
|
personMock.getFacesByIds.mockResolvedValue([faceStub.face1]);
|
chore(server): Check asset permissions in bulk (#5329)
Modify Access repository, to evaluate `asset` permissions in bulk.
Queries have been validated to match what they currently generate for single ids.
Queries:
* `asset` album access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "albums" "AlbumEntity"
LEFT JOIN "albums_assets_assets" "AlbumEntity_AlbumEntity__AlbumEntity_assets"
ON "AlbumEntity_AlbumEntity__AlbumEntity_assets"."albumsId"="AlbumEntity"."id"
LEFT JOIN "assets" "AlbumEntity__AlbumEntity_assets"
ON "AlbumEntity__AlbumEntity_assets"."id"="AlbumEntity_AlbumEntity__AlbumEntity_assets"."assetsId"
AND "AlbumEntity__AlbumEntity_assets"."deletedAt" IS NULL
LEFT JOIN "albums_shared_users_users" "AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"
ON "AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"."albumsId"="AlbumEntity"."id"
LEFT JOIN "users" "AlbumEntity__AlbumEntity_sharedUsers"
ON "AlbumEntity__AlbumEntity_sharedUsers"."id"="AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"."usersId"
AND "AlbumEntity__AlbumEntity_sharedUsers"."deletedAt" IS NULL
WHERE
(
("AlbumEntity"."ownerId" = $1 AND "AlbumEntity__AlbumEntity_assets"."id" = $2)
OR ("AlbumEntity__AlbumEntity_sharedUsers"."id" = $3 AND "AlbumEntity__AlbumEntity_assets"."id" = $4)
OR ("AlbumEntity"."ownerId" = $5 AND "AlbumEntity__AlbumEntity_assets"."livePhotoVideoId" = $6)
OR ("AlbumEntity__AlbumEntity_sharedUsers"."id" = $7 AND "AlbumEntity__AlbumEntity_assets"."livePhotoVideoId" = $8)
)
AND "AlbumEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT
"asset"."id" AS "assetId",
"asset"."livePhotoVideoId" AS "livePhotoVideoId"
FROM "albums" "album"
INNER JOIN "albums_assets_assets" "album_asset"
ON "album_asset"."albumsId"="album"."id"
INNER JOIN "assets" "asset"
ON "asset"."id"="album_asset"."assetsId"
AND "asset"."deletedAt" IS NULL
LEFT JOIN "albums_shared_users_users" "album_sharedUsers"
ON "album_sharedUsers"."albumsId"="album"."id"
LEFT JOIN "users" "sharedUsers"
ON "sharedUsers"."id"="album_sharedUsers"."usersId"
AND "sharedUsers"."deletedAt" IS NULL
WHERE
(
"album"."ownerId" = $1
OR "sharedUsers"."id" = $2
)
AND (
"asset"."id" IN ($3, $4)
OR "asset"."livePhotoVideoId" IN ($5, $6)
)
AND "album"."deletedAt" IS NULL
```
* `asset` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "assets" "AssetEntity"
WHERE
"AssetEntity"."id" = $1
AND "AssetEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT
"AssetEntity"."id" AS "AssetEntity_id"
FROM "assets" "AssetEntity"
WHERE
"AssetEntity"."id" IN ($1, $2)
AND "AssetEntity"."ownerId" = $3
```
* `asset` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__PartnerEntity_sharedWith"
ON "PartnerEntity__PartnerEntity_sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__PartnerEntity_sharedWith"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__PartnerEntity_sharedBy"
ON "PartnerEntity__PartnerEntity_sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__PartnerEntity_sharedBy"."deletedAt" IS NULL
LEFT JOIN "assets" "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"
ON "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."ownerId"="PartnerEntity__PartnerEntity_sharedBy"."id"
AND "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity__PartnerEntity_sharedWith"."id" = $1
AND "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."id" = $2
)
LIMIT 1
-- After
SELECT
"asset"."id" AS "assetId"
FROM "partners" "partner"
INNER JOIN "users" "sharedBy"
ON "sharedBy"."id"="partner"."sharedById"
AND "sharedBy"."deletedAt" IS NULL
INNER JOIN "assets" "asset"
ON "asset"."ownerId"="sharedBy"."id"
AND "asset"."deletedAt" IS NULL
WHERE
"partner"."sharedWithId" = $1
AND "asset"."id" IN ($2, $3)
```
* `asset` shared link access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "shared_links" "SharedLinkEntity"
LEFT JOIN "albums" "SharedLinkEntity__SharedLinkEntity_album"
ON "SharedLinkEntity__SharedLinkEntity_album"."id"="SharedLinkEntity"."albumId"
AND "SharedLinkEntity__SharedLinkEntity_album"."deletedAt" IS NULL
LEFT JOIN "albums_assets_assets" "760f12c00d97bdcec1ce224d1e3bf449859942b6"
ON "760f12c00d97bdcec1ce224d1e3bf449859942b6"."albumsId"="SharedLinkEntity__SharedLinkEntity_album"."id"
LEFT JOIN "assets" "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"
ON "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."id"="760f12c00d97bdcec1ce224d1e3bf449859942b6"."assetsId"
AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."deletedAt" IS NULL
LEFT JOIN "shared_link__asset" "SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"
ON "SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"."sharedLinksId"="SharedLinkEntity"."id"
LEFT JOIN "assets" "SharedLinkEntity__SharedLinkEntity_assets"
ON "SharedLinkEntity__SharedLinkEntity_assets"."id"="SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"."assetsId"
AND "SharedLinkEntity__SharedLinkEntity_assets"."deletedAt" IS NULL
WHERE (
("SharedLinkEntity"."id" = $1 AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."id" = $2)
OR ("SharedLinkEntity"."id" = $3 AND "SharedLinkEntity__SharedLinkEntity_assets"."id" = $4)
OR ("SharedLinkEntity"."id" = $5 AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."livePhotoVideoId" = $6)
OR ("SharedLinkEntity"."id" = $7 AND "SharedLinkEntity__SharedLinkEntity_assets"."livePhotoVideoId" = $8)
)
)
LIMIT 1
-- After
SELECT
"assets"."id" AS "assetId",
"assets"."livePhotoVideoId" AS "assetLivePhotoVideoId",
"albumAssets"."id" AS "albumAssetId",
"albumAssets"."livePhotoVideoId" AS "albumAssetLivePhotoVideoId"
FROM "shared_links" "sharedLink"
LEFT JOIN "albums" "album"
ON "album"."id"="sharedLink"."albumId"
AND "album"."deletedAt" IS NULL
LEFT JOIN "shared_link__asset" "assets_sharedLink"
ON "assets_sharedLink"."sharedLinksId"="sharedLink"."id"
LEFT JOIN "assets" "assets"
ON "assets"."id"="assets_sharedLink"."assetsId"
AND "assets"."deletedAt" IS NULL
LEFT JOIN "albums_assets_assets" "album_albumAssets"
ON "album_albumAssets"."albumsId"="album"."id"
LEFT JOIN "assets" "albumAssets"
ON "albumAssets"."id"="album_albumAssets"."assetsId"
AND "albumAssets"."deletedAt" IS NULL
WHERE
"sharedLink"."id" = $1
AND (
"assets"."id" IN ($2, $3)
OR "albumAssets"."id" IN ($4, $5)
OR "assets"."livePhotoVideoId" IN ($6, $7)
OR "albumAssets"."livePhotoVideoId" IN ($8, $9)
)
```
2023-12-02 03:56:41 +01:00
|
|
|
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set([assetStub.image.id]));
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set(['person-1']));
|
2023-07-03 00:46:20 +02:00
|
|
|
|
|
|
|
await expect(
|
|
|
|
sut.update(authStub.admin, 'person-1', { featureFaceAssetId: faceStub.face1.assetId }),
|
|
|
|
).resolves.toEqual(responseDto);
|
|
|
|
|
2023-12-05 16:43:15 +01:00
|
|
|
expect(personMock.update).toHaveBeenCalledWith({ id: 'person-1', faceAssetId: faceStub.face1.id });
|
2023-09-27 22:46:46 +02:00
|
|
|
expect(personMock.getFacesByIds).toHaveBeenCalledWith([
|
|
|
|
{
|
|
|
|
assetId: faceStub.face1.assetId,
|
|
|
|
personId: 'person-1',
|
|
|
|
},
|
|
|
|
]);
|
2023-09-26 09:03:22 +02:00
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.GENERATE_PERSON_THUMBNAIL, data: { id: 'person-1' } });
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-07-03 00:46:20 +02:00
|
|
|
});
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
it('should throw an error when the face feature assetId is invalid', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.withName);
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set(['person-1']));
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
await expect(sut.update(authStub.admin, 'person-1', { featureFaceAssetId: '-1' })).rejects.toThrow(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
expect(personMock.update).not.toHaveBeenCalled();
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-09-11 17:56:38 +02:00
|
|
|
});
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
|
2023-07-23 05:00:43 +02:00
|
|
|
describe('updateAll', () => {
|
|
|
|
it('should throw an error when personId is invalid', async () => {
|
2024-03-07 21:34:57 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set());
|
2023-09-18 23:22:44 +02:00
|
|
|
|
2024-03-07 21:34:57 +01:00
|
|
|
await expect(sut.updateAll(authStub.admin, { people: [{ id: 'person-1', name: 'Person 1' }] })).resolves.toEqual([
|
|
|
|
{ error: BulkIdErrorReason.UNKNOWN, id: 'person-1', success: false },
|
|
|
|
]);
|
2023-07-23 05:00:43 +02:00
|
|
|
expect(personMock.update).not.toHaveBeenCalled();
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-07-23 05:00:43 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-12-05 16:43:15 +01:00
|
|
|
describe('reassignFaces', () => {
|
|
|
|
it('should throw an error if user has no access to the person', async () => {
|
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set());
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
sut.reassignFaces(authStub.admin, personStub.noName.id, {
|
|
|
|
data: [{ personId: 'asset-face-1', assetId: '' }],
|
|
|
|
}),
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
expect(jobMock.queue).not.toHaveBeenCalledWith();
|
2024-01-01 21:45:42 +01:00
|
|
|
expect(jobMock.queueAll).not.toHaveBeenCalledWith();
|
2023-12-05 16:43:15 +01:00
|
|
|
});
|
|
|
|
it('should reassign a face', async () => {
|
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set([personStub.withName.id]));
|
|
|
|
personMock.getById.mockResolvedValue(personStub.noName);
|
2023-12-17 19:10:21 +01:00
|
|
|
accessMock.person.checkFaceOwnerAccess.mockResolvedValue(new Set([faceStub.face1.id]));
|
2023-12-05 16:43:15 +01:00
|
|
|
personMock.getFacesByIds.mockResolvedValue([faceStub.face1]);
|
|
|
|
personMock.reassignFace.mockResolvedValue(1);
|
|
|
|
personMock.getRandomFace.mockResolvedValue(faceStub.primaryFace1);
|
|
|
|
await expect(
|
|
|
|
sut.reassignFaces(authStub.admin, personStub.noName.id, {
|
|
|
|
data: [{ personId: personStub.withName.id, assetId: assetStub.image.id }],
|
|
|
|
}),
|
|
|
|
).resolves.toEqual([personStub.noName]);
|
|
|
|
|
2024-01-01 21:45:42 +01:00
|
|
|
expect(jobMock.queueAll).toHaveBeenCalledWith([
|
|
|
|
{
|
|
|
|
name: JobName.GENERATE_PERSON_THUMBNAIL,
|
|
|
|
data: { id: personStub.newThumbnail.id },
|
|
|
|
},
|
|
|
|
]);
|
2023-12-05 16:43:15 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('handlePersonMigration', () => {
|
|
|
|
it('should not move person files', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(null);
|
2024-03-15 14:16:54 +01:00
|
|
|
await expect(sut.handlePersonMigration(personStub.noName)).resolves.toBe(JobStatus.FAILED);
|
2023-12-05 16:43:15 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getFacesById', () => {
|
|
|
|
it('should get the bounding boxes for an asset', async () => {
|
|
|
|
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set([faceStub.face1.assetId]));
|
|
|
|
personMock.getFaces.mockResolvedValue([faceStub.primaryFace1]);
|
|
|
|
await expect(sut.getFacesById(authStub.admin, { id: faceStub.face1.assetId })).resolves.toStrictEqual([
|
|
|
|
mapFaces(faceStub.primaryFace1, authStub.admin),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
it('should reject if the user has not access to the asset', async () => {
|
|
|
|
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set());
|
|
|
|
personMock.getFaces.mockResolvedValue([faceStub.primaryFace1]);
|
|
|
|
await expect(sut.getFacesById(authStub.admin, { id: faceStub.primaryFace1.assetId })).rejects.toBeInstanceOf(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('createNewFeaturePhoto', () => {
|
|
|
|
it('should change person feature photo', async () => {
|
|
|
|
personMock.getRandomFace.mockResolvedValue(faceStub.primaryFace1);
|
|
|
|
await sut.createNewFeaturePhoto([personStub.newThumbnail.id]);
|
2024-01-01 21:45:42 +01:00
|
|
|
expect(jobMock.queueAll).toHaveBeenCalledWith([
|
|
|
|
{
|
|
|
|
name: JobName.GENERATE_PERSON_THUMBNAIL,
|
|
|
|
data: { id: personStub.newThumbnail.id },
|
|
|
|
},
|
|
|
|
]);
|
2023-12-05 16:43:15 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('reassignFacesById', () => {
|
|
|
|
it('should create a new person', async () => {
|
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set([personStub.noName.id]));
|
2023-12-17 19:10:21 +01:00
|
|
|
accessMock.person.checkFaceOwnerAccess.mockResolvedValue(new Set([faceStub.face1.id]));
|
2023-12-05 16:43:15 +01:00
|
|
|
personMock.getFaceById.mockResolvedValue(faceStub.face1);
|
|
|
|
personMock.reassignFace.mockResolvedValue(1);
|
|
|
|
personMock.getById.mockResolvedValue(personStub.noName);
|
|
|
|
personMock.getRandomFace.mockResolvedValue(null);
|
|
|
|
await expect(
|
|
|
|
sut.reassignFacesById(authStub.admin, personStub.noName.id, {
|
|
|
|
id: faceStub.face1.id,
|
|
|
|
}),
|
|
|
|
).resolves.toEqual({
|
|
|
|
birthDate: personStub.noName.birthDate,
|
|
|
|
isHidden: personStub.noName.isHidden,
|
|
|
|
id: personStub.noName.id,
|
|
|
|
name: personStub.noName.name,
|
|
|
|
thumbnailPath: personStub.noName.thumbnailPath,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(jobMock.queue).not.toHaveBeenCalledWith();
|
2024-01-01 21:45:42 +01:00
|
|
|
expect(jobMock.queueAll).not.toHaveBeenCalledWith();
|
2023-12-05 16:43:15 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail if user has not the correct permissions on the asset', async () => {
|
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set([personStub.noName.id]));
|
|
|
|
personMock.getFaceById.mockResolvedValue(faceStub.face1);
|
|
|
|
personMock.reassignFace.mockResolvedValue(1);
|
|
|
|
personMock.getById.mockResolvedValue(personStub.noName);
|
|
|
|
personMock.getRandomFace.mockResolvedValue(null);
|
|
|
|
await expect(
|
|
|
|
sut.reassignFacesById(authStub.admin, personStub.noName.id, {
|
|
|
|
id: faceStub.face1.id,
|
|
|
|
}),
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
|
|
|
expect(jobMock.queue).not.toHaveBeenCalledWith();
|
2024-01-01 21:45:42 +01:00
|
|
|
expect(jobMock.queueAll).not.toHaveBeenCalledWith();
|
2023-12-05 16:43:15 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('createPerson', () => {
|
|
|
|
it('should create a new person', async () => {
|
|
|
|
personMock.create.mockResolvedValue(personStub.primaryPerson);
|
|
|
|
|
2024-03-07 21:34:57 +01:00
|
|
|
await expect(sut.create(authStub.admin, {})).resolves.toBe(personStub.primaryPerson);
|
|
|
|
|
|
|
|
expect(personMock.create).toHaveBeenCalledWith({ ownerId: authStub.admin.user.id });
|
2023-12-05 16:43:15 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
describe('handlePersonCleanup', () => {
|
|
|
|
it('should delete people without faces', async () => {
|
|
|
|
personMock.getAllWithoutFaces.mockResolvedValue([personStub.noName]);
|
2023-12-05 16:43:15 +01:00
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
await sut.handlePersonCleanup();
|
2023-12-05 16:43:15 +01:00
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
expect(personMock.delete).toHaveBeenCalledWith([personStub.noName]);
|
|
|
|
expect(storageMock.unlink).toHaveBeenCalledWith(personStub.noName.thumbnailPath);
|
2023-12-05 16:43:15 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
describe('handleQueueDetectFaces', () => {
|
2024-03-15 14:16:54 +01:00
|
|
|
it('should skip if machine learning is disabled', async () => {
|
2024-01-18 06:08:48 +01:00
|
|
|
configMock.load.mockResolvedValue([{ key: SystemConfigKey.MACHINE_LEARNING_ENABLED, value: false }]);
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
await expect(sut.handleQueueDetectFaces({})).resolves.toBe(JobStatus.SKIPPED);
|
2024-01-18 06:08:48 +01:00
|
|
|
expect(jobMock.queue).not.toHaveBeenCalled();
|
|
|
|
expect(jobMock.queueAll).not.toHaveBeenCalled();
|
|
|
|
expect(configMock.load).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should queue missing assets', async () => {
|
|
|
|
assetMock.getWithout.mockResolvedValue({
|
|
|
|
items: [assetStub.image],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
2023-12-08 17:15:46 +01:00
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
await sut.handleQueueDetectFaces({});
|
2023-12-08 17:15:46 +01:00
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
expect(assetMock.getWithout).toHaveBeenCalledWith({ skip: 0, take: 1000 }, WithoutProperty.FACES);
|
|
|
|
expect(jobMock.queueAll).toHaveBeenCalledWith([
|
|
|
|
{
|
|
|
|
name: JobName.FACE_DETECTION,
|
|
|
|
data: { id: assetStub.image.id },
|
|
|
|
},
|
|
|
|
]);
|
2023-12-08 17:15:46 +01:00
|
|
|
});
|
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
it('should queue all assets', async () => {
|
|
|
|
assetMock.getAll.mockResolvedValue({
|
|
|
|
items: [assetStub.image],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
|
|
|
personMock.getAll.mockResolvedValue({
|
|
|
|
items: [personStub.withName],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
2023-05-17 19:07:17 +02:00
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
await sut.handleQueueDetectFaces({ force: true });
|
2023-05-17 19:07:17 +02:00
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
expect(assetMock.getAll).toHaveBeenCalled();
|
2024-01-01 21:45:42 +01:00
|
|
|
expect(jobMock.queueAll).toHaveBeenCalledWith([
|
2024-01-18 06:08:48 +01:00
|
|
|
{
|
|
|
|
name: JobName.FACE_DETECTION,
|
|
|
|
data: { id: assetStub.image.id },
|
|
|
|
},
|
2024-01-01 21:45:42 +01:00
|
|
|
]);
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
2024-01-18 06:08:48 +01:00
|
|
|
|
|
|
|
it('should delete existing people and faces if forced', async () => {
|
|
|
|
personMock.getAll.mockResolvedValue({
|
|
|
|
items: [faceStub.face1.person],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
|
|
|
personMock.getAllFaces.mockResolvedValue({
|
|
|
|
items: [faceStub.face1],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
|
|
|
assetMock.getAll.mockResolvedValue({
|
|
|
|
items: [assetStub.image],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
await sut.handleQueueDetectFaces({ force: true });
|
|
|
|
|
|
|
|
expect(assetMock.getAll).toHaveBeenCalled();
|
|
|
|
expect(jobMock.queueAll).toHaveBeenCalledWith([
|
|
|
|
{
|
|
|
|
name: JobName.FACE_DETECTION,
|
|
|
|
data: { id: assetStub.image.id },
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
expect(personMock.delete).toHaveBeenCalledWith([faceStub.face1.person]);
|
|
|
|
expect(storageMock.unlink).toHaveBeenCalledWith(faceStub.face1.person.thumbnailPath);
|
|
|
|
});
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
2023-07-11 23:52:41 +02:00
|
|
|
|
2023-09-27 22:46:46 +02:00
|
|
|
describe('handleQueueRecognizeFaces', () => {
|
2024-03-15 14:16:54 +01:00
|
|
|
it('should skip if machine learning is disabled', async () => {
|
2024-01-25 07:27:39 +01:00
|
|
|
jobMock.getJobCounts.mockResolvedValue({ active: 1, waiting: 0, paused: 0, completed: 0, failed: 0, delayed: 0 });
|
2023-09-27 22:46:46 +02:00
|
|
|
configMock.load.mockResolvedValue([{ key: SystemConfigKey.MACHINE_LEARNING_ENABLED, value: false }]);
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
await expect(sut.handleQueueRecognizeFaces({})).resolves.toBe(JobStatus.SKIPPED);
|
2024-01-25 07:27:39 +01:00
|
|
|
expect(jobMock.queueAll).not.toHaveBeenCalled();
|
2023-09-27 22:46:46 +02:00
|
|
|
expect(configMock.load).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
it('should skip if recognition jobs are already queued', async () => {
|
2024-01-25 07:27:39 +01:00
|
|
|
jobMock.getJobCounts.mockResolvedValue({ active: 1, waiting: 1, paused: 0, completed: 0, failed: 0, delayed: 0 });
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
await expect(sut.handleQueueRecognizeFaces({})).resolves.toBe(JobStatus.SKIPPED);
|
2024-01-25 07:27:39 +01:00
|
|
|
expect(jobMock.queueAll).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2023-09-27 22:46:46 +02:00
|
|
|
it('should queue missing assets', async () => {
|
2024-01-25 07:27:39 +01:00
|
|
|
jobMock.getJobCounts.mockResolvedValue({ active: 1, waiting: 0, paused: 0, completed: 0, failed: 0, delayed: 0 });
|
2024-01-18 06:08:48 +01:00
|
|
|
personMock.getAllFaces.mockResolvedValue({
|
|
|
|
items: [faceStub.face1],
|
2023-09-27 22:46:46 +02:00
|
|
|
hasNextPage: false,
|
|
|
|
});
|
2024-01-18 06:08:48 +01:00
|
|
|
|
2023-09-27 22:46:46 +02:00
|
|
|
await sut.handleQueueRecognizeFaces({});
|
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
expect(personMock.getAllFaces).toHaveBeenCalledWith({ skip: 0, take: 1000 }, { where: { personId: IsNull() } });
|
2024-01-01 21:45:42 +01:00
|
|
|
expect(jobMock.queueAll).toHaveBeenCalledWith([
|
|
|
|
{
|
2024-01-18 06:08:48 +01:00
|
|
|
name: JobName.FACIAL_RECOGNITION,
|
|
|
|
data: { id: faceStub.face1.id, deferred: false },
|
2024-01-01 21:45:42 +01:00
|
|
|
},
|
|
|
|
]);
|
2023-09-27 22:46:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should queue all assets', async () => {
|
2024-01-25 07:27:39 +01:00
|
|
|
jobMock.getJobCounts.mockResolvedValue({ active: 1, waiting: 0, paused: 0, completed: 0, failed: 0, delayed: 0 });
|
2024-01-18 06:08:48 +01:00
|
|
|
personMock.getAll.mockResolvedValue({
|
|
|
|
items: [],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
|
|
|
personMock.getAllFaces.mockResolvedValue({
|
|
|
|
items: [faceStub.face1],
|
2023-09-27 22:46:46 +02:00
|
|
|
hasNextPage: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
await sut.handleQueueRecognizeFaces({ force: true });
|
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
expect(personMock.getAllFaces).toHaveBeenCalledWith({ skip: 0, take: 1000 }, {});
|
2024-01-01 21:45:42 +01:00
|
|
|
expect(jobMock.queueAll).toHaveBeenCalledWith([
|
|
|
|
{
|
2024-01-18 06:08:48 +01:00
|
|
|
name: JobName.FACIAL_RECOGNITION,
|
|
|
|
data: { id: faceStub.face1.id, deferred: false },
|
2024-01-01 21:45:42 +01:00
|
|
|
},
|
|
|
|
]);
|
2024-01-18 06:08:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should delete existing people and faces if forced', async () => {
|
2024-01-25 07:27:39 +01:00
|
|
|
jobMock.getJobCounts.mockResolvedValue({ active: 1, waiting: 0, paused: 0, completed: 0, failed: 0, delayed: 0 });
|
2024-01-18 06:08:48 +01:00
|
|
|
personMock.getAll.mockResolvedValue({
|
|
|
|
items: [faceStub.face1.person],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
|
|
|
personMock.getAllFaces.mockResolvedValue({
|
|
|
|
items: [faceStub.face1],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
await sut.handleQueueRecognizeFaces({ force: true });
|
|
|
|
|
|
|
|
expect(personMock.getAllFaces).toHaveBeenCalledWith({ skip: 0, take: 1000 }, {});
|
2024-01-01 21:45:42 +01:00
|
|
|
expect(jobMock.queueAll).toHaveBeenCalledWith([
|
|
|
|
{
|
2024-01-18 06:08:48 +01:00
|
|
|
name: JobName.FACIAL_RECOGNITION,
|
|
|
|
data: { id: faceStub.face1.id, deferred: false },
|
2024-01-01 21:45:42 +01:00
|
|
|
},
|
|
|
|
]);
|
2024-01-18 06:08:48 +01:00
|
|
|
expect(personMock.delete).toHaveBeenCalledWith([faceStub.face1.person]);
|
|
|
|
expect(storageMock.unlink).toHaveBeenCalledWith(faceStub.face1.person.thumbnailPath);
|
2023-09-27 22:46:46 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
describe('handleDetectFaces', () => {
|
2024-03-15 14:16:54 +01:00
|
|
|
it('should skip if machine learning is disabled', async () => {
|
2023-09-27 22:46:46 +02:00
|
|
|
configMock.load.mockResolvedValue([{ key: SystemConfigKey.MACHINE_LEARNING_ENABLED, value: false }]);
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
await expect(sut.handleDetectFaces({ id: 'foo' })).resolves.toBe(JobStatus.SKIPPED);
|
2023-09-27 22:46:46 +02:00
|
|
|
expect(assetMock.getByIds).not.toHaveBeenCalled();
|
|
|
|
expect(configMock.load).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip when no resize path', async () => {
|
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.noResizePath]);
|
2024-01-18 06:08:48 +01:00
|
|
|
await sut.handleDetectFaces({ id: assetStub.noResizePath.id });
|
2023-09-27 22:46:46 +02:00
|
|
|
expect(machineLearningMock.detectFaces).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2023-11-05 17:07:29 +01:00
|
|
|
it('should skip it the asset has already been processed', async () => {
|
|
|
|
assetMock.getByIds.mockResolvedValue([
|
|
|
|
{
|
|
|
|
...assetStub.noResizePath,
|
|
|
|
faces: [
|
|
|
|
{
|
|
|
|
id: 'asset-face-1',
|
|
|
|
assetId: assetStub.noResizePath.id,
|
|
|
|
personId: faceStub.face1.personId,
|
|
|
|
} as AssetFaceEntity,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]);
|
2024-01-18 06:08:48 +01:00
|
|
|
await sut.handleDetectFaces({ id: assetStub.noResizePath.id });
|
2023-11-05 17:07:29 +01:00
|
|
|
expect(machineLearningMock.detectFaces).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2023-09-27 22:46:46 +02:00
|
|
|
it('should handle no results', async () => {
|
2023-11-10 02:55:00 +01:00
|
|
|
const start = Date.now();
|
|
|
|
|
2023-09-27 22:46:46 +02:00
|
|
|
machineLearningMock.detectFaces.mockResolvedValue([]);
|
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.image]);
|
2024-01-18 06:08:48 +01:00
|
|
|
await sut.handleDetectFaces({ id: assetStub.image.id });
|
2023-09-27 22:46:46 +02:00
|
|
|
expect(machineLearningMock.detectFaces).toHaveBeenCalledWith(
|
|
|
|
'http://immich-machine-learning:3003',
|
|
|
|
{
|
2024-04-02 06:56:56 +02:00
|
|
|
imagePath: assetStub.image.previewPath,
|
2023-09-27 22:46:46 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
enabled: true,
|
2024-03-07 04:20:38 +01:00
|
|
|
maxDistance: 0.5,
|
2023-09-27 22:46:46 +02:00
|
|
|
minScore: 0.7,
|
2024-01-18 06:08:48 +01:00
|
|
|
minFaces: 3,
|
2023-09-27 22:46:46 +02:00
|
|
|
modelName: 'buffalo_l',
|
|
|
|
},
|
|
|
|
);
|
2024-01-25 07:27:39 +01:00
|
|
|
expect(personMock.createFaces).not.toHaveBeenCalled();
|
2023-09-27 22:46:46 +02:00
|
|
|
expect(jobMock.queue).not.toHaveBeenCalled();
|
2024-01-01 21:45:42 +01:00
|
|
|
expect(jobMock.queueAll).not.toHaveBeenCalled();
|
2023-11-10 02:55:00 +01:00
|
|
|
|
|
|
|
expect(assetMock.upsertJobStatus).toHaveBeenCalledWith({
|
|
|
|
assetId: assetStub.image.id,
|
|
|
|
facesRecognizedAt: expect.any(Date),
|
|
|
|
});
|
|
|
|
expect(assetMock.upsertJobStatus.mock.calls[0][0].facesRecognizedAt?.getTime()).toBeGreaterThan(start);
|
2023-09-27 22:46:46 +02:00
|
|
|
});
|
|
|
|
|
2024-01-25 07:27:39 +01:00
|
|
|
it('should create a face with no person and queue recognition job', async () => {
|
|
|
|
personMock.createFaces.mockResolvedValue([faceStub.face1.id]);
|
2023-09-27 22:46:46 +02:00
|
|
|
machineLearningMock.detectFaces.mockResolvedValue([detectFaceMock]);
|
2024-02-13 02:50:47 +01:00
|
|
|
searchMock.searchFaces.mockResolvedValue([{ face: faceStub.face1, distance: 0.7 }]);
|
2023-09-27 22:46:46 +02:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.image]);
|
2024-01-25 07:27:39 +01:00
|
|
|
const face = {
|
2023-09-27 22:46:46 +02:00
|
|
|
assetId: 'asset-id',
|
|
|
|
embedding: [1, 2, 3, 4],
|
|
|
|
boundingBoxX1: 100,
|
|
|
|
boundingBoxY1: 100,
|
|
|
|
boundingBoxX2: 200,
|
|
|
|
boundingBoxY2: 200,
|
|
|
|
imageHeight: 500,
|
|
|
|
imageWidth: 400,
|
2024-01-25 07:27:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
await sut.handleDetectFaces({ id: assetStub.image.id });
|
|
|
|
|
|
|
|
expect(personMock.createFaces).toHaveBeenCalledWith([face]);
|
|
|
|
expect(jobMock.queueAll).toHaveBeenCalledWith([
|
|
|
|
{ name: JobName.FACIAL_RECOGNITION, data: { id: faceStub.face1.id } },
|
|
|
|
]);
|
2024-01-18 06:08:48 +01:00
|
|
|
expect(personMock.reassignFace).not.toHaveBeenCalled();
|
|
|
|
expect(personMock.reassignFaces).not.toHaveBeenCalled();
|
2023-09-27 22:46:46 +02:00
|
|
|
});
|
2024-01-18 06:08:48 +01:00
|
|
|
});
|
2023-09-27 22:46:46 +02:00
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
describe('handleRecognizeFaces', () => {
|
2024-03-15 14:16:54 +01:00
|
|
|
it('should fail if face does not exist', async () => {
|
2024-01-18 06:08:48 +01:00
|
|
|
personMock.getFaceByIdWithAssets.mockResolvedValue(null);
|
2023-09-27 22:46:46 +02:00
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
expect(await sut.handleRecognizeFaces({ id: faceStub.face1.id })).toBe(JobStatus.FAILED);
|
2023-09-27 22:46:46 +02:00
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
expect(personMock.reassignFaces).not.toHaveBeenCalled();
|
|
|
|
expect(personMock.create).not.toHaveBeenCalled();
|
2024-01-25 07:27:39 +01:00
|
|
|
expect(personMock.createFaces).not.toHaveBeenCalled();
|
2024-01-18 06:08:48 +01:00
|
|
|
});
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
it('should fail if face does not have asset', async () => {
|
2024-01-29 02:17:54 +01:00
|
|
|
const face = { ...faceStub.face1, asset: null } as AssetFaceEntity & { asset: null };
|
|
|
|
personMock.getFaceByIdWithAssets.mockResolvedValue(face);
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
expect(await sut.handleRecognizeFaces({ id: faceStub.face1.id })).toBe(JobStatus.FAILED);
|
2024-01-29 02:17:54 +01:00
|
|
|
|
|
|
|
expect(personMock.reassignFaces).not.toHaveBeenCalled();
|
|
|
|
expect(personMock.create).not.toHaveBeenCalled();
|
|
|
|
expect(personMock.createFaces).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
it('should skip if face already has an assigned person', async () => {
|
2024-01-18 06:08:48 +01:00
|
|
|
personMock.getFaceByIdWithAssets.mockResolvedValue(faceStub.face1);
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
expect(await sut.handleRecognizeFaces({ id: faceStub.face1.id })).toBe(JobStatus.SKIPPED);
|
2024-01-18 06:08:48 +01:00
|
|
|
|
|
|
|
expect(personMock.reassignFaces).not.toHaveBeenCalled();
|
|
|
|
expect(personMock.create).not.toHaveBeenCalled();
|
2024-01-25 07:27:39 +01:00
|
|
|
expect(personMock.createFaces).not.toHaveBeenCalled();
|
2024-01-18 06:08:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should match existing person', async () => {
|
|
|
|
if (!faceStub.primaryFace1.person) {
|
|
|
|
throw new Error('faceStub.primaryFace1.person is null');
|
|
|
|
}
|
|
|
|
|
|
|
|
const faces = [
|
2024-02-02 04:18:00 +01:00
|
|
|
{ face: faceStub.noPerson1, distance: 0 },
|
2024-01-18 06:08:48 +01:00
|
|
|
{ face: faceStub.primaryFace1, distance: 0.2 },
|
|
|
|
{ face: faceStub.noPerson2, distance: 0.3 },
|
|
|
|
{ face: faceStub.face1, distance: 0.4 },
|
|
|
|
] as FaceSearchResult[];
|
|
|
|
|
|
|
|
configMock.load.mockResolvedValue([
|
|
|
|
{ key: SystemConfigKey.MACHINE_LEARNING_FACIAL_RECOGNITION_MIN_FACES, value: 1 },
|
|
|
|
]);
|
2024-02-13 02:50:47 +01:00
|
|
|
searchMock.searchFaces.mockResolvedValue(faces);
|
2024-01-18 06:08:48 +01:00
|
|
|
personMock.getFaceByIdWithAssets.mockResolvedValue(faceStub.noPerson1);
|
|
|
|
personMock.create.mockResolvedValue(faceStub.primaryFace1.person);
|
|
|
|
|
|
|
|
await sut.handleRecognizeFaces({ id: faceStub.noPerson1.id });
|
|
|
|
|
|
|
|
expect(personMock.create).not.toHaveBeenCalled();
|
|
|
|
expect(personMock.reassignFaces).toHaveBeenCalledTimes(1);
|
|
|
|
expect(personMock.reassignFaces).toHaveBeenCalledWith({
|
|
|
|
faceIds: expect.arrayContaining([faceStub.noPerson1.id]),
|
|
|
|
newPersonId: faceStub.primaryFace1.person.id,
|
|
|
|
});
|
|
|
|
expect(personMock.reassignFaces).toHaveBeenCalledWith({
|
|
|
|
faceIds: expect.not.arrayContaining([faceStub.face1.id]),
|
|
|
|
newPersonId: faceStub.primaryFace1.person.id,
|
2023-09-27 22:46:46 +02:00
|
|
|
});
|
|
|
|
});
|
2024-01-18 06:08:48 +01:00
|
|
|
|
|
|
|
it('should create a new person if the face is a core point with no person', async () => {
|
|
|
|
const faces = [
|
2024-02-02 04:18:00 +01:00
|
|
|
{ face: faceStub.noPerson1, distance: 0 },
|
2024-01-18 06:08:48 +01:00
|
|
|
{ face: faceStub.noPerson2, distance: 0.3 },
|
|
|
|
] as FaceSearchResult[];
|
|
|
|
|
|
|
|
configMock.load.mockResolvedValue([
|
|
|
|
{ key: SystemConfigKey.MACHINE_LEARNING_FACIAL_RECOGNITION_MIN_FACES, value: 1 },
|
|
|
|
]);
|
2024-02-13 02:50:47 +01:00
|
|
|
searchMock.searchFaces.mockResolvedValue(faces);
|
2024-01-18 06:08:48 +01:00
|
|
|
personMock.getFaceByIdWithAssets.mockResolvedValue(faceStub.noPerson1);
|
|
|
|
personMock.create.mockResolvedValue(personStub.withName);
|
|
|
|
|
|
|
|
await sut.handleRecognizeFaces({ id: faceStub.noPerson1.id });
|
|
|
|
|
|
|
|
expect(personMock.create).toHaveBeenCalledWith({
|
|
|
|
ownerId: faceStub.noPerson1.asset.ownerId,
|
|
|
|
faceAssetId: faceStub.noPerson1.id,
|
|
|
|
});
|
|
|
|
expect(personMock.reassignFaces).toHaveBeenCalledWith({
|
|
|
|
faceIds: [faceStub.noPerson1.id],
|
|
|
|
newPersonId: personStub.withName.id,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-02-07 16:56:39 +01:00
|
|
|
it('should not queue face with no matches', async () => {
|
2024-02-02 04:18:00 +01:00
|
|
|
const faces = [{ face: faceStub.noPerson1, distance: 0 }] as FaceSearchResult[];
|
2024-01-18 06:08:48 +01:00
|
|
|
|
2024-02-13 02:50:47 +01:00
|
|
|
searchMock.searchFaces.mockResolvedValue(faces);
|
2024-02-07 16:56:39 +01:00
|
|
|
personMock.getFaceByIdWithAssets.mockResolvedValue(faceStub.noPerson1);
|
|
|
|
personMock.create.mockResolvedValue(personStub.withName);
|
|
|
|
|
|
|
|
await sut.handleRecognizeFaces({ id: faceStub.noPerson1.id });
|
|
|
|
|
|
|
|
expect(jobMock.queue).not.toHaveBeenCalled();
|
2024-02-13 02:50:47 +01:00
|
|
|
expect(searchMock.searchFaces).toHaveBeenCalledTimes(1);
|
2024-02-07 16:56:39 +01:00
|
|
|
expect(personMock.create).not.toHaveBeenCalled();
|
|
|
|
expect(personMock.reassignFaces).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should defer non-core faces to end of queue', async () => {
|
|
|
|
const faces = [
|
|
|
|
{ face: faceStub.noPerson1, distance: 0 },
|
|
|
|
{ face: faceStub.noPerson2, distance: 0.4 },
|
|
|
|
] as FaceSearchResult[];
|
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
configMock.load.mockResolvedValue([
|
2024-02-07 16:56:39 +01:00
|
|
|
{ key: SystemConfigKey.MACHINE_LEARNING_FACIAL_RECOGNITION_MIN_FACES, value: 3 },
|
2024-01-18 06:08:48 +01:00
|
|
|
]);
|
2024-02-13 02:50:47 +01:00
|
|
|
searchMock.searchFaces.mockResolvedValue(faces);
|
2024-01-18 06:08:48 +01:00
|
|
|
personMock.getFaceByIdWithAssets.mockResolvedValue(faceStub.noPerson1);
|
|
|
|
personMock.create.mockResolvedValue(personStub.withName);
|
|
|
|
|
|
|
|
await sut.handleRecognizeFaces({ id: faceStub.noPerson1.id });
|
|
|
|
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
name: JobName.FACIAL_RECOGNITION,
|
|
|
|
data: { id: faceStub.noPerson1.id, deferred: true },
|
|
|
|
});
|
2024-02-13 02:50:47 +01:00
|
|
|
expect(searchMock.searchFaces).toHaveBeenCalledTimes(1);
|
2024-01-18 06:08:48 +01:00
|
|
|
expect(personMock.create).not.toHaveBeenCalled();
|
|
|
|
expect(personMock.reassignFaces).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2024-02-07 16:56:39 +01:00
|
|
|
it('should not assign person to deferred non-core face with no matching person', async () => {
|
|
|
|
const faces = [
|
|
|
|
{ face: faceStub.noPerson1, distance: 0 },
|
|
|
|
{ face: faceStub.noPerson2, distance: 0.4 },
|
|
|
|
] as FaceSearchResult[];
|
2024-01-18 06:08:48 +01:00
|
|
|
|
|
|
|
configMock.load.mockResolvedValue([
|
2024-02-07 16:56:39 +01:00
|
|
|
{ key: SystemConfigKey.MACHINE_LEARNING_FACIAL_RECOGNITION_MIN_FACES, value: 3 },
|
2024-01-18 06:08:48 +01:00
|
|
|
]);
|
2024-02-13 02:50:47 +01:00
|
|
|
searchMock.searchFaces.mockResolvedValueOnce(faces).mockResolvedValueOnce([]);
|
2024-01-18 06:08:48 +01:00
|
|
|
personMock.getFaceByIdWithAssets.mockResolvedValue(faceStub.noPerson1);
|
|
|
|
personMock.create.mockResolvedValue(personStub.withName);
|
|
|
|
|
|
|
|
await sut.handleRecognizeFaces({ id: faceStub.noPerson1.id, deferred: true });
|
|
|
|
|
|
|
|
expect(jobMock.queue).not.toHaveBeenCalled();
|
2024-02-13 02:50:47 +01:00
|
|
|
expect(searchMock.searchFaces).toHaveBeenCalledTimes(2);
|
2024-01-18 06:08:48 +01:00
|
|
|
expect(personMock.create).not.toHaveBeenCalled();
|
|
|
|
expect(personMock.reassignFaces).not.toHaveBeenCalled();
|
|
|
|
});
|
2023-09-27 22:46:46 +02:00
|
|
|
});
|
2024-01-18 06:08:48 +01:00
|
|
|
|
2023-09-27 22:46:46 +02:00
|
|
|
describe('handleGeneratePersonThumbnail', () => {
|
2024-03-15 14:16:54 +01:00
|
|
|
it('should skip if machine learning is disabled', async () => {
|
2023-09-27 22:46:46 +02:00
|
|
|
configMock.load.mockResolvedValue([{ key: SystemConfigKey.MACHINE_LEARNING_ENABLED, value: false }]);
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
await expect(sut.handleGeneratePersonThumbnail({ id: 'person-1' })).resolves.toBe(JobStatus.SKIPPED);
|
2023-09-27 22:46:46 +02:00
|
|
|
expect(assetMock.getByIds).not.toHaveBeenCalled();
|
|
|
|
expect(configMock.load).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip a person not found', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(null);
|
|
|
|
await sut.handleGeneratePersonThumbnail({ id: 'person-1' });
|
|
|
|
expect(mediaMock.crop).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip a person without a face asset id', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.noThumbnail);
|
|
|
|
await sut.handleGeneratePersonThumbnail({ id: 'person-1' });
|
|
|
|
expect(mediaMock.crop).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2023-12-05 16:43:15 +01:00
|
|
|
it('should skip a person with a face asset id not found', async () => {
|
|
|
|
personMock.getById.mockResolvedValue({ ...personStub.primaryPerson, faceAssetId: faceStub.middle.id });
|
|
|
|
personMock.getFaceByIdWithAssets.mockResolvedValue(faceStub.face1);
|
2023-09-27 22:46:46 +02:00
|
|
|
await sut.handleGeneratePersonThumbnail({ id: 'person-1' });
|
|
|
|
expect(mediaMock.crop).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip a person with a face asset id without a thumbnail', async () => {
|
|
|
|
personMock.getById.mockResolvedValue({ ...personStub.primaryPerson, faceAssetId: faceStub.middle.assetId });
|
2023-12-05 16:43:15 +01:00
|
|
|
personMock.getFaceByIdWithAssets.mockResolvedValue(faceStub.face1);
|
2023-09-27 22:46:46 +02:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.noResizePath]);
|
|
|
|
await sut.handleGeneratePersonThumbnail({ id: 'person-1' });
|
|
|
|
expect(mediaMock.crop).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate a thumbnail', async () => {
|
|
|
|
personMock.getById.mockResolvedValue({ ...personStub.primaryPerson, faceAssetId: faceStub.middle.assetId });
|
2023-12-05 16:43:15 +01:00
|
|
|
personMock.getFaceByIdWithAssets.mockResolvedValue(faceStub.middle);
|
2023-10-11 04:14:44 +02:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.primaryImage]);
|
2023-09-27 22:46:46 +02:00
|
|
|
|
|
|
|
await sut.handleGeneratePersonThumbnail({ id: 'person-1' });
|
|
|
|
|
|
|
|
expect(assetMock.getByIds).toHaveBeenCalledWith([faceStub.middle.assetId]);
|
2023-10-11 04:14:44 +02:00
|
|
|
expect(storageMock.mkdirSync).toHaveBeenCalledWith('upload/thumbs/admin_id/pe/rs');
|
|
|
|
expect(mediaMock.crop).toHaveBeenCalledWith('/uploads/admin-id/thumbs/path.jpg', {
|
2023-09-27 22:46:46 +02:00
|
|
|
left: 95,
|
|
|
|
top: 95,
|
|
|
|
width: 110,
|
|
|
|
height: 110,
|
|
|
|
});
|
2023-10-11 04:14:44 +02:00
|
|
|
expect(mediaMock.resize).toHaveBeenCalledWith(croppedFace, 'upload/thumbs/admin_id/pe/rs/person-1.jpeg', {
|
2023-09-27 22:46:46 +02:00
|
|
|
format: 'jpeg',
|
|
|
|
size: 250,
|
|
|
|
quality: 80,
|
|
|
|
colorspace: Colorspace.P3,
|
|
|
|
});
|
|
|
|
expect(personMock.update).toHaveBeenCalledWith({
|
|
|
|
id: 'person-1',
|
2023-10-11 04:14:44 +02:00
|
|
|
thumbnailPath: 'upload/thumbs/admin_id/pe/rs/person-1.jpeg',
|
2023-09-27 22:46:46 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate a thumbnail without going negative', async () => {
|
|
|
|
personMock.getById.mockResolvedValue({ ...personStub.primaryPerson, faceAssetId: faceStub.start.assetId });
|
2023-12-05 16:43:15 +01:00
|
|
|
personMock.getFaceByIdWithAssets.mockResolvedValue(faceStub.start);
|
2023-09-27 22:46:46 +02:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.image]);
|
|
|
|
|
|
|
|
await sut.handleGeneratePersonThumbnail({ id: 'person-1' });
|
|
|
|
|
|
|
|
expect(mediaMock.crop).toHaveBeenCalledWith('/uploads/user-id/thumbs/path.jpg', {
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
width: 510,
|
|
|
|
height: 510,
|
|
|
|
});
|
2023-10-11 04:14:44 +02:00
|
|
|
expect(mediaMock.resize).toHaveBeenCalledWith(croppedFace, 'upload/thumbs/admin_id/pe/rs/person-1.jpeg', {
|
2023-09-27 22:46:46 +02:00
|
|
|
format: 'jpeg',
|
|
|
|
size: 250,
|
|
|
|
quality: 80,
|
|
|
|
colorspace: Colorspace.P3,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate a thumbnail without overflowing', async () => {
|
|
|
|
personMock.getById.mockResolvedValue({ ...personStub.primaryPerson, faceAssetId: faceStub.end.assetId });
|
2023-12-05 16:43:15 +01:00
|
|
|
personMock.getFaceByIdWithAssets.mockResolvedValue(faceStub.end);
|
2023-10-11 04:14:44 +02:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.primaryImage]);
|
2023-09-27 22:46:46 +02:00
|
|
|
|
|
|
|
await sut.handleGeneratePersonThumbnail({ id: 'person-1' });
|
|
|
|
|
2023-10-11 04:14:44 +02:00
|
|
|
expect(mediaMock.crop).toHaveBeenCalledWith('/uploads/admin-id/thumbs/path.jpg', {
|
2023-09-27 22:46:46 +02:00
|
|
|
left: 297,
|
|
|
|
top: 297,
|
|
|
|
width: 202,
|
|
|
|
height: 202,
|
|
|
|
});
|
2023-10-11 04:14:44 +02:00
|
|
|
expect(mediaMock.resize).toHaveBeenCalledWith(croppedFace, 'upload/thumbs/admin_id/pe/rs/person-1.jpeg', {
|
2023-09-27 22:46:46 +02:00
|
|
|
format: 'jpeg',
|
|
|
|
size: 250,
|
|
|
|
quality: 80,
|
|
|
|
colorspace: Colorspace.P3,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-07-11 23:52:41 +02:00
|
|
|
describe('mergePerson', () => {
|
2023-09-18 23:22:44 +02:00
|
|
|
it('should require person.write and person.merge permission', async () => {
|
|
|
|
personMock.getById.mockResolvedValueOnce(personStub.primaryPerson);
|
|
|
|
personMock.getById.mockResolvedValueOnce(personStub.mergePerson);
|
|
|
|
|
|
|
|
await expect(sut.mergePerson(authStub.admin, 'person-1', { ids: ['person-2'] })).rejects.toBeInstanceOf(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(personMock.reassignFaces).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
expect(personMock.delete).not.toHaveBeenCalled();
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-09-18 23:22:44 +02:00
|
|
|
});
|
|
|
|
|
2024-01-19 18:52:26 +01:00
|
|
|
it('should merge two people without smart merge', async () => {
|
2023-07-11 23:52:41 +02:00
|
|
|
personMock.getById.mockResolvedValueOnce(personStub.primaryPerson);
|
|
|
|
personMock.getById.mockResolvedValueOnce(personStub.mergePerson);
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValueOnce(new Set(['person-1']));
|
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValueOnce(new Set(['person-2']));
|
2023-07-11 23:52:41 +02:00
|
|
|
|
|
|
|
await expect(sut.mergePerson(authStub.admin, 'person-1', { ids: ['person-2'] })).resolves.toEqual([
|
|
|
|
{ id: 'person-2', success: true },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(personMock.reassignFaces).toHaveBeenCalledWith({
|
|
|
|
newPersonId: personStub.primaryPerson.id,
|
|
|
|
oldPersonId: personStub.mergePerson.id,
|
|
|
|
});
|
|
|
|
|
2024-01-18 02:52:11 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
|
|
|
});
|
|
|
|
|
2024-01-19 18:52:26 +01:00
|
|
|
it('should merge two people with smart merge', async () => {
|
|
|
|
personMock.getById.mockResolvedValueOnce(personStub.randomPerson);
|
|
|
|
personMock.getById.mockResolvedValueOnce(personStub.primaryPerson);
|
|
|
|
personMock.update.mockResolvedValue({ ...personStub.randomPerson, name: personStub.primaryPerson.name });
|
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValueOnce(new Set(['person-3']));
|
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValueOnce(new Set(['person-1']));
|
|
|
|
|
|
|
|
await expect(sut.mergePerson(authStub.admin, 'person-3', { ids: ['person-1'] })).resolves.toEqual([
|
|
|
|
{ id: 'person-1', success: true },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(personMock.reassignFaces).toHaveBeenCalledWith({
|
|
|
|
newPersonId: personStub.randomPerson.id,
|
|
|
|
oldPersonId: personStub.primaryPerson.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(personMock.update).toHaveBeenCalledWith({
|
|
|
|
id: personStub.randomPerson.id,
|
|
|
|
name: personStub.primaryPerson.name,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
|
|
|
});
|
|
|
|
|
2023-07-11 23:52:41 +02:00
|
|
|
it('should throw an error when the primary person is not found', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(null);
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set(['person-1']));
|
2023-07-11 23:52:41 +02:00
|
|
|
|
|
|
|
await expect(sut.mergePerson(authStub.admin, 'person-1', { ids: ['person-2'] })).rejects.toBeInstanceOf(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(personMock.delete).not.toHaveBeenCalled();
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-07-11 23:52:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle invalid merge ids', async () => {
|
|
|
|
personMock.getById.mockResolvedValueOnce(personStub.primaryPerson);
|
|
|
|
personMock.getById.mockResolvedValueOnce(null);
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValueOnce(new Set(['person-1']));
|
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValueOnce(new Set(['person-2']));
|
2023-07-11 23:52:41 +02:00
|
|
|
|
|
|
|
await expect(sut.mergePerson(authStub.admin, 'person-1', { ids: ['person-2'] })).resolves.toEqual([
|
|
|
|
{ id: 'person-2', success: false, error: BulkIdErrorReason.NOT_FOUND },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(personMock.reassignFaces).not.toHaveBeenCalled();
|
|
|
|
expect(personMock.delete).not.toHaveBeenCalled();
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-07-11 23:52:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle an error reassigning faces', async () => {
|
2024-01-19 18:52:26 +01:00
|
|
|
personMock.getById.mockResolvedValueOnce(personStub.primaryPerson);
|
|
|
|
personMock.getById.mockResolvedValueOnce(personStub.mergePerson);
|
2023-07-11 23:52:41 +02:00
|
|
|
personMock.reassignFaces.mockRejectedValue(new Error('update failed'));
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValueOnce(new Set(['person-1']));
|
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValueOnce(new Set(['person-2']));
|
2023-07-11 23:52:41 +02:00
|
|
|
|
|
|
|
await expect(sut.mergePerson(authStub.admin, 'person-1', { ids: ['person-2'] })).resolves.toEqual([
|
|
|
|
{ id: 'person-2', success: false, error: BulkIdErrorReason.UNKNOWN },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(personMock.delete).not.toHaveBeenCalled();
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-07-11 23:52:41 +02:00
|
|
|
});
|
|
|
|
});
|
2023-10-24 17:53:49 +02:00
|
|
|
|
|
|
|
describe('getStatistics', () => {
|
|
|
|
it('should get correct number of person', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.primaryPerson);
|
|
|
|
personMock.getStatistics.mockResolvedValue(statistics);
|
chore(server): Check more permissions in bulk (#5315)
Modify Access repository, to evaluate `authDevice`, `library`, `partner`,
`person`, and `timeline` permissions in bulk.
Queries have been validated to match what they currently generate for
single ids.
As an extra performance improvement, we now use a custom QueryBuilder
for the Partners queries, to avoid the eager relationships that add
unneeded `LEFT JOIN` clauses. We only filter based on the ids present in
the `partners` table, so those joins can be avoided.
Queries:
* `library` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" = $1
AND "LibraryEntity"."ownerId" = $2
AND "LibraryEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT "LibraryEntity"."id" AS "LibraryEntity_id"
FROM "libraries" "LibraryEntity"
WHERE
"LibraryEntity"."id" IN ($1, $2)
AND "LibraryEntity"."ownerId" = $3
AND "LibraryEntity"."deletedAt" IS NULL
```
* `library` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `authDevice` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" = $2
)
LIMIT 1
-- After
SELECT "UserTokenEntity"."id" AS "UserTokenEntity_id"
FROM "user_token" "UserTokenEntity"
WHERE
"UserTokenEntity"."userId" = $1
AND "UserTokenEntity"."id" IN ($2, $3)
```
* `timeline` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
* `person` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" = $1
AND "PersonEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT "PersonEntity"."id" AS "PersonEntity_id"
FROM "person" "PersonEntity"
WHERE
"PersonEntity"."id" IN ($1, $2)
AND "PersonEntity"."ownerId" = $3
```
* `partner` update access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity"."sharedWithId" = $1
AND "PartnerEntity"."sharedById" = $2
)
LIMIT 1
-- After
SELECT
"partner"."sharedById" AS "partner_sharedById",
"partner"."sharedWithId" AS "partner_sharedWithId"
FROM "partners" "partner"
WHERE
"partner"."sharedById" IN ($1, $2)
AND "partner"."sharedWithId" = $3
```
2023-11-26 13:50:41 +01:00
|
|
|
accessMock.person.checkOwnerAccess.mockResolvedValue(new Set(['person-1']));
|
2023-10-24 17:53:49 +02:00
|
|
|
await expect(sut.getStatistics(authStub.admin, 'person-1')).resolves.toEqual({ assets: 3 });
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-10-24 17:53:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should require person.read permission', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.primaryPerson);
|
|
|
|
await expect(sut.getStatistics(authStub.admin, 'person-1')).rejects.toBeInstanceOf(BadRequestException);
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.person.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['person-1']));
|
2023-10-24 17:53:49 +02:00
|
|
|
});
|
|
|
|
});
|
2023-12-08 17:15:46 +01:00
|
|
|
|
|
|
|
describe('mapFace', () => {
|
|
|
|
it('should map a face', () => {
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(mapFaces(faceStub.face1, { user: personStub.withName.owner })).toEqual({
|
2023-12-08 17:15:46 +01:00
|
|
|
boundingBoxX1: 0,
|
|
|
|
boundingBoxX2: 1,
|
|
|
|
boundingBoxY1: 0,
|
|
|
|
boundingBoxY2: 1,
|
2024-01-18 06:08:48 +01:00
|
|
|
id: faceStub.face1.id,
|
2023-12-08 17:15:46 +01:00
|
|
|
imageHeight: 1024,
|
|
|
|
imageWidth: 1024,
|
|
|
|
person: mapPerson(personStub.withName),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not map person if person is null', () => {
|
|
|
|
expect(mapFaces({ ...faceStub.face1, person: null }, authStub.user1).person).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not map person if person does not match auth user id', () => {
|
|
|
|
expect(mapFaces(faceStub.face1, authStub.user1).person).toBeNull();
|
|
|
|
});
|
|
|
|
});
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|