Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/luis/src/commands/luis/application/rename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import {CLIError, Command, flags} from '@microsoft/bf-cli-command'

const utils = require('../../../utils/index')

export default class LuisApplicationRename extends Command {
static description = 'Renames the application and updates its description'

static examples = [`
$ bf luis:application:rename --endpoint {ENDPOINT} --subscriptionKey {SUBSCRIPTION_KEY} --appId {APP_ID} --name {NAME} --description {DESCRIPTION}
`]

static flags = {
help: flags.help({char: 'h'}),
endpoint: flags.string({description: 'LUIS endpoint hostname'}),
subscriptionKey: flags.string({description: 'LUIS cognitive services subscription key (aka Ocp-Apim-Subscription-Key)'}),
appId: flags.string({description: 'LUIS applicaton id'}),
name: flags.string({description: 'LUIS application name'}),
description: flags.string({description: 'LUIS application description'}),
}

async run() {
const {flags} = this.parse(LuisApplicationRename)
const flagLabels = Object.keys(LuisApplicationRename.flags)
const configDir = this.config.configDir

const {
endpoint,
subscriptionKey,
appId,
name,
description
} = await utils.processInputs(flags, flagLabels, configDir)

const requiredProps = {endpoint, subscriptionKey, appId, name}
utils.validateRequiredProps(requiredProps)

const client = utils.getLUISClient(subscriptionKey, endpoint)

const applicationUpdateObject = {name, description}

try {
const appUpdateStatus = await client.apps.update(appId, applicationUpdateObject)
if (appUpdateStatus.code === 'Success') {
this.log('App successfully renamed')
}
} catch (err) {
throw new CLIError(`Failed to rename app: ${err}`)
}
}
}
63 changes: 63 additions & 0 deletions packages/luis/test/commands/luis/application/rename.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {expect, test} from '@oclif/test'
const sinon = require('sinon')
const uuidv1 = require('uuid/v1')
const utils = require('../../../../src/utils/index')

describe('luis:application:rename', () => {

before(() => {
const newAppId = uuidv1()
})

beforeEach(() => {
sinon.stub(utils, 'processInputs').returnsArg(0)
})

afterEach(() => {
sinon.restore();
});

test
.stdout()
.command(['luis:application:rename', '--help'])
.it('should print the help contents when --help is passed as an argument', ctx => {
expect(ctx.stdout).to.contain('Renames the application and updates its description')
})

test
.stdout()
.stderr()
.command(['luis:application:rename', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--appId', uuidv1(), '--subscriptionKey', uuidv1(), '--description', 'test description'])
.it('displays an error if any required input parameters are missing', ctx => {
expect(ctx.stderr).to.contain(`Required input property 'name' missing.`)
})

test
.stdout()
.stderr()
.command(['luis:application:rename', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--appId', uuidv1(), '--name', 'sample-app', '--description', 'test description'])
.it('displays an error if any required input parameters are missing', ctx => {
expect(ctx.stderr).to.contain(`Required input property 'subscriptionKey' missing.`)
})

test
.nock('https://westus.api.cognitive.microsoft.com', api => api
.put(uri => uri.includes('apps'))
.reply(200, {"code":"Success","message":"Operation Successful"})
)
.stdout()
.stderr()
.command(['luis:application:rename', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1(), '--appId', uuidv1(), '--name', 'sample-app', '--description', 'test description'])
.it('renames a LUIS application and displays a success message', ctx => {
expect(ctx.stdout).to.contain('App successfully renamed')
})

test
.stdout()
.stderr()
.command(['luis:application:rename', '--endpoint', 'undefined', '--subscriptionKey', uuidv1(), '--appId', uuidv1(), '--name', 'sample-app', '--description', 'test description'])
.it('fails to create an app and displays an error message if the endpoint is null', ctx => {
expect(ctx.stderr).to.contain('Access denied due to invalid subscription key or wrong API endpoint.')
})

})