Skip to content

Commit

Permalink
fix: Issue enforcing user limits on start plan (#7188)
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoE105 authored and netroy committed Sep 21, 2023
1 parent c8fb06f commit 50a4c5e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/cli/src/controllers/passwordReset.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class PasswordResetController {

const user = await this.userService.findOne({
where: { id: decodedToken.sub },
relations: ['authIdentities'],
relations: ['authIdentities', 'globalRole'],
});

if (!user) {
Expand Down
46 changes: 46 additions & 0 deletions packages/cli/test/integration/passwordReset.api.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { v4 as uuid } from 'uuid';
import { compare } from 'bcryptjs';
import { License } from '@/License';

import * as Db from '@/Db';
import config from '@/config';
Expand All @@ -25,19 +26,22 @@ config.set('userManagement.jwtSecret', randomString(5, 10));
let globalOwnerRole: Role;
let globalMemberRole: Role;
let owner: User;
let member: User;

const externalHooks = utils.mockInstance(ExternalHooks);
const testServer = utils.setupTestServer({ endpointGroups: ['passwordReset'] });
const jwtService = Container.get(JwtService);

beforeAll(async () => {
await utils.initEncryptionKey();
globalOwnerRole = await testDb.getGlobalOwnerRole();
globalMemberRole = await testDb.getGlobalMemberRole();
});

beforeEach(async () => {
await testDb.truncate(['User']);
owner = await testDb.createUser({ globalRole: globalOwnerRole });
member = await testDb.createUser({ globalRole: globalMemberRole });
externalHooks.run.mockReset();
});

Expand Down Expand Up @@ -253,4 +257,46 @@ describe('POST /change-password', () => {

expect(externalHooks.run).not.toHaveBeenCalled();
});

test('owner should be able to reset its password when quota:users = 1', async () => {
jest.spyOn(Container.get(License), 'getUsersLimit').mockReturnValueOnce(1);

const resetPasswordToken = jwtService.signData({ sub: owner.id });
const response = await testServer.authlessAgent.post('/change-password').send({
token: resetPasswordToken,
userId: owner.id,
password: passwordToStore,
});

expect(response.statusCode).toBe(200);

const authToken = utils.getAuthToken(response);
expect(authToken).toBeDefined();

const { password: storedPassword } = await Db.collections.User.findOneByOrFail({
id: owner.id,
});

const comparisonResult = await compare(passwordToStore, storedPassword);
expect(comparisonResult).toBe(true);
expect(storedPassword).not.toBe(passwordToStore);

expect(externalHooks.run).toHaveBeenCalledWith('user.password.update', [
owner.email,
storedPassword,
]);
});

test('member should not be able to reset its password when quota:users = 1', async () => {
jest.spyOn(Container.get(License), 'getUsersLimit').mockReturnValueOnce(1);

const resetPasswordToken = jwtService.signData({ sub: member.id });
const response = await testServer.authlessAgent.post('/change-password').send({
token: resetPasswordToken,
userId: member.id,
password: passwordToStore,
});

expect(response.statusCode).toBe(403);
});
});

0 comments on commit 50a4c5e

Please sign in to comment.