Skip to content

Commit f62591d

Browse files
merge
2 parents f086ebe + a536d89 commit f62591d

File tree

6 files changed

+136
-82
lines changed

6 files changed

+136
-82
lines changed

lib/actions/createEvent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function processAction(msg, cfg) {
3535

3636
const apiCall = `/me/calendars/${calendarId}/events`;
3737

38-
const instance = new ApiClient(cfg);
38+
const instance = new ApiClient(cfg, self);
3939

4040
function createEvent(postRequestBody) {
4141
console.log('Creating Event with properties:');

lib/apiClient.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const request = require('request-promise');
22

3-
function ApiClient(config) {
3+
function ApiClient(config, component) {
44

55
const microsoftGraphURI = 'https://graph.microsoft.com/v1.0';
66
const refreshTokenURI = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
@@ -20,11 +20,22 @@ function ApiClient(config) {
2020
}
2121
};
2222

23+
function emitUpdateKeys(responseBody) {
24+
if (component) {
25+
console.log('Updating token');
26+
component.emit('updateKeys', {
27+
oauth: responseBody
28+
});
29+
}
30+
return responseBody;
31+
}
32+
33+
2334
function getAuthorizationHeader(body) {
2435
return body.access_token;
2536
}
2637

27-
return request(options).then(getAuthorizationHeader);
38+
return request(options).then(emitUpdateKeys).then(getAuthorizationHeader);
2839
}
2940

3041
return {
@@ -64,7 +75,9 @@ function ApiClient(config) {
6475
}
6576

6677
return refreshToken().then(setOptions).then(request);
67-
}
78+
},
79+
80+
getRefreshedToken: refreshToken
6881
};
6982
}
7083

lib/triggers/contacts.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
const messages = require('elasticio-node').messages;
44
const co = require('co');
55
const MicrosoftGraph = require('msgraph-sdk-javascript');
6-
const rp = require('request-promise');
76
const _ = require('lodash');
7+
const ApiClient = require('../apiClient');
88

99
/**
1010
* This method will be called from elastic.io platform providing following data
@@ -19,31 +19,27 @@ function processAction(msg, cfg, snapshot) {
1919
// Should be in ISO-Date format
2020
snapshot.lastModifiedDateTime = snapshot.lastModifiedDateTime || new Date(0).toISOString();
2121

22+
const self = this;
2223
// Main loop
2324
return co(function* mainLoop() {
2425
console.log('Refreshing an OAuth Token');
25-
const newToken = yield rp({
26-
method: 'POST',
27-
uri: 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
28-
json: true,
29-
form: {
30-
refresh_token: cfg.oauth.refresh_token,
31-
scope: cfg.oauth.scope,
32-
grant_type: 'refresh_token',
33-
client_id: process.env.MSAPP_CLIENT_ID,
34-
client_secret: process.env.MSAPP_CLIENT_SECRET
35-
}
36-
});
37-
console.log('Updating token');
38-
this.emit('updateKeys', {
39-
oauth: newToken
40-
});
26+
27+
const instance = new ApiClient(cfg, self);
28+
29+
//checking if refresh token was successful
30+
const newAccessToken
31+
= yield instance.getRefreshedToken()
32+
.catch(() => this.emit('error', new Error('Failed to refresh token')));
33+
34+
if (!newAccessToken) {
35+
return;
36+
}
4137

4238
const client = MicrosoftGraph.init({
4339
defaultVersion: 'v1.0',
4440
debugLogging: true,
4541
authProvider: (done) => {
46-
done(null, newToken.access_token);
42+
done(null, newAccessToken);
4743
}
4844
});
4945

package.json

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
{
2-
"name": "outlook-component",
3-
"version": "0.0.3",
4-
"description": "elastic.io integration component for Office 365 Outlook REST API",
5-
"homepage": "http://www.outlook.com/",
6-
"author": {
7-
"name": "elastic.io GmbH",
8-
"email": "[email protected]",
9-
"url": "http://elastic.io"
10-
},
11-
"files": [
12-
"lib"
13-
],
14-
"scripts": {
15-
"pretest": "eslint lib spec --fix",
16-
"test": "jasmine"
17-
},
18-
"main": "lib/index.js",
19-
"keywords": [
20-
"office365",
21-
"outlook",
22-
"rest",
23-
"elasticio",
24-
"elasticio-component"
25-
],
26-
"dependencies": {
27-
"bluebird": "^3.4.6",
28-
"co": "^4.6.0",
29-
"elasticio-node": "0.0.8",
30-
"elasticio-sailor-nodejs": "1.3.0",
31-
"lodash": "^4.17.2",
32-
"moment": "^2.1.16",
33-
"moment-timezone": "^0.5.9",
34-
"msgraph-sdk-javascript": "^0.1.2",
35-
"q": "^1.4.1",
36-
"request": "^2.75.0",
37-
"request-promise": "^4.1.1"
38-
},
39-
"devDependencies": {
40-
"elasticio-cli": "1.0.0",
41-
"eslint": "^3.1.2",
42-
"jasmine": "^2.5.2",
43-
"nock": "9.0.2"
44-
},
45-
"repository": "elasticio/outlook",
46-
"license": "Apache-2.0"
47-
}
2+
"name": "outlook-component",
3+
"version": "0.0.3",
4+
"description": "elastic.io integration component for Office 365 Outlook REST API",
5+
"homepage": "http://www.outlook.com/",
6+
"author": {
7+
"name": "elastic.io GmbH",
8+
"email": "[email protected]",
9+
"url": "http://elastic.io"
10+
},
11+
"files": [
12+
"lib"
13+
],
14+
"scripts": {
15+
"pretest": "eslint lib spec --fix",
16+
"test": "jasmine"
17+
},
18+
"main": "lib/index.js",
19+
"keywords": [
20+
"office365",
21+
"outlook",
22+
"rest",
23+
"elasticio",
24+
"elasticio-component"
25+
],
26+
"dependencies": {
27+
"bluebird": "^3.4.6",
28+
"co": "^4.6.0",
29+
"elasticio-node": "0.0.8",
30+
"elasticio-sailor-nodejs": "2.1.3",
31+
"lodash": "^4.17.2",
32+
"moment": "^2.1.16",
33+
"moment-timezone": "^0.5.9",
34+
"msgraph-sdk-javascript": "^0.1.2",
35+
"q": "^1.4.1",
36+
"request": "^2.75.0",
37+
"request-promise": "^4.1.1"
38+
},
39+
"devDependencies": {
40+
"elasticio-cli": "1.0.0",
41+
"eslint": "^3.1.2",
42+
"jasmine": "^2.5.2",
43+
"nock": "9.0.2"
44+
},
45+
"repository": "elasticio/outlook",
46+
"license": "Apache-2.0"
47+
}

spec/actions/createEvent.spec.js

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ describe('Outlook Create Event', function test() {
3131

3232
function checkResults() {
3333
let calls = self.emit.calls;
34-
expect(calls.count()).toEqual(2);
35-
expect(calls.argsFor(0)[0]).toEqual('data');
36-
expect(calls.argsFor(1)[0]).toEqual('end');
37-
expect(calls.argsFor(0)[1].body).toEqual({
34+
expect(calls.count()).toEqual(3);
35+
expect(calls.argsFor(0)[0]).toEqual('updateKeys');
36+
expect(calls.argsFor(0)[1]).toEqual({
37+
oauth: {
38+
access_token: 1
39+
}
40+
});
41+
expect(calls.argsFor(1)[0]).toEqual('data');
42+
expect(calls.argsFor(2)[0]).toEqual('end');
43+
expect(calls.argsFor(1)[1].body).toEqual({
3844
id: 'testid12345',
3945
subject: 'Unit Test - Simple Event',
4046
body: {
@@ -74,10 +80,16 @@ describe('Outlook Create Event', function test() {
7480

7581
function checkResults() {
7682
let calls = self.emit.calls;
77-
expect(calls.count()).toEqual(2);
78-
expect(calls.argsFor(0)[0]).toEqual('data');
79-
expect(calls.argsFor(1)[0]).toEqual('end');
80-
expect(calls.argsFor(0)[1].body).toEqual({
83+
expect(calls.count()).toEqual(3);
84+
expect(calls.argsFor(0)[0]).toEqual('updateKeys');
85+
expect(calls.argsFor(0)[1]).toEqual({
86+
oauth: {
87+
access_token: 1
88+
}
89+
});
90+
expect(calls.argsFor(1)[0]).toEqual('data');
91+
expect(calls.argsFor(2)[0]).toEqual('end');
92+
expect(calls.argsFor(1)[1].body).toEqual({
8193
id: 'testid12345',
8294
subject: 'Unit Test - Simple Event',
8395
body: {
@@ -140,9 +152,15 @@ describe('Outlook Create Event', function test() {
140152

141153
function checkResults() {
142154
let calls = self.emit.calls;
143-
expect(calls.count()).toEqual(2);
144-
expect(calls.argsFor(0)[0]).toEqual('error');
145-
expect(calls.argsFor(1)[0]).toEqual('end');
155+
expect(calls.count()).toEqual(3);
156+
expect(calls.argsFor(0)[0]).toEqual('updateKeys');
157+
expect(calls.argsFor(0)[1]).toEqual({
158+
oauth: {
159+
access_token: 1
160+
}
161+
});
162+
expect(calls.argsFor(1)[0]).toEqual('error');
163+
expect(calls.argsFor(2)[0]).toEqual('end');
146164
expect(scope1.isDone()).toBeTruthy();
147165
expect(scope2.isDone()).toBeTruthy();
148166
}
@@ -166,9 +184,15 @@ describe('Outlook Create Event', function test() {
166184

167185
function checkResults() {
168186
let calls = self.emit.calls;
169-
expect(calls.count()).toEqual(2);
170-
expect(calls.argsFor(0)[0]).toEqual('error');
171-
expect(calls.argsFor(1)[0]).toEqual('end');
187+
expect(calls.count()).toEqual(3);
188+
expect(calls.argsFor(0)[0]).toEqual('updateKeys');
189+
expect(calls.argsFor(0)[1]).toEqual({
190+
oauth: {
191+
access_token: 1
192+
}
193+
});
194+
expect(calls.argsFor(1)[0]).toEqual('error');
195+
expect(calls.argsFor(2)[0]).toEqual('end');
172196
expect(scope1.isDone()).toBeTruthy();
173197
expect(scope2.isDone()).toBeTruthy();
174198
}

spec/triggers/getContacts.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,25 @@ describe('Outlook Contacts', function test() {
6666
.catch(done.fail);
6767
});
6868

69+
it('should emit error on unsuccessful refresh token request', done => {
70+
const scope1 = nock(refreshTokenUri).post(refreshTokenApi)
71+
.reply(401, {
72+
access_token: 1
73+
});
74+
75+
function checkResults() {
76+
let calls = self.emit.calls;
77+
expect(calls.count()).toEqual(1);
78+
expect(calls.argsFor(0)[0]).toEqual('error');
79+
expect(calls.argsFor(0)[1]).toEqual(jasmine.any(Error));
80+
expect(calls.argsFor(0)[1].message).toEqual('Failed to refresh token');
81+
expect(scope1.isDone()).toBeTruthy();
82+
}
83+
84+
trigger.process.call(self, {}, cfg, {})
85+
.then(checkResults)
86+
.then(done)
87+
.catch(done.fail);
88+
});
89+
6990
});

0 commit comments

Comments
 (0)