2014-01-18 05:31:27 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-09-19 16:00:37 +03:00
|
|
|
'use strict'
|
|
|
|
|
2015-05-14 06:33:05 +03:00
|
|
|
var P = require('../lib/promise')
|
2014-01-18 05:31:27 +04:00
|
|
|
var request = require('request')
|
2015-07-16 06:18:12 +03:00
|
|
|
const EventEmitter = require('events').EventEmitter
|
2014-01-18 05:31:27 +04:00
|
|
|
|
2016-09-14 07:51:51 +03:00
|
|
|
/* eslint-disable no-console */
|
2016-11-08 23:04:46 +03:00
|
|
|
module.exports = function (host, port, printLogs) {
|
2014-01-18 05:31:27 +04:00
|
|
|
|
2014-01-18 23:29:39 +04:00
|
|
|
host = host || '127.0.0.1'
|
|
|
|
port = port || 9001
|
2014-01-18 05:31:27 +04:00
|
|
|
|
2015-07-16 06:18:12 +03:00
|
|
|
const eventEmitter = new EventEmitter()
|
|
|
|
|
2016-11-08 23:04:46 +03:00
|
|
|
function log() {
|
|
|
|
if (printLogs) {
|
|
|
|
console.log.apply(console, arguments)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-18 23:29:39 +04:00
|
|
|
function waitForCode(email) {
|
|
|
|
return waitForEmail(email)
|
|
|
|
.then(
|
|
|
|
function (emailData) {
|
2017-03-21 02:27:14 +03:00
|
|
|
var code = emailData.headers['x-verify-code'] ||
|
|
|
|
emailData.headers['x-recovery-code']
|
|
|
|
if (! code) {
|
|
|
|
throw new Error('email did not contain a verification code')
|
|
|
|
}
|
|
|
|
return code
|
2014-01-18 23:29:39 +04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2014-01-18 05:31:27 +04:00
|
|
|
|
2014-01-18 23:29:39 +04:00
|
|
|
function loop(name, tries, cb) {
|
2016-03-02 12:37:20 +03:00
|
|
|
var url = 'http://' + host + ':' + port + '/mail/' + encodeURIComponent(name)
|
2016-11-08 23:04:46 +03:00
|
|
|
log('checking mail', url)
|
2014-01-18 23:29:39 +04:00
|
|
|
request({ url: url, method: 'GET' },
|
|
|
|
function (err, res, body) {
|
2016-11-08 23:04:46 +03:00
|
|
|
log('mail status', res && res.statusCode, 'tries', tries)
|
|
|
|
log('mail body', body)
|
2014-01-18 23:29:39 +04:00
|
|
|
var json = null
|
|
|
|
try {
|
2018-05-10 19:44:38 +03:00
|
|
|
json = JSON.parse(body)
|
|
|
|
|
|
|
|
if (json.length === 1) {
|
|
|
|
json = json[0]
|
|
|
|
}
|
2014-01-18 23:29:39 +04:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return cb(e)
|
|
|
|
}
|
2014-01-18 05:31:27 +04:00
|
|
|
|
2017-04-28 22:07:53 +03:00
|
|
|
if (! json) {
|
2014-01-18 23:29:39 +04:00
|
|
|
if (tries === 0) {
|
|
|
|
return cb(new Error('could not get mail for ' + url))
|
|
|
|
}
|
|
|
|
return setTimeout(loop.bind(null, name, --tries, cb), 1000)
|
|
|
|
}
|
2016-11-08 23:04:46 +03:00
|
|
|
log('deleting mail', url)
|
2014-01-18 23:29:39 +04:00
|
|
|
request({ url: url, method: 'DELETE' },
|
|
|
|
function (err, res, body) {
|
|
|
|
cb(err, json)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2014-01-18 05:31:27 +04:00
|
|
|
|
2014-01-18 23:29:39 +04:00
|
|
|
function waitForEmail(email) {
|
|
|
|
var d = P.defer()
|
|
|
|
loop(email.split('@')[0], 20, function (err, json) {
|
2015-07-16 06:18:12 +03:00
|
|
|
if (err) {
|
|
|
|
eventEmitter.emit('email:error', email, err)
|
|
|
|
return d.reject(err)
|
|
|
|
}
|
|
|
|
eventEmitter.emit('email:message', email, json)
|
|
|
|
return d.resolve(json)
|
2014-01-18 23:29:39 +04:00
|
|
|
})
|
|
|
|
return d.promise
|
|
|
|
}
|
2014-01-18 05:31:27 +04:00
|
|
|
|
2017-05-29 11:54:27 +03:00
|
|
|
function waitForSms (phoneNumber) {
|
|
|
|
return waitForEmail(`sms.${phoneNumber}@restmail.net`)
|
|
|
|
}
|
|
|
|
|
2014-01-18 23:29:39 +04:00
|
|
|
return {
|
|
|
|
waitForEmail: waitForEmail,
|
2015-07-16 06:18:12 +03:00
|
|
|
waitForCode: waitForCode,
|
2017-05-29 11:54:27 +03:00
|
|
|
waitForSms: waitForSms,
|
2015-07-16 06:18:12 +03:00
|
|
|
eventEmitter: eventEmitter
|
2014-01-18 23:29:39 +04:00
|
|
|
}
|
2014-01-18 05:31:27 +04:00
|
|
|
}
|