-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathget-platform-notification-usage.e2e-ee.ts
More file actions
153 lines (128 loc) · 4.88 KB
/
get-platform-notification-usage.e2e-ee.ts
File metadata and controls
153 lines (128 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { PinoLogger } from '@novu/application-generic';
import {
GetPlatformNotificationUsage,
GetPlatformNotificationUsageCommand,
} from '@novu/ee-billing';
import { CommunityOrganizationRepository, EnvironmentRepository, NotificationRepository } from '@novu/dal';
import { ApiServiceLevelEnum, isClerkEnabled } from '@novu/shared';
import { UserSession } from '@novu/testing';
import { expect } from 'chai';
import sinon from 'sinon';
describe('GetPlatformNotificationUsage #novu-v2', () => {
const environmentRepo = new EnvironmentRepository();
const notificationRepo = new NotificationRepository();
const communityOrganizationRepo = new CommunityOrganizationRepository();
const mockWorkflowRunRepository = {
getPlatformUsageByDateRange: sinon.stub().resolves([]),
};
const mockFeatureFlagsService = {
getFlag: sinon.stub().resolves(false),
};
const mockCacheService = {
cacheEnabled: sinon.stub().returns(false),
get: sinon.stub().resolves(undefined),
};
const createUseCase = () => {
return new GetPlatformNotificationUsage(
mockWorkflowRunRepository,
environmentRepo,
notificationRepo,
communityOrganizationRepo,
mockFeatureFlagsService,
mockCacheService,
new PinoLogger({})
);
};
let session: UserSession;
beforeEach(async () => {
session = new UserSession();
await session.initialize();
mockWorkflowRunRepository.getPlatformUsageByDateRange.reset();
mockWorkflowRunRepository.getPlatformUsageByDateRange.resolves([]);
mockFeatureFlagsService.getFlag.reset();
mockFeatureFlagsService.getFlag.resolves(false);
});
it(`should return an empty array when there is no recorded usage`, async () => {
const useCase = createUseCase();
// Create organizations without notifications
const orgsWithoutNotificationsPromises = new Array(10).fill(null).map(async () => {
const orgSession = new UserSession();
await orgSession.initialize();
return Promise.resolve(orgSession.organization._id);
});
await Promise.all(orgsWithoutNotificationsPromises);
const result = await useCase.execute(
GetPlatformNotificationUsageCommand.create({
startDate: new Date('2021-01-01'),
endDate: new Date('2021-01-31'),
})
);
expect(result).to.deep.equal([]);
});
it(`should return the usage for the given date range`, async () => {
const useCase = createUseCase();
const mockStartDate = new Date('2021-01-01');
const mockNotificationDate = new Date('2021-01-05');
const mockEndDate = new Date('2021-01-31');
const notificationCountPerIndex = 10;
const orgCount = 10;
const organizations: any[] = [];
for (let index = 0; index < orgCount; index += 1) {
const orgSession = new UserSession();
await orgSession.initialize();
const notificationsCount = notificationCountPerIndex * (index + 1);
await notificationRepo.insertMany(
new Array(notificationsCount).fill({
_organizationId: orgSession.organization._id,
_environmentId: orgSession.environment._id,
createdAt: mockNotificationDate,
})
);
await orgSession.updateOrganizationServiceLevel(ApiServiceLevelEnum.BUSINESS);
organizations.push({ id: orgSession.organization._id, notificationsCount });
}
let expectedResult = organizations.map((org) => ({
_id: org.id.toString(),
apiServiceLevel: ApiServiceLevelEnum.BUSINESS,
notificationsCount: org.notificationsCount,
}));
if (isClerkEnabled()) {
// we have just one organization in Clerk - we don't create new ones on initialize()
expectedResult = [expectedResult[expectedResult.length - 1]];
}
const result = await useCase.execute(
GetPlatformNotificationUsageCommand.create({
startDate: mockStartDate,
endDate: mockEndDate,
})
);
expect(result).to.include.deep.members(expectedResult.splice(0, 1));
});
it(`should return the usage for the given single organization`, async () => {
await session.updateOrganizationServiceLevel(ApiServiceLevelEnum.BUSINESS);
const useCase = createUseCase();
const notificationsCount = 110;
const mockNotificationDate = new Date('2021-01-05');
await notificationRepo.insertMany(
new Array(notificationsCount).fill({
_organizationId: session.organization._id,
_environmentId: session.environment._id,
createdAt: mockNotificationDate,
})
);
const result = await useCase.execute(
GetPlatformNotificationUsageCommand.create({
startDate: new Date('2021-01-01'),
endDate: new Date('2021-01-31'),
organizationId: session.organization._id,
})
);
expect(result).to.deep.equal([
{
_id: session.organization._id.toString(),
apiServiceLevel: ApiServiceLevelEnum.BUSINESS,
notificationsCount,
},
]);
});
});