2024-02-19 22:34:18 -05:00
|
|
|
import { LoginResponseDto, createPartner } from '@immich/sdk';
|
|
|
|
|
import { createUserDto } from 'src/fixtures';
|
|
|
|
|
import { errorDto } from 'src/responses';
|
2024-03-07 10:14:36 -05:00
|
|
|
import { app, asBearerAuth, utils } from 'src/utils';
|
2023-09-11 17:56:38 +02:00
|
|
|
import request from 'supertest';
|
2024-02-19 22:34:18 -05:00
|
|
|
import { beforeAll, describe, expect, it } from 'vitest';
|
2023-09-11 17:56:38 +02:00
|
|
|
|
2024-05-22 13:24:57 -04:00
|
|
|
describe('/partners', () => {
|
2024-02-19 22:34:18 -05:00
|
|
|
let admin: LoginResponseDto;
|
2023-09-11 17:56:38 +02:00
|
|
|
let user1: LoginResponseDto;
|
|
|
|
|
let user2: LoginResponseDto;
|
2023-11-14 20:08:22 -05:00
|
|
|
let user3: LoginResponseDto;
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2024-03-07 10:14:36 -05:00
|
|
|
await utils.resetDatabase();
|
2023-10-18 18:02:42 -04:00
|
|
|
|
2024-03-07 10:14:36 -05:00
|
|
|
admin = await utils.adminSetup();
|
2023-09-11 17:56:38 +02:00
|
|
|
|
2023-11-14 20:08:22 -05:00
|
|
|
[user1, user2, user3] = await Promise.all([
|
2024-03-07 10:14:36 -05:00
|
|
|
utils.userSetup(admin.accessToken, createUserDto.user1),
|
|
|
|
|
utils.userSetup(admin.accessToken, createUserDto.user2),
|
|
|
|
|
utils.userSetup(admin.accessToken, createUserDto.user3),
|
2023-10-18 18:02:42 -04:00
|
|
|
]);
|
2023-11-14 20:08:22 -05:00
|
|
|
|
|
|
|
|
await Promise.all([
|
2024-02-29 11:26:55 -05:00
|
|
|
createPartner({ id: user2.userId }, { headers: asBearerAuth(user1.accessToken) }),
|
|
|
|
|
createPartner({ id: user1.userId }, { headers: asBearerAuth(user2.accessToken) }),
|
2023-11-14 20:08:22 -05:00
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-22 13:24:57 -04:00
|
|
|
describe('GET /partners', () => {
|
2023-09-11 17:56:38 +02:00
|
|
|
it('should require authentication', async () => {
|
2024-05-22 13:24:57 -04:00
|
|
|
const { status, body } = await request(app).get('/partners');
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
|
expect(status).toBe(401);
|
2024-02-19 22:34:18 -05:00
|
|
|
expect(body).toEqual(errorDto.unauthorized);
|
2023-09-11 17:56:38 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should get all partners shared by user', async () => {
|
2024-02-19 22:34:18 -05:00
|
|
|
const { status, body } = await request(app)
|
2024-05-22 13:24:57 -04:00
|
|
|
.get('/partners')
|
2023-09-11 17:56:38 +02:00
|
|
|
.set('Authorization', `Bearer ${user1.accessToken}`)
|
2024-02-19 22:34:18 -05:00
|
|
|
.query({ direction: 'shared-by' });
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
|
expect(status).toBe(200);
|
|
|
|
|
expect(body).toEqual([expect.objectContaining({ id: user2.userId })]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should get all partners that share with user', async () => {
|
2024-02-19 22:34:18 -05:00
|
|
|
const { status, body } = await request(app)
|
2024-05-22 13:24:57 -04:00
|
|
|
.get('/partners')
|
2023-09-11 17:56:38 +02:00
|
|
|
.set('Authorization', `Bearer ${user1.accessToken}`)
|
2024-02-19 22:34:18 -05:00
|
|
|
.query({ direction: 'shared-with' });
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
|
expect(status).toBe(200);
|
|
|
|
|
expect(body).toEqual([expect.objectContaining({ id: user2.userId })]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-22 13:24:57 -04:00
|
|
|
describe('POST /partners/:id', () => {
|
2023-09-11 17:56:38 +02:00
|
|
|
it('should require authentication', async () => {
|
2024-05-22 13:24:57 -04:00
|
|
|
const { status, body } = await request(app).post(`/partners/${user3.userId}`);
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
|
expect(status).toBe(401);
|
2024-02-19 22:34:18 -05:00
|
|
|
expect(body).toEqual(errorDto.unauthorized);
|
2023-09-11 17:56:38 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should share with new partner', async () => {
|
2024-02-19 22:34:18 -05:00
|
|
|
const { status, body } = await request(app)
|
2024-05-22 13:24:57 -04:00
|
|
|
.post(`/partners/${user3.userId}`)
|
2023-09-11 17:56:38 +02:00
|
|
|
.set('Authorization', `Bearer ${user1.accessToken}`);
|
|
|
|
|
|
|
|
|
|
expect(status).toBe(201);
|
2023-11-14 20:08:22 -05:00
|
|
|
expect(body).toEqual(expect.objectContaining({ id: user3.userId }));
|
2023-09-11 17:56:38 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not share with new partner if already sharing with this partner', async () => {
|
2024-02-19 22:34:18 -05:00
|
|
|
const { status, body } = await request(app)
|
2024-05-22 13:24:57 -04:00
|
|
|
.post(`/partners/${user2.userId}`)
|
2023-09-11 17:56:38 +02:00
|
|
|
.set('Authorization', `Bearer ${user1.accessToken}`);
|
|
|
|
|
|
|
|
|
|
expect(status).toBe(400);
|
2024-02-29 11:26:55 -05:00
|
|
|
expect(body).toEqual(expect.objectContaining({ message: 'Partner already exists' }));
|
2023-09-11 17:56:38 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-22 13:24:57 -04:00
|
|
|
describe('PUT /partners/:id', () => {
|
2023-11-11 15:06:19 -06:00
|
|
|
it('should require authentication', async () => {
|
2024-05-22 13:24:57 -04:00
|
|
|
const { status, body } = await request(app).put(`/partners/${user2.userId}`);
|
2023-11-11 15:06:19 -06:00
|
|
|
|
|
|
|
|
expect(status).toBe(401);
|
2024-02-19 22:34:18 -05:00
|
|
|
expect(body).toEqual(errorDto.unauthorized);
|
2023-11-11 15:06:19 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should update partner', async () => {
|
2024-02-19 22:34:18 -05:00
|
|
|
const { status, body } = await request(app)
|
2024-05-22 13:24:57 -04:00
|
|
|
.put(`/partners/${user2.userId}`)
|
2023-11-11 15:06:19 -06:00
|
|
|
.set('Authorization', `Bearer ${user1.accessToken}`)
|
|
|
|
|
.send({ inTimeline: false });
|
|
|
|
|
|
|
|
|
|
expect(status).toBe(200);
|
2024-02-29 11:26:55 -05:00
|
|
|
expect(body).toEqual(expect.objectContaining({ id: user2.userId, inTimeline: false }));
|
2023-11-11 15:06:19 -06:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-22 13:24:57 -04:00
|
|
|
describe('DELETE /partners/:id', () => {
|
2023-09-11 17:56:38 +02:00
|
|
|
it('should require authentication', async () => {
|
2024-05-22 13:24:57 -04:00
|
|
|
const { status, body } = await request(app).delete(`/partners/${user3.userId}`);
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
|
expect(status).toBe(401);
|
2024-02-19 22:34:18 -05:00
|
|
|
expect(body).toEqual(errorDto.unauthorized);
|
2023-09-11 17:56:38 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should delete partner', async () => {
|
2024-02-19 22:34:18 -05:00
|
|
|
const { status } = await request(app)
|
2024-05-22 13:24:57 -04:00
|
|
|
.delete(`/partners/${user3.userId}`)
|
2023-09-11 17:56:38 +02:00
|
|
|
.set('Authorization', `Bearer ${user1.accessToken}`);
|
|
|
|
|
|
2025-08-08 15:56:37 -04:00
|
|
|
expect(status).toBe(204);
|
2023-09-11 17:56:38 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw a bad request if partner not found', async () => {
|
2024-02-19 22:34:18 -05:00
|
|
|
const { status, body } = await request(app)
|
2024-05-22 13:24:57 -04:00
|
|
|
.delete(`/partners/${user3.userId}`)
|
2023-09-11 17:56:38 +02:00
|
|
|
.set('Authorization', `Bearer ${user1.accessToken}`);
|
|
|
|
|
|
|
|
|
|
expect(status).toBe(400);
|
2024-02-29 11:26:55 -05:00
|
|
|
expect(body).toEqual(expect.objectContaining({ message: 'Partner not found' }));
|
2023-09-11 17:56:38 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|