Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tender-goats-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Fixes an issue with airgapped restrictions ignoring the warning period.
1 change: 1 addition & 0 deletions apps/meteor/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default {
'<rootDir>/app/cloud/server/functions/supportedVersionsToken/**.spec.ts',
'<rootDir>/app/utils/lib/**.spec.ts',
'<rootDir>/server/lib/auditServerEvents/**.spec.ts',
'<rootDir>/server/cron/**.spec.ts',
'<rootDir>/app/api/server/**.spec.ts',
'<rootDir>/app/api/server/middlewares/**.spec.ts',
],
Expand Down
53 changes: 53 additions & 0 deletions apps/meteor/server/cron/usageReport.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { AirGappedRestriction } from '@rocket.chat/license';
import { Statistics } from '@rocket.chat/models';

import { sendUsageReportAndComputeRestriction } from './usageReport';

jest.mock('@rocket.chat/license', () => ({
AirGappedRestriction: {
computeRestriction: jest.fn(),
},
}));

jest.mock('@rocket.chat/models', () => ({
Statistics: {
findLastStatsToken: jest.fn(),
},
}));

jest.mock('../../app/statistics/server/functions/sendUsageReport', () => ({
sendUsageReport: () => undefined,
}));

describe('sendUsageReportAndComputeRestriction', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should pass statsToken to computeRestriction when provided', async () => {
const mockStatsToken = 'test-token';
await sendUsageReportAndComputeRestriction(mockStatsToken);

expect(AirGappedRestriction.computeRestriction).toHaveBeenCalledWith(mockStatsToken);
expect(Statistics.findLastStatsToken).not.toHaveBeenCalled();
});

it('should use findLastStatsToken result when statsToken is omitted', async () => {
const mockLastToken = 'last-token';
(Statistics.findLastStatsToken as jest.Mock).mockResolvedValue(mockLastToken);

await sendUsageReportAndComputeRestriction();

expect(Statistics.findLastStatsToken).toHaveBeenCalled();
expect(AirGappedRestriction.computeRestriction).toHaveBeenCalledWith(mockLastToken);
});

it('should pass undefined to computeRestriction when both statsToken is omitted and findLastStatsToken returns undefined', async () => {
(Statistics.findLastStatsToken as jest.Mock).mockResolvedValue(undefined);

await sendUsageReportAndComputeRestriction();

expect(Statistics.findLastStatsToken).toHaveBeenCalled();
expect(AirGappedRestriction.computeRestriction).toHaveBeenCalledWith(undefined);
});
});
14 changes: 12 additions & 2 deletions apps/meteor/server/cron/usageReport.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { cronJobs } from '@rocket.chat/cron';
import { AirGappedRestriction } from '@rocket.chat/license';
import type { Logger } from '@rocket.chat/logger';
import { Statistics } from '@rocket.chat/models';

import { sendUsageReport } from '../../app/statistics/server/functions/sendUsageReport';

export const sendUsageReportAndComputeRestriction = async (statsToken?: string) => {
// If the report failed to be sent we need to get the last existing token
// to ensure that the restriction respects the warning period.
// If no token is passed, the workspace will be instantly restricted.
const token = statsToken || (await Statistics.findLastStatsToken());
void AirGappedRestriction.computeRestriction(token);
};

export async function usageReportCron(logger: Logger): Promise<void> {
const name = 'Generate and save statistics';

const statsToken = await sendUsageReport(logger);
void AirGappedRestriction.computeRestriction(statsToken);
await sendUsageReportAndComputeRestriction(statsToken);

const now = new Date();

return cronJobs.add(name, `12 ${now.getHours()} * * *`, async () => {
const statsToken = await sendUsageReport(logger);
void AirGappedRestriction.computeRestriction(statsToken);
await sendUsageReportAndComputeRestriction(statsToken);
});
}
Loading