Skip to content

Commit 7c8ba67

Browse files
committed
test for findNextAvailableTime.js
1 parent 6e8e290 commit 7c8ba67

File tree

5 files changed

+121
-3
lines changed

5 files changed

+121
-3
lines changed

lib/actions/checkAvailability.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ async function processAction(msg, cfg) {
3131
});
3232

3333
const currentTime = msg.body.time || (new Date()).toISOString();
34+
3435
const events = await client
3536
.api('/me/events')
3637
.filter(`start/dateTime le '${currentTime}' and end/dateTime ge '${currentTime}'`)

lib/actions/findNextAvailableTime.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
11
const elasticio = require('elasticio-node');
22
const messages = elasticio.messages;
3+
const MicrosoftGraph = require('msgraph-sdk-javascript');
34
const ApiClient = require('../apiClient');
45

56
module.exports.process = processAction;
67

78
async function processAction(msg, cfg) {
8-
const client = ApiClient(cfg);
9+
10+
console.log('Refreshing an OAuth Token');
11+
12+
const instance = new ApiClient(cfg, this);
13+
14+
//checking if refresh token was successful
15+
let newAccessToken;
16+
try {
17+
newAccessToken = await instance.getRefreshedToken();
18+
} catch (e) {
19+
throw new Error('Failed to refresh token');
20+
}
21+
22+
if (!newAccessToken) {
23+
return;
24+
}
25+
26+
const client = MicrosoftGraph.init({
27+
defaultVersion: 'v1.0',
28+
debugLogging: true,
29+
authProvider: done => {
30+
done(null, newAccessToken);
31+
}
32+
});
33+
934
let time = msg.body.time || (new Date()).toISOString();
10-
const events = await client.get(`/me/events?$filter=start/dateTime le '${time}'
11-
and end/dateTime ge '${time}'`);
35+
36+
const events = await client
37+
.api('/me/events')
38+
.filter(`start/dateTime le '${time}' and end/dateTime ge '${time}'`)
39+
.get();
40+
1241
if (events.value.length) {
1342
time = events.value.pop().end.dateTime;
1443
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'use strict';
2+
3+
describe('Outlook Find next available time', function test() {
4+
5+
const nock = require('nock');
6+
const action = require('../../lib/actions/findNextAvailableTime');
7+
8+
const cfg = require('../data/configuration.in.json');
9+
const jsonIn = require('../data/findNextAvailableTime_test.in.json');
10+
const jsonOut = require('../data/findNextAvailableTime_test.out.json');
11+
12+
const refreshTokenUri = 'https://login.microsoftonline.com';
13+
const refreshTokenApi = '/common/oauth2/v2.0/token';
14+
const microsoftGraphUri = 'https://graph.microsoft.com/v1.0';
15+
const microsoftGraphApi = '/me/events';
16+
17+
18+
var self;
19+
beforeEach(function createSpy() {
20+
self = jasmine.createSpyObj('self', ['emit']);
21+
});
22+
23+
it('should return available=true on success request - case: http 200', done => {
24+
const scope1 = nock(refreshTokenUri).post(refreshTokenApi)
25+
.reply(200, {
26+
access_token: 1
27+
});
28+
29+
const scope2 = nock(microsoftGraphUri).get(microsoftGraphApi)
30+
.query({
31+
$filter: `start/dateTime le '${jsonIn.time}' and end/dateTime ge '${jsonIn.time}'`
32+
})
33+
.reply(200, jsonOut);
34+
35+
function checkResults(data) {
36+
const calls = self.emit.calls;
37+
expect(calls.count()).toEqual(1);
38+
expect(calls.argsFor(0)[0]).toEqual('updateKeys');
39+
expect(calls.argsFor(0)[1]).toEqual({
40+
oauth: {
41+
access_token: 1
42+
}
43+
});
44+
45+
const { time, subject } = jsonIn;
46+
47+
expect(data.body).toEqual({
48+
time,
49+
subject
50+
});
51+
expect(scope1.isDone()).toBeTruthy();
52+
expect(scope2.isDone()).toBeTruthy();
53+
}
54+
55+
action.process.call(self, {
56+
body: jsonIn
57+
}, cfg, {})
58+
.then(checkResults)
59+
.then(done)
60+
.catch(done.fail);
61+
});
62+
63+
it('should throw error on unsuccessful refresh token request', done => {
64+
const scope1 = nock(refreshTokenUri).post(refreshTokenApi)
65+
.reply(401, {
66+
access_token: 1
67+
});
68+
69+
action.process.call(self, {
70+
body: jsonIn
71+
}, cfg, {})
72+
.catch(err => {
73+
expect(err).toEqual(jasmine.any(Error));
74+
expect(err.message).toEqual('Failed to refresh token');
75+
expect(scope1.isDone()).toBeTruthy();
76+
done();
77+
});
78+
79+
});
80+
81+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"time": "2017-06-06T12:00:00",
3+
"subject": "Some important meeting"
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"value": []
3+
}

0 commit comments

Comments
 (0)