|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/* eslint-env mocha */ |
| 19 | + |
| 20 | +'use strict' |
| 21 | + |
| 22 | +const assert = require('assert') |
| 23 | +const mock = require('mock-fs') |
| 24 | +const path = require('path') |
| 25 | +const os = require('os') |
| 26 | + |
| 27 | +const ibmcloudUtils = require('../ibmcloud-utils') |
| 28 | +const client = require('../client') |
| 29 | + |
| 30 | +describe('ibmcloud-utils', function () { |
| 31 | + describe('ibmcloud-utils.token-expiration', function () { |
| 32 | + const ibmCloudFunctionsPropsPath = |
| 33 | + process.env.IC_FN_CONFIG_FILE || path.join(os.homedir(), '.bluemix/plugins/cloud-functions/config.json') |
| 34 | + const ibmCloudPropsPath = process.env.IC_CONFIG_FILE || path.join(os.homedir(), '.bluemix/config.json') |
| 35 | + |
| 36 | + it('read timestamp', function () { |
| 37 | + mock({ |
| 38 | + [ibmCloudFunctionsPropsPath]: '{ "IamTimeTokenRefreshed": "2021-03-15T13:24:14+01:00" }' |
| 39 | + }) |
| 40 | + const timestamp = ibmcloudUtils.getIamTokenTimestamp() |
| 41 | + assert.strictEqual(timestamp.getTime(), Date.UTC(2021, 2, 15, 12, 24, 14)) |
| 42 | + }) |
| 43 | + |
| 44 | + it('token not expired', function () { |
| 45 | + const timeRefreshed = new Date(2021, 2, 13, 13, 24, 14) |
| 46 | + const timeReference = new Date(2021, 2, 13, 13, 30, 0) |
| 47 | + const tokenExpired = ibmcloudUtils.iamTokenExpired(timeRefreshed, timeReference) |
| 48 | + assert.strictEqual(tokenExpired, false) |
| 49 | + }) |
| 50 | + |
| 51 | + it('token expired', function () { |
| 52 | + const timeRefreshed = new Date(2021, 2, 13, 13, 24, 14) |
| 53 | + const timeReference = new Date(2021, 2, 13, 14, 25, 0) |
| 54 | + const tokenExpired = ibmcloudUtils.iamTokenExpired(timeRefreshed, timeReference) |
| 55 | + assert.strictEqual(tokenExpired, true) |
| 56 | + }) |
| 57 | + |
| 58 | + it('client fails when token expired', function () { |
| 59 | + mock({ |
| 60 | + [ibmCloudFunctionsPropsPath]: '{ "IamTimeTokenRefreshed": "2021-03-14T12:00:00+01:00", ' + |
| 61 | + '"WskCliNamespaceId": "some-namespace-id", ' + |
| 62 | + '"WskCliNamespaceMode": "IAM" }', |
| 63 | + [ibmCloudPropsPath]: '{ "IAMToken": "some-token" }' |
| 64 | + }) |
| 65 | + |
| 66 | + assert.throws(() => client(), /IAM token expired/) |
| 67 | + }) |
| 68 | + }) |
| 69 | +}) |
0 commit comments