forked from StateVoicesNational/Spoke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_helpers.js
737 lines (682 loc) · 17.9 KB
/
test_helpers.js
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
import _ from "lodash";
import {
createLoaders,
createTables,
dropTables,
User,
CampaignContact,
r
} from "../src/server/models/";
import { graphql } from "graphql";
export async function setupTest() {
await createTables();
}
export async function cleanupTest() {
await dropTables();
}
export function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export function getContext(context) {
return {
...context,
req: {},
loaders: createLoaders()
};
}
export async function createUser(
userInfo = {},
organizationId = null,
role = null
) {
const defaultUserInfo = {
auth0_id: "test123",
first_name: "TestUserFirst",
last_name: "TestUserLast",
cell: "555-555-5555",
email: "[email protected]"
};
const user = new User({
...defaultUserInfo,
...userInfo
});
await user.save();
if (organizationId && role) {
await r.knex("user_organization").insert({
user_id: user.id,
organization_id: organizationId,
role
});
}
return user;
}
export async function createContacts(campaign, count = 1) {
const campaignId = campaign.id;
const contacts = [];
const startNum = "+15155500000";
for (let i = 0; i < count; i++) {
const contact = new CampaignContact({
first_name: `Ann${i}`,
last_name: `Lewis${i}`,
cell: startNum.substr(0, startNum.length - String(i).length) + String(i),
zip: "12345",
campaign_id: campaignId
});
await contact.save();
contacts.push(contact);
}
return contacts;
}
import { makeExecutableSchema } from "graphql-tools";
import { resolvers } from "../src/server/api/schema";
import { schema } from "../src/api/schema";
const mySchema = makeExecutableSchema({
typeDefs: schema,
resolvers,
allowUndefinedInResolve: true
});
/**
* Get the text for a query parsed with the gql tag
*/
function getGqlOperationText(op) {
return op.loc && op.loc.source.body;
}
export async function runGql(operation, vars, user) {
const operationText = getGqlOperationText(operation) || operation;
const rootValue = {};
const context = getContext({ user });
const result = await graphql(
mySchema,
operationText,
rootValue,
context,
vars
);
if (result && result.errors) {
console.log("runGql failed " + JSON.stringify(result));
}
return result;
}
export const updateUserRoles = async (
adminUser,
organizationId,
userId,
roles
) => {
const query = `mutation editOrganizationRoles(
$organizationId: String!,
$userId: String!,
$roles: [String]) {
editOrganizationRoles(userId: $userId, organizationId: $organizationId, roles: $roles) {
id
}
}`;
const variables = {
organizationId,
userId,
roles
};
const result = await runGql(query, variables, adminUser);
if (result && result.errors) {
throw new Exception(
"editOrganizationRoles failed " + JSON.stringify(result)
);
}
return result;
};
export async function createInvite() {
const rootValue = {};
const inviteQuery = `mutation {
createInvite(invite: {is_valid: true}) {
id
}
}`;
const context = getContext();
return await graphql(mySchema, inviteQuery, rootValue, context);
}
export async function createOrganization(user, invite) {
const rootValue = {};
const name = "Testy test organization";
const userId = user.id;
const inviteId = invite.data.createInvite.id;
const context = getContext({ user });
const orgQuery = `mutation createOrganization($name: String!, $userId: String!, $inviteId: String!) {
createOrganization(name: $name, userId: $userId, inviteId: $inviteId) {
id
uuid
}
}`;
const variables = {
userId,
name,
inviteId
};
return await graphql(mySchema, orgQuery, rootValue, context, variables);
}
export async function setTwilioAuth(user, organization) {
const rootValue = {};
const accountSid = "test_twilio_account_sid";
const authToken = "test_twlio_auth_token";
const messageServiceSid = "test_message_service";
const orgId = organization.data.createOrganization.id;
const context = getContext({ user });
const twilioQuery = `
mutation updateTwilioAuth(
$twilioAccountSid: String
$twilioAuthToken: String
$twilioMessageServiceSid: String
$organizationId: String!
) {
updateTwilioAuth(
twilioAccountSid: $twilioAccountSid
twilioAuthToken: $twilioAuthToken
twilioMessageServiceSid: $twilioMessageServiceSid
organizationId: $organizationId
) {
id
twilioAccountSid
twilioAuthToken
twilioMessageServiceSid
}
}`;
const variables = {
organizationId: orgId,
twilioAccountSid: accountSid,
twilioAuthToken: authToken,
twilioMessageServiceSid: messageServiceSid
};
const result = await graphql(
mySchema,
twilioQuery,
rootValue,
context,
variables
);
if (result && result.errors) {
console.log("updateTwilioAuth failed " + JSON.stringify(result));
}
return result;
}
export async function createCampaign(
user,
organization,
title = "test campaign",
args = {}
) {
const rootValue = {};
const description = "test description";
const organizationId = organization.data.createOrganization.id;
const context = getContext({ user });
const campaignQuery = `mutation createCampaign($input: CampaignInput!) {
createCampaign(campaign: $input) {
id
}
}`;
const variables = {
input: {
title,
description,
organizationId,
...args
}
};
const result = await graphql(
mySchema,
campaignQuery,
rootValue,
context,
variables
);
if (result.errors) {
throw new Exception("Create campaign failed " + JSON.stringify(result));
}
return result.data.createCampaign;
}
export async function saveCampaign(
user,
campaign,
title = "test campaign",
useOwnMessagingService = "false"
) {
const rootValue = {};
const description = "test description";
const organizationId = campaign.organizationId;
const context = getContext({ user });
const campaignQuery = `mutation editCampaign($campaignId: String!, $campaign: CampaignInput!) {
editCampaign(id: $campaignId, campaign: $campaign) {
id
title
useOwnMessagingService
}
}`;
const variables = {
campaign: {
title,
description,
organizationId,
useOwnMessagingService
},
campaignId: campaign.id
};
const result = await graphql(
mySchema,
campaignQuery,
rootValue,
context,
variables
);
if (result.errors) {
throw new Exception("Create campaign failed " + JSON.stringify(result));
}
return result.data.editCampaign;
}
export async function copyCampaign(campaignId, user) {
const rootValue = {};
const query = `mutation copyCampaign($campaignId: String!) {
copyCampaign(id: $campaignId) {
id
}
}`;
const context = getContext({ user });
return await graphql(mySchema, query, rootValue, context, { campaignId });
}
export async function createTexter(organization, userInfo = {}) {
const rootValue = {};
const defaultUserInfo = {
auth0_id: "test456",
first_name: "TestTexterFirst",
last_name: "TestTexterLast",
cell: "555-555-6666",
email: "[email protected]"
};
const user = await createUser(
{ ...defaultUserInfo, ...userInfo },
organization.data.createOrganization.id,
"TEXTER"
);
if (user.errors) {
throw new Exception("createUsers failed " + JSON.stringify(user));
}
const joinQuery = `
mutation joinOrganization($organizationUuid: String!) {
joinOrganization(organizationUuid: $organizationUuid) {
id
}
}`;
const variables = {
organizationUuid: organization.data.createOrganization.uuid
};
const context = getContext({ user });
const result = await graphql(
mySchema,
joinQuery,
rootValue,
context,
variables
);
if (result.errors) {
throw new Exception("joinOrganization failed " + JSON.stringify(result));
}
return user;
}
export async function assignTexter(admin, user, campaign, assignments) {
// optional argument assignments could look like:
// [{id: userId1, needsMessageCount: 10}, {id: userId2, needsMessageCount: 100}]
// needsMessageCount: total desired number of unmessaged contacts
// contactsCount: (messagedCount from texter) + needsMessageCount (above)
// If a userId has an existing assignment, then, also include `contactsCount: <current>`
const rootValue = {};
const campaignEditQuery = `
mutation editCampaign($campaignId: String!, $campaign: CampaignInput!) {
editCampaign(id: $campaignId, campaign: $campaign) {
id
}
}`;
const context = getContext({ user: admin });
const updateCampaign = Object.assign({}, campaign);
const campaignId = updateCampaign.id;
updateCampaign.texters = assignments || [
{
id: user.id
}
];
delete updateCampaign.id;
delete updateCampaign.contacts;
const variables = {
campaignId,
campaign: updateCampaign
};
const result = await graphql(
mySchema,
campaignEditQuery,
rootValue,
context,
variables
);
if (result.errors) {
throw new Exception("assignTexter failed " + JSON.stringify(result));
}
return result;
}
export async function sendMessage(campaignContactId, user, message) {
const rootValue = {};
const query = `
mutation sendMessage($message: MessageInput!, $campaignContactId: String!) {
sendMessage(message: $message, campaignContactId: $campaignContactId) {
id
messageStatus
messages {
id
createdAt
text
isFromContact
}
}
}`;
const context = getContext({ user });
const variables = {
message,
campaignContactId
};
const result = await graphql(mySchema, query, rootValue, context, variables);
if (result.errors) {
console.log("sendMessage errors", result);
}
return result;
}
export async function bulkSendMessages(assignmentId, user) {
const query = `
mutation bulkSendMessage($assignmentId: Int!) {
bulkSendMessages(assignmentId: $assignmentId) {
id
}
}`;
const variables = {
assignmentId
};
return runGql(query, variables, user);
}
export function buildScript(steps = 2, choices = 1) {
const makeStep = (step, max, choice = "") => ({
id: `new${step}_${choice}`,
questionText: `hmm${step}`,
script:
step === 1
? "{lastName}"
: step === 0
? "autorespond {zip}"
: `${choice}Step Script ${step}`,
answerOption: `${choice}hmm${step}`,
answerActions: "",
parentInteractionId: step > 0 ? "new" + (step - 1) + "_" : null,
isDeleted: false,
interactionSteps: choice ? [] : createSteps(step + 1, max)
});
const createSteps = (step, max) => {
if (max <= step) {
return [];
}
const rv = [makeStep(step, max)];
for (let i = 1; i < choices; i++) {
rv.push(makeStep(step, max, i));
}
return rv;
};
return createSteps(0, steps);
}
export async function createScript(
admin,
campaign,
{ interactionSteps, steps = 2, choices = 1, campaignGqlFragment } = {}
) {
const rootValue = {};
const campaignEditQuery = `
mutation editCampaign($campaignId: String!, $campaign: CampaignInput!) {
editCampaign(id: $campaignId, campaign: $campaign) {
${campaignGqlFragment || "id"}
}
}`;
// function to create a recursive set of steps of arbitrary depth
let builtInteractionSteps;
if (!interactionSteps) {
builtInteractionSteps = buildScript(steps, choices);
}
const context = getContext({ user: admin });
const campaignId = campaign.id;
const variables = {
campaignId,
campaign: {
interactionSteps: interactionSteps || builtInteractionSteps[0]
}
};
return await graphql(
mySchema,
campaignEditQuery,
rootValue,
context,
variables
);
}
export async function createCannedResponses(admin, campaign, cannedResponses) {
// cannedResponses: {title, text}
const rootValue = {};
const campaignEditQuery = `
mutation editCampaign($campaignId: String!, $campaign: CampaignInput!) {
editCampaign(id: $campaignId, campaign: $campaign) {
id
}
}`;
const context = getContext({ user: admin });
const campaignId = campaign.id;
const variables = {
campaignId,
campaign: {
cannedResponses
}
};
return await graphql(
mySchema,
campaignEditQuery,
rootValue,
context,
variables
);
}
jest.mock("../src/server/mail");
export async function startCampaign(admin, campaign) {
const rootValue = {};
const startCampaignQuery = `mutation startCampaign($campaignId: String!) {
startCampaign(id: $campaignId) {
id
}
}`;
const context = getContext({ user: admin });
const variables = { campaignId: campaign.id };
return await graphql(
mySchema,
startCampaignQuery,
rootValue,
context,
variables
);
}
export async function getCampaignContact(id) {
return await r
.knex("campaign_contact")
.where({ id })
.first();
}
export async function getOptOut(assignmentId, cell) {
return await r
.knex("opt_out")
.where({
cell,
assignment_id: assignmentId
})
.first();
}
export async function createStartedCampaign() {
const testAdminUser = await createUser();
const testInvite = await createInvite();
const testOrganization = await createOrganization(testAdminUser, testInvite);
const organizationId = testOrganization.data.createOrganization.id;
const testCampaign = await createCampaign(testAdminUser, testOrganization);
const testContacts = await createContacts(testCampaign, 100);
const testTexterUser = await createTexter(testOrganization);
const testTexterUser2 = await createTexter(testOrganization);
await startCampaign(testAdminUser, testCampaign);
await assignTexter(testAdminUser, testTexterUser, testCampaign);
const dbCampaignContact = await getCampaignContact(testContacts[0].id);
const assignmentId = dbCampaignContact.assignment_id;
const assignment = (await r.knex("assignment").where("id", assignmentId))[0];
const testSuperAdminUser = await createUser(
{
auth0_id: "2024561111",
first_name: "super",
last_name: "admin",
cell: "202-456-1111",
email: "[email protected]",
is_superadmin: true
},
organizationId,
"ADMIN"
);
return {
testAdminUser,
testInvite,
testOrganization,
testCampaign,
testTexterUser,
testTexterUser2,
testContacts,
assignmentId,
assignment,
testSuperAdminUser,
organizationId,
dbCampaignContact
};
}
export const getConversations = async (
user,
organizationId,
contactsFilter,
campaignsFilter,
assignmentsFilter
) => {
const cursor = {
offset: 0,
limit: 1000
};
const variables = {
cursor,
organizationId,
contactsFilter,
campaignsFilter,
assignmentsFilter
};
const conversationsQuery = `
query Q(
$organizationId: String!
$cursor: OffsetLimitCursor!
$contactsFilter: ContactsFilter
$campaignsFilter: CampaignsFilter
$assignmentsFilter: AssignmentsFilter
$utc: String
) {
conversations(
cursor: $cursor
organizationId: $organizationId
campaignsFilter: $campaignsFilter
contactsFilter: $contactsFilter
assignmentsFilter: $assignmentsFilter
utc: $utc
) {
pageInfo {
limit
offset
total
}
conversations {
texter {
id
displayName
roles(organizationId: $organizationId)
}
contact {
id
assignmentId
firstName
lastName
cell
messageStatus
messages {
id
text
isFromContact
}
optOut {
cell
}
}
campaign {
id
title
}
}
}
}
`;
return runGql(conversationsQuery, variables, user);
};
export const createJob = async (campaign, overrides) => {
const job = {
campaign_id: campaign.id,
payload: "fake_payload",
queue_name: "1:fake_queue_name",
job_type: "fake_job_type",
locks_queue: true,
assigned: true,
status: 0,
...(overrides && overrides)
};
const [job_id] = await r
.knex("job_request")
.returning("id")
.insert(job);
job.id = job_id;
return job;
};
export const makeRunnableMutations = (mutationsToWrap, user, ownProps) => {
const newMutations = {};
Object.keys(mutationsToWrap).forEach(k => {
newMutations[k] = async (...args) => {
// TODO validate received args against args in the schema
const toWrap = mutationsToWrap[k](ownProps)(...args);
return runGql(toWrap.mutation, toWrap.variables, user);
};
});
return newMutations;
};
export const runComponentQueries = async (queries, user, ownProps) => {
const keys = Object.keys(queries);
const promises = keys.map(k => {
const query = queries[k];
const opts = query.options(ownProps);
return runGql(query.query, opts.variables, user);
});
const resolvedPromises = await Promise.all(promises);
const queryResults = {};
for (let i = 0; i < keys.length; i++) {
const dataKey = Object.keys(resolvedPromises[i].data)[0];
const key = keys[i];
queryResults[key] = {
// as implemented here refetch does not hit the resolvers
// and returns the data that was originally feteched
refetch: async () => {
return Promise.resolve(resolvedPromises[i].data[dataKey]);
}
};
queryResults[key][dataKey] = resolvedPromises[i].data[dataKey];
}
return queryResults;
};