2022-05-19 18:30:47 -05:00
|
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
|
|
import { INestApplication } from '@nestjs/common';
|
|
|
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
|
|
import request from 'supertest';
|
|
|
|
import { clearDb, authCustom } from './test-utils';
|
2022-06-11 16:12:06 -05:00
|
|
|
import { databaseConfig } from '@app/database/config/database.config';
|
2022-05-19 18:30:47 -05:00
|
|
|
import { UserModule } from '../src/api-v1/user/user.module';
|
|
|
|
import { ImmichJwtModule } from '../src/modules/immich-jwt/immich-jwt.module';
|
2022-06-06 11:16:03 -05:00
|
|
|
import { UserService } from '../src/api-v1/user/user.service';
|
|
|
|
import { CreateUserDto } from '../src/api-v1/user/dto/create-user.dto';
|
2022-06-25 12:53:06 -05:00
|
|
|
import { UserResponseDto } from '../src/api-v1/user/response-dto/user-response.dto';
|
2022-07-16 23:43:31 -05:00
|
|
|
import { DataSource } from 'typeorm';
|
2022-05-19 18:30:47 -05:00
|
|
|
|
2022-06-06 11:16:03 -05:00
|
|
|
function _createUser(userService: UserService, data: CreateUserDto) {
|
|
|
|
return userService.createUser(data);
|
2022-05-19 18:30:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('User', () => {
|
|
|
|
let app: INestApplication;
|
2022-07-16 23:43:31 -05:00
|
|
|
let database: DataSource;
|
2022-05-19 18:30:47 -05:00
|
|
|
|
|
|
|
afterAll(async () => {
|
2022-07-16 23:43:31 -05:00
|
|
|
await clearDb(database);
|
2022-05-19 18:30:47 -05:00
|
|
|
await app.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('without auth', () => {
|
|
|
|
beforeAll(async () => {
|
|
|
|
const moduleFixture: TestingModule = await Test.createTestingModule({
|
|
|
|
imports: [UserModule, ImmichJwtModule, TypeOrmModule.forRoot(databaseConfig)],
|
|
|
|
}).compile();
|
|
|
|
|
|
|
|
app = moduleFixture.createNestApplication();
|
2022-07-16 23:43:31 -05:00
|
|
|
database = app.get(DataSource);
|
2022-05-19 18:30:47 -05:00
|
|
|
await app.init();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await app.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('prevents fetching users if not auth', async () => {
|
|
|
|
const { status } = await request(app.getHttpServer()).get('/user');
|
|
|
|
expect(status).toEqual(401);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with auth', () => {
|
2022-06-06 11:16:03 -05:00
|
|
|
let userService: UserService;
|
2022-06-25 12:53:06 -05:00
|
|
|
let authUser: UserResponseDto;
|
2022-05-19 18:30:47 -05:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
const builder = Test.createTestingModule({
|
2022-06-06 11:16:03 -05:00
|
|
|
imports: [UserModule, TypeOrmModule.forRoot(databaseConfig)],
|
2022-05-19 18:30:47 -05:00
|
|
|
});
|
|
|
|
const moduleFixture: TestingModule = await authCustom(builder, () => authUser).compile();
|
|
|
|
|
|
|
|
app = moduleFixture.createNestApplication();
|
2022-06-06 11:16:03 -05:00
|
|
|
userService = app.get(UserService);
|
2022-07-16 23:43:31 -05:00
|
|
|
database = app.get(DataSource);
|
2022-05-19 18:30:47 -05:00
|
|
|
await app.init();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with users in DB', () => {
|
|
|
|
const authUserEmail = 'auth-user@test.com';
|
|
|
|
const userOneEmail = 'one@test.com';
|
|
|
|
const userTwoEmail = 'two@test.com';
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
await Promise.allSettled([
|
2022-06-06 11:16:03 -05:00
|
|
|
_createUser(userService, {
|
|
|
|
firstName: 'auth-user',
|
|
|
|
lastName: 'test',
|
|
|
|
email: authUserEmail,
|
|
|
|
password: '1234',
|
|
|
|
}).then((user) => (authUser = user)),
|
|
|
|
_createUser(userService, {
|
|
|
|
firstName: 'one',
|
|
|
|
lastName: 'test',
|
|
|
|
email: userOneEmail,
|
|
|
|
password: '1234',
|
|
|
|
}),
|
|
|
|
_createUser(userService, {
|
|
|
|
firstName: 'two',
|
|
|
|
lastName: 'test',
|
|
|
|
email: userTwoEmail,
|
|
|
|
password: '1234',
|
|
|
|
}),
|
2022-05-19 18:30:47 -05:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fetches the user collection excluding the auth user', async () => {
|
2022-07-10 21:41:45 -05:00
|
|
|
const { status, body } = await request(app.getHttpServer()).get('/user?isAll=false');
|
2022-05-19 18:30:47 -05:00
|
|
|
expect(status).toEqual(200);
|
|
|
|
expect(body).toHaveLength(2);
|
|
|
|
expect(body).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
{
|
|
|
|
email: userOneEmail,
|
2022-06-06 11:16:03 -05:00
|
|
|
firstName: 'one',
|
|
|
|
lastName: 'test',
|
2022-05-19 18:30:47 -05:00
|
|
|
id: expect.anything(),
|
|
|
|
createdAt: expect.anything(),
|
2022-06-06 11:16:03 -05:00
|
|
|
isAdmin: false,
|
2022-06-27 15:13:07 -05:00
|
|
|
shouldChangePassword: true,
|
2022-06-06 11:16:03 -05:00
|
|
|
profileImagePath: '',
|
2022-05-19 18:30:47 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
email: userTwoEmail,
|
2022-06-06 11:16:03 -05:00
|
|
|
firstName: 'two',
|
|
|
|
lastName: 'test',
|
2022-05-19 18:30:47 -05:00
|
|
|
id: expect.anything(),
|
|
|
|
createdAt: expect.anything(),
|
2022-06-06 11:16:03 -05:00
|
|
|
isAdmin: false,
|
2022-06-27 15:13:07 -05:00
|
|
|
shouldChangePassword: true,
|
2022-06-06 11:16:03 -05:00
|
|
|
profileImagePath: '',
|
2022-05-19 18:30:47 -05:00
|
|
|
},
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
expect(body).toEqual(expect.not.arrayContaining([expect.objectContaining({ email: authUserEmail })]));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|