From dcce91f5dfbae633233e681ce14f805d7a1062de Mon Sep 17 00:00:00 2001 From: hartyt Date: Tue, 29 Jun 2021 15:53:29 +0100 Subject: [PATCH] feat(cloudshell): add service, tests and examples to project --- .gitignore | 1 + README.md | 1 + examples/ibm-cloud-shell.v1.test.js | 155 +++++++++ ibm-cloud-shell/v1.ts | 342 ++++++++++++++++++++ scripts/typedoc/generate_typedoc.sh | 1 + test/integration/ibm-cloud-shell.v1.test.js | 108 +++++++ test/unit/ibm-cloud-shell.v1.test.js | 285 ++++++++++++++++ 7 files changed, 893 insertions(+) create mode 100644 examples/ibm-cloud-shell.v1.test.js create mode 100644 ibm-cloud-shell/v1.ts create mode 100644 test/integration/ibm-cloud-shell.v1.test.js create mode 100644 test/unit/ibm-cloud-shell.v1.test.js diff --git a/.gitignore b/.gitignore index 70f77166..3d318c92 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ global-tagging/*.js iam-access-groups/*.js iam-identity/*.js iam-policy-management/*.js +ibm-cloud-shell/*.js open-service-broker/*.js resource-manager/*.js resource-controller/*.js diff --git a/README.md b/README.md index cd692db6..f010d04b 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ Service Name | Import Path [IAM Access Groups](https://cloud.ibm.com/apidocs/iam-access-groups) | ibm-platform-services/iam-access-groups/v2 [IAM Identity Service](https://cloud.ibm.com/apidocs/iam-identity-token-api) | ibm-platform-services/iam-identity/v1 [IAM Policy Management](https://cloud.ibm.com/apidocs/iam-policy-management) | ibm-platform-services/iam-policy-management/v1 +[IBM Cloud Shell](https://cloud.ibm.com/apidocs/cloudshell) | ibm-platform-services/ibm-cloud-shell/v1 [Open Service Broker](https://cloud.ibm.com/apidocs/resource-controller/ibm-cloud-osb-api) | ibm-platform-services/open-service-broker/v1 [Posture Management](https://cloud.ibm.com/apidocs/security-compliance/posture) | ibm-platform-services/posture-management/v1 [Resource Controller](https://cloud.ibm.com/apidocs/resource-controller/resource-controller) | ibm-platform-services/resource-controller/v2 diff --git a/examples/ibm-cloud-shell.v1.test.js b/examples/ibm-cloud-shell.v1.test.js new file mode 100644 index 00000000..fa1e56ce --- /dev/null +++ b/examples/ibm-cloud-shell.v1.test.js @@ -0,0 +1,155 @@ +/** +* @jest-environment node +*/ +/** + * (C) Copyright IBM Corp. 2021. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +'use strict'; + +/* eslint-disable no-console */ + +const IbmCloudShellV1 = require('../dist/ibm-cloud-shell/v1'); +// eslint-disable-next-line node/no-unpublished-require +const authHelper = require('../test/resources/auth-helper.js'); +// You can use the readExternalSources method to access additional configuration values +// const { readExternalSources } = require('ibm-cloud-sdk-core'); + +// +// This file provides an example of how to use the IBM Cloud Shell service. +// +// The following configuration properties are assumed to be defined: +// IBM_CLOUD_SHELL_URL= +// IBM_CLOUD_SHELL_AUTH_TYPE=iam +// IBM_CLOUD_SHELL_APIKEY= +// IBM_CLOUD_SHELL_AUTH_URL= +// IBM_CLOUD_SHELL_ACCOUNT_ID= +// +// These configuration properties can be exported as environment variables, or stored +// in a configuration file and then: +// export IBM_CREDENTIALS_FILE= +// +const configFile = 'ibm_cloud_shell_v1.env'; + +const describe = authHelper.prepareTests(configFile); + +// Save original console.log +const originalLog = console.log; + +// Mocks for console.log and console.warn +const consoleLogMock = jest.spyOn(console, 'log'); +const consoleWarnMock = jest.spyOn(console, 'warn'); + +describe('IbmCloudShellV1', () => { + + // begin-common + + const ibmCloudShellService = IbmCloudShellV1.newInstance({}); + + // end-common + + // To access additional configuration values, uncomment this line and extract the values from config + const config = readExternalSources(IbmCloudShellV1.DEFAULT_SERVICE_NAME); + let accountId = config.accountId; + expect(accountId).not.toBeNull(); + + test('getAccountSettings request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + originalLog('getAccountSettings() result:'); + // begin-get_account_settings + + const params = { + accountId: accountId, + }; + + ibmCloudShellService.getAccountSettings(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-get_account_settings + }); + test('updateAccountSettings request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + originalLog('updateAccountSettings() result:'); + // begin-update_account_settings + + // Feature + const featureModel = [ + { + enabled: true, + key: 'server.file_manager', + }, + { + enabled: true, + key: 'server.web_preview', + }, + ]; + + // RegionSetting + const regionSettingModel = [ + { + enabled: true, + key: 'eu-de', + }, + { + enabled: true, + key: 'jp-tok', + }, + { + enabled: true, + key: 'us-south', + }, + ]; + + const params = { + accountId: accountId, + rev: '130-${accountId}', + defaultEnableNewFeatures: true, + defaultEnableNewRegions: true, + enabled: true, + features: featureModel, + regions: regionSettingModel, + }; + + ibmCloudShellService.updateAccountSettings(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-update_account_settings + }); +}); diff --git a/ibm-cloud-shell/v1.ts b/ibm-cloud-shell/v1.ts new file mode 100644 index 00000000..17683320 --- /dev/null +++ b/ibm-cloud-shell/v1.ts @@ -0,0 +1,342 @@ +/** + * (C) Copyright IBM Corp. 2021. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * IBM OpenAPI SDK Code Generator Version: 3.33.0-caf29bd0-20210603-225214 + */ + +import * as extend from 'extend'; +import { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http'; +import { + Authenticator, + BaseService, + getAuthenticatorFromEnvironment, + getMissingParams, + UserOptions, +} from 'ibm-cloud-sdk-core'; +import { getSdkHeaders } from '../lib/common'; + +/** + * API docs for IBM Cloud Shell repository + */ + +class IbmCloudShellV1 extends BaseService { + static DEFAULT_SERVICE_URL: string = 'https://api.shell.cloud.ibm.com'; + + static DEFAULT_SERVICE_NAME: string = 'ibm_cloud_shell'; + + /************************* + * Factory method + ************************/ + + /** + * Constructs an instance of IbmCloudShellV1 with passed in options and external configuration. + * + * @param {UserOptions} [options] - The parameters to send to the service. + * @param {string} [options.serviceName] - The name of the service to configure + * @param {Authenticator} [options.authenticator] - The Authenticator object used to authenticate requests to the service + * @param {string} [options.serviceUrl] - The URL for the service + * @returns {IbmCloudShellV1} + */ + + public static newInstance(options: UserOptions): IbmCloudShellV1 { + options = options || {}; + + if (!options.serviceName) { + options.serviceName = this.DEFAULT_SERVICE_NAME; + } + if (!options.authenticator) { + options.authenticator = getAuthenticatorFromEnvironment(options.serviceName); + } + const service = new IbmCloudShellV1(options); + service.configureService(options.serviceName); + if (options.serviceUrl) { + service.setServiceUrl(options.serviceUrl); + } + return service; + } + + /** + * Construct a IbmCloudShellV1 object. + * + * @param {Object} options - Options for the service. + * @param {string} [options.serviceUrl] - The base url to use when contacting the service. The base url may differ between IBM Cloud regions. + * @param {OutgoingHttpHeaders} [options.headers] - Default headers that shall be included with every request to the service. + * @param {Authenticator} options.authenticator - The Authenticator object used to authenticate requests to the service + * @constructor + * @returns {IbmCloudShellV1} + */ + constructor(options: UserOptions) { + options = options || {}; + + super(options); + if (options.serviceUrl) { + this.setServiceUrl(options.serviceUrl); + } else { + this.setServiceUrl(IbmCloudShellV1.DEFAULT_SERVICE_URL); + } + } + + /************************* + * accountSettings + ************************/ + + /** + * Get account settings. + * + * Retrieve account settings for the given account ID. Call this method to get details about a particular account + * setting, whether Cloud Shell is enabled, the list of enabled regions and the list of enabled features. Users need + * to be an account owner or users need to be assigned an IAM policy with the Administrator role for the Cloud Shell + * account management service. + * + * @param {Object} params - The parameters to send to the service. + * @param {string} params.accountId - The account ID in which the account settings belong to. + * @param {OutgoingHttpHeaders} [params.headers] - Custom request headers + * @returns {Promise>} + */ + public getAccountSettings( + params: IbmCloudShellV1.GetAccountSettingsParams + ): Promise> { + const _params = { ...params }; + const requiredParams = ['accountId']; + + const missingParams = getMissingParams(_params, requiredParams); + if (missingParams) { + return Promise.reject(missingParams); + } + + const path = { + 'account_id': _params.accountId, + }; + + const sdkHeaders = getSdkHeaders( + IbmCloudShellV1.DEFAULT_SERVICE_NAME, + 'v1', + 'getAccountSettings' + ); + + const parameters = { + options: { + url: '/api/v1/user/accounts/{account_id}/settings', + method: 'GET', + path, + }, + defaultOptions: extend(true, {}, this.baseOptions, { + headers: extend( + true, + sdkHeaders, + { + 'Accept': 'application/json', + }, + _params.headers + ), + }), + }; + + return this.createRequest(parameters); + } + + /** + * Update account settings. + * + * Update account settings for the given account ID. Call this method to update account settings configuration, you + * can enable or disable Cloud Shell, enable or disable available regions and enable and disable features. To update + * account settings, users need to be an account owner or users need to be assigned an IAM policy with the + * Administrator role for the Cloud Shell account management service. + * + * @param {Object} params - The parameters to send to the service. + * @param {string} params.accountId - The account ID in which the account settings belong to. + * @param {string} [params.rev] - Unique revision number for the settings object. + * @param {boolean} [params.defaultEnableNewFeatures] - You can choose which Cloud Shell features are available in the + * account and whether any new features are enabled as they become available. The feature settings apply only to the + * enabled Cloud Shell locations. + * @param {boolean} [params.defaultEnableNewRegions] - Set whether Cloud Shell is enabled in a specific location for + * the account. The location determines where user and session data are stored. By default, users are routed to the + * nearest available location. + * @param {boolean} [params.enabled] - When enabled, Cloud Shell is available to all users in the account. + * @param {Feature[]} [params.features] - List of Cloud Shell features. + * @param {RegionSetting[]} [params.regions] - List of Cloud Shell region settings. + * @param {OutgoingHttpHeaders} [params.headers] - Custom request headers + * @returns {Promise>} + */ + public updateAccountSettings( + params: IbmCloudShellV1.UpdateAccountSettingsParams + ): Promise> { + const _params = { ...params }; + const requiredParams = ['accountId']; + + const missingParams = getMissingParams(_params, requiredParams); + if (missingParams) { + return Promise.reject(missingParams); + } + + const body = { + '_rev': _params.rev, + 'default_enable_new_features': _params.defaultEnableNewFeatures, + 'default_enable_new_regions': _params.defaultEnableNewRegions, + 'enabled': _params.enabled, + 'features': _params.features, + 'regions': _params.regions, + }; + + const path = { + 'account_id': _params.accountId, + }; + + const sdkHeaders = getSdkHeaders( + IbmCloudShellV1.DEFAULT_SERVICE_NAME, + 'v1', + 'updateAccountSettings' + ); + + const parameters = { + options: { + url: '/api/v1/user/accounts/{account_id}/settings', + method: 'POST', + body, + path, + }, + defaultOptions: extend(true, {}, this.baseOptions, { + headers: extend( + true, + sdkHeaders, + { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + _params.headers + ), + }), + }; + + return this.createRequest(parameters); + } +} + +/************************* + * interfaces + ************************/ + +namespace IbmCloudShellV1 { + /** An operation response. */ + export interface Response { + result: T; + status: number; + statusText: string; + headers: IncomingHttpHeaders; + } + + /** The callback for a service request. */ + export type Callback = (error: any, response?: Response) => void; + + /** The body of a service request that returns no response data. */ + export interface Empty {} + + /** A standard JS object, defined to avoid the limitations of `Object` and `object` */ + export interface JsonObject { + [key: string]: any; + } + + /************************* + * request interfaces + ************************/ + + /** Parameters for the `getAccountSettings` operation. */ + export interface GetAccountSettingsParams { + /** The account ID in which the account settings belong to. */ + accountId: string; + headers?: OutgoingHttpHeaders; + } + + /** Parameters for the `updateAccountSettings` operation. */ + export interface UpdateAccountSettingsParams { + /** The account ID in which the account settings belong to. */ + accountId: string; + /** Unique revision number for the settings object. */ + rev?: string; + /** You can choose which Cloud Shell features are available in the account and whether any new features are + * enabled as they become available. The feature settings apply only to the enabled Cloud Shell locations. + */ + defaultEnableNewFeatures?: boolean; + /** Set whether Cloud Shell is enabled in a specific location for the account. The location determines where + * user and session data are stored. By default, users are routed to the nearest available location. + */ + defaultEnableNewRegions?: boolean; + /** When enabled, Cloud Shell is available to all users in the account. */ + enabled?: boolean; + /** List of Cloud Shell features. */ + features?: Feature[]; + /** List of Cloud Shell region settings. */ + regions?: RegionSetting[]; + headers?: OutgoingHttpHeaders; + } + + /************************* + * model interfaces + ************************/ + + /** Definition of Cloud Shell account settings. */ + export interface AccountSettings { + /** Unique id of the settings object. */ + _id?: string; + /** Unique revision number for the settings object. */ + _rev?: string; + /** The id of the account the settings belong to. */ + account_id?: string; + /** Creation timestamp. */ + created_at?: string; + /** IAM ID of creator. */ + created_by?: string; + /** You can choose which Cloud Shell features are available in the account and whether any new features are + * enabled as they become available. The feature settings apply only to the enabled Cloud Shell locations. + */ + default_enable_new_features?: boolean; + /** Set whether Cloud Shell is enabled in a specific location for the account. The location determines where + * user and session data are stored. By default, users are routed to the nearest available location. + */ + default_enable_new_regions?: boolean; + /** When enabled, Cloud Shell is available to all users in the account. */ + enabled?: boolean; + /** List of Cloud Shell features. */ + features?: Feature[]; + /** List of Cloud Shell region settings. */ + regions?: RegionSetting[]; + /** Type of api response object. */ + type?: string; + /** Timestamp of last update. */ + updated_at?: string; + /** IAM ID of last updater. */ + updated_by?: string; + } + + /** Describes a Cloud Shell feature. */ + export interface Feature { + /** State of the feature. */ + enabled?: boolean; + /** Name of the feature. */ + key?: string; + } + + /** Describes a Cloud Shell region setting. */ + export interface RegionSetting { + /** State of the region. */ + enabled?: boolean; + /** Name of the region. */ + key?: string; + } +} + +export = IbmCloudShellV1; diff --git a/scripts/typedoc/generate_typedoc.sh b/scripts/typedoc/generate_typedoc.sh index 8f768b35..ea0b86c9 100755 --- a/scripts/typedoc/generate_typedoc.sh +++ b/scripts/typedoc/generate_typedoc.sh @@ -15,6 +15,7 @@ ./iam-access-groups/v2.ts \ ./iam-identity/v1.ts \ ./iam-policy-management/v1.ts \ + ./ibm-cloud-shell/v1.ts \ ./open-service-broker/v1.ts \ ./resource-controller/v2 \ ./resource-manager/v2 \ diff --git a/test/integration/ibm-cloud-shell.v1.test.js b/test/integration/ibm-cloud-shell.v1.test.js new file mode 100644 index 00000000..d99754f8 --- /dev/null +++ b/test/integration/ibm-cloud-shell.v1.test.js @@ -0,0 +1,108 @@ +/* eslint-disable no-console */ +/** + * (C) Copyright IBM Corp. 2021. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const { readExternalSources } = require('ibm-cloud-sdk-core'); +const IbmCloudShellV1 = require('../../dist/ibm-cloud-shell/v1'); +const authHelper = require('../resources/auth-helper.js'); + +// testcase timeout value (200s). +const timeout = 200000; + +// Location of our config file. +const configFile = 'ibm_cloud_shell_v1.env'; + +const describe = authHelper.prepareTests(configFile); + +describe('IbmCloudShellV1_integration', () => { + const ibmCloudShellService = IbmCloudShellV1.newInstance({}); + + expect(ibmCloudShellService).not.toBeNull(); + + const config = readExternalSources(IbmCloudShellV1.DEFAULT_SERVICE_NAME); + expect(config).not.toBeNull(); + + jest.setTimeout(timeout); + + const { accountId } = config; + expect(accountId).not.toBeNull(); + + test('getAccountSettings()', async () => { + const params = { + accountId, + }; + + const res = await ibmCloudShellService.getAccountSettings(params); + expect(res).toBeDefined(); + expect(res.result).toBeDefined(); + expect(res.status).toEqual(200); + }); + test('updateAccountSettings()', async () => { + const getRes = await ibmCloudShellService.getAccountSettings({ + accountId, + }); + const existingAccountSettings = getRes.result; + // Request models needed by this operation. + + // Feature + const featureModel = [ + { + enabled: false, + key: 'server.file_manager', + }, + { + enabled: true, + key: 'server.web_preview', + }, + ]; + + // RegionSetting + const regionSettingModel = [ + { + enabled: true, + key: 'eu-de', + }, + { + enabled: false, + key: 'jp-tok', + }, + { + enabled: false, + key: 'us-south', + }, + ]; + + const params = { + accountId, + rev: existingAccountSettings._rev, + defaultEnableNewFeatures: false, + defaultEnableNewRegions: true, + enabled: true, + features: featureModel, + regions: regionSettingModel, + }; + + const res = await ibmCloudShellService.updateAccountSettings(params); + expect(res).toBeDefined(); + expect(res.result).toBeDefined(); + expect(res.status).toEqual(200); + expect(res.result.default_enable_new_features).toEqual(false); + expect(res.result.default_enable_new_regions).toEqual(true); + expect(res.result.enabled).toEqual(true); + expect(res.result.features).toEqual(featureModel); + expect(res.result.regions).toEqual(regionSettingModel); + }); +}); diff --git a/test/unit/ibm-cloud-shell.v1.test.js b/test/unit/ibm-cloud-shell.v1.test.js new file mode 100644 index 00000000..fa50c270 --- /dev/null +++ b/test/unit/ibm-cloud-shell.v1.test.js @@ -0,0 +1,285 @@ +/** + * (C) Copyright IBM Corp. 2021. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +'use strict'; + +// need to import the whole package to mock getAuthenticatorFromEnvironment +const core = require('ibm-cloud-sdk-core'); +const { NoAuthAuthenticator, unitTestUtils } = core; + +const IbmCloudShellV1 = require('../../dist/ibm-cloud-shell/v1'); + +const { + getOptions, + checkUrlAndMethod, + checkMediaHeaders, + expectToBePromise, +} = unitTestUtils; + +const ibmCloudShellServiceOptions = { + authenticator: new NoAuthAuthenticator(), + url: 'https://api.shell.cloud.ibm.com', +}; + +const ibmCloudShellService = new IbmCloudShellV1(ibmCloudShellServiceOptions); + +// dont actually create a request +const createRequestMock = jest.spyOn(ibmCloudShellService, 'createRequest'); +createRequestMock.mockImplementation(() => Promise.resolve()); + +// dont actually construct an authenticator +const getAuthenticatorMock = jest.spyOn(core, 'getAuthenticatorFromEnvironment'); +getAuthenticatorMock.mockImplementation(() => new NoAuthAuthenticator()); + +afterEach(() => { + createRequestMock.mockClear(); + getAuthenticatorMock.mockClear(); +}); + +describe('IbmCloudShellV1', () => { + describe('the newInstance method', () => { + test('should use defaults when options not provided', () => { + const testInstance = IbmCloudShellV1.newInstance(); + + expect(getAuthenticatorMock).toHaveBeenCalled(); + expect(testInstance.baseOptions.authenticator).toBeInstanceOf(NoAuthAuthenticator); + expect(testInstance.baseOptions.serviceName).toBe(IbmCloudShellV1.DEFAULT_SERVICE_NAME); + expect(testInstance.baseOptions.serviceUrl).toBe(IbmCloudShellV1.DEFAULT_SERVICE_URL); + expect(testInstance).toBeInstanceOf(IbmCloudShellV1); + }); + + test('should set serviceName, serviceUrl, and authenticator when provided', () => { + const options = { + authenticator: new NoAuthAuthenticator(), + serviceUrl: 'custom.com', + serviceName: 'my-service', + }; + + const testInstance = IbmCloudShellV1.newInstance(options); + + expect(getAuthenticatorMock).not.toHaveBeenCalled(); + expect(testInstance.baseOptions.authenticator).toBeInstanceOf(NoAuthAuthenticator); + expect(testInstance.baseOptions.serviceUrl).toBe('custom.com'); + expect(testInstance.baseOptions.serviceName).toBe('my-service'); + expect(testInstance).toBeInstanceOf(IbmCloudShellV1); + }); + }); + describe('the constructor', () => { + test('use user-given service url', () => { + const options = { + authenticator: new NoAuthAuthenticator(), + serviceUrl: 'custom.com', + }; + + const testInstance = new IbmCloudShellV1(options); + + expect(testInstance.baseOptions.serviceUrl).toBe('custom.com'); + }); + + test('use default service url', () => { + const options = { + authenticator: new NoAuthAuthenticator(), + }; + + const testInstance = new IbmCloudShellV1(options); + + expect(testInstance.baseOptions.serviceUrl).toBe(IbmCloudShellV1.DEFAULT_SERVICE_URL); + }); + }); + describe('getAccountSettings', () => { + describe('positive tests', () => { + test('should pass the right params to createRequest', () => { + // Construct the params object for operation getAccountSettings + const accountId = '12345678-abcd-1a2b-a1b2-1234567890ab'; + const params = { + accountId: accountId, + }; + + const getAccountSettingsResult = ibmCloudShellService.getAccountSettings(params); + + // all methods should return a Promise + expectToBePromise(getAccountSettingsResult); + + // assert that create request was called + expect(createRequestMock).toHaveBeenCalledTimes(1); + + const options = getOptions(createRequestMock); + + checkUrlAndMethod(options, '/api/v1/user/accounts/{account_id}/settings', 'GET'); + const expectedAccept = 'application/json'; + const expectedContentType = undefined; + checkMediaHeaders(createRequestMock, expectedAccept, expectedContentType); + expect(options.path['account_id']).toEqual(accountId); + }); + + test('should prioritize user-given headers', () => { + // parameters + const accountId = '12345678-abcd-1a2b-a1b2-1234567890ab'; + const userAccept = 'fake/accept'; + const userContentType = 'fake/contentType'; + const params = { + accountId, + headers: { + Accept: userAccept, + 'Content-Type': userContentType, + }, + }; + + ibmCloudShellService.getAccountSettings(params); + checkMediaHeaders(createRequestMock, userAccept, userContentType); + }); + }); + + describe('negative tests', () => { + test('should enforce required parameters', async done => { + let err; + try { + await ibmCloudShellService.getAccountSettings({}); + } catch (e) { + err = e; + } + + expect(err.message).toMatch(/Missing required parameters/); + done(); + }); + + test('should reject promise when required params are not given', done => { + const getAccountSettingsPromise = ibmCloudShellService.getAccountSettings(); + expectToBePromise(getAccountSettingsPromise); + + getAccountSettingsPromise.catch(err => { + expect(err.message).toMatch(/Missing required parameters/); + done(); + }); + }); + }); + }); + describe('updateAccountSettings', () => { + describe('positive tests', () => { + // Request models needed by this operation. + + // Feature + const featureModel = [ + { + enabled: true, + key: 'server.file_manager', + }, + { + enabled: true, + key: 'server.web_preview', + }, + ]; + + // RegionSetting + const regionSettingModel = [ + { + enabled: true, + key: 'eu-de', + }, + { + enabled: true, + key: 'jp-tok', + }, + { + enabled: true, + key: 'us-south', + }, + ]; + + test('should pass the right params to createRequest', () => { + // Construct the params object for operation updateAccountSettings + const accountId = '12345678-abcd-1a2b-a1b2-1234567890ab'; + const rev = '130-12345678-abcd-1a2b-a1b2-1234567890ab'; + const defaultEnableNewFeatures = true; + const defaultEnableNewRegions = true; + const enabled = true; + const features = featureModel; + const regions = regionSettingModel; + const params = { + accountId: accountId, + rev: rev, + defaultEnableNewFeatures: defaultEnableNewFeatures, + defaultEnableNewRegions: defaultEnableNewRegions, + enabled: enabled, + features: features, + regions: regions, + }; + + const updateAccountSettingsResult = ibmCloudShellService.updateAccountSettings(params); + + // all methods should return a Promise + expectToBePromise(updateAccountSettingsResult); + + // assert that create request was called + expect(createRequestMock).toHaveBeenCalledTimes(1); + + const options = getOptions(createRequestMock); + + checkUrlAndMethod(options, '/api/v1/user/accounts/{account_id}/settings', 'POST'); + const expectedAccept = 'application/json'; + const expectedContentType = 'application/json'; + checkMediaHeaders(createRequestMock, expectedAccept, expectedContentType); + expect(options.body['_rev']).toEqual(rev); + expect(options.body['default_enable_new_features']).toEqual(defaultEnableNewFeatures); + expect(options.body['default_enable_new_regions']).toEqual(defaultEnableNewRegions); + expect(options.body['enabled']).toEqual(enabled); + expect(options.body['features']).toEqual(features); + expect(options.body['regions']).toEqual(regions); + expect(options.path['account_id']).toEqual(accountId); + }); + + test('should prioritize user-given headers', () => { + // parameters + const accountId = '12345678-abcd-1a2b-a1b2-1234567890ab'; + const userAccept = 'fake/accept'; + const userContentType = 'fake/contentType'; + const params = { + accountId, + headers: { + Accept: userAccept, + 'Content-Type': userContentType, + }, + }; + + ibmCloudShellService.updateAccountSettings(params); + checkMediaHeaders(createRequestMock, userAccept, userContentType); + }); + }); + + describe('negative tests', () => { + test('should enforce required parameters', async done => { + let err; + try { + await ibmCloudShellService.updateAccountSettings({}); + } catch (e) { + err = e; + } + + expect(err.message).toMatch(/Missing required parameters/); + done(); + }); + + test('should reject promise when required params are not given', done => { + const updateAccountSettingsPromise = ibmCloudShellService.updateAccountSettings(); + expectToBePromise(updateAccountSettingsPromise); + + updateAccountSettingsPromise.catch(err => { + expect(err.message).toMatch(/Missing required parameters/); + done(); + }); + }); + }); + }); +});