Skip to content

Commit 6e8e290

Browse files
committed
test for checkAvailability
1 parent 3ef99bb commit 6e8e290

File tree

4 files changed

+112
-3
lines changed

4 files changed

+112
-3
lines changed

lib/actions/checkAvailability.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
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+
console.log('Refreshing an OAuth Token');
10+
11+
const instance = new ApiClient(cfg, this);
12+
13+
//checking if refresh token was successful
14+
let newAccessToken;
15+
try {
16+
newAccessToken = await instance.getRefreshedToken();
17+
} catch (e) {
18+
throw new Error('Failed to refresh token');
19+
}
20+
21+
if (!newAccessToken) {
22+
return;
23+
}
24+
25+
const client = MicrosoftGraph.init({
26+
defaultVersion: 'v1.0',
27+
debugLogging: true,
28+
authProvider: done => {
29+
done(null, newAccessToken);
30+
}
31+
});
32+
933
const currentTime = msg.body.time || (new Date()).toISOString();
10-
const events = await client.get(`/me/events?$filter=start/dateTime le '${currentTime}'
11-
and end/dateTime ge '${currentTime}'`);
34+
const events = await client
35+
.api('/me/events')
36+
.filter(`start/dateTime le '${currentTime}' and end/dateTime ge '${currentTime}'`)
37+
.get();
1238

1339
return messages.newMessageWithBody({
1440
available: !events.value.length
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'use strict';
2+
3+
describe('Outlook Check Availability', function test() {
4+
5+
const nock = require('nock');
6+
const action = require('../../lib/actions/checkAvailability');
7+
8+
const cfg = require('../data/configuration.in.json');
9+
const jsonIn = require('../data/checkAvailability_test.in.json');
10+
const jsonOut = require('../data/checkAvailability_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+
expect(data.body).toEqual({
45+
available: true
46+
});
47+
expect(scope1.isDone()).toBeTruthy();
48+
expect(scope2.isDone()).toBeTruthy();
49+
}
50+
51+
action.process.call(self, {
52+
body: jsonIn
53+
}, cfg, {})
54+
.then(checkResults)
55+
.then(done)
56+
.catch(done.fail);
57+
});
58+
59+
it('should throw error on unsuccessful refresh token request', done => {
60+
const scope1 = nock(refreshTokenUri).post(refreshTokenApi)
61+
.reply(401, {
62+
access_token: 1
63+
});
64+
65+
action.process.call(self, {
66+
body: jsonIn
67+
}, cfg, {})
68+
.catch(err => {
69+
expect(err).toEqual(jasmine.any(Error));
70+
expect(err.message).toEqual('Failed to refresh token');
71+
expect(scope1.isDone()).toBeTruthy();
72+
done();
73+
});
74+
75+
});
76+
77+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"time": "2017-06-06T12:00:00"
3+
}
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)