2024-02-19 22:34:18 -05:00
|
|
|
import { LoginResponseDto, createPartner } from '@immich/sdk';
|
|
|
|
|
import { createUserDto } from 'src/fixtures';
|
|
|
|
|
import { errorDto } from 'src/responses';
|
|
|
|
|
import { apiUtils, app, asBearerAuth, dbUtils } 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-02-19 22:34:18 -05:00
|
|
|
describe('/partner', () => {
|
|
|
|
|
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-02-19 22:34:18 -05:00
|
|
|
apiUtils.setup();
|
|
|
|
|
await dbUtils.reset();
|
2023-10-18 18:02:42 -04:00
|
|
|
|
2024-02-19 22:34:18 -05:00
|
|
|
admin = await apiUtils.adminSetup();
|
2023-09-11 17:56:38 +02:00
|
|
|
|
2023-11-14 20:08:22 -05:00
|
|
|
[user1, user2, user3] = await Promise.all([
|
2024-02-19 22:34:18 -05:00
|
|
|
apiUtils.userSetup(admin.accessToken, createUserDto.user1),
|
|
|
|
|
apiUtils.userSetup(admin.accessToken, createUserDto.user2),
|
|
|
|
|
apiUtils.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
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2023-09-11 17:56:38 +02:00
|
|
|
describe('GET /partner', () => {
|
|
|
|
|
it('should require authentication', async () => {
|
2024-02-19 22:34:18 -05:00
|
|
|
const { status, body } = await request(app).get('/partner');
|
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)
|
2023-09-11 17:56:38 +02:00
|
|
|
.get('/partner')
|
|
|
|
|
.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)
|
2023-09-11 17:56:38 +02:00
|
|
|
.get('/partner')
|
|
|
|
|
.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 })]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('POST /partner/:id', () => {
|
|
|
|
|
it('should require authentication', async () => {
|
2024-02-29 11:26:55 -05:00
|
|
|
const { status, body } = await request(app).post(`/partner/${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)
|
2023-11-14 20:08:22 -05:00
|
|
|
.post(`/partner/${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)
|
2023-09-11 17:56:38 +02:00
|
|
|
.post(`/partner/${user2.userId}`)
|
|
|
|
|
.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
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-11 15:06:19 -06:00
|
|
|
describe('PUT /partner/:id', () => {
|
|
|
|
|
it('should require authentication', async () => {
|
2024-02-29 11:26:55 -05:00
|
|
|
const { status, body } = await request(app).put(`/partner/${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)
|
2023-11-11 15:06:19 -06:00
|
|
|
.put(`/partner/${user2.userId}`)
|
|
|
|
|
.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
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-09-11 17:56:38 +02:00
|
|
|
describe('DELETE /partner/:id', () => {
|
|
|
|
|
it('should require authentication', async () => {
|
2024-02-29 11:26:55 -05:00
|
|
|
const { status, body } = await request(app).delete(`/partner/${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)
|
2023-11-14 20:08:22 -05:00
|
|
|
.delete(`/partner/${user3.userId}`)
|
2023-09-11 17:56:38 +02:00
|
|
|
.set('Authorization', `Bearer ${user1.accessToken}`);
|
|
|
|
|
|
|
|
|
|
expect(status).toBe(200);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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)
|
2023-11-14 20:08:22 -05:00
|
|
|
.delete(`/partner/${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
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|