2023-06-20 18:08:43 -07:00
|
|
|
import {
|
|
|
|
AlbumResponseDto,
|
|
|
|
AuthService,
|
|
|
|
CreateAlbumDto,
|
|
|
|
SharedLinkCreateDto,
|
|
|
|
SharedLinkResponseDto,
|
|
|
|
UserService,
|
|
|
|
} from '@app/domain';
|
2023-06-16 12:54:17 -07:00
|
|
|
import { AppModule } from '@app/immich/app.module';
|
2023-06-08 08:01:07 -07:00
|
|
|
import { AuthUserDto } from '@app/immich/decorators/auth-user.decorator';
|
2023-06-20 18:08:43 -07:00
|
|
|
import { SharedLinkType } from '@app/infra/entities';
|
2023-06-16 12:54:17 -07:00
|
|
|
import { INestApplication } from '@nestjs/common';
|
|
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
|
|
import request from 'supertest';
|
2022-07-16 21:43:31 -07:00
|
|
|
import { DataSource } from 'typeorm';
|
2023-06-16 12:54:17 -07:00
|
|
|
import { authCustom, clearDb, getAuthUser } from '../test/test-utils';
|
2022-06-18 08:56:36 -07:00
|
|
|
|
2023-03-25 19:46:48 -07: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;
|
|
|
|
}
|
|
|
|
|
2023-06-20 18:08:43 -07:00
|
|
|
async function _createAlbumSharedLink(app: INestApplication, data: Omit<SharedLinkCreateDto, 'type'>) {
|
|
|
|
const res = await request(app.getHttpServer())
|
|
|
|
.post('/shared-link')
|
|
|
|
.send({ ...data, type: SharedLinkType.ALBUM });
|
2023-03-25 19:46:48 -07:00
|
|
|
expect(res.status).toEqual(201);
|
|
|
|
return res.body as SharedLinkResponseDto;
|
2022-06-18 08:56:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('Album', () => {
|
|
|
|
let app: INestApplication;
|
2022-07-16 21:43:31 -07:00
|
|
|
let database: DataSource;
|
2022-06-18 08:56:36 -07:00
|
|
|
|
|
|
|
describe('without auth', () => {
|
|
|
|
beforeAll(async () => {
|
2023-01-31 11:11:49 -07:00
|
|
|
const moduleFixture: TestingModule = await Test.createTestingModule({ imports: [AppModule] }).compile();
|
2022-06-18 08:56:36 -07:00
|
|
|
|
|
|
|
app = moduleFixture.createNestApplication();
|
2022-07-16 21:43:31 -07:00
|
|
|
database = app.get(DataSource);
|
2022-06-18 08:56:36 -07:00
|
|
|
await app.init();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
2023-01-15 12:08:24 -07:00
|
|
|
await clearDb(database);
|
2022-06-18 08:56:36 -07: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 12:08:24 -07:00
|
|
|
let authService: AuthService;
|
2023-02-06 19:47:06 -07:00
|
|
|
let authUser: AuthUserDto;
|
2022-06-18 08:56:36 -07:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-01-31 11:11:49 -07:00
|
|
|
const builder = Test.createTestingModule({ imports: [AppModule] });
|
2023-02-06 19:47:06 -07:00
|
|
|
authUser = getAuthUser();
|
2022-06-18 08:56:36 -07:00
|
|
|
const moduleFixture: TestingModule = await authCustom(builder, () => authUser).compile();
|
|
|
|
|
|
|
|
app = moduleFixture.createNestApplication();
|
|
|
|
userService = app.get(UserService);
|
2023-01-15 12:08:24 -07:00
|
|
|
authService = app.get(AuthService);
|
2022-07-16 21:43:31 -07:00
|
|
|
database = app.get(DataSource);
|
2022-06-18 08:56:36 -07:00
|
|
|
await app.init();
|
|
|
|
});
|
|
|
|
|
2023-01-15 12:08:24 -07:00
|
|
|
afterAll(async () => {
|
|
|
|
await app.close();
|
|
|
|
});
|
|
|
|
|
2023-03-25 19:46:48 -07: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-06 19:47:06 -07:00
|
|
|
|
2023-03-25 19:46:48 -07:00
|
|
|
it('rejects invalid assetId param', async () => {
|
|
|
|
const { status } = await request(app.getHttpServer()).get('/album?assetId=invalid');
|
|
|
|
expect(status).toEqual(400);
|
|
|
|
});
|
2023-02-06 19:47:06 -07:00
|
|
|
|
2023-03-25 19:46:48 -07: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 08:56:36 -07:00
|
|
|
|
|
|
|
describe('with albums in DB', () => {
|
2023-03-25 19:46:48 -07:00
|
|
|
const userOneSharedUser = 'userOneSharedUser';
|
|
|
|
const userOneSharedLink = 'userOneSharedLink';
|
2022-06-18 08:56:36 -07:00
|
|
|
const userOneNotShared = 'userOneNotShared';
|
2023-03-25 19:46:48 -07:00
|
|
|
const userTwoSharedUser = 'userTwoSharedUser';
|
|
|
|
const userTwoSharedLink = 'userTwoSharedLink';
|
2022-06-18 08:56:36 -07:00
|
|
|
const userTwoNotShared = 'userTwoNotShared';
|
|
|
|
let userOne: AuthUserDto;
|
|
|
|
let userTwo: AuthUserDto;
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
// setup users
|
2023-01-15 12:08:24 -07: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 08:56:36 -07:00
|
|
|
// add user one albums
|
|
|
|
authUser = userOne;
|
2023-03-25 19:46:48 -07:00
|
|
|
const userOneAlbums = await Promise.all([
|
|
|
|
_createAlbum(app, { albumName: userOneSharedUser, sharedWithUserIds: [userTwo.id] }),
|
|
|
|
_createAlbum(app, { albumName: userOneSharedLink }),
|
2022-06-18 08:56:36 -07:00
|
|
|
_createAlbum(app, { albumName: userOneNotShared }),
|
|
|
|
]);
|
2023-03-25 19:46:48 -07:00
|
|
|
|
|
|
|
// add shared link to userOneSharedLink album
|
|
|
|
await _createAlbumSharedLink(app, { albumId: userOneAlbums[1].id });
|
|
|
|
|
2022-06-18 08:56:36 -07:00
|
|
|
// add user two albums
|
|
|
|
authUser = userTwo;
|
2023-03-25 19:46:48 -07:00
|
|
|
const userTwoAlbums = await Promise.all([
|
|
|
|
_createAlbum(app, { albumName: userTwoSharedUser, sharedWithUserIds: [userOne.id] }),
|
|
|
|
_createAlbum(app, { albumName: userTwoSharedLink }),
|
2022-06-18 08:56:36 -07:00
|
|
|
_createAlbum(app, { albumName: userTwoNotShared }),
|
|
|
|
]);
|
2023-03-25 19:46:48 -07:00
|
|
|
|
|
|
|
// add shared link to userTwoSharedLink album
|
|
|
|
await _createAlbumSharedLink(app, { albumId: userTwoAlbums[1].id });
|
|
|
|
|
2022-06-18 08:56:36 -07:00
|
|
|
// set user one as authed for next requests
|
|
|
|
authUser = userOne;
|
|
|
|
});
|
|
|
|
|
2023-01-15 12:08:24 -07:00
|
|
|
afterAll(async () => {
|
|
|
|
await clearDb(database);
|
|
|
|
});
|
|
|
|
|
2022-06-18 08:56:36 -07: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-25 19:46:48 -07:00
|
|
|
expect(body).toHaveLength(3);
|
2022-06-18 08:56:36 -07:00
|
|
|
expect(body).toEqual(
|
|
|
|
expect.arrayContaining([
|
2023-03-25 19:46:48 -07:00
|
|
|
expect.objectContaining({ ownerId: userOne.id, albumName: userOneSharedUser, shared: true }),
|
|
|
|
expect.objectContaining({ ownerId: userOne.id, albumName: userOneSharedLink, shared: true }),
|
2022-06-18 08:56:36 -07: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-25 19:46:48 -07:00
|
|
|
expect(body).toHaveLength(3);
|
2022-06-18 08:56:36 -07:00
|
|
|
expect(body).toEqual(
|
|
|
|
expect.arrayContaining([
|
2023-03-25 19:46:48 -07: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 08:56:36 -07: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-25 19:46:48 -07: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 08:56:36 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|