diff --git a/server/test/db/index.ts b/server/test/db/index.ts deleted file mode 100644 index dd92f0e27..000000000 --- a/server/test/db/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { dataSource } from '@app/infra'; - -export const db = { - reset: async () => { - if (!dataSource.isInitialized) { - await dataSource.initialize(); - } - - await dataSource.transaction(async (em) => { - for (const entity of dataSource.entityMetadatas) { - if (entity.tableName === 'users') { - continue; - } - await em.query(`DELETE FROM ${entity.tableName} CASCADE;`); - } - await em.query(`DELETE FROM "users" CASCADE;`); - }); - }, - disconnect: async () => { - if (dataSource.isInitialized) { - await dataSource.destroy(); - } - }, -}; diff --git a/server/test/e2e/activity.e2e-spec.ts b/server/test/e2e/activity.e2e-spec.ts index 726dc64ed..3fe38e715 100644 --- a/server/test/e2e/activity.e2e-spec.ts +++ b/server/test/e2e/activity.e2e-spec.ts @@ -3,8 +3,7 @@ import { ActivityController } from '@app/immich'; import { AssetFileUploadResponseDto } from '@app/immich/api-v1/asset/response-dto/asset-file-upload-response.dto'; import { ActivityEntity } from '@app/infra/entities'; import { api } from '@test/api'; -import { db } from '@test/db'; -import { errorStub, uuidStub } from '@test/fixtures'; +import { errorStub, userDto, uuidStub } from '@test/fixtures'; import { testApp } from '@test/test-utils'; import request from 'supertest'; @@ -17,14 +16,13 @@ describe(`${ActivityController.name} (e2e)`, () => { beforeAll(async () => { [server] = await testApp.create(); - await db.reset(); + await testApp.reset(); await api.authApi.adminSignUp(server); admin = await api.authApi.adminLogin(server); asset = await api.assetApi.upload(server, admin.accessToken, 'example'); - const credentials = { email: 'user1@immich.app', password: 'Password123' }; - await api.userApi.create(server, admin.accessToken, { ...credentials, name: 'User 1' }); - nonOwner = await api.authApi.login(server, credentials); + await api.userApi.create(server, admin.accessToken, userDto.user1); + nonOwner = await api.authApi.login(server, userDto.user1); album = await api.albumApi.create(server, admin.accessToken, { albumName: 'Album 1', diff --git a/server/test/e2e/album.e2e-spec.ts b/server/test/e2e/album.e2e-spec.ts index 8c372a421..f5f6bb66e 100644 --- a/server/test/e2e/album.e2e-spec.ts +++ b/server/test/e2e/album.e2e-spec.ts @@ -3,8 +3,7 @@ import { AlbumController } from '@app/immich'; import { AssetFileUploadResponseDto } from '@app/immich/api-v1/asset/response-dto/asset-file-upload-response.dto'; import { SharedLinkType } from '@app/infra/entities'; import { api } from '@test/api'; -import { db } from '@test/db'; -import { errorStub, uuidStub } from '@test/fixtures'; +import { errorStub, userDto, uuidStub } from '@test/fixtures'; import { testApp } from '@test/test-utils'; import request from 'supertest'; @@ -33,26 +32,18 @@ describe(`${AlbumController.name} (e2e)`, () => { }); beforeEach(async () => { - await db.reset(); + await testApp.reset(); await api.authApi.adminSignUp(server); admin = await api.authApi.adminLogin(server); await Promise.all([ - api.userApi.create(server, admin.accessToken, { - email: 'user1@immich.app', - password: 'Password123', - name: 'User 1', - }), - api.userApi.create(server, admin.accessToken, { - email: 'user2@immich.app', - password: 'Password123', - name: 'User 2', - }), + api.userApi.create(server, admin.accessToken, userDto.user1), + api.userApi.create(server, admin.accessToken, userDto.user2), ]); [user1, user2] = await Promise.all([ - api.authApi.login(server, { email: 'user1@immich.app', password: 'Password123' }), - api.authApi.login(server, { email: 'user2@immich.app', password: 'Password123' }), + api.authApi.login(server, userDto.user1), + api.authApi.login(server, userDto.user2), ]); user1Asset = await api.assetApi.upload(server, user1.accessToken, 'example'); diff --git a/server/test/e2e/asset.e2e-spec.ts b/server/test/e2e/asset.e2e-spec.ts index 8fe56f2bc..53468a480 100644 --- a/server/test/e2e/asset.e2e-spec.ts +++ b/server/test/e2e/asset.e2e-spec.ts @@ -14,24 +14,12 @@ import { AssetEntity, AssetType, LibraryType, SharedLinkType } from '@app/infra/ import { AssetRepository } from '@app/infra/repositories'; import { INestApplication } from '@nestjs/common'; import { api } from '@test/api'; -import { errorStub, uuidStub } from '@test/fixtures'; +import { errorStub, userDto, uuidStub } from '@test/fixtures'; import { testApp } from '@test/test-utils'; import { randomBytes } from 'crypto'; import { DateTime } from 'luxon'; import request from 'supertest'; -const user1Dto = { - email: 'user1@immich.app', - password: 'Password123', - name: 'User 1', -}; - -const user2Dto = { - email: 'user2@immich.app', - password: 'Password123', - name: 'User 2', -}; - const today = DateTime.fromObject({ year: 2023, month: 11, day: 3 }); const yesterday = today.minus({ days: 1 }); @@ -104,13 +92,13 @@ describe(`${AssetController.name} (e2e)`, () => { const admin = await api.authApi.adminLogin(server); await Promise.all([ - api.userApi.create(server, admin.accessToken, user1Dto), - api.userApi.create(server, admin.accessToken, user2Dto), + api.userApi.create(server, admin.accessToken, userDto.user1), + api.userApi.create(server, admin.accessToken, userDto.user2), ]); [user1, user2] = await Promise.all([ - api.authApi.login(server, { email: user1Dto.email, password: user1Dto.password }), - api.authApi.login(server, { email: user2Dto.email, password: user2Dto.password }), + api.authApi.login(server, userDto.user1), + api.authApi.login(server, userDto.user2), ]); const [user1Libraries, user2Libraries] = await Promise.all([ diff --git a/server/test/e2e/auth.e2e-spec.ts b/server/test/e2e/auth.e2e-spec.ts index 97a8551ff..f7ab84772 100644 --- a/server/test/e2e/auth.e2e-spec.ts +++ b/server/test/e2e/auth.e2e-spec.ts @@ -1,6 +1,5 @@ import { AuthController } from '@app/immich'; import { api } from '@test/api'; -import { db } from '@test/db'; import { adminSignupStub, changePasswordStub, @@ -49,7 +48,7 @@ describe(`${AuthController.name} (e2e)`, () => { }); beforeEach(async () => { - await db.reset(); + await testApp.reset(); await api.authApi.adminSignUp(server); const response = await api.authApi.adminLogin(server); accessToken = response.accessToken; @@ -57,7 +56,7 @@ describe(`${AuthController.name} (e2e)`, () => { describe('POST /auth/admin-sign-up', () => { beforeEach(async () => { - await db.reset(); + await testApp.reset(); }); const invalid = [ diff --git a/server/test/e2e/formats.e2e-spec.ts b/server/test/e2e/formats.e2e-spec.ts index f2fce83ac..40f43a92a 100644 --- a/server/test/e2e/formats.e2e-spec.ts +++ b/server/test/e2e/formats.e2e-spec.ts @@ -1,7 +1,7 @@ import { LoginResponseDto } from '@app/domain'; import { AssetType, LibraryType } from '@app/infra/entities'; import { api } from '@test/api'; -import { IMMICH_TEST_ASSET_PATH, db, runAllTests, testApp } from '@test/test-utils'; +import { IMMICH_TEST_ASSET_PATH, runAllTests, testApp } from '@test/test-utils'; describe(`Supported file formats (e2e)`, () => { let server: any; @@ -176,7 +176,7 @@ describe(`Supported file formats (e2e)`, () => { }); beforeEach(async () => { - await db.reset(); + await testApp.reset(); await api.authApi.adminSignUp(server); admin = await api.authApi.adminLogin(server); await api.userApi.setExternalPath(server, admin.accessToken, admin.userId, '/'); diff --git a/server/test/e2e/library.e2e-spec.ts b/server/test/e2e/library.e2e-spec.ts index d8905a8c1..92c604e00 100644 --- a/server/test/e2e/library.e2e-spec.ts +++ b/server/test/e2e/library.e2e-spec.ts @@ -2,28 +2,16 @@ import { LibraryResponseDto, LoginResponseDto } from '@app/domain'; import { LibraryController } from '@app/immich'; import { AssetType, LibraryType } from '@app/infra/entities'; import { api } from '@test/api'; -import { IMMICH_TEST_ASSET_PATH, IMMICH_TEST_ASSET_TEMP_PATH, db, restoreTempFolder, testApp } from '@test/test-utils'; +import { IMMICH_TEST_ASSET_PATH, IMMICH_TEST_ASSET_TEMP_PATH, restoreTempFolder, testApp } from '@test/test-utils'; import * as fs from 'fs'; import request from 'supertest'; import { utimes } from 'utimes'; -import { errorStub, uuidStub } from '../fixtures'; +import { errorStub, userDto, uuidStub } from '../fixtures'; describe(`${LibraryController.name} (e2e)`, () => { let server: any; let admin: LoginResponseDto; - const user1Dto = { - email: 'user1@immich.app', - password: 'Password123', - name: 'User 1', - }; - - const user2Dto = { - email: 'user2@immich.app', - password: 'Password123', - name: 'User 2', - }; - beforeAll(async () => { [server] = await testApp.create({ jobs: true }); }); @@ -34,7 +22,7 @@ describe(`${LibraryController.name} (e2e)`, () => { }); beforeEach(async () => { - await db.reset(); + await testApp.reset(); await restoreTempFolder(); await api.authApi.adminSignUp(server); admin = await api.authApi.adminLogin(server); @@ -195,8 +183,8 @@ describe(`${LibraryController.name} (e2e)`, () => { }); it('should allow a user to create a library', async () => { - await api.userApi.create(server, admin.accessToken, user1Dto); - const user1 = await api.authApi.login(server, { email: user1Dto.email, password: user1Dto.password }); + await api.userApi.create(server, admin.accessToken, userDto.user1); + const user1 = await api.authApi.login(server, userDto.user1); const { status, body } = await request(server) .post('/library') @@ -337,11 +325,15 @@ describe(`${LibraryController.name} (e2e)`, () => { }); it("should not allow getting another user's library", async () => { - await api.userApi.create(server, admin.accessToken, user1Dto); - const user1 = await api.authApi.login(server, { email: user1Dto.email, password: user1Dto.password }); + await Promise.all([ + api.userApi.create(server, admin.accessToken, userDto.user1), + api.userApi.create(server, admin.accessToken, userDto.user2), + ]); - await api.userApi.create(server, admin.accessToken, user2Dto); - const user2 = await api.authApi.login(server, { email: user2Dto.email, password: user2Dto.password }); + const [user1, user2] = await Promise.all([ + api.authApi.login(server, userDto.user1), + api.authApi.login(server, userDto.user2), + ]); const library = await api.libraryApi.create(server, user1.accessToken, { type: LibraryType.EXTERNAL }); diff --git a/server/test/e2e/oauth.e2e-spec.ts b/server/test/e2e/oauth.e2e-spec.ts index d48cefdaa..b2c997076 100644 --- a/server/test/e2e/oauth.e2e-spec.ts +++ b/server/test/e2e/oauth.e2e-spec.ts @@ -1,6 +1,5 @@ import { OAuthController } from '@app/immich'; import { api } from '@test/api'; -import { db } from '@test/db'; import { errorStub } from '@test/fixtures'; import { testApp } from '@test/test-utils'; import request from 'supertest'; @@ -17,7 +16,7 @@ describe(`${OAuthController.name} (e2e)`, () => { }); beforeEach(async () => { - await db.reset(); + await testApp.reset(); await api.authApi.adminSignUp(server); }); diff --git a/server/test/e2e/partner.e2e-spec.ts b/server/test/e2e/partner.e2e-spec.ts index f4b69f115..512491b69 100644 --- a/server/test/e2e/partner.e2e-spec.ts +++ b/server/test/e2e/partner.e2e-spec.ts @@ -1,29 +1,10 @@ import { LoginResponseDto, PartnerDirection } from '@app/domain'; import { PartnerController } from '@app/immich'; import { api } from '@test/api'; -import { db } from '@test/db'; -import { errorStub } from '@test/fixtures'; +import { errorStub, userDto } from '@test/fixtures'; import { testApp } from '@test/test-utils'; import request from 'supertest'; -const user1Dto = { - email: 'user1@immich.app', - password: 'Password123', - name: 'User 1', -}; - -const user2Dto = { - email: 'user2@immich.app', - password: 'Password123', - name: 'User 2', -}; - -const user3Dto = { - email: 'user3@immich.app', - password: 'Password123', - name: 'User 3', -}; - describe(`${PartnerController.name} (e2e)`, () => { let server: any; let user1: LoginResponseDto; @@ -33,20 +14,20 @@ describe(`${PartnerController.name} (e2e)`, () => { beforeAll(async () => { [server] = await testApp.create(); - await db.reset(); + await testApp.reset(); await api.authApi.adminSignUp(server); const admin = await api.authApi.adminLogin(server); await Promise.all([ - api.userApi.create(server, admin.accessToken, user1Dto), - api.userApi.create(server, admin.accessToken, user2Dto), - api.userApi.create(server, admin.accessToken, user3Dto), + api.userApi.create(server, admin.accessToken, userDto.user1), + api.userApi.create(server, admin.accessToken, userDto.user2), + api.userApi.create(server, admin.accessToken, userDto.user3), ]); [user1, user2, user3] = await Promise.all([ - api.authApi.login(server, { email: user1Dto.email, password: user1Dto.password }), - api.authApi.login(server, { email: user2Dto.email, password: user2Dto.password }), - api.authApi.login(server, { email: user3Dto.email, password: user3Dto.password }), + api.authApi.login(server, userDto.user1), + api.authApi.login(server, userDto.user2), + api.authApi.login(server, userDto.user3), ]); await Promise.all([ diff --git a/server/test/e2e/person.e2e-spec.ts b/server/test/e2e/person.e2e-spec.ts index 298e4418e..e3796619e 100644 --- a/server/test/e2e/person.e2e-spec.ts +++ b/server/test/e2e/person.e2e-spec.ts @@ -3,7 +3,6 @@ import { PersonController } from '@app/immich'; import { PersonEntity } from '@app/infra/entities'; import { INestApplication } from '@nestjs/common'; import { api } from '@test/api'; -import { db } from '@test/db'; import { errorStub, uuidStub } from '@test/fixtures'; import { testApp } from '@test/test-utils'; import request from 'supertest'; @@ -27,7 +26,7 @@ describe(`${PersonController.name}`, () => { }); beforeEach(async () => { - await db.reset(); + await testApp.reset(); await api.authApi.adminSignUp(server); loginResponse = await api.authApi.adminLogin(server); accessToken = loginResponse.accessToken; diff --git a/server/test/e2e/server-info.e2e-spec.ts b/server/test/e2e/server-info.e2e-spec.ts index 98fff5222..cc1c8defa 100644 --- a/server/test/e2e/server-info.e2e-spec.ts +++ b/server/test/e2e/server-info.e2e-spec.ts @@ -1,17 +1,10 @@ import { LoginResponseDto } from '@app/domain'; import { ServerInfoController } from '@app/immich'; import { api } from '@test/api'; -import { db } from '@test/db'; -import { errorStub } from '@test/fixtures'; +import { errorStub, userDto } from '@test/fixtures'; import { testApp } from '@test/test-utils'; import request from 'supertest'; -const user1Dto = { - email: 'user1@immich.app', - password: 'Password123', - name: 'User 1', -}; - describe(`${ServerInfoController.name} (e2e)`, () => { let server: any; let admin: LoginResponseDto; @@ -20,11 +13,11 @@ describe(`${ServerInfoController.name} (e2e)`, () => { beforeAll(async () => { [server] = await testApp.create(); - await db.reset(); + await testApp.reset(); await api.authApi.adminSignUp(server); admin = await api.authApi.adminLogin(server); - await api.userApi.create(server, admin.accessToken, user1Dto); - nonAdmin = await api.authApi.login(server, user1Dto); + await api.userApi.create(server, admin.accessToken, userDto.user1); + nonAdmin = await api.authApi.login(server, userDto.user1); }); afterAll(async () => { diff --git a/server/test/e2e/shared-link.e2e-spec.ts b/server/test/e2e/shared-link.e2e-spec.ts index 368d7408d..09c0a48ce 100644 --- a/server/test/e2e/shared-link.e2e-spec.ts +++ b/server/test/e2e/shared-link.e2e-spec.ts @@ -3,23 +3,11 @@ import { SharedLinkController } from '@app/immich'; import { SharedLinkType } from '@app/infra/entities'; import { INestApplication } from '@nestjs/common'; import { api } from '@test/api'; -import { errorStub, uuidStub } from '@test/fixtures'; +import { errorStub, userDto, uuidStub } from '@test/fixtures'; import { testApp } from '@test/test-utils'; import { DateTime } from 'luxon'; import request from 'supertest'; -const user1Dto = { - email: 'user1@immich.app', - password: 'Password123', - name: 'User 1', -}; - -const user2Dto = { - email: 'user2@immich.app', - password: 'Password123', - name: 'User 2', -}; - const today = new Date(); describe(`${SharedLinkController.name} (e2e)`, () => { @@ -46,13 +34,13 @@ describe(`${SharedLinkController.name} (e2e)`, () => { admin = await api.authApi.adminLogin(server); await Promise.all([ - api.userApi.create(server, admin.accessToken, user1Dto), - api.userApi.create(server, admin.accessToken, user2Dto), + api.userApi.create(server, admin.accessToken, userDto.user1), + api.userApi.create(server, admin.accessToken, userDto.user2), ]); [user1, user2] = await Promise.all([ - api.authApi.login(server, { email: user1Dto.email, password: user1Dto.password }), - api.authApi.login(server, { email: user2Dto.email, password: user2Dto.password }), + api.authApi.login(server, userDto.user1), + api.authApi.login(server, userDto.user2), ]); const asset = await api.assetApi.create(server, user1.accessToken, { diff --git a/server/test/e2e/system-config.e2e-spec.ts b/server/test/e2e/system-config.e2e-spec.ts index d03fe06de..15ed54da3 100644 --- a/server/test/e2e/system-config.e2e-spec.ts +++ b/server/test/e2e/system-config.e2e-spec.ts @@ -1,17 +1,10 @@ import { LoginResponseDto } from '@app/domain'; import { SystemConfigController } from '@app/immich'; import { api } from '@test/api'; -import { db } from '@test/db'; -import { errorStub } from '@test/fixtures'; +import { errorStub, userDto } from '@test/fixtures'; import { testApp } from '@test/test-utils'; import request from 'supertest'; -const user1Dto = { - email: 'user1@immich.app', - password: 'Password123', - name: 'User 1', -}; - describe(`${SystemConfigController.name} (e2e)`, () => { let server: any; let admin: LoginResponseDto; @@ -20,11 +13,11 @@ describe(`${SystemConfigController.name} (e2e)`, () => { beforeAll(async () => { [server] = await testApp.create(); - await db.reset(); + await testApp.reset(); await api.authApi.adminSignUp(server); admin = await api.authApi.adminLogin(server); - await api.userApi.create(server, admin.accessToken, user1Dto); - nonAdmin = await api.authApi.login(server, user1Dto); + await api.userApi.create(server, admin.accessToken, userDto.user1); + nonAdmin = await api.authApi.login(server, userDto.user1); }); afterAll(async () => { diff --git a/server/test/e2e/user.e2e-spec.ts b/server/test/e2e/user.e2e-spec.ts index 41ce7c116..41f393b7d 100644 --- a/server/test/e2e/user.e2e-spec.ts +++ b/server/test/e2e/user.e2e-spec.ts @@ -4,8 +4,7 @@ import { UserEntity } from '@app/infra/entities'; import { INestApplication } from '@nestjs/common'; import { getRepositoryToken } from '@nestjs/typeorm'; import { api } from '@test/api'; -import { db } from '@test/db'; -import { errorStub, userSignupStub, userStub } from '@test/fixtures'; +import { errorStub, userDto, userSignupStub, userStub } from '@test/fixtures'; import { testApp } from '@test/test-utils'; import request from 'supertest'; import { Repository } from 'typeorm'; @@ -28,7 +27,7 @@ describe(`${UserController.name}`, () => { }); beforeEach(async () => { - await db.reset(); + await testApp.reset(); await api.authApi.adminSignUp(server); loginResponse = await api.authApi.adminLogin(server); accessToken = loginResponse.accessToken; @@ -36,11 +35,6 @@ describe(`${UserController.name}`, () => { userService = app.get(UserService); }); - afterAll(async () => { - await db.disconnect(); - await app.close(); - }); - describe('GET /user', () => { it('should require authentication', async () => { const { status, body } = await request(server).get('/user'); @@ -74,11 +68,7 @@ describe(`${UserController.name}`, () => { }); it('should include deleted users', async () => { - const user1 = await api.userApi.create(server, accessToken, { - email: `user1@immich.app`, - password: 'Password123', - name: `User 1`, - }); + const user1 = await api.userApi.create(server, accessToken, userDto.user1); await api.userApi.delete(server, accessToken, user1.id); @@ -86,6 +76,7 @@ describe(`${UserController.name}`, () => { .get(`/user`) .query({ isAll: false }) .set('Authorization', `Bearer ${accessToken}`); + expect(status).toBe(200); expect(body).toHaveLength(2); expect(body[0]).toMatchObject({ id: user1.id, email: 'user1@immich.app', deletedAt: expect.any(String) }); diff --git a/server/test/fixtures/user.stub.ts b/server/test/fixtures/user.stub.ts index c070a6769..9a03579af 100644 --- a/server/test/fixtures/user.stub.ts +++ b/server/test/fixtures/user.stub.ts @@ -1,6 +1,24 @@ import { UserAvatarColor, UserEntity } from '@app/infra/entities'; import { authStub } from './auth.stub'; +export const userDto = { + user1: { + email: 'user1@immich.app', + password: 'Password123', + name: 'User 1', + }, + user2: { + email: 'user2@immich.app', + password: 'Password123', + name: 'User 2', + }, + user3: { + email: 'user3@immich.app', + password: 'Password123', + name: 'User 3', + }, +}; + export const userStub = { admin: Object.freeze({ ...authStub.admin,