|
| 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 | +}); |
0 commit comments