/
create-organization.e2e.ts
195 lines (166 loc) · 8.46 KB
/
create-organization.e2e.ts
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { expect } from 'chai';
import {
MemberRepository,
OrganizationRepository,
UserRepository,
IntegrationRepository,
EnvironmentRepository,
} from '@novu/dal';
import { UserSession } from '@novu/testing';
import {
ApiServiceLevelEnum,
EmailProviderIdEnum,
ICreateOrganizationDto,
JobTitleEnum,
MemberRoleEnum,
SmsProviderIdEnum,
} from '@novu/shared';
describe('Create Organization - /organizations (POST)', async () => {
let session: UserSession;
const organizationRepository = new OrganizationRepository();
const userRepository = new UserRepository();
const memberRepository = new MemberRepository();
const integrationRepository = new IntegrationRepository();
const environmentRepository = new EnvironmentRepository();
before(async () => {
session = new UserSession();
await session.initialize({
noOrganization: true,
});
});
describe('Valid Creation', () => {
it('should add the user as admin', async () => {
const { body } = await session.testAgent
.post('/v1/organizations')
.send({
name: 'Test Org 2',
})
.expect(201);
const dbOrganization = await organizationRepository.findById(body.data._id);
const members = await memberRepository.getOrganizationMembers(dbOrganization?._id as string);
expect(members.length).to.eq(1);
expect(members[0]._userId).to.eq(session.user._id);
expect(members[0].roles[0]).to.eq(MemberRoleEnum.ADMIN);
});
it('should create organization with correct name', async () => {
const demoOrganization = {
name: 'Hello Org',
};
const { body } = await session.testAgent.post('/v1/organizations').send(demoOrganization).expect(201);
expect(body.data.name).to.eq(demoOrganization.name);
});
it('should not create organization with no name', async () => {
await session.testAgent.post('/v1/organizations').send({}).expect(400);
});
it('should create organization with apiServiceLevel of free by default', async () => {
const testOrganization = {
name: 'Free Org',
};
const { body } = await session.testAgent.post('/v1/organizations').send(testOrganization).expect(201);
const dbOrganization = await organizationRepository.findById(body.data._id);
expect(dbOrganization?.apiServiceLevel).to.eq(ApiServiceLevelEnum.FREE);
});
it('should create organization with questionnaire data', async () => {
const testOrganization: ICreateOrganizationDto = {
name: 'Org Name',
productUseCases: {
in_app: true,
multi_channel: true,
},
domain: 'org.com',
};
const { body } = await session.testAgent.post('/v1/organizations').send(testOrganization).expect(201);
const dbOrganization = await organizationRepository.findById(body.data._id);
expect(dbOrganization?.name).to.eq(testOrganization.name);
expect(dbOrganization?.domain).to.eq(testOrganization.domain);
expect(dbOrganization?.productUseCases?.in_app).to.eq(testOrganization.productUseCases?.in_app);
expect(dbOrganization?.productUseCases?.multi_channel).to.eq(testOrganization.productUseCases?.multi_channel);
});
it('should update user job title on organization creation', async () => {
const testOrganization: ICreateOrganizationDto = {
name: 'Org Name',
jobTitle: JobTitleEnum.PRODUCT_MANAGER,
};
await session.testAgent.post('/v1/organizations').send(testOrganization).expect(201);
const user = await userRepository.findById(session.user._id);
expect(user?.jobTitle).to.eq(testOrganization.jobTitle);
});
it('should create organization with built in Novu integrations and set them as primary', async () => {
const testOrganization: ICreateOrganizationDto = {
name: 'Org Name',
};
const { body } = await session.testAgent.post('/v1/organizations').send(testOrganization).expect(201);
const integrations = await integrationRepository.find({ _organizationId: body.data._id });
const environments = await environmentRepository.find({ _organizationId: body.data._id });
const productionEnv = environments.find((e) => e.name === 'Production');
const developmentEnv = environments.find((e) => e.name === 'Development');
const novuEmailIntegration = integrations.filter(
(i) => i.active && i.name === 'Novu Email' && i.providerId === EmailProviderIdEnum.Novu
);
const novuSmsIntegration = integrations.filter(
(i) => i.active && i.name === 'Novu SMS' && i.providerId === SmsProviderIdEnum.Novu
);
const novuEmailIntegrationProduction = novuEmailIntegration.filter(
(el) => el._environmentId === productionEnv?._id
);
const novuEmailIntegrationDevelopment = novuEmailIntegration.filter(
(el) => el._environmentId === developmentEnv?._id
);
const novuSmsIntegrationProduction = novuSmsIntegration.filter((el) => el._environmentId === productionEnv?._id);
const novuSmsIntegrationDevelopment = novuSmsIntegration.filter(
(el) => el._environmentId === developmentEnv?._id
);
expect(integrations.length).to.eq(4);
expect(novuEmailIntegration?.length).to.eq(2);
expect(novuSmsIntegration?.length).to.eq(2);
expect(novuEmailIntegrationProduction.length).to.eq(1);
expect(novuSmsIntegrationProduction.length).to.eq(1);
expect(novuEmailIntegrationDevelopment.length).to.eq(1);
expect(novuSmsIntegrationDevelopment.length).to.eq(1);
expect(novuEmailIntegrationProduction[0].primary).to.eq(true);
expect(novuSmsIntegrationProduction[0].primary).to.eq(true);
expect(novuEmailIntegrationDevelopment[0].primary).to.eq(true);
expect(novuSmsIntegrationDevelopment[0].primary).to.eq(true);
});
it('when Novu Email credentials are not set it should not create Novu Email integration', async () => {
const oldNovuEmailIntegrationApiKey = process.env.NOVU_EMAIL_INTEGRATION_API_KEY;
process.env.NOVU_EMAIL_INTEGRATION_API_KEY = '';
const testOrganization: ICreateOrganizationDto = {
name: 'Org Name',
};
const { body } = await session.testAgent.post('/v1/organizations').send(testOrganization).expect(201);
const integrations = await integrationRepository.find({ _organizationId: body.data._id });
const environments = await environmentRepository.find({ _organizationId: body.data._id });
const productionEnv = environments.find((e) => e.name === 'Production');
const developmentEnv = environments.find((e) => e.name === 'Development');
const novuSmsIntegration = integrations.filter(
(i) => i.active && i.name === 'Novu SMS' && i.providerId === SmsProviderIdEnum.Novu
);
expect(integrations.length).to.eq(2);
expect(novuSmsIntegration?.length).to.eq(2);
expect(novuSmsIntegration.filter((el) => el._environmentId === productionEnv?._id).length).to.eq(1);
expect(novuSmsIntegration.filter((el) => el._environmentId === developmentEnv?._id).length).to.eq(1);
process.env.NOVU_EMAIL_INTEGRATION_API_KEY = oldNovuEmailIntegrationApiKey;
});
it('when Novu SMS credentials are not set it should not create Novu SMS integration', async () => {
const oldNovuSmsIntegrationAccountSid = process.env.NOVU_SMS_INTEGRATION_ACCOUNT_SID;
process.env.NOVU_SMS_INTEGRATION_ACCOUNT_SID = '';
const testOrganization: ICreateOrganizationDto = {
name: 'Org Name',
};
const { body } = await session.testAgent.post('/v1/organizations').send(testOrganization).expect(201);
const integrations = await integrationRepository.find({ _organizationId: body.data._id });
const environments = await environmentRepository.find({ _organizationId: body.data._id });
const productionEnv = environments.find((e) => e.name === 'Production');
const developmentEnv = environments.find((e) => e.name === 'Development');
const novuEmailIntegrations = integrations.filter(
(i) => i.active && i.name === 'Novu Email' && i.providerId === EmailProviderIdEnum.Novu
);
expect(integrations.length).to.eq(2);
expect(novuEmailIntegrations?.length).to.eq(2);
expect(novuEmailIntegrations.filter((el) => el._environmentId === productionEnv?._id).length).to.eq(1);
expect(novuEmailIntegrations.filter((el) => el._environmentId === developmentEnv?._id).length).to.eq(1);
process.env.NOVU_SMS_INTEGRATION_ACCOUNT_SID = oldNovuSmsIntegrationAccountSid;
});
});
});