Skip to content

Commit

Permalink
fix missing role in create user
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 30, 2024
1 parent 6aa277b commit 8f27ba1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion api/src/modules/auth/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class AuthenticationService {
createUser: CreateUserDto,
): Promise<{ newUser: User; plainTextPassword: string }> {
// TODO: This is sync, check how to improve it
const { email, name, partnerName } = createUser;
const { email, name, partnerName, role } = createUser;
const plainTextPassword = randomBytes(8).toString('hex');
const passwordHash = await bcrypt.hash(plainTextPassword, 10);
const existingUser = await this.usersService.findByEmail(createUser.email);
Expand All @@ -70,6 +70,7 @@ export class AuthenticationService {
password: passwordHash,
partnerName,
isActive: false,
role,
});
this.eventBus.publish(
new NewUserEvent(newUser.id, newUser.email, API_EVENT_TYPES.USER_CREATED),
Expand Down
29 changes: 28 additions & 1 deletion api/test/integration/auth/create-user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('Create Users', () => {
testManager.getModule<MockEmailService>(IEmailServiceToken);
});
beforeEach(async () => {
jest.clearAllMocks();
const { user, jwtToken: token } = await testManager.setUpTestUser();
testUser = user;
jwtToken = token;
Expand Down Expand Up @@ -71,7 +72,7 @@ describe('Create Users', () => {
);
});

test('An Admin registers a new user ', async () => {
test('An Admin registers a new user, and if no role provided, it should be set as partner ', async () => {
// Given a admin user exists with valid credentials
// beforeAll
const newUser = {
Expand All @@ -94,6 +95,32 @@ describe('Create Users', () => {
.findOne({ where: { email: newUser.email } });

expect(createdUser.isActive).toBe(false);
expect(createdUser.role).toBe(ROLES.PARTNER);
expect(mockEmailService.sendMail).toHaveBeenCalledTimes(1);
});

test('An Admin can register another admin by setting a role', async () => {
const newUser = {
email: '[email protected]',
partnerName: 'test',
role: ROLES.ADMIN,
};
const response = await testManager
.request()
.post('/admin/users')
.set('Authorization', `Bearer ${jwtToken}`)
.send(newUser);

// Then the user should receive a 201 status code
expect(response.status).toBe(HttpStatus.CREATED);
// And the user should not be active
const createdUser = await testManager
.getDataSource()
.getRepository(User)
.findOne({ where: { email: newUser.email } });

expect(createdUser.isActive).toBe(false);
expect(createdUser.role).toBe(ROLES.ADMIN);
expect(mockEmailService.sendMail).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 8f27ba1

Please sign in to comment.