Refactor API for albums feature (#155)

* Rename "shared" to "album"

Prepare moving "SharedAlbums" to "Albums"

* Update server album API endpoints

* Update mobile app album endpoints

Also add `putRequest` to mobile network.service

* Add GET album collection filter

- allow to filter by owner = 'mine' | 'their'
- make sharedWithUserIds no longer required when creating an album

* Rename remaining variables to "album"

* Add ParseMeUUIDPipe to validate uuid or `me`

* Add album params validation

* Update todo in mobile album service.

* Setup e2e testing

* Add user e2e tests

* Rename database host env variable to DB_HOST

* Add some `Album` e2e tests

Also fix issues found with the tests

* Force push (try to recover DB_HOST env)

* Rename db host env variable to `DB_HOSTNAME`

* Remove unnecessary `initDb` from test-utils

The current database.config is running the migrations:
`migrationsRun: true`

* Remove `initDb` usage from album e2e test

* Update GET albums filter to `shared`

- add filter by all / shared / not shared
- add response DTOs
- add GET albums e2e tests

* Update album e2e tests for user.service changes

* Update mobile app to use album response DTOs

* Refactor album-service DB into album-registry

- DB logic refactored into album-repository making it easier to test
- add some album-service unit tests
- add `clearMocks` to jest configuration

* Finish implementing album.service unit tests

* Rename response DTO

Make them consistent with rest of the project naming

* Update debug log messages in mobile network service

* Rename table `shared_albums` to `albums`

* Rename table `asset_shared_album`

* Rename Albums `sharedAssets` to `assets`

* Update tests to match updated "delete" response

* Fixed asset cannot be compared in Set by adding Equatable package

* Remove hero effect to fixed janky animation

Co-authored-by: Alex <alex.tran1502@gmail.com>
This commit is contained in:
Jaime Baez
2022-06-18 17:56:36 +02:00
committed by GitHub
parent 3511b69fc8
commit 517a3363d6
43 changed files with 1486 additions and 725 deletions

View File

@@ -0,0 +1,161 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import request from 'supertest';
import { clearDb, getAuthUser, authCustom } from './test-utils';
import { databaseConfig } from '@app/database/config/database.config';
import { AlbumModule } from '../src/api-v1/album/album.module';
import { CreateAlbumDto } from '../src/api-v1/album/dto/create-album.dto';
import { ImmichJwtModule } from '../src/modules/immich-jwt/immich-jwt.module';
import { AuthUserDto } from '../src/decorators/auth-user.decorator';
import { UserService } from '../src/api-v1/user/user.service';
import { UserModule } from '../src/api-v1/user/user.module';
function _createAlbum(app: INestApplication, data: CreateAlbumDto) {
return request(app.getHttpServer()).post('/album').send(data);
}
describe('Album', () => {
let app: INestApplication;
afterAll(async () => {
await clearDb();
await app.close();
});
describe('without auth', () => {
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AlbumModule, ImmichJwtModule, TypeOrmModule.forRoot(databaseConfig)],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
afterAll(async () => {
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 authUser: AuthUserDto;
let userService: UserService;
beforeAll(async () => {
const builder = Test.createTestingModule({
imports: [AlbumModule, UserModule, TypeOrmModule.forRoot(databaseConfig)],
});
authUser = getAuthUser(); // set default auth user
const moduleFixture: TestingModule = await authCustom(builder, () => authUser).compile();
app = moduleFixture.createNestApplication();
userService = app.get(UserService);
await app.init();
});
describe('with empty DB', () => {
afterEach(async () => {
await clearDb();
});
it('creates an album', async () => {
const data: CreateAlbumDto = {
albumName: 'first albbum',
};
const { status, body } = await _createAlbum(app, data);
expect(status).toEqual(201);
expect(body).toEqual(
expect.objectContaining({
ownerId: authUser.id,
albumName: data.albumName,
}),
);
});
});
describe('with albums in DB', () => {
const userOneShared = 'userOneShared';
const userOneNotShared = 'userOneNotShared';
const userTwoShared = 'userTwoShared';
const userTwoNotShared = 'userTwoNotShared';
let userOne: AuthUserDto;
let userTwo: AuthUserDto;
beforeAll(async () => {
// setup users
const result = await Promise.all([
userService.createUser({
email: 'one@test.com',
password: '1234',
firstName: 'one',
lastName: 'test',
}),
userService.createUser({
email: 'two@test.com',
password: '1234',
firstName: 'two',
lastName: 'test',
}),
]);
userOne = result[0];
userTwo = result[1];
// add user one albums
authUser = userOne;
await Promise.all([
_createAlbum(app, { albumName: userOneShared, sharedWithUserIds: [userTwo.id] }),
_createAlbum(app, { albumName: userOneNotShared }),
]);
// add user two albums
authUser = userTwo;
await Promise.all([
_createAlbum(app, { albumName: userTwoShared, sharedWithUserIds: [userOne.id] }),
_createAlbum(app, { albumName: userTwoNotShared }),
]);
// set user one as authed for next requests
authUser = userOne;
});
it('returns the album collection including owned and shared', async () => {
const { status, body } = await request(app.getHttpServer()).get('/album');
expect(status).toEqual(200);
expect(body).toHaveLength(3);
expect(body).toEqual(
expect.arrayContaining([
expect.objectContaining({ ownerId: userOne.id, albumName: userOneShared, shared: true }),
expect.objectContaining({ ownerId: userOne.id, albumName: userOneNotShared, shared: false }),
expect.objectContaining({ ownerId: userTwo.id, albumName: userTwoShared, shared: true }),
]),
);
});
it('returns the album collection filtered by shared', async () => {
const { status, body } = await request(app.getHttpServer()).get('/album?shared=true');
expect(status).toEqual(200);
expect(body).toHaveLength(2);
expect(body).toEqual(
expect.arrayContaining([
expect.objectContaining({ ownerId: userOne.id, albumName: userOneShared, shared: true }),
expect.objectContaining({ ownerId: userTwo.id, albumName: userTwoShared, shared: true }),
]),
);
});
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 }),
]),
);
});
});
});
});