2023-09-27 22:46:46 +02:00
|
|
|
import { IPersonRepository, LoginResponseDto } from '@app/domain';
|
2023-10-06 23:32:28 +02:00
|
|
|
import { PersonController } from '@app/immich';
|
2023-09-11 17:56:38 +02:00
|
|
|
import { PersonEntity } from '@app/infra/entities';
|
2023-08-18 22:10:29 +02:00
|
|
|
import { INestApplication } from '@nestjs/common';
|
2023-09-11 17:56:38 +02:00
|
|
|
import { api } from '@test/api';
|
|
|
|
import { db } from '@test/db';
|
|
|
|
import { errorStub, uuidStub } from '@test/fixtures';
|
2023-10-19 00:02:42 +02:00
|
|
|
import { testApp } from '@test/test-utils';
|
2023-08-18 22:10:29 +02:00
|
|
|
import request from 'supertest';
|
|
|
|
|
|
|
|
describe(`${PersonController.name}`, () => {
|
|
|
|
let app: INestApplication;
|
|
|
|
let server: any;
|
|
|
|
let loginResponse: LoginResponseDto;
|
|
|
|
let accessToken: string;
|
2023-09-11 17:56:38 +02:00
|
|
|
let personRepository: IPersonRepository;
|
|
|
|
let visiblePerson: PersonEntity;
|
|
|
|
let hiddenPerson: PersonEntity;
|
2023-08-18 22:10:29 +02:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-10-19 00:02:42 +02:00
|
|
|
[server, app] = await testApp.create();
|
2023-09-11 17:56:38 +02:00
|
|
|
personRepository = app.get<IPersonRepository>(IPersonRepository);
|
2023-08-18 22:10:29 +02:00
|
|
|
});
|
|
|
|
|
2023-10-19 00:02:42 +02:00
|
|
|
afterAll(async () => {
|
|
|
|
await testApp.teardown();
|
|
|
|
});
|
|
|
|
|
2023-08-18 22:10:29 +02:00
|
|
|
beforeEach(async () => {
|
|
|
|
await db.reset();
|
2023-09-11 17:56:38 +02:00
|
|
|
await api.authApi.adminSignUp(server);
|
|
|
|
loginResponse = await api.authApi.adminLogin(server);
|
2023-08-18 22:10:29 +02:00
|
|
|
accessToken = loginResponse.accessToken;
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
const faceAsset = await api.assetApi.upload(server, accessToken, 'face_asset');
|
|
|
|
visiblePerson = await personRepository.create({
|
|
|
|
ownerId: loginResponse.userId,
|
|
|
|
name: 'visible_person',
|
|
|
|
thumbnailPath: '/thumbnail/face_asset',
|
|
|
|
});
|
2023-09-27 22:46:46 +02:00
|
|
|
await personRepository.createFace({ assetId: faceAsset.id, personId: visiblePerson.id });
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
hiddenPerson = await personRepository.create({
|
|
|
|
ownerId: loginResponse.userId,
|
|
|
|
name: 'hidden_person',
|
|
|
|
isHidden: true,
|
|
|
|
thumbnailPath: '/thumbnail/face_asset',
|
|
|
|
});
|
2023-09-27 22:46:46 +02:00
|
|
|
await personRepository.createFace({ assetId: faceAsset.id, personId: hiddenPerson.id });
|
2023-08-18 22:10:29 +02:00
|
|
|
});
|
|
|
|
|
2023-09-11 17:56:38 +02:00
|
|
|
describe('GET /person', () => {
|
|
|
|
beforeEach(async () => {});
|
|
|
|
|
|
|
|
it('should require authentication', async () => {
|
|
|
|
const { status, body } = await request(server).get('/person');
|
|
|
|
|
|
|
|
expect(status).toBe(401);
|
|
|
|
expect(body).toEqual(errorStub.unauthorized);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return all people (including hidden)', async () => {
|
|
|
|
const { status, body } = await request(server)
|
|
|
|
.get('/person')
|
|
|
|
.set('Authorization', `Bearer ${accessToken}`)
|
|
|
|
.query({ withHidden: true });
|
|
|
|
|
|
|
|
expect(status).toBe(200);
|
|
|
|
expect(body).toEqual({
|
|
|
|
total: 2,
|
|
|
|
visible: 1,
|
|
|
|
people: [
|
|
|
|
expect.objectContaining({ name: 'visible_person' }),
|
|
|
|
expect.objectContaining({ name: 'hidden_person' }),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return only visible people', async () => {
|
|
|
|
const { status, body } = await request(server).get('/person').set('Authorization', `Bearer ${accessToken}`);
|
|
|
|
|
|
|
|
expect(status).toBe(200);
|
|
|
|
expect(body).toEqual({
|
|
|
|
total: 1,
|
|
|
|
visible: 1,
|
|
|
|
people: [expect.objectContaining({ name: 'visible_person' })],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('GET /person/:id', () => {
|
|
|
|
it('should require authentication', async () => {
|
|
|
|
const { status, body } = await request(server).get(`/person/${uuidStub.notFound}`);
|
|
|
|
|
|
|
|
expect(status).toBe(401);
|
|
|
|
expect(body).toEqual(errorStub.unauthorized);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw error if person with id does not exist', async () => {
|
|
|
|
const { status, body } = await request(server)
|
|
|
|
.get(`/person/${uuidStub.notFound}`)
|
|
|
|
.set('Authorization', `Bearer ${accessToken}`);
|
|
|
|
|
|
|
|
expect(status).toBe(400);
|
2023-09-20 13:16:33 +02:00
|
|
|
expect(body).toEqual(errorStub.badRequest());
|
2023-09-11 17:56:38 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return person information', async () => {
|
|
|
|
const { status, body } = await request(server)
|
|
|
|
.get(`/person/${visiblePerson.id}`)
|
|
|
|
.set('Authorization', `Bearer ${accessToken}`);
|
|
|
|
|
|
|
|
expect(status).toBe(200);
|
|
|
|
expect(body).toEqual(expect.objectContaining({ id: visiblePerson.id }));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-08-18 22:10:29 +02:00
|
|
|
describe('PUT /person/:id', () => {
|
|
|
|
it('should require authentication', async () => {
|
|
|
|
const { status, body } = await request(server).put(`/person/${uuidStub.notFound}`);
|
|
|
|
expect(status).toBe(401);
|
|
|
|
expect(body).toEqual(errorStub.unauthorized);
|
|
|
|
});
|
|
|
|
|
2023-09-20 13:16:33 +02:00
|
|
|
for (const { key, type } of [
|
|
|
|
{ key: 'name', type: 'string' },
|
|
|
|
{ key: 'featureFaceAssetId', type: 'string' },
|
|
|
|
{ key: 'isHidden', type: 'boolean value' },
|
|
|
|
]) {
|
2023-09-01 18:40:00 +02:00
|
|
|
it(`should not allow null ${key}`, async () => {
|
|
|
|
const { status, body } = await request(server)
|
2023-09-11 17:56:38 +02:00
|
|
|
.put(`/person/${visiblePerson.id}`)
|
2023-09-01 18:40:00 +02:00
|
|
|
.set('Authorization', `Bearer ${accessToken}`)
|
|
|
|
.send({ [key]: null });
|
|
|
|
expect(status).toBe(400);
|
2023-09-20 13:16:33 +02:00
|
|
|
expect(body).toEqual(errorStub.badRequest([`${key} must be a ${type}`]));
|
2023-09-01 18:40:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should not accept invalid birth dates', async () => {
|
2023-09-20 13:16:33 +02:00
|
|
|
for (const { birthDate, response } of [
|
2023-10-30 17:48:38 +02:00
|
|
|
{ birthDate: false, response: 'Not found or no person.write access' },
|
2023-09-20 13:16:33 +02:00
|
|
|
{ birthDate: 'false', response: ['birthDate must be a Date instance'] },
|
2023-10-30 17:48:38 +02:00
|
|
|
{ birthDate: '123567', response: 'Not found or no person.write access' },
|
|
|
|
{ birthDate: 123567, response: 'Not found or no person.write access' },
|
2023-09-20 13:16:33 +02:00
|
|
|
]) {
|
2023-08-18 22:10:29 +02:00
|
|
|
const { status, body } = await request(server)
|
|
|
|
.put(`/person/${uuidStub.notFound}`)
|
|
|
|
.set('Authorization', `Bearer ${accessToken}`)
|
|
|
|
.send({ birthDate });
|
|
|
|
expect(status).toBe(400);
|
2023-09-20 13:16:33 +02:00
|
|
|
expect(body).toEqual(errorStub.badRequest(response));
|
2023-08-18 22:10:29 +02:00
|
|
|
}
|
|
|
|
});
|
2023-09-01 18:40:00 +02:00
|
|
|
|
2023-08-18 22:10:29 +02:00
|
|
|
it('should update a date of birth', async () => {
|
|
|
|
const { status, body } = await request(server)
|
2023-09-11 17:56:38 +02:00
|
|
|
.put(`/person/${visiblePerson.id}`)
|
2023-08-18 22:10:29 +02:00
|
|
|
.set('Authorization', `Bearer ${accessToken}`)
|
|
|
|
.send({ birthDate: '1990-01-01T05:00:00.000Z' });
|
|
|
|
expect(status).toBe(200);
|
|
|
|
expect(body).toMatchObject({ birthDate: '1990-01-01' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should clear a date of birth', async () => {
|
|
|
|
const person = await personRepository.create({
|
|
|
|
birthDate: new Date('1990-01-01'),
|
|
|
|
ownerId: loginResponse.userId,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(person.birthDate).toBeDefined();
|
|
|
|
|
|
|
|
const { status, body } = await request(server)
|
|
|
|
.put(`/person/${person.id}`)
|
|
|
|
.set('Authorization', `Bearer ${accessToken}`)
|
|
|
|
.send({ birthDate: null });
|
|
|
|
expect(status).toBe(200);
|
|
|
|
expect(body).toMatchObject({ birthDate: null });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|