Skip to content

Commit 5e908f9

Browse files
authored
Merge pull request #10 from elasticio/PLT-1366
updateKeys in all actions and triggers
2 parents 4688b73 + 79088eb commit 5e908f9

File tree

5 files changed

+89
-35
lines changed

5 files changed

+89
-35
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,7 +1,7 @@
11
'use strict';
22
const request = require('request-promise');
33

4-
function ApiClient(config) {
4+
function ApiClient(config, component) {
55

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

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

28-
return request(options).then(getAuthorizationHeader);
39+
return request(options).then(emitUpdateKeys).then(getAuthorizationHeader);
2940
}
3041

3142
return {
@@ -65,7 +76,9 @@ function ApiClient(config) {
6576
}
6677

6778
return refreshToken().then(setOptions).then(request);
68-
}
79+
},
80+
81+
getRefreshedToken: refreshToken
6982
};
7083
}
7184

lib/triggers/contacts.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const co = require('co');
55
const MicrosoftGraph = require('msgraph-sdk-javascript');
66
const rp = require('request-promise');
77
const _ = require('lodash');
8+
const ApiClient = require('../apiClient');
89

910
/**
1011
* This method will be called from elastic.io platform providing following data
@@ -19,31 +20,26 @@ function processAction(msg, cfg, snapshot) {
1920
// Should be in ISO-Date format
2021
snapshot.lastModifiedDateTime = snapshot.lastModifiedDateTime || new Date(0).toISOString();
2122

23+
const self = this;
2224
// Main loop
2325
return co(function* mainLoop() {
2426
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-
});
27+
28+
const instance = new ApiClient(cfg, self);
29+
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

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)