2022-06-18 17:56:36 +02:00
|
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
|
|
import { INestApplication } from '@nestjs/common';
|
|
|
|
import request from 'supertest';
|
2023-06-08 17:01:07 +02:00
|
|
|
import { clearDb, getAuthUser, authCustom } from '../test/test-utils';
|
2023-05-25 04:10:45 +02:00
|
|
|
import { CreateAlbumDto } from '@app/domain';
|
2023-06-08 17:01:07 +02:00
|
|
|
import { CreateAlbumShareLinkDto } from '@app/immich/api-v1/album/dto/create-album-shared-link.dto';
|
|
|
|
import { AuthUserDto } from '@app/immich/decorators/auth-user.decorator';
|
2023-03-26 04:46:48 +02:00
|
|
|
import { AlbumResponseDto, AuthService, SharedLinkResponseDto, UserService } from '@app/domain';
|
2022-07-17 06:43:31 +02:00
|
|
|
import { DataSource } from 'typeorm';
|
2023-06-08 17:01:07 +02:00
|
|
|
import { AppModule } from '@app/immich/app.module';
|
2022-06-18 17:56:36 +02:00
|
|
|
|
2023-03-26 04:46:48 +02:00
|
|
|
async function _createAlbum(app: INestApplication, data: CreateAlbumDto) {
|
|
|
|
const res = await request(app.getHttpServer()).post('/album').send(data);
|
|
|
|
expect(res.status).toEqual(201);
|
|
|
|
return res.body as AlbumResponseDto;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _createAlbumSharedLink(app: INestApplication, data: CreateAlbumShareLinkDto) {
|
|
|
|
const res = await request(app.getHttpServer()).post('/album/create-shared-link').send(data);
|
|
|
|
expect(res.status).toEqual(201);
|
|
|
|
return res.body as SharedLinkResponseDto;
|
2022-06-18 17:56:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('Album', () => {
|
|
|
|
let app: INestApplication;
|
2022-07-17 06:43:31 +02:00
|
|
|
let database: DataSource;
|
2022-06-18 17:56:36 +02:00
|
|
|
|
|
|
|
describe('without auth', () => {
|
|
|
|
beforeAll(async () => {
|
2023-01-31 20:11:49 +02:00
|
|
|
const moduleFixture: TestingModule = await Test.createTestingModule({ imports: [AppModule] }).compile();
|
2022-06-18 17:56:36 +02:00
|
|
|
|
|
|
|
app = moduleFixture.createNestApplication();
|
2022-07-17 06:43:31 +02:00
|
|
|
database = app.get(DataSource);
|
2022-06-18 17:56:36 +02:00
|
|
|
await app.init();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
2023-01-15 21:08:24 +02:00
|
|
|
await clearDb(database);
|
2022-06-18 17:56:36 +02:00
|
|
|
await app.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('prevents fetching albums if not auth', async () => {
|
|
|
|
const { status } = await request(app.getHttpServer()).get('/album');
|
|
|
|
expect(status).toEqual(401);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with auth', () => {
|
|
|
|
let userService: UserService;
|
2023-01-15 21:08:24 +02:00
|
|
|
let authService: AuthService;
|
2023-02-07 04:47:06 +02:00
|
|
|
let authUser: AuthUserDto;
|
2022-06-18 17:56:36 +02:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-01-31 20:11:49 +02:00
|
|
|
const builder = Test.createTestingModule({ imports: [AppModule] });
|
2023-02-07 04:47:06 +02:00
|
|
|
authUser = getAuthUser();
|
2022-06-18 17:56:36 +02:00
|
|
|
const moduleFixture: TestingModule = await authCustom(builder, () => authUser).compile();
|
|
|
|
|
|
|
|
app = moduleFixture.createNestApplication();
|
|
|
|
userService = app.get(UserService);
|
2023-01-15 21:08:24 +02:00
|
|
|
authService = app.get(AuthService);
|
2022-07-17 06:43:31 +02:00
|
|
|
database = app.get(DataSource);
|
2022-06-18 17:56:36 +02:00
|
|
|
await app.init();
|
|
|
|
});
|
|
|
|
|
2023-01-15 21:08:24 +02:00
|
|
|
afterAll(async () => {
|
|
|
|
await app.close();
|
|
|
|
});
|
|
|
|
|
2023-03-26 04:46:48 +02:00
|
|
|
describe('with empty DB', () => {
|
|
|
|
it('rejects invalid shared param', async () => {
|
|
|
|
const { status } = await request(app.getHttpServer()).get('/album?shared=invalid');
|
|
|
|
expect(status).toEqual(400);
|
|
|
|
});
|
2023-02-07 04:47:06 +02:00
|
|
|
|
2023-03-26 04:46:48 +02:00
|
|
|
it('rejects invalid assetId param', async () => {
|
|
|
|
const { status } = await request(app.getHttpServer()).get('/album?assetId=invalid');
|
|
|
|
expect(status).toEqual(400);
|
|
|
|
});
|
2023-02-07 04:47:06 +02:00
|
|
|
|
2023-03-26 04:46:48 +02:00
|
|
|
// TODO - Until someone figure out how to passed in a logged in user to the request.
|
|
|
|
// it('creates an album', async () => {
|
|
|
|
// const data: CreateAlbumDto = {
|
|
|
|
// albumName: 'first albbum',
|
|
|
|
// };
|
|
|
|
// const body = await _createAlbum(app, data);
|
|
|
|
// expect(body).toEqual(
|
|
|
|
// expect.objectContaining({
|
|
|
|
// ownerId: authUser.id,
|
|
|
|
// albumName: data.albumName,
|
|
|
|
// }),
|
|
|
|
// );
|
|
|
|
// });
|
|
|
|
});
|
2022-06-18 17:56:36 +02:00
|
|
|
|
|
|
|
describe('with albums in DB', () => {
|
2023-03-26 04:46:48 +02:00
|
|
|
const userOneSharedUser = 'userOneSharedUser';
|
|
|
|
const userOneSharedLink = 'userOneSharedLink';
|
2022-06-18 17:56:36 +02:00
|
|
|
const userOneNotShared = 'userOneNotShared';
|
2023-03-26 04:46:48 +02:00
|
|
|
const userTwoSharedUser = 'userTwoSharedUser';
|
|
|
|
const userTwoSharedLink = 'userTwoSharedLink';
|
2022-06-18 17:56:36 +02:00
|
|
|
const userTwoNotShared = 'userTwoNotShared';
|
|
|
|
let userOne: AuthUserDto;
|
|
|
|
let userTwo: AuthUserDto;
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
// setup users
|
2023-01-15 21:08:24 +02:00
|
|
|
const adminSignUpDto = await authService.adminSignUp({
|
|
|
|
email: 'one@test.com',
|
|
|
|
password: '1234',
|
|
|
|
firstName: 'one',
|
|
|
|
lastName: 'test',
|
|
|
|
});
|
|
|
|
userOne = { ...adminSignUpDto, isAdmin: true }; // TODO: find out why adminSignUp doesn't have isAdmin (maybe can just return UserResponseDto)
|
|
|
|
|
|
|
|
userTwo = await userService.createUser({
|
|
|
|
email: 'two@test.com',
|
|
|
|
password: '1234',
|
|
|
|
firstName: 'two',
|
|
|
|
lastName: 'test',
|
|
|
|
});
|
|
|
|
|
2022-06-18 17:56:36 +02:00
|
|
|
// add user one albums
|
|
|
|
authUser = userOne;
|
2023-03-26 04:46:48 +02:00
|
|
|
const userOneAlbums = await Promise.all([
|
|
|
|
_createAlbum(app, { albumName: userOneSharedUser, sharedWithUserIds: [userTwo.id] }),
|
|
|
|
_createAlbum(app, { albumName: userOneSharedLink }),
|
2022-06-18 17:56:36 +02:00
|
|
|
_createAlbum(app, { albumName: userOneNotShared }),
|
|
|
|
]);
|
2023-03-26 04:46:48 +02:00
|
|
|
|
|
|
|
// add shared link to userOneSharedLink album
|
|
|
|
await _createAlbumSharedLink(app, { albumId: userOneAlbums[1].id });
|
|
|
|
|
2022-06-18 17:56:36 +02:00
|
|
|
// add user two albums
|
|
|
|
authUser = userTwo;
|
2023-03-26 04:46:48 +02:00
|
|
|
const userTwoAlbums = await Promise.all([
|
|
|
|
_createAlbum(app, { albumName: userTwoSharedUser, sharedWithUserIds: [userOne.id] }),
|
|
|
|
_createAlbum(app, { albumName: userTwoSharedLink }),
|
2022-06-18 17:56:36 +02:00
|
|
|
_createAlbum(app, { albumName: userTwoNotShared }),
|
|
|
|
]);
|
2023-03-26 04:46:48 +02:00
|
|
|
|
|
|
|
// add shared link to userTwoSharedLink album
|
|
|
|
await _createAlbumSharedLink(app, { albumId: userTwoAlbums[1].id });
|
|
|
|
|
2022-06-18 17:56:36 +02:00
|
|
|
// set user one as authed for next requests
|
|
|
|
authUser = userOne;
|
|
|
|
});
|
|
|
|
|
2023-01-15 21:08:24 +02:00
|
|
|
afterAll(async () => {
|
|
|
|
await clearDb(database);
|
|
|
|
});
|
|
|
|
|
2022-06-18 17:56:36 +02:00
|
|
|
it('returns the album collection including owned and shared', async () => {
|
|
|
|
const { status, body } = await request(app.getHttpServer()).get('/album');
|
|
|
|
expect(status).toEqual(200);
|
2023-03-26 04:46:48 +02:00
|
|
|
expect(body).toHaveLength(3);
|
2022-06-18 17:56:36 +02:00
|
|
|
expect(body).toEqual(
|
|
|
|
expect.arrayContaining([
|
2023-03-26 04:46:48 +02:00
|
|
|
expect.objectContaining({ ownerId: userOne.id, albumName: userOneSharedUser, shared: true }),
|
|
|
|
expect.objectContaining({ ownerId: userOne.id, albumName: userOneSharedLink, shared: true }),
|
2022-06-18 17:56:36 +02:00
|
|
|
expect.objectContaining({ ownerId: userOne.id, albumName: userOneNotShared, shared: false }),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the album collection filtered by shared', async () => {
|
|
|
|
const { status, body } = await request(app.getHttpServer()).get('/album?shared=true');
|
|
|
|
expect(status).toEqual(200);
|
2023-03-26 04:46:48 +02:00
|
|
|
expect(body).toHaveLength(3);
|
2022-06-18 17:56:36 +02:00
|
|
|
expect(body).toEqual(
|
|
|
|
expect.arrayContaining([
|
2023-03-26 04:46:48 +02:00
|
|
|
expect.objectContaining({ ownerId: userOne.id, albumName: userOneSharedUser, shared: true }),
|
|
|
|
expect.objectContaining({ ownerId: userOne.id, albumName: userOneSharedLink, shared: true }),
|
|
|
|
expect.objectContaining({ ownerId: userTwo.id, albumName: userTwoSharedUser, shared: true }),
|
2022-06-18 17:56:36 +02:00
|
|
|
]),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the album collection filtered by NOT shared', async () => {
|
|
|
|
const { status, body } = await request(app.getHttpServer()).get('/album?shared=false');
|
|
|
|
expect(status).toEqual(200);
|
|
|
|
expect(body).toHaveLength(1);
|
|
|
|
expect(body).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
expect.objectContaining({ ownerId: userOne.id, albumName: userOneNotShared, shared: false }),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
});
|
2023-03-26 04:46:48 +02:00
|
|
|
|
|
|
|
// TODO: Add asset to album and test if it returns correctly.
|
|
|
|
it('returns the album collection filtered by assetId', async () => {
|
|
|
|
const { status, body } = await request(app.getHttpServer()).get(
|
|
|
|
'/album?assetId=ecb120db-45a2-4a65-9293-51476f0d8790',
|
|
|
|
);
|
|
|
|
expect(status).toEqual(200);
|
|
|
|
expect(body).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: Add asset to album and test if it returns correctly.
|
|
|
|
it('returns the album collection filtered by assetId and ignores shared=true', async () => {
|
|
|
|
const { status, body } = await request(app.getHttpServer()).get(
|
|
|
|
'/album?shared=true&assetId=ecb120db-45a2-4a65-9293-51476f0d8790',
|
|
|
|
);
|
|
|
|
expect(status).toEqual(200);
|
|
|
|
expect(body).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: Add asset to album and test if it returns correctly.
|
|
|
|
it('returns the album collection filtered by assetId and ignores shared=false', async () => {
|
|
|
|
const { status, body } = await request(app.getHttpServer()).get(
|
|
|
|
'/album?shared=false&assetId=ecb120db-45a2-4a65-9293-51476f0d8790',
|
|
|
|
);
|
|
|
|
expect(status).toEqual(200);
|
|
|
|
expect(body).toHaveLength(0);
|
|
|
|
});
|
2022-06-18 17:56:36 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|