Skip to content

Commit

Permalink
test: add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thenkei committed May 16, 2023
1 parent e3414bf commit ae03c87
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/forestadmin-client/src/events-subscription/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export default class EventsSubscriptionService {
private async handleSeverEventRefreshRenderings(event: ServerEvent) {
if (!event.data) {
this.options.logger('Debug', 'Server Event - RefreshRenderings missing required data.');

return;
}

const { renderingIds } = JSON.parse(event.data as unknown as string);
Expand All @@ -73,7 +75,7 @@ export default class EventsSubscriptionService {
}

if (event.message)
this.options.logger('Debug', `Server Event - Error: ${JSON.stringify(event)}`);
this.options.logger('Warn', `Server Event - Error: ${JSON.stringify(event)}`);
}

private onEventOpen() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ export default class ActionPermissionService {

this.options.logger(
'Debug',
`User ${roleId} is ${isAllowed ? '' : 'not '}allowed to perform
}${actionName}`,
`User ${roleId} is ${isAllowed ? '' : 'not '}allowed to perform ${actionName}`,
);

return isAllowed;
Expand Down Expand Up @@ -142,6 +141,8 @@ export default class ActionPermissionService {
}

public invalidateCache() {
this.options.logger('Debug', 'Invalidating roles permissions cache..');

this.permissionsPromise = undefined;
this.permissionExpirationTimestamp = undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export default class UserPermissionService {
}

public invalidateCache() {
this.options.logger('Debug', 'Invalidating users permissions cache..');

this.userInfoById = undefined;
this.cacheExpirationTimestamp = undefined;
}
Expand Down
93 changes: 93 additions & 0 deletions packages/forestadmin-client/test/events-subscription/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ describe('EventsSubscriptionService', () => {
expect.any(Function),
);
});

describe('when server events are deactivated', () => {
test('should not do anything', async () => {
const eventsSubscriptionService = new EventsSubscriptionService(
{ ...options, useServerEvents: false },
refreshEventsHandlerService,
);

eventsSubscriptionService.subscribeEvents();

expect(addEventListener).not.toHaveBeenCalled();
});
});
});

describe('handleSeverEvents', () => {
Expand Down Expand Up @@ -115,6 +128,86 @@ describe('EventsSubscriptionService', () => {
expect(refreshEventsHandlerService.refreshRenderings).toHaveBeenCalled();
expect(refreshEventsHandlerService.refreshRenderings).toHaveBeenCalledWith(['13', 24]);
});
describe('on malformed event', () => {
test('should not do anything', async () => {
const eventsSubscriptionService = new EventsSubscriptionService(
options,
refreshEventsHandlerService,
);
eventsSubscriptionService.subscribeEvents();

// eslint-disable-next-line @typescript-eslint/dot-notation
events[ServerEventType.RefreshRenderings]({
data: null,
});

expect(refreshEventsHandlerService.refreshRenderings).not.toHaveBeenCalled();
});
});
});
});

describe('onEventOpen', () => {
test('should refreshEverything using refreshEventsHandlerService', async () => {
const eventsSubscriptionService = new EventsSubscriptionService(
options,
refreshEventsHandlerService,
);

eventsSubscriptionService.subscribeEvents();

// eslint-disable-next-line @typescript-eslint/dot-notation
events['open']();

expect(refreshEventsHandlerService.refreshEverything).toHaveBeenCalled();

expect(options.logger).toHaveBeenCalledWith(
'Debug',
'Server Event - Open EventSource (SSE) connection with Forest Admin servers',
);
});
});

describe('onEventError', () => {
describe('when error status is Bad Gateway (502)', () => {
test('should delegate to refreshEventsHandlerService', async () => {
const eventsSubscriptionService = new EventsSubscriptionService(
options,
refreshEventsHandlerService,
);

eventsSubscriptionService.subscribeEvents();

// eslint-disable-next-line @typescript-eslint/dot-notation
events['error']({ status: 502 });

expect(options.logger).toHaveBeenCalledWith(
'Debug',
'Server Event - Connection lost (ForestAdmin servers are restarting)',
);
});
});

describe('other error case', () => {
test('should delegate to refreshEventsHandlerService', async () => {
const eventsSubscriptionService = new EventsSubscriptionService(
options,
refreshEventsHandlerService,
);

eventsSubscriptionService.subscribeEvents();

// eslint-disable-next-line @typescript-eslint/dot-notation
events['error']({
status: 404,
message: 'some error message that might help customer to track issues',
});
expect(options.logger).toHaveBeenCalledWith(
'Warn',
'Server Event - Error: {"status":404,"message":"some error message that' +
' might help customer to track issues"}',
);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,90 @@ describe('RenderingPermissionService', () => {
});
});

describe('invalidateAllCache', () => {
it('should invalidate the cache for all renderings', async () => {
const {
renderingPermission,
getUserInfoMock,
serverInterface,
hashServerChartsMock,
hashChartRequestMock,
} = setup();

const userInfo = {
id: 42,
firstName: 'Jane',
lastName: 'Doe',
email: 'jane@forest.com',
permissionLevel: PermissionLevel.User,
};

getUserInfoMock.mockResolvedValue(userInfo);

const stats = { lines: [{ type: 'Line' }] };
serverInterface.getRenderingPermissions = jest.fn().mockResolvedValue({
collections: {},
stats,
team: {},
});
hashServerChartsMock.mockReturnValue(new Set(['HASH']));
hashChartRequestMock.mockReturnValue('HASH');

const resultA1 = await renderingPermission.canExecuteChart({
renderingId: 60,
chartRequest: {
type: ChartType.Value,
sourceCollectionName: 'jedi',
aggregateFieldName: 'strength',
aggregator: 'Sum',
},
userId: 42,
});

const resultB1 = await renderingPermission.canExecuteChart({
renderingId: 63,
chartRequest: {
type: ChartType.Value,
sourceCollectionName: 'jedi',
aggregateFieldName: 'strength',
aggregator: 'Sum',
},
userId: 42,
});

renderingPermission.invalidateAllCache();

const resultA2 = await renderingPermission.canExecuteChart({
renderingId: 60,
chartRequest: {
type: ChartType.Value,
sourceCollectionName: 'jedi',
aggregateFieldName: 'strength',
aggregator: 'Sum',
},
userId: 42,
});

const resultB2 = await renderingPermission.canExecuteChart({
renderingId: 63,
chartRequest: {
type: ChartType.Value,
sourceCollectionName: 'jedi',
aggregateFieldName: 'strength',
aggregator: 'Sum',
},
userId: 42,
});

expect(resultA1).toBe(true);
expect(resultA2).toBe(true);
expect(resultB1).toBe(true);
expect(resultB2).toBe(true);

expect(serverInterface.getRenderingPermissions).toHaveBeenCalledTimes(4);
});
});

describe('canExecuteSegmentQuery', () => {
it('should return true if the segment query is allowed', async () => {
const {
Expand Down

0 comments on commit ae03c87

Please sign in to comment.