-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fetch.test.js
326 lines (324 loc) · 14.2 KB
/
Fetch.test.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
const Fetch = require('./Fetch.js');
const sampleData = require('./sampleMail.json');
describe(`Fetch test suite`, () => {
describe(`test constructor`, () => {
// test 1
it(`should throw error if no username or password provided`, () => {
expect(() => {
let fetcher = new Fetch('sample.address@foo.com', '');
}).toThrow('Invalid object initialization - must provide username and password');
expect(() => {
let fetcher2 = new Fetch('', 'password123!');
}).toThrow('Invalid object initialization - must provide username and password');
expect(() => {
let fetcher3 = new Fetch('', '');
}).toThrow('Invalid object initialization - must provide username and password');
});
// test 2
it(`should initialize object`, () => {
let fetcher = new Fetch('sample.address@foo.com', 'asdf');
expect(fetcher._username).toEqual('sample.address@foo.com');
expect(fetcher._password).toEqual('asdf');
expect(fetcher._tokenOptions).not.toEqual(null);
});
});
describe(`test getter functions`, () => {
let fetcher;
beforeEach(() => {
fetcher = new Fetch('sample.address@foo.com', 'pass123');
});
afterEach(() => {
fetcher = null;
});
// test 3
it(`should return username`, () => {
let username = fetcher.username;
expect(username).toEqual('sample.address@foo.com');
});
// test 4
it(`should return password`, () => {
let pass = fetcher.password;
expect(pass).toEqual('pass123');
});
// test 5
it(`should return token options object`, () => {
let token = {
url: 'https://login.windows.net/common/oauth2/token',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
json: true };
expect(fetcher.tokenOptions.url).toEqual(token.url);
expect(fetcher.tokenOptions.headers).toEqual(token.headers);
expect(fetcher.tokenOptions.json).toEqual(token.json);
});
});
describe(`test setter functions`, () => {
let fetcher;
beforeEach(() => {
fetcher = new Fetch('sample.address@foo.com', 'pass123');
});
afterEach(() => {
fetcher = null;
});
// test 6
it(`should set username`, () => {
let user = 'sampleuser';
fetcher.username = user;
expect(fetcher.username).toEqual(user);
});
// test 7
it(`should set password`, () => {
let pass = 'samplepass';
fetcher.password = pass;
expect(fetcher.password).toEqual(pass);
});
});
let fetcher;
let inboxId = "AQMkAGYzNmEwMAA5Mi01ZGZjLTRkZTQtODM2Yi0wMjY5NDc2ODdhYjYALgAAA0ej4msDk8tLiWUK3LkhzWQBAD6DO4zu5CJOvkSSgsE4pUkAAAIBDAAAAA==";
beforeEach(() => {
fetcher = new Fetch('sample.address@foo.com', 'pass123');
});
afterEach(() => {
fetcher = null;
});
describe(`folderList`, () => {
// test 8
it(`should contain all existing folders`, () => {
let folders = fetcher.folderList(sampleData);
expect(folders[0].name).toEqual('Archive');
expect(folders[1].name).toEqual('Conversation History');
expect(folders[2].name).toEqual('Deleted Items');
expect(folders[3].name).toEqual('Drafts');
expect(folders[4].name).toEqual('Inbox');
expect(folders[5].name).toEqual('Junk Email');expect(folders[6].name).toEqual('Outbox');
expect(folders[7].name).toEqual('RSS Subscriptions');
expect(folders[8].name).toEqual('Sent Items');
expect(folders[9].name).toEqual('Sync Issues');
});
// test 9
it(`should not include non-existing folders`, () => {
let folders = fetcher.folderList(sampleData);
expect(folders[0].name).not.toEqual('Invalid Folder');
expect(() => {
expect(folders[10].name);
}).toThrowError();
});
// test 10
it(`should show error when sample data is invalid`, () => {
expect(() => {
folders = fetcher.folderList(null);
}).toThrowError();
});
// test 11
it(`should have a name and id for each folder`, () => {
let folders = fetcher.folderList(sampleData);
expect(folders[0].name).toBeTruthy();
expect(folders[0].id).toBeTruthy();
});
});
describe(`profileInfo`, () => {
// test 12
it(`should contain all existing profile data`, () => {
let profile = fetcher.profileInfo(sampleData);
expect(profile['Display Name']).toEqual('John Smith');
expect(profile['Email Address']).toEqual('sample.address@foo.com');
});
// test 13
it(`should throw error when sample data is invalid`, () => {
expect(() => {
let profile = fetcher.profileInfo(null);
}).toThrowError();
});
});
describe(`totalQuantity`, () => {
// test 14
it(`should return total number of emails across all folders`, () => {
let total = fetcher.totalQuantity(sampleData);
expect(total).toEqual(sampleData.messages.length);
});
});
describe(`allMail`, () => {
// test 15
it(`should return contents of emails across all folders`, () => {
let total = fetcher.allMail(sampleData);
expect(total).toEqual(sampleData.messages);
expect(total.length).toEqual(sampleData.messages.length);
});
});
describe(`folderExists`, () => {
// test 16
it(`should return zero or greater folder index if folder exists`, () => {
let finder = fetcher.folderExists(sampleData, "Inbox");
expect(finder).toBeGreaterThan(-1);
expect(finder).toBeLessThan(sampleData.folders.length-1)
});
// test 17
it(`should throw error if folder does not exist`, () => {
expect(() => {
let finder = fetcher.folderExists(sampleData, "Inbex");
}).toThrowError('Error - Invalid Folder Name');
});
});
describe(`folderQuantity`, () => {
// test 18
it(`should return number of messages in valid folder`, () => {
let quantity = fetcher.folderQuantity(sampleData, "Inbox");
let nexter = 0;
for(let i = 0; i < sampleData.messages.length; i++) {
if(sampleData.messages[i].folderId === inboxId) {
nexter++;
}
}
expect(quantity).toEqual(nexter);
});
// test 19
it(`should throw error if folder does not exist`, () => {
expect(() => {
let quantity = fetcher.folderQuantity(sampleData, "Inbex");
}).toThrowError('Error - Invalid Folder Name');
});
});
describe(`folderMail`, () => {
// test 20
it(`should return contents of messages in valid folder`, () => {
let actual = fetcher.folderMail(sampleData, "Inbox");
let expected = [];
for(let i = 0; i < sampleData.messages.length; i++) {
if(sampleData.messages[i].folderId === inboxId) {
expected.push(sampleData.messages[i]);
}
}
expect(actual).toEqual(expected);
});
});
describe(`toQuantity`, () => {
// test 21
it(`should return number of messages with 'to' field in valid folder`, () => {
let quantity = fetcher.toQuantity(sampleData, "sample2@foo.no", "Inbox");
let nexter = 0;
for(let i = 0; i < sampleData.messages.length; i++) {
if(sampleData.messages[i].folderId === inboxId) {
for(let j = 0; j < sampleData.messages[i].to.length; j++) {
if(sampleData.messages[i].to[j].toUpperCase().trim() === "sample2@foo.no".toUpperCase().trim()){
nexter++
}
}
}
}
expect(quantity).toEqual(nexter);
});
});
describe(`toMail`, () => {
// test 22
it(`should return contents of messages with 'to' field in valid folder`, () => {
let actual = fetcher.toMail(sampleData, "sample3@foo.no", "Inbox");
let expected = [];
for(let i = 0; i < sampleData.messages.length; i++) {
if(sampleData.messages[i].folderId === inboxId) {
for(let j = 0; j < sampleData.messages[i].to.length; j++) {
if(sampleData.messages[i].to[j].toUpperCase().trim() === "sample3@foo.no".toUpperCase().trim()){
expected.push(sampleData.messages[i]);
}
}
}
}
expect(actual).toEqual(expected);
});
});
describe(`fromQuantity`, () => {
// test 23
it(`should return number of messages with 'from' field in valid folder`, () => {
let quantity = fetcher.fromQuantity(sampleData, "sample3@foo.no", "Inbox");
let nexter = 0;
for(let i = 0; i < sampleData.messages.length; i++) {
if(sampleData.messages[i].folderId === inboxId) {
if(sampleData.messages[i].from.toUpperCase().trim() === "sample3@foo.no".toUpperCase().trim()){
nexter++;
}
}
}
expect(quantity).toEqual(nexter);
});
});
describe(`fromMail`, () => {
// test 24
it(`should return contents of messages with 'from' field in valid folder`, () => {
let actual = fetcher.fromMail(sampleData, "sample3@foo.no", "Inbox");
let expected = [];
for(let i = 0; i < sampleData.messages.length; i++) {
if(sampleData.messages[i].folderId === inboxId) {
if(sampleData.messages[i].from.toUpperCase().trim() === "sample3@foo.no".toUpperCase().trim()){
expected.push(sampleData.messages[i]);;
}
}
}
expect(actual).toEqual(expected);
});
});
describe(`subjectQuantity`, () => {
// test 25
it(`should return number of messages with 'subject' field in valid folder`, () => {
let quantity = fetcher.subjectQuantity(sampleData, "Testing", "Inbox");
let nexter = 0;
for(let i = 0; i < sampleData.messages.length; i++) {
if(sampleData.messages[i].folderId === inboxId) {
if(sampleData.messages[i].subject.toUpperCase().trim() === "Testing".toUpperCase().trim()){
nexter++;
}
}
}
expect(quantity).toEqual(nexter);
});
});
describe(`subjectMail`, () => {
// test 26
it(`should return contents of messages with 'subject' field in valid folder`, () => {
let actual = fetcher.subjectMail(sampleData, "Testing", "Inbox");
let expected = [];
for(let i = 0; i < sampleData.messages.length; i++) {
if(sampleData.messages[i].folderId === inboxId) {
if(sampleData.messages[i].from.toUpperCase().trim() === "Testing".toUpperCase().trim()){
expected.push(sampleData.messages[i]);;
}
}
}
expect(actual).toEqual(expected);
});
});
describe(`dateTimeQuantity`, () => {
// test 27
it(`should return number of messages within valid date-time range in valid folder`, () => {
let quantity = fetcher.dateTimeQuantity(sampleData, "2018-07-10", "00:07:35", "2018-07-12", "00:08:00", "Inbox");
expect(quantity).toEqual(2);
});
// test 28
it(`should throw error if entire date-time range is not provided`, () => {
expect(() => {
let quantity = fetcher.dateTimeQuantity(sampleData, "Inbox");
}).toThrowError('Error - Invalid Date-Time Range');
});
});
describe(`dateTimeMail`, () => {
// test 29
it(`should return contents of messages within valid date-time range in valid folder`, () => {
let actual = fetcher.dateTimeMail(sampleData, "2018-07-10", "00:07:35", "2018-07-12", "00:08:00", "Inbox");
expected = [];
expected.push(sampleData.messages[3]);
expected.push(sampleData.messages[4]);
expect(actual).toEqual(expected);
});
// test 30
it(`should throw error if entire date-time range is not provided`, () => {
expect(() => {
let actual = fetcher.dateTimeMail(sampleData, '2018-07-10', '00:06:00', "Inbox");
}).toThrowError('Error - Invalid Date-Time Range');
});
});
describe(`bodyContainsString`, () => {
// test 31
it(`should return contents of messages containing body in valid folder`, () => {
let actual = fetcher.bodyContainsString(sampleData, "fasdf", "Inbox");
let expected = [].concat(sampleData.messages[4], sampleData.messages[8]);
expect(actual).toEqual(expected);
});
});
});