Problem: Sending an SMS to a User.
Expected Design
UserAuthenticator.js
const SMSGateway = require('./SMSGateway.js');
const Database = {
getUser: (userId) => {
return GLOBAL_DATABASE.get({ table: 'users', id: userId });
},
}
const API_KEY_SMS = 'a9346fe22e914ae85a40d95930d99010';
class UserAuth {
constructor(smsG) {
// gateway
this.smsG = SMSGateway(API_KEY_SMS)
}
authenticate (userId) {
this.sendVerificationcode(userId)
}
sendVerificationcode(userId) {
this.smsG.send({ number: Database.getUser(userId).number, userName: Database.getUser(userId).name,
verificationCode: this._generate_verification_code() })
}
_generate_verification_code(self) {
return '1234';
}
}
SMSGateway.js
const http = require('http');
const url = '<https://smsbt.io/sms>'
const SMSGateway = (apiKey) => {
let paramApiKey = apiKey
return {
send: function(data){
http.post(url, {
headers: { apiKey: apiKey }
payload: { apiKey: apiKey, number: data.number, message: `Your verification code is ${data.verificationCode}` }
})
}
}
}
UserAuthenticator.spec.js
describe('User Authenticator', () => {
it('should authenticate a user', () => {
const userAuthenticator = new UserAuthenticator();
GLOBAL_DATABASE.get = () => ({ number: 1234, userName: 'Fred' })
expect(userAuthenticator).to.not.be(null)
expect(userAuthenticator).to.be.instanceOf(UserAuthenticator)
userAuthenticator.authenticate('user-id')
});
});