Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VM custom button dialog - use the VM instead of the parent service #1542

Merged
merged 2 commits into from
May 3, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ function StateController ($state, $stateParams, CollectionsApi, EventNotificatio
target_id: vm.serviceId,
target_type: 'service'
}
if (vm.vmId) {
options.target_type = 'vm'
options.target_id = vm.vmId
}

const dialogId = vm.resourceAction.dialog_id
const resolveDialogs = CollectionsApi.query(`service_dialogs/${dialogId}`, options)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* global $state, $controller, CollectionsApi, Notifications, DialogFieldRefresh */
/* eslint-disable no-unused-expressions */
describe('State: services.custom_button_details', () => {
let dialog, dialogData, dialogFields, button
const dialogId = 213
const serviceId = 123

let dialog, dialogData, dialogFields, button, dialogResponse

beforeEach(() => {
module('app.states')
Expand Down Expand Up @@ -41,92 +44,126 @@ describe('State: services.custom_button_details', () => {
'dialogField2': 2
}
}
})

describe('controller', () => {
let collectionsApiSpy, controller, notificationsErrorSpy, notificationsSuccessSpy

beforeEach(() => {
bard.inject('$controller', '$log', '$state', '$stateParams', '$rootScope', 'CollectionsApi', 'Notifications', 'DialogFieldRefresh')
dialogResponse = {
content: [dialog],
id: dialogId
}
})

sinon.stub(DialogFieldRefresh, 'refreshDialogField')
describe('for a service', () => {
it('sends target_type=service service dialog query on init', (done) => {
bard.inject('$controller', '$state', '$stateParams', 'CollectionsApi')
const spy = sinon.stub(CollectionsApi, 'query').returns(Promise.resolve(dialogResponse))
const resourceActionId = button.resource_action.id

const apiQueries = sinon.stub(CollectionsApi, 'get')
apiQueries.onFirstCall().returns(Promise.resolve(dialog))
apiQueries.onSecondCall().returns(Promise.resolve({}))
controller = $controller($state.get('services.custom_button_details').controller, {
$controller($state.get('services.custom_button_details').controller, {
$stateParams: {
dialogId: 213,
button: button,
serviceId: 123,
dialogId,
button,
serviceId,
serviceTemplateCatalogId: 321
}
})
controller.setDialogData(dialogData)
})

describe('controller initialization', () => {
it('is created successfully', () => {
expect(controller).to.be.defined
})
})
describe('dialog values are updated', () => {
it('has values updated', () => {
const dialogValues = {
'dialogField1': 1,
'dialogField2': 2
}
expect(controller.dialogData).to.eql(dialogValues)
done()

expect(spy).to.have.been.calledWith(`service_dialogs/${dialogId}`, {
expand: 'resources',
attributes: 'content',
resource_action_id: resourceActionId,
target_type: 'service',
target_id: serviceId
})
})
describe('controller#submitCustomButton', () => {
describe('when the API call is successful', () => {
beforeEach(() => {
const successResponse = {
message: 'Great Success!'
}

collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.resolve(successResponse))
notificationsSuccessSpy = sinon.spy(Notifications, 'success')
})
describe('after init', () => {
let collectionsApiSpy, controller, notificationsErrorSpy, notificationsSuccessSpy

it('POSTs to the services API', () => {
controller.submitCustomButton()
expect(collectionsApiSpy).to.have.been.calledWith(
'services',
123,
{},
'{"action":"buttonName","resource":{"dialogField1":1,"dialogField2":2}}'
)
})
beforeEach(() => {
bard.inject('$controller', '$log', '$state', '$stateParams', '$rootScope', 'CollectionsApi', 'Notifications', 'DialogFieldRefresh')

it('makes a notification success call', (done) => {
controller.submitCustomButton()
done()
expect(notificationsSuccessSpy).to.have.been.calledWith('Great Success!')
sinon.stub(DialogFieldRefresh, 'refreshDialogField')

const apiQueries = sinon.stub(CollectionsApi, 'get')
apiQueries.onFirstCall().returns(Promise.resolve(dialog))
apiQueries.onSecondCall().returns(Promise.resolve({}))
controller = $controller($state.get('services.custom_button_details').controller, {
$stateParams: {
dialogId,
button,
serviceId,
serviceTemplateCatalogId: 321
}
})
controller.setDialogData(dialogData)
})

it('goes to the service details', (done) => {
controller.submitCustomButton()
done()
expect($state.is('services.details')).to.be.true
describe('controller initialization', () => {
it('is created successfully', () => {
expect(controller).to.be.defined
})
})

describe('when the API call fails', () => {
beforeEach(() => {
const errorResponse = 'oopsies'
describe('dialog values are updated', () => {
it('has values updated', () => {
const dialogValues = {
'dialogField1': 1,
'dialogField2': 2
}
expect(controller.dialogData).to.eql(dialogValues)
})
})

collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.reject(errorResponse))
notificationsErrorSpy = sinon.spy(Notifications, 'error')
describe('controller#submitCustomButton', () => {
describe('when the API call is successful', () => {
beforeEach(() => {
const successResponse = {
message: 'Great Success!'
}

collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.resolve(successResponse))
notificationsSuccessSpy = sinon.spy(Notifications, 'success')
})

it('POSTs to the services API', () => {
controller.submitCustomButton()
expect(collectionsApiSpy).to.have.been.calledWith(
'services',
serviceId,
{},
'{"action":"buttonName","resource":{"dialogField1":1,"dialogField2":2}}'
)
})

it('makes a notification success call', (done) => {
controller.submitCustomButton()
done()
expect(notificationsSuccessSpy).to.have.been.calledWith('Great Success!')
})

it('goes to the service details', (done) => {
controller.submitCustomButton()
done()
expect($state.is('services.details')).to.be.true
})
})

it('makes a notification error call', (done) => {
controller.submitCustomButton()
done()
expect(notificationsErrorSpy).to.have.been.calledWith(
'There was an error submitting this request: oopsies'
)
describe('when the API call fails', () => {
beforeEach(() => {
const errorResponse = 'oopsies'

collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.reject(errorResponse))
notificationsErrorSpy = sinon.spy(Notifications, 'error')
})

it('makes a notification error call', (done) => {
controller.submitCustomButton()
done()
expect(notificationsErrorSpy).to.have.been.calledWith(
'There was an error submitting this request: oopsies'
)
})
})
})
})
Expand All @@ -135,20 +172,6 @@ describe('State: services.custom_button_details', () => {
describe('Custom button actions for a VM', () => {
let collectionsApiSpy, controller

const dialogFields = [{
name: 'dialogField1',
default_value: '1'
}, {
name: 'dialogField2',
default_value: '2'
}]
const dialog = {
dialog_tabs: [{
dialog_groups: [{
dialog_fields: dialogFields
}]
}]
}
const button = {
name: 'buttonName',
applies_to_id: 456,
Expand All @@ -159,48 +182,78 @@ describe('State: services.custom_button_details', () => {
}
}

beforeEach(() => {
bard.inject('$controller', '$log', '$state', '$stateParams', '$rootScope', 'CollectionsApi', 'Notifications', 'DialogFieldRefresh')
sinon.stub(DialogFieldRefresh, 'refreshDialogField')
const dialogResponse = {content: [dialog], id: 213}
const apiQueries = sinon.stub(CollectionsApi, 'get')
apiQueries.onFirstCall().returns(Promise.resolve(dialogResponse))
apiQueries.onSecondCall().returns(Promise.resolve({}))
it('sends target_type=vm service dialog query on init', (done) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're still missing the spec for when the vmId doesn't exist, right? There are still cases where the service dialogs endpoint is going to request with the target_type of 'service' and the target_id with that service's id.

I guess maybe this spec didn't exist before, but I think it needs to be added now that we have these two different calls happening.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 👍. The tests should probably be reorganized a bit to be more symmetrical, and looks like we have some SUI test inconsistencies (3 tests randomly in jest not karma), can't use object spread there, etc.

But for now, I just added a similar test for init without vmId :).

bard.inject('$controller', '$state', '$stateParams', 'CollectionsApi')
const spy = sinon.stub(CollectionsApi, 'query').returns(Promise.resolve(dialogResponse))
const vmId = 456
const resourceActionId = button.resource_action.id

controller = $controller($state.get('services.custom_button_details').controller, {
$controller($state.get('services.custom_button_details').controller, {
$stateParams: {
dialogId: 213,
dialogId,
button: button,
serviceId: 123,
vmId: 456,
serviceId,
vmId,
serviceTemplateCatalogId: 321
}
})

controller.setDialogData(dialogData)
done()

expect(spy).to.have.been.calledWith(`service_dialogs/${dialogId}`, {
expand: 'resources',
attributes: 'content',
resource_action_id: resourceActionId,
target_type: 'vm',
target_id: vmId
})
})
it('POSTs to the vms API', (done) => {
const successResponse = {
message: 'Great Success!'
}

collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.resolve(successResponse))
sinon.spy(Notifications, 'success')
describe('after init', () => {
beforeEach(() => {
bard.inject('$controller', '$log', '$state', '$stateParams', '$rootScope', 'CollectionsApi', 'Notifications', 'DialogFieldRefresh')
sinon.stub(DialogFieldRefresh, 'refreshDialogField')
const apiQueries = sinon.stub(CollectionsApi, 'get')
apiQueries.onFirstCall().returns(Promise.resolve(dialogResponse))
apiQueries.onSecondCall().returns(Promise.resolve({}))

controller.submitCustomButton()
done()
controller = $controller($state.get('services.custom_button_details').controller, {
$stateParams: {
dialogId,
button: button,
serviceId,
vmId: 456,
serviceTemplateCatalogId: 321
}
})

expect(collectionsApiSpy).to.have.been.calledWith(
'vms',
456,
{},
'{"action":"buttonName","resource":{"dialogField1":1,"dialogField2":2}}'
)
})
it('goes to the resource details', (done) => {
controller.submitCustomButton()
done()
expect($state.is('services.resource-details')).to.be.true
controller.setDialogData(dialogData)
})

it('POSTs to the vms API', (done) => {
const successResponse = {
message: 'Great Success!'
}

collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.resolve(successResponse))
sinon.spy(Notifications, 'success')

controller.submitCustomButton()
done()

expect(collectionsApiSpy).to.have.been.calledWith(
'vms',
456,
{},
'{"action":"buttonName","resource":{"dialogField1":1,"dialogField2":2}}'
)
})

it('goes to the resource details', (done) => {
controller.submitCustomButton()
done()
expect($state.is('services.resource-details')).to.be.true
})
})
})

Expand All @@ -213,12 +266,12 @@ describe('State: services.custom_button_details', () => {
sinon.stub(DialogFieldRefresh, 'refreshDialogField')

controller = $controller($state.get('services.custom_button_details').controller, {
dialog: {content: [dialog], id: 213},
dialog: dialogResponse,
service: {},
$stateParams: {
dialogId: 213,
dialogId,
button: button,
serviceId: 123,
serviceId,
serviceTemplateCatalogId: 321
}
})
Expand All @@ -232,7 +285,7 @@ describe('State: services.custom_button_details', () => {
dialogData.data,
['fieldName'],
'service_dialogs',
{dialogId: 213, resourceActionId: 789, targetId: 123, targetType: 'service'}
{dialogId, resourceActionId: 789, targetId: serviceId, targetType: 'service'}
)
})
})
Expand All @@ -243,12 +296,12 @@ describe('State: services.custom_button_details', () => {
sinon.stub(DialogFieldRefresh, 'refreshDialogField')

controller = $controller($state.get('services.custom_button_details').controller, {
dialog: {content: [dialog], id: 213},
dialog: dialogResponse,
service: {},
$stateParams: {
dialogId: 213,
dialogId,
button: button,
serviceId: 123,
serviceId,
vmId: 456,
serviceTemplateCatalogId: 321
}
Expand All @@ -263,7 +316,7 @@ describe('State: services.custom_button_details', () => {
dialogData.data,
['fieldName'],
'service_dialogs',
{dialogId: 213, resourceActionId: 789, targetId: 456, targetType: 'vm'}
{dialogId, resourceActionId: 789, targetId: 456, targetType: 'vm'}
)
})
})
Expand Down