|
2 | 2 | import { faker } from '@faker-js/faker'; |
3 | 3 | import { INestApplication } from '@nestjs/common'; |
4 | 4 | import request from 'supertest'; |
| 5 | +import { DataSource } from 'typeorm'; |
5 | 6 | import { Constants } from '../../src/helpers/constants/constants.js'; |
6 | 7 | import { TestUtils } from './test.utils.js'; |
7 | 8 | import { isSaaS } from '../../src/helpers/app/is-saas.js'; |
8 | 9 | import { InvitedUserInCompanyAndConnectionGroupDs } from '../../src/entities/company-info/application/data-structures/invited-user-in-company-and-connection-group.ds.js'; |
| 10 | +import { BaseType } from '../../src/common/data-injection.tokens.js'; |
| 11 | +import { UserEntity } from '../../src/entities/user/user.entity.js'; |
| 12 | +import { CompanyInfoEntity } from '../../src/entities/company-info/company-info.entity.js'; |
| 13 | +import { generateGwtToken } from '../../src/entities/user/utils/generate-gwt-token.js'; |
| 14 | +import { UserRoleEnum } from '../../src/entities/user/enums/user-role.enum.js'; |
9 | 15 |
|
10 | 16 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
11 | 17 | export async function registerUserAndReturnUserInfo(app: INestApplication): Promise<{ |
12 | 18 | token: string; |
13 | 19 | email: string; |
14 | 20 | password: string; |
15 | 21 | }> { |
16 | | - if (!isSaaS()) { |
17 | | - return await loginTestAdminUserAndReturnInfo(app); |
18 | | - } |
19 | | - // return await registerUserOnCoreAndReturnUserInfo(app); |
20 | | - return await registerUserOnSaasAndReturnUserInfo(); |
21 | | -} |
22 | | - |
23 | | -async function loginTestAdminUserAndReturnInfo(app: INestApplication): Promise<{ |
24 | | - token: string; |
25 | | - email: string; |
26 | | - password: string; |
27 | | -}> { |
28 | | - const userLoginInfo = { |
29 | | - email: 'admin@email.local', |
30 | | - password: 'test12345', |
31 | | - }; |
32 | | - const loginAdminUserResponse = await request(app.getHttpServer()) |
33 | | - .post('/user/login/') |
34 | | - .send(userLoginInfo) |
35 | | - .set('Content-Type', 'application/json') |
36 | | - .set('Accept', 'application/json'); |
| 22 | + const dataSource = app.get<DataSource>(BaseType.DATA_SOURCE); |
| 23 | + const userRepository = dataSource.getRepository(UserEntity); |
| 24 | + const companyRepository = dataSource.getRepository(CompanyInfoEntity); |
| 25 | + |
| 26 | + const email = `${faker.lorem.words(1)}_${faker.lorem.words(1)}_${faker.internet.email()}`.toLowerCase(); |
| 27 | + const password = `#r@dY^e&7R4b5Ib@31iE4xbn`; |
| 28 | + const companyName = `${faker.lorem.words(1)}_${faker.lorem.words(1)}_${faker.lorem.words(1)}_${faker.company.name()}`; |
| 29 | + |
| 30 | + // Create company |
| 31 | + const company = companyRepository.create({ |
| 32 | + id: faker.string.uuid(), |
| 33 | + name: companyName, |
| 34 | + }); |
| 35 | + const savedCompany = await companyRepository.save(company); |
| 36 | + |
| 37 | + // Create user |
| 38 | + const user = userRepository.create({ |
| 39 | + email, |
| 40 | + password, |
| 41 | + isActive: true, |
| 42 | + company: savedCompany, |
| 43 | + role: UserRoleEnum.ADMIN, |
| 44 | + }); |
| 45 | + const savedUser = await userRepository.save(user); |
37 | 46 |
|
38 | | - const loginAdminUserResponseJson = JSON.parse(loginAdminUserResponse.text); |
39 | | - if (loginAdminUserResponse.status > 201) { |
40 | | - console.info('loginAdminUserResponseJson.text -> ', loginAdminUserResponseJson); |
41 | | - } |
| 47 | + // Generate JWT token |
| 48 | + const tokenData = generateGwtToken(savedUser, []); |
| 49 | + const token = `${Constants.JWT_COOKIE_KEY_NAME}=${tokenData.token}`; |
42 | 50 |
|
43 | | - const token = `${Constants.JWT_COOKIE_KEY_NAME}=${TestUtils.getJwtTokenFromResponse(loginAdminUserResponse)}`; |
44 | | - return { token: token, ...userLoginInfo }; |
| 51 | + return { token, email, password }; |
45 | 52 | } |
46 | 53 |
|
47 | 54 | export async function registerUserOnSaasAndReturnUserInfo( |
|
0 commit comments