This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (123 loc) · 5.1 KB
/
index.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
const crypto = require("crypto");
const fetch = require("node-fetch");
const promiseRetry = require("promise-retry");
/**
* Helper with disposable mailbox api that is available within test execution.
*/
class Mailbox extends Helper {
/**
*
* @param {Object} config configuration can be overridden by values found in `codecept.json`
*/
constructor(config) {
super(config);
this.mailbox = {
address: "",
messages: []
};
this.options = {};
Object.assign(this.options, config);
}
/**
*
* @param {string=} name - Optional string to be used in mainlbox address
* @returns {object} - Creates a mailbox object and checks for mail before returning.
* @example
* I.createMailbox('testmail') //{address: "[email protected]", messages: {error: "there are no messages yet"}}
* I.createMailbox() //{address: "[email protected]", messages: {error: "there are no messages yet"}}
*/
createMailbox(name) {
const y = this.mailbox;
_createAddress(name)
.then(a => (y.address = a))
.then(_getMessages)
.then(m => (y.messages = m));
return y;
}
/**
*
* @param {object=} mailbox - Takes a mailbox object and detletes the last (i.e. most recent) value from messages and the mail server.
*/
deleteLatestMessage(mailbox = this.mailbox) {
if (mailbox.messages !== [] && !Array.isArray(mailbox.messages)) {
let y = mailbox
.messages
.pop();
return _deleteMessage(y.mail_id);
}
return;
}
/**
* @returns The mailbox object in it's current state, i.e. if you have called the methods elsewhere in this file without arguments, the Mailbox object is updated internally. This returns it.
* @param {boolean} debug - Logs the mailbox out to the console to aid debugging.
*/
getMailbox(debug = false) {
debug && console.log(this.mailbox)
return this.mailbox;
}
/**
* @returns The mailbox object in it's current state, i.e. if you have called the methods elsewhere in this file without arguments, the Mailbox object is updated internally. This returns it.
*/
getMailbox() {
return this.mailbox;
}
/**
*
* @param {*} mailbox {object=} mailbox - Takes a mailbox object and assigns a `mailbox.latest` object with the last (i.e. most recent) value in the messages array returned from the server.
*
*/
getLatestMessage(mailbox = this.mailbox) {
_getMessages(mailbox.address).then(m => (m != typeof "object" ?
(mailbox.latest = m.pop()) :
(mailbox.latest = m))).catch(error => this.lastError);
return mailbox.latest;
}
/**
*
* @param {string} id - Takes a mail_id string and returns it from the `mailbox.messages` array
*/
getMailById(id) {
return this
.mailbox
.messages
.filter(obj => {
return obj.mail_id == id;
});
}
/**
*
* @param {object=} mailbox - Takes a mailbox object and makes 10 attempts at retrieving messages from the server. Once a suitable response is received the `mailbox.messages` and `mailbox.latest` are updated.
*/
waitForMessage(mailbox = this.mailbox) {
return promiseRetry(retry => {
return _getMessages(mailbox.address).then(res => {
if (!Array.isArray(res)) {
retry(res);
}
return (mailbox.messages = res);
})
.then(m => (mailbox.latest = m.pop()))
.catch(error => this.lastError);
});
}
}
const apiUrl = "http://api.temp-mail.ru";
const _hashAddress = str => new Promise((resolve, reject) => str ?
resolve(crypto.createHash("md5").update(str).digest("hex")) :
reject("No string provided"));
const _transformResponse = res => res.json();
const _fetchTransform = path => fetch(`${apiUrl}${path || ""}/format/json/`).then(_transformResponse);
const _randArrayItem = arr => arr[Math.floor(Math.random() * arr.length)];
const _randString = () => Math
.random()
.toString(32)
.substring(2);
const _genRandomMail = () => _fetchTransform("/request/domains/").then(r => _randString() + _randArrayItem(r));
const _genNamedMail = str => _fetchTransform("/request/domains/").then(r => str + _randArrayItem(r));
const _createAddress = name => (name ?
_genNamedMail(name) :
_genRandomMail());
const _deleteMessage = msgId => _fetchTransform(`/request/delete/id/${msgId}`);
const _getMessages = addr => _hashAddress(addr).then(hash => _fetchTransform(`/request/mail/id/${hash}`));
const _delay = t => new Promise(resolve => setTimeout(resolve, t));
module.exports = Mailbox;