user.e2e-spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { Test, TestingModule } from '@nestjs/testing';
  2. import { INestApplication } from '@nestjs/common';
  3. import { TypeOrmModule } from '@nestjs/typeorm';
  4. import request from 'supertest';
  5. import { clearDb, authCustom } from './test-utils';
  6. import { databaseConfig } from '../src/config/database.config';
  7. import { UserModule } from '../src/api-v1/user/user.module';
  8. import { ImmichJwtModule } from '../src/modules/immich-jwt/immich-jwt.module';
  9. import { UserService } from '../src/api-v1/user/user.service';
  10. import { CreateUserDto } from '../src/api-v1/user/dto/create-user.dto';
  11. import { User } from '../src/api-v1/user/response-dto/user';
  12. function _createUser(userService: UserService, data: CreateUserDto) {
  13. return userService.createUser(data);
  14. }
  15. describe('User', () => {
  16. let app: INestApplication;
  17. afterAll(async () => {
  18. await clearDb();
  19. await app.close();
  20. });
  21. describe('without auth', () => {
  22. beforeAll(async () => {
  23. const moduleFixture: TestingModule = await Test.createTestingModule({
  24. imports: [UserModule, ImmichJwtModule, TypeOrmModule.forRoot(databaseConfig)],
  25. }).compile();
  26. app = moduleFixture.createNestApplication();
  27. await app.init();
  28. });
  29. afterAll(async () => {
  30. await app.close();
  31. });
  32. it('prevents fetching users if not auth', async () => {
  33. const { status } = await request(app.getHttpServer()).get('/user');
  34. expect(status).toEqual(401);
  35. });
  36. });
  37. describe('with auth', () => {
  38. let userService: UserService;
  39. let authUser: User;
  40. beforeAll(async () => {
  41. const builder = Test.createTestingModule({
  42. imports: [UserModule, TypeOrmModule.forRoot(databaseConfig)],
  43. });
  44. const moduleFixture: TestingModule = await authCustom(builder, () => authUser).compile();
  45. app = moduleFixture.createNestApplication();
  46. userService = app.get(UserService);
  47. await app.init();
  48. });
  49. describe('with users in DB', () => {
  50. const authUserEmail = 'auth-user@test.com';
  51. const userOneEmail = 'one@test.com';
  52. const userTwoEmail = 'two@test.com';
  53. beforeAll(async () => {
  54. await Promise.allSettled([
  55. _createUser(userService, {
  56. firstName: 'auth-user',
  57. lastName: 'test',
  58. email: authUserEmail,
  59. password: '1234',
  60. }).then((user) => (authUser = user)),
  61. _createUser(userService, {
  62. firstName: 'one',
  63. lastName: 'test',
  64. email: userOneEmail,
  65. password: '1234',
  66. }),
  67. _createUser(userService, {
  68. firstName: 'two',
  69. lastName: 'test',
  70. email: userTwoEmail,
  71. password: '1234',
  72. }),
  73. ]);
  74. });
  75. it('fetches the user collection excluding the auth user', async () => {
  76. const { status, body } = await request(app.getHttpServer()).get('/user');
  77. expect(status).toEqual(200);
  78. expect(body).toHaveLength(2);
  79. expect(body).toEqual(
  80. expect.arrayContaining([
  81. {
  82. email: userOneEmail,
  83. firstName: 'one',
  84. lastName: 'test',
  85. id: expect.anything(),
  86. createdAt: expect.anything(),
  87. isAdmin: false,
  88. isFirstLoggedIn: true,
  89. profileImagePath: '',
  90. },
  91. {
  92. email: userTwoEmail,
  93. firstName: 'two',
  94. lastName: 'test',
  95. id: expect.anything(),
  96. createdAt: expect.anything(),
  97. isAdmin: false,
  98. isFirstLoggedIn: true,
  99. profileImagePath: '',
  100. },
  101. ]),
  102. );
  103. expect(body).toEqual(expect.not.arrayContaining([expect.objectContaining({ email: authUserEmail })]));
  104. });
  105. });
  106. });
  107. });