|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const expect = require('chai').expect; |
| 4 | +const sinon = require('sinon'); |
| 5 | +const fs = require('fs-extra'); |
| 6 | +const chaiAsPromised = require('chai-as-promised'); |
| 7 | +const CliTokenManager = require('../cliTokenManager.js'); |
| 8 | + |
| 9 | +require('chai').use(chaiAsPromised); |
| 10 | + |
| 11 | +describe('CliTokenManager', () => { |
| 12 | + describe('#getAuthHeader()', () => { |
| 13 | + it('should return bearer token from configuration', () => { |
| 14 | + const cliTokenManager = new CliTokenManager() |
| 15 | + const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiJiMDhmODZhZi0zNWRhLTQ4ZjItOGZhYi1jZWYzOTA0NjYwYmQifQ.-xN_h82PHVTCMA9vdoHrcZxH-x5mb11y1537t3rGzcM' |
| 16 | + cliTokenManager.readTokenFromConfig = () => token |
| 17 | + cliTokenManager.isTokenExpired = () => false |
| 18 | + const header = `Bearer ${token}` |
| 19 | + return cliTokenManager.getAuthHeader().then(result => { |
| 20 | + expect(result).to.equal(header); |
| 21 | + }) |
| 22 | + }); |
| 23 | + |
| 24 | + it('should return refreshed bearer token when token is expired', () => { |
| 25 | + const cliTokenManager = new CliTokenManager() |
| 26 | + const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiJiMDhmODZhZi0zNWRhLTQ4ZjItOGZhYi1jZWYzOTA0NjYwYmQifQ.-xN_h82PHVTCMA9vdoHrcZxH-x5mb11y1537t3rGzcM' |
| 27 | + cliTokenManager.readTokenFromConfig = () => null |
| 28 | + cliTokenManager.isTokenExpired = () => true |
| 29 | + cliTokenManager.refreshToken = () => Promise.resolve(token) |
| 30 | + const header = `Bearer ${token}` |
| 31 | + return cliTokenManager.getAuthHeader().then(result => { |
| 32 | + expect(result).to.equal(header); |
| 33 | + }) |
| 34 | + }); |
| 35 | + }) |
| 36 | + |
| 37 | + describe('#readTokenFromConfig()', () => { |
| 38 | + it('should return bearer token from default configuration file', () => { |
| 39 | + const readFile = (path, format) => { |
| 40 | + expect(path).to.equal(config_path) |
| 41 | + expect(format).to.equal('utf-8') |
| 42 | + return JSON.stringify({ IAMToken: `Bearer ${config_token}`}) |
| 43 | + } |
| 44 | + |
| 45 | + const cliTokenManager = new CliTokenManager(null, readFile) |
| 46 | + const config_token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiJiMDhmODZhZi0zNWRhLTQ4ZjItOGZhYi1jZWYzOTA0NjYwYmQifQ.-xN_h82PHVTCMA9vdoHrcZxH-x5mb11y1537t3rGzcM' |
| 47 | + const config_path = `~/.bluemix/config.json` |
| 48 | + const token = cliTokenManager.readTokenFromConfig(config_path) |
| 49 | + expect(token).to.equal(config_token) |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('#isTokenExpired()', () => { |
| 54 | + it('should return true for expired JWT tokens', () => { |
| 55 | + const cliTokenManager = new CliTokenManager() |
| 56 | + // created from http://jwtbuilder.jamiekurtz.com/ |
| 57 | + // JWT expired in 2000. |
| 58 | + const expired_token = 'eyJraWQiOiIyMDE5MDIwNCIsImFsZyI6IlJTMjU2In0.eyJpYW1faWQiOiJJQk1pZC0yNzAwMDJQUzIxIiwiaWQiOiJJQk1pZC0yNzAwMDJQUzIxIiwicmVhbG1pZCI6IklCTWlkIiwiaWRlbnRpZmllciI6IjI3MDAwMlBTMjEiLCJnaXZlbl9uYW1lIjoiSmFtZXMiLCJmYW1pbHlfbmFtZSI6IlRob21hcyIsIm5hbWUiOiJKYW1lcyBUaG9tYXMiLCJlbWFpbCI6ImphbWVzLnRob21hc0B1ay5pYm0uY29tIiwic3ViIjoiamFtZXMudGhvbWFzQHVrLmlibS5jb20iLCJhY2NvdW50Ijp7InZhbGlkIjp0cnVlLCJic3MiOiI4ZDYzZmIxY2M1ZTk5ZTg2ZGQ3MjI5ZGRkZmExNjY0OSJ9LCJpYXQiOjE1NjM0NDAyMzEsImV4cCI6MTU2MzQ0MzgzMSwiaXNzIjoiaHR0cHM6Ly9pYW0uY2xvdWQuaWJtLmNvbS9pZGVudGl0eSIsImdyYW50X3R5cGUiOiJwYXNzd29yZCIsInNjb3BlIjoiaWJtIG9wZW5pZCIsImNsaWVudF9pZCI6ImJ4IiwiYWNyIjoxLCJhbXIiOlsicHdkIl19.DhgBTV_dxtSirpSoe-H_xXfxBKYIrxFqiu4eVluTq78Sqp9FCCQoMSuJBD0ysHsD-0sIp5yHq03-0DnAdldnD2YkFRwrDXY-9uG5cJGB1vH3l6X6BaWprGG-AcswqeTklnjCrRqIiUr5EU9odZAfwbDPYdoE21gudS2kMZoVgezJsUtYz2tJH-I-1JfbBPuTLLuhWVr4ZPP2GzOvI7xpWBVwMYmUviLrxD_-Gq2vJyly1rNBYA4VZKf1G46yT790EqRz9N3o18bmKUxDCP6ur2oVHwGNQy15fn8LsiylHf4s9p9yPuLtgExN6FcdMfPU8hUT1UWfaWssjpetk3crjA' |
| 59 | + const expired = cliTokenManager.isTokenExpired(expired_token) |
| 60 | + expect(expired).to.equal(true) |
| 61 | + }); |
| 62 | + |
| 63 | + it('should return false for non-expired JWT tokens', () => { |
| 64 | + const cliTokenManager = new CliTokenManager() |
| 65 | + // created from http://jwtbuilder.jamiekurtz.com/ - example JWT expires in 2100. |
| 66 | + // I won't be around when this unit test starts failing... |
| 67 | + const expired_token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE1NjM0NTM2OTYsImV4cCI6NDExOTUxMTI5NiwiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIkdpdmVuTmFtZSI6IkpvaG5ueSIsIlN1cm5hbWUiOiJSb2NrZXQiLCJFbWFpbCI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJSb2xlIjpbIk1hbmFnZXIiLCJQcm9qZWN0IEFkbWluaXN0cmF0b3IiXX0.WNqaMqKIqkKXT731uGV8jnJmNj74qYUSiZeLLYl6ME0' |
| 68 | + const expired = cliTokenManager.isTokenExpired(expired_token) |
| 69 | + expect(expired).to.equal(false) |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('#configFilePath()', () => { |
| 74 | + it('should return default config location', () => { |
| 75 | + const default_path = `${process.env['HOME']}/.bluemix/config.json` |
| 76 | + expect(CliTokenManager.configFilePath()).to.equal(default_path) |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('#refreshToken()', () => { |
| 81 | + it('should return current token once command has executed', () => { |
| 82 | + const cliTokenManager = new CliTokenManager() |
| 83 | + const token = 'eyj0exaioijkv1qilcjhbgcioijiuzi1nij9.eyj1c2vyswqioijimdhmodzhzi0znwrhltq4zjitogzhyi1jzwyzota0njywymqifq.-xn_h82phvtcma9vdohrczxh-x5mb11y1537t3rgzcm' |
| 84 | + cliTokenManager.readTokenFromConfig = () => token |
| 85 | + cliTokenManager.exec = (cmd, cb) => { |
| 86 | + expect(cmd).to.equal(cliTokenManager.refresh_command) |
| 87 | + setTimeout(() => cb(), 0) |
| 88 | + } |
| 89 | + |
| 90 | + return cliTokenManager.refreshToken().then(_token => { |
| 91 | + expect(_token).to.equal(token) |
| 92 | + }) |
| 93 | + }); |
| 94 | + |
| 95 | + it('should throw error when refresh token command fails', () => { |
| 96 | + const cliTokenManager = new CliTokenManager() |
| 97 | + cliTokenManager.exec = (_, cb) => { |
| 98 | + setTimeout(() => cb(new Error("cmd failed")), 0) |
| 99 | + } |
| 100 | + |
| 101 | + return expect(cliTokenManager.refreshToken()).to.eventually.be.rejectedWith(/^IAM token from IBM Cloud CLI/); |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments