-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor TwilioSmsProvider into two separate modules (messaging, veri…
…fy) (#8871) ## Description Closes #8755 Builds on the earlier [configurable verify PR](#8783). Note: the mocking is SUPER ugly and feels almost useless here; very open to ideas on structuring the tests better, but I think this is another arg in favor of refactoring the SMS module cc @emilhotkowski 😅 ## Changes - Splits `TwilioSmsProvider` into two providers: `TwilioVerifyProvider` and `TwilioMessagingProvider`; providers can now be configured by country `SMS_PROVIDERS_X=twiliomessaging,twilioverify,...`, and Verify/Messaging will be by default randomized (instead of preferring Verify as previously). (I'm really not attached to the env var names, if someone has a better idea that's less clunky, I'm all 👂's!) - `twilio` provider in env variable setting is now syntactic sugar for both `twilioverify` and `twiliomessaging`, to keep backwards compatibility of env vars - Adds unit tests for the `sendSms` function (where the above changes are located) - Mocking of the twilio module to make this test possible ^ - Adds a test script to really test out the `sendSms` function (i.e. using the real twilio module), usable by adding the `TEST_SMS_RECIPIENT` parameter in `.env.development` (and making sure the necessary twilio SID + AUTH vars are also set there, just as needed for running the AS locally) - Splits out `SmsFields` from `AttestationModel` interface to make it possible to write the above two types of tests. It isn't a super clean break right now (makes sense to revisit with the full refactor of the SMS module), but it roughly separates into: `SmsFields` = fields required to produce an actual SMS, `AttestationModel` = metadata regarding production of the SMS (i.e. provider, attempt) + underlying data model Relevant docs PR: celo-org/docs#211 ## Testing - [x] used the two test scripts - [x] tested this locally - [x] deployed to Alfajores - [x] tested verification flows on Alfajores -- able to switch on/off for DE (my phone number), other flows work as expected
- Loading branch information
1 parent
26b63eb
commit 7d47b0b
Showing
16 changed files
with
481 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
packages/attestation-service/scripts/test-send-sms-twilio.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* Script for testing TwilioSmsProvider.sendSms | ||
* (Verify & Messaging Services) using the real twilio API. | ||
* Uses `.env.development` file for sensitive info; set your phone number | ||
* as TEST_SMS_RECIPIENT to receive the messages and check against | ||
* expected output (logged in console). | ||
* Comment out cases under `testCases` as desired. | ||
*/ | ||
|
||
import { fetchEnv, fetchEnvOrDefault } from '../src/env' | ||
import { SmsFields } from '../src/models/attestation' | ||
import { readUnsupportedRegionsFromEnv } from '../src/sms/base' | ||
import { TwilioSmsProvider } from '../src/sms/twilio' | ||
import { TwilioMessagingProvider } from '../src/sms/twilioMessaging' | ||
import { TwilioVerifyProvider } from '../src/sms/twilioVerify' | ||
;(async function main() { | ||
const twilioSid = fetchEnv('TWILIO_ACCOUNT_SID') | ||
const messagingServiceSid = fetchEnv('TWILIO_MESSAGING_SERVICE_SID') | ||
const verifyServiceSid = fetchEnvOrDefault('TWILIO_VERIFY_SERVICE_SID', '') | ||
const twilioAuthToken = fetchEnv('TWILIO_AUTH_TOKEN') | ||
const unsupportedRegionCodes = readUnsupportedRegionsFromEnv( | ||
'TWILIO_UNSUPPORTED_REGIONS', | ||
'TWILIO_BLACKLIST' | ||
) | ||
const testPhoneNumber = fetchEnv('TEST_SMS_RECIPIENT') | ||
const countryCode = 'DE' | ||
|
||
enum SendMethod { | ||
MESSAGE_SERVICE = 'message service', | ||
VERIFY = 'verify API', | ||
} | ||
|
||
type TestCase = { | ||
id: string | ||
expectedSendMethod: SendMethod | ||
} | ||
|
||
const testCases: TestCase[] = [ | ||
{ | ||
id: '000000', | ||
expectedSendMethod: SendMethod.MESSAGE_SERVICE, | ||
}, | ||
{ | ||
id: '111111', | ||
expectedSendMethod: SendMethod.VERIFY, | ||
}, | ||
] | ||
|
||
testCases.map(async (testCase: TestCase) => { | ||
const attestation: SmsFields = { | ||
account: '0x123', | ||
identifier: '0x456', | ||
issuer: '0x789', | ||
countryCode: countryCode, | ||
phoneNumber: testPhoneNumber, | ||
message: '', | ||
securityCode: '', | ||
attestationCode: '123', | ||
appSignature: undefined, | ||
language: 'en', | ||
} | ||
|
||
let twilioSmsProvider: TwilioSmsProvider | ||
let expectedMsg: string | ||
let expectedSidStart: string | ||
|
||
switch (testCase.expectedSendMethod) { | ||
case SendMethod.MESSAGE_SERVICE: | ||
twilioSmsProvider = new TwilioMessagingProvider( | ||
twilioSid, | ||
twilioAuthToken, | ||
unsupportedRegionCodes, | ||
messagingServiceSid | ||
) | ||
expectedMsg = attestation.message = 'via-message:' + testCase.id | ||
expectedSidStart = 'SM' | ||
break | ||
case SendMethod.VERIFY: | ||
twilioSmsProvider = new TwilioVerifyProvider( | ||
twilioSid, | ||
twilioAuthToken, | ||
unsupportedRegionCodes, | ||
verifyServiceSid | ||
) | ||
expectedMsg = 'Your Celo verification code is: ' + testCase.id | ||
attestation.securityCode = testCase.id | ||
expectedSidStart = 'VE' | ||
break | ||
} | ||
|
||
try { | ||
await twilioSmsProvider.initialize() | ||
const messageSID = await twilioSmsProvider.sendSms(attestation) | ||
const messageSIDStart = messageSID.substring(0, 2) | ||
console.log(`Message SID for id ${testCase.id}: ${messageSID}`) | ||
console.log(`SMS should match: ${expectedMsg}`) | ||
if (messageSIDStart !== expectedSidStart) { | ||
throw new Error(`Returned message SID did not match expected starting letters`) | ||
} | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
}) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.