2022-05-20 01:30:47 +02:00
|
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
|
|
import { INestApplication } from '@nestjs/common';
|
|
|
|
import request from 'supertest';
|
|
|
|
import { clearDb, authCustom } from './test-utils';
|
2023-01-31 13:11:49 -05:00
|
|
|
import { CreateUserDto, UserService, AuthUserDto } from '@app/domain';
|
2022-07-16 23:43:31 -05:00
|
|
|
import { DataSource } from 'typeorm';
|
2023-01-23 23:13:42 -05:00
|
|
|
import { AuthService } from '@app/domain';
|
2023-01-27 20:50:07 +00:00
|
|
|
import { AppModule } from '../src/app.module';
|
2022-05-20 01:30:47 +02:00
|
|
|
|
2023-01-15 20:08:24 +01:00
|
|
|
function _createUser(userService: UserService, data: CreateUserDto) {
|
2022-06-06 18:16:03 +02:00
|
|
|
return userService.createUser(data);
|
2022-05-20 01:30:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('User', () => {
|
|
|
|
let app: INestApplication;
|
2022-07-16 23:43:31 -05:00
|
|
|
let database: DataSource;
|
2022-05-20 01:30:47 +02:00
|
|
|
|
|
|
|
afterAll(async () => {
|
2022-07-16 23:43:31 -05:00
|
|
|
await clearDb(database);
|
2022-05-20 01:30:47 +02:00
|
|
|
await app.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('without auth', () => {
|
|
|
|
beforeAll(async () => {
|
2023-01-31 13:11:49 -05:00
|
|
|
const moduleFixture: TestingModule = await Test.createTestingModule({ imports: [AppModule] }).compile();
|
2022-05-20 01:30:47 +02:00
|
|
|
|
|
|
|
app = moduleFixture.createNestApplication();
|
2022-07-16 23:43:31 -05:00
|
|
|
database = app.get(DataSource);
|
2022-05-20 01:30:47 +02: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 18:16:03 +02:00
|
|
|
let userService: UserService;
|
2023-01-15 20:08:24 +01:00
|
|
|
let authService: AuthService;
|
|
|
|
let authUser: AuthUserDto;
|
2022-05-20 01:30:47 +02:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-01-31 13:11:49 -05:00
|
|
|
const builder = Test.createTestingModule({ imports: [AppModule] });
|
2022-05-20 01:30:47 +02:00
|
|
|
const moduleFixture: TestingModule = await authCustom(builder, () => authUser).compile();
|
|
|
|
|
|
|
|
app = moduleFixture.createNestApplication();
|
2022-06-06 18:16:03 +02:00
|
|
|
userService = app.get(UserService);
|
2023-01-15 20:08:24 +01:00
|
|
|
authService = app.get(AuthService);
|
2022-07-16 23:43:31 -05:00
|
|
|
database = app.get(DataSource);
|
2022-05-20 01:30:47 +02: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 () => {
|
2022-12-23 21:08:50 +01:00
|
|
|
// first user must be admin
|
2023-01-15 20:08:24 +01:00
|
|
|
const adminSignupResponseDto = await authService.adminSignUp({
|
2022-12-23 21:08:50 +01:00
|
|
|
firstName: 'auth-user',
|
|
|
|
lastName: 'test',
|
|
|
|
email: authUserEmail,
|
|
|
|
password: '1234',
|
|
|
|
});
|
2023-01-15 20:08:24 +01:00
|
|
|
authUser = { ...adminSignupResponseDto, isAdmin: true }; // TODO: find out why adminSignUp doesn't have isAdmin (maybe can just return UserResponseDto)
|
2022-05-20 01:30:47 +02:00
|
|
|
await Promise.allSettled([
|
2022-06-06 18:16:03 +02:00
|
|
|
_createUser(userService, {
|
|
|
|
firstName: 'one',
|
|
|
|
lastName: 'test',
|
|
|
|
email: userOneEmail,
|
|
|
|
password: '1234',
|
|
|
|
}),
|
|
|
|
_createUser(userService, {
|
|
|
|
firstName: 'two',
|
|
|
|
lastName: 'test',
|
|
|
|
email: userTwoEmail,
|
|
|
|
password: '1234',
|
|
|
|
}),
|
2022-05-20 01:30:47 +02: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-20 01:30:47 +02:00
|
|
|
expect(status).toEqual(200);
|
|
|
|
expect(body).toHaveLength(2);
|
|
|
|
expect(body).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
{
|
|
|
|
email: userOneEmail,
|
2022-06-06 18:16:03 +02:00
|
|
|
firstName: 'one',
|
|
|
|
lastName: 'test',
|
2022-05-20 01:30:47 +02:00
|
|
|
id: expect.anything(),
|
|
|
|
createdAt: expect.anything(),
|
2022-06-06 18:16:03 +02:00
|
|
|
isAdmin: false,
|
2022-06-27 15:13:07 -05:00
|
|
|
shouldChangePassword: true,
|
2022-06-06 18:16:03 +02:00
|
|
|
profileImagePath: '',
|
2022-11-07 16:53:47 -05:00
|
|
|
deletedAt: null,
|
2023-03-03 23:38:30 +01:00
|
|
|
updatedAt: expect.anything(),
|
2022-12-26 10:35:52 -05:00
|
|
|
oauthId: '',
|
2022-05-20 01:30:47 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
email: userTwoEmail,
|
2022-06-06 18:16:03 +02:00
|
|
|
firstName: 'two',
|
|
|
|
lastName: 'test',
|
2022-05-20 01:30:47 +02:00
|
|
|
id: expect.anything(),
|
|
|
|
createdAt: expect.anything(),
|
2022-06-06 18:16:03 +02:00
|
|
|
isAdmin: false,
|
2022-06-27 15:13:07 -05:00
|
|
|
shouldChangePassword: true,
|
2022-06-06 18:16:03 +02:00
|
|
|
profileImagePath: '',
|
2022-11-07 16:53:47 -05:00
|
|
|
deletedAt: null,
|
2023-03-03 23:38:30 +01:00
|
|
|
updatedAt: expect.anything(),
|
2022-12-26 10:35:52 -05:00
|
|
|
oauthId: '',
|
2022-05-20 01:30:47 +02:00
|
|
|
},
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
expect(body).toEqual(expect.not.arrayContaining([expect.objectContaining({ email: authUserEmail })]));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|