Skip to content

Commit e296e12

Browse files
committed
added check for fail to refresh token in contacts action
1 parent ccefb98 commit e296e12

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lib/triggers/contacts.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,20 @@ function processAction(msg, cfg, snapshot) {
2525
return co(function* mainLoop() {
2626
console.log('Refreshing an OAuth Token');
2727

28-
const instance = new ApiClient(cfg, this);
28+
const instance = new ApiClient(cfg, self);
2929

30-
const newAccessToken = yield instance.getRefreshedToken();
30+
function emitError(message) {
31+
this.emit('error', new Error(message));
32+
}
33+
34+
const refreshTokenError = 'Failed to refresh token';
35+
const newAccessToken
36+
= yield instance.getRefreshedToken()
37+
.catch(emitError.bind(self, refreshTokenError));
38+
39+
if (!newAccessToken) {
40+
return;
41+
}
3142

3243
const client = MicrosoftGraph.init({
3344
defaultVersion: 'v1.0',

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)