Skip to content

Commit be2d2bf

Browse files
authored
Merge pull request #177 from twilio-labs/disable-calls
Disable calls by default for the verify template
2 parents 34093fd + a51ee80 commit be2d2bf

File tree

2 files changed

+86
-43
lines changed

2 files changed

+86
-43
lines changed

verify/functions/start-verify.js

+40-24
Original file line numberDiff line numberDiff line change
@@ -19,57 +19,73 @@
1919
* }
2020
*/
2121

22-
exports.handler = function(context, event, callback) {
22+
exports.handler = function (context, event, callback) {
2323
const response = new Twilio.Response();
24-
response.appendHeader('Content-Type', 'application/json');
25-
24+
response.appendHeader("Content-Type", "application/json");
25+
2626
// uncomment to support CORS
2727
// response.appendHeader('Access-Control-Allow-Origin', '*');
2828
// response.appendHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
2929
// response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
3030

31-
if (typeof event.to === 'undefined') {
31+
if (typeof event.to === "undefined") {
3232
response.setBody({
33-
"success": false,
34-
"error": {
35-
"message": "Missing parameter; please provide a phone number or email.",
36-
"moreInfo": "https://www.twilio.com/docs/verify/api/verification"
37-
}
38-
})
33+
success: false,
34+
error: {
35+
message: "Missing parameter; please provide a phone number or email.",
36+
moreInfo: "https://www.twilio.com/docs/verify/api/verification",
37+
},
38+
});
39+
response.setStatusCode(400);
40+
return callback(null, response);
41+
}
42+
43+
// DELETE THIS BLOCK IF YOU WANT TO ENABLE THE VOICE CHANNEL
44+
// Learn more about toll fraud
45+
// https://www.twilio.com/docs/verify/preventing-toll-fraud
46+
if (event.channel === "call") {
47+
response.setBody({
48+
success: false,
49+
error: {
50+
message:
51+
"Calls disabled by default. Update the code in <code>start-verify.js</code> to enable.",
52+
moreInfo: "https://www.twilio.com/docs/verify/preventing-toll-fraud",
53+
},
54+
});
3955
response.setStatusCode(400);
4056
return callback(null, response);
4157
}
4258

4359
const client = context.getTwilioClient();
4460
const service = context.VERIFY_SERVICE_SID;
4561
const to = event.to;
46-
const channel = (typeof event.channel === 'undefined') ? "sms" : event.channel;
47-
const locale = (typeof event.locale === 'undefined') ? "en" : event.locale;
62+
const channel = typeof event.channel === "undefined" ? "sms" : event.channel;
63+
const locale = typeof event.locale === "undefined" ? "en" : event.locale;
4864

49-
client.verify.services(service)
50-
.verifications
51-
.create({
65+
client.verify
66+
.services(service)
67+
.verifications.create({
5268
to: to,
5369
channel: channel,
54-
locale: locale
70+
locale: locale,
5571
})
56-
.then(verification => {
72+
.then((verification) => {
5773
console.log(`Sent verification: '${verification.sid}'`);
5874
response.setStatusCode(200);
5975
response.setBody({
60-
"success": true
76+
success: true,
6177
});
6278
callback(null, response);
6379
})
64-
.catch(error => {
80+
.catch((error) => {
6581
console.log(error);
6682
response.setStatusCode(error.status);
6783
response.setBody({
68-
"success": false,
69-
"error": {
70-
"message": error.message,
71-
"moreInfo": error.moreInfo
72-
}
84+
success: false,
85+
error: {
86+
message: error.message,
87+
moreInfo: error.moreInfo,
88+
},
7389
});
7490
callback(null, response);
7591
});

verify/tests/start-verify.test.js

+46-19
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,80 @@
1-
const startVerifyFunction = require('../functions/start-verify').handler;
2-
const helpers = require('../../test/test-helper');
1+
const startVerifyFunction = require("../functions/start-verify").handler;
2+
const helpers = require("../../test/test-helper");
33

44
const mockService = {
55
verifications: {
6-
create: jest.fn(() => Promise.resolve({
7-
sid: "my-new-sid"
8-
}))
9-
}
10-
}
6+
create: jest.fn(() =>
7+
Promise.resolve({
8+
sid: "my-new-sid",
9+
})
10+
),
11+
},
12+
};
1113

1214
const mockClient = {
1315
verify: {
14-
services: jest.fn(() => mockService)
15-
}
16-
}
16+
services: jest.fn(() => mockService),
17+
},
18+
};
1719

1820
const testContext = {
19-
VERIFY_SERVICE_SID: 'default',
20-
getTwilioClient: () => mockClient
21+
VERIFY_SERVICE_SID: "default",
22+
getTwilioClient: () => mockClient,
2123
};
2224

23-
describe('verify/start-verification', () => {
25+
describe("verify/start-verification", () => {
2426
beforeAll(() => {
2527
helpers.setup({});
2628
});
2729
afterAll(() => {
2830
helpers.teardown();
2931
});
3032

31-
test('returns an error response when required parameters are missing', done => {
33+
test("returns an error response when required parameters are missing", (done) => {
3234
const callback = (err, result) => {
3335
expect(result).toBeDefined();
3436
expect(result._body.success).toEqual(false);
35-
expect(result._body.error.message).toEqual("Missing parameter; please provide a phone number or email.");
36-
expect(mockClient.verify.services).not.toHaveBeenCalledWith(testContext.VERIFY_SERVICE_SID);
37+
expect(result._body.error.message).toEqual(
38+
"Missing parameter; please provide a phone number or email."
39+
);
40+
expect(mockClient.verify.services).not.toHaveBeenCalledWith(
41+
testContext.VERIFY_SERVICE_SID
42+
);
3743
done();
3844
};
3945
const event = {};
4046
startVerifyFunction(testContext, event, callback);
4147
});
4248

43-
test('returns success with valid request', done => {
49+
test('returns an error when "call" is the channel parameter', (done) => {
50+
const callback = (err, result) => {
51+
expect(result).toBeDefined();
52+
expect(result._body.success).toEqual(false);
53+
expect(result._body.error.message).toEqual(
54+
"Calls disabled by default. Update the code in <code>start-verify.js</code> to enable."
55+
);
56+
expect(mockClient.verify.services).not.toHaveBeenCalledWith(
57+
testContext.VERIFY_SERVICE_SID
58+
);
59+
done();
60+
};
61+
const event = {
62+
to: "+17341234567",
63+
channel: "call",
64+
};
65+
startVerifyFunction(testContext, event, callback);
66+
});
67+
68+
test("returns success with valid request", (done) => {
4469
const callback = (err, result) => {
4570
expect(result).toBeDefined();
4671
expect(result._body.success).toEqual(true);
47-
expect(mockClient.verify.services).toHaveBeenCalledWith(testContext.VERIFY_SERVICE_SID);
72+
expect(mockClient.verify.services).toHaveBeenCalledWith(
73+
testContext.VERIFY_SERVICE_SID
74+
);
4875
done();
4976
};
50-
const event = { "to": "+17341234567" }
77+
const event = { to: "+17341234567" };
5178
startVerifyFunction(testContext, event, callback);
5279
});
5380
});

0 commit comments

Comments
 (0)