From c6bd25cd70d6320b1574dbfa39e7e26f1cfc814e Mon Sep 17 00:00:00 2001 From: spaliwal Date: Thu, 9 Apr 2020 15:24:56 +0530 Subject: [PATCH 1/4] Added command to get action url. Command can get urls for all actions, individual action and also fetch its CDN urls based on flag. --- src/commands/app/get-url.js | 71 +++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/commands/app/get-url.js diff --git a/src/commands/app/get-url.js b/src/commands/app/get-url.js new file mode 100644 index 00000000..d0e732d2 --- /dev/null +++ b/src/commands/app/get-url.js @@ -0,0 +1,71 @@ +/* +Copyright 2019 Adobe. All rights reserved. +This file is licensed to you 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 REPRESENTATIONS +OF ANY KIND, either express or implied. See the License for the specific language +governing permissions and limitations under the License. +*/ + +const ora = require('ora') +const chalk = require('chalk') +const fs = require('fs-extra') + +const { flags } = require('@oclif/command') + +const BaseCommand = require('../../BaseCommand') +const AppScripts = require('@adobe/aio-app-scripts') +const { wrapError } = require('../../lib/app-helper') + +class GetUrlCommand extends BaseCommand { + async run () { + // cli input + const { args } = this.parse(GetUrlCommand) + const { flags } = this.parse(GetUrlCommand) + const scripts = AppScripts({ listeners: {} }) + + try { + let options = {} + if(args.action) + options.action = args.action + if(flags.cdn) + options.cdn = flags.cdn + const urls = await scripts.getUrls(options) + if(urls.runtime) { + this.log("Runtime URLs") + Object.entries(urls.runtime).forEach(([key, value]) => { + this.log(chalk.blue(chalk.bold(`${key} `)) + ' - ' + chalk.blue(chalk.bold(`${value} `))) + }) + } + + if(urls.cdn) { + this.log("CDN URLs") + Object.entries(urls.cdn).forEach(([key, value]) => { + this.log(chalk.blue(chalk.bold(`${key} `)) + ' - ' + chalk.blue(chalk.bold(`${value} `))) + }) + } + return urls + } catch (error) { + this.error(wrapError(error)) + } + + } +} + +GetUrlCommand.description = 'Get action URL' + +GetUrlCommand.flags = { + ...BaseCommand.flags, + 'cdn': flags.boolean({ + description: 'Display CDN based action URLs' + }) +} + +GetUrlCommand.args = [ + { name: 'action' } +] + +module.exports = GetUrlCommand From 35eef087b13e6a0bf88abc55e34ec4c4b03454d9 Mon Sep 17 00:00:00 2001 From: spaliwal Date: Thu, 9 Apr 2020 18:24:59 +0530 Subject: [PATCH 2/4] Added tests --- src/commands/app/get-url.js | 25 +++--- test/commands/app/geturl.test.js | 140 +++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+), 15 deletions(-) create mode 100644 test/commands/app/geturl.test.js diff --git a/src/commands/app/get-url.js b/src/commands/app/get-url.js index d0e732d2..9959159d 100644 --- a/src/commands/app/get-url.js +++ b/src/commands/app/get-url.js @@ -10,9 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ -const ora = require('ora') const chalk = require('chalk') -const fs = require('fs-extra') const { flags } = require('@oclif/command') @@ -28,22 +26,20 @@ class GetUrlCommand extends BaseCommand { const scripts = AppScripts({ listeners: {} }) try { - let options = {} - if(args.action) - options.action = args.action - if(flags.cdn) - options.cdn = flags.cdn + const options = {} + options.action = args.action + options.cdn = flags.cdn const urls = await scripts.getUrls(options) - if(urls.runtime) { - this.log("Runtime URLs") - Object.entries(urls.runtime).forEach(([key, value]) => { + if (urls.runtime) { + this.log('Runtime URLs') + Object.entries(urls.runtime).forEach(([key, value]) => { this.log(chalk.blue(chalk.bold(`${key} `)) + ' - ' + chalk.blue(chalk.bold(`${value} `))) }) } - if(urls.cdn) { - this.log("CDN URLs") - Object.entries(urls.cdn).forEach(([key, value]) => { + if (urls.cdn) { + this.log('CDN URLs') + Object.entries(urls.cdn).forEach(([key, value]) => { this.log(chalk.blue(chalk.bold(`${key} `)) + ' - ' + chalk.blue(chalk.bold(`${value} `))) }) } @@ -51,7 +47,6 @@ class GetUrlCommand extends BaseCommand { } catch (error) { this.error(wrapError(error)) } - } } @@ -59,7 +54,7 @@ GetUrlCommand.description = 'Get action URL' GetUrlCommand.flags = { ...BaseCommand.flags, - 'cdn': flags.boolean({ + cdn: flags.boolean({ description: 'Display CDN based action URLs' }) } diff --git a/test/commands/app/geturl.test.js b/test/commands/app/geturl.test.js new file mode 100644 index 00000000..ffcf8cbb --- /dev/null +++ b/test/commands/app/geturl.test.js @@ -0,0 +1,140 @@ +/* +Copyright 2019 Adobe. All rights reserved. +This file is licensed to you 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 REPRESENTATIONS +OF ANY KIND, either express or implied. See the License for the specific language +governing permissions and limitations under the License. +*/ + +const TheCommand = require('../../../src/commands/app/get-url') +const BaseCommand = require('../../../src/BaseCommand') + +jest.mock('@adobe/aio-app-scripts') +const mockScripts = require('@adobe/aio-app-scripts')() + +beforeEach(() => { + jest.restoreAllMocks() +}) + +test('exports', async () => { + expect(typeof TheCommand).toEqual('function') + expect(TheCommand.prototype instanceof BaseCommand).toBeTruthy() +}) + +test('description', async () => { + expect(TheCommand.description).toBeDefined() +}) + +test('aliases', async () => { + expect(TheCommand.aliases).toEqual([]) +}) + +test('flags', async () => { + expect(typeof TheCommand.flags.cdn).toBe('object') + expect(typeof TheCommand.flags.cdn.description).toBe('string') +}) + +describe('run', () => { + beforeEach(() => { + mockScripts.getUrls = mockScripts.logs + mockScripts.getUrls.mockReset() + }) + + test('get all action urls', async () => { + const command = new TheCommand([]) + command.error = jest.fn() + command.log = jest.fn() + const retVal = { + runtime: { + action: 'https://fake_ns.adobeioruntime.net/api/v1/web/sample-app-1.0.0/action' + } + } + mockScripts.getUrls.mockResolvedValue(retVal) + const urls = await command.run() + expect(command.error).toHaveBeenCalledTimes(0) + expect(mockScripts.getUrls).toBeCalledWith({}) + expect(urls).toBe(retVal) + }) + + test('get empty action urls', async () => { + const command = new TheCommand([]) + command.error = jest.fn() + command.log = jest.fn() + const retVal = {} + mockScripts.getUrls.mockResolvedValue(retVal) + const urls = await command.run() + expect(command.error).toHaveBeenCalledTimes(0) + expect(mockScripts.getUrls).toBeCalledWith({}) + expect(urls).toBe(retVal) + }) + + test('get all action urls with cdn flag', async () => { + const command = new TheCommand([]) + command.error = jest.fn() + command.log = jest.fn() + const retVal = { + runtime: { + action: 'https://fake_ns.adobeioruntime.net/api/v1/web/sample-app-1.0.0/action' + }, + cdn: { + action: 'https://fake_ns.adobeio-static.net/api/v1/web/sample-app-1.0.0/action' + } + } + mockScripts.getUrls.mockResolvedValue(retVal) + const urls = await command.run(['--cdn']) + expect(command.error).toHaveBeenCalledTimes(0) + expect(mockScripts.getUrls).toBeCalledWith({}) + expect(urls).toBe(retVal) + }) + + test('get single action url', async () => { + const command = new TheCommand([]) + command.error = jest.fn() + command.log = jest.fn() + command.args = { action: 'action' } + const retVal = { + runtime: { + action: 'https://fake_ns.adobeioruntime.net/api/v1/web/sample-app-1.0.0/action' + } + } + mockScripts.getUrls.mockResolvedValue(retVal) + const urls = await command.run() + expect(command.error).toHaveBeenCalledTimes(0) + expect(mockScripts.getUrls).toBeCalledWith({}) + expect(urls).toBe(retVal) + }) + + test('get single action url with cdn flag', async () => { + const command = new TheCommand([]) + command.error = jest.fn() + command.log = jest.fn() + command.args = { action: 'action' } + const retVal = { + runtime: { + action: 'https://fake_ns.adobeioruntime.net/api/v1/web/sample-app-1.0.0/action' + }, + cdn: { + action: 'https://fake_ns.adobeio-static.net/api/v1/web/sample-app-1.0.0/action' + } + } + mockScripts.getUrls.mockResolvedValue(retVal) + const urls = await command.run(['--cdn']) + expect(command.error).toHaveBeenCalledTimes(0) + expect(mockScripts.getUrls).toBeCalledWith({}) + expect(urls).toBe(retVal) + }) + + test('get single action url with non existing action', async () => { + const command = new TheCommand([]) + command.error = jest.fn() + command.log = jest.fn() + command.args = { action: 'invalid' } + mockScripts.getUrls.mockRejectedValue('error') + await command.run() + expect(command.error).toHaveBeenCalledWith(expect.objectContaining({ message: 'error' })) + }) +}) From 5b26a0d76dbc771b4e2a7571049112368055da37 Mon Sep 17 00:00:00 2001 From: spaliwal Date: Wed, 15 Apr 2020 14:13:57 +0530 Subject: [PATCH 3/4] Closed review comments --- src/commands/app/get-url.js | 2 +- test/commands/app/geturl.test.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/commands/app/get-url.js b/src/commands/app/get-url.js index 9959159d..c0b6a213 100644 --- a/src/commands/app/get-url.js +++ b/src/commands/app/get-url.js @@ -50,7 +50,7 @@ class GetUrlCommand extends BaseCommand { } } -GetUrlCommand.description = 'Get action URL' +GetUrlCommand.description = 'Get action URLs' GetUrlCommand.flags = { ...BaseCommand.flags, diff --git a/test/commands/app/geturl.test.js b/test/commands/app/geturl.test.js index ffcf8cbb..0a3d9dbc 100644 --- a/test/commands/app/geturl.test.js +++ b/test/commands/app/geturl.test.js @@ -58,6 +58,7 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockScripts.getUrls).toBeCalledWith({}) expect(urls).toBe(retVal) + expect(command.log).toHaveBeenCalledWith(expect.stringContaining(urls.runtime.action)) }) test('get empty action urls', async () => { @@ -89,6 +90,8 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockScripts.getUrls).toBeCalledWith({}) expect(urls).toBe(retVal) + expect(command.log).toHaveBeenCalledWith(expect.stringContaining(urls.runtime.action)) + expect(command.log).toHaveBeenCalledWith(expect.stringContaining(urls.cdn.action)) }) test('get single action url', async () => { @@ -106,6 +109,7 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockScripts.getUrls).toBeCalledWith({}) expect(urls).toBe(retVal) + expect(command.log).toHaveBeenCalledWith(expect.stringContaining(urls.runtime.action)) }) test('get single action url with cdn flag', async () => { @@ -126,6 +130,8 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockScripts.getUrls).toBeCalledWith({}) expect(urls).toBe(retVal) + expect(command.log).toHaveBeenCalledWith(expect.stringContaining(urls.runtime.action)) + expect(command.log).toHaveBeenCalledWith(expect.stringContaining(urls.cdn.action)) }) test('get single action url with non existing action', async () => { From 8e1cd92adf801795b65bcd0686e09c90a63fef5f Mon Sep 17 00:00:00 2001 From: spaliwal Date: Wed, 15 Apr 2020 14:25:18 +0530 Subject: [PATCH 4/4] close review comment --- src/commands/app/get-url.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/commands/app/get-url.js b/src/commands/app/get-url.js index c0b6a213..4763fa3b 100644 --- a/src/commands/app/get-url.js +++ b/src/commands/app/get-url.js @@ -21,8 +21,7 @@ const { wrapError } = require('../../lib/app-helper') class GetUrlCommand extends BaseCommand { async run () { // cli input - const { args } = this.parse(GetUrlCommand) - const { flags } = this.parse(GetUrlCommand) + const { args, flags } = this.parse(GetUrlCommand) const scripts = AppScripts({ listeners: {} }) try {