Skip to content

Commit

Permalink
Pass additional information when refreshing a dialog field
Browse files Browse the repository at this point in the history
Resource action id, target id, and target type need to be passed to the
API in order to establish the context for dialog fields when they are
refreshed via the /service_dialogs endpoint
  • Loading branch information
eclarizio committed Dec 4, 2017
1 parent 3a27baa commit 6acbcf5
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 106 deletions.
9 changes: 6 additions & 3 deletions client/app/core/dialog-field-refresh.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ export function DialogFieldRefreshFactory (CollectionsApi) {

return service

function refreshDialogField (dialogData, dialogField, url, resourceId) {
function refreshDialogField (dialogData, dialogField, url, idList) {
return new Promise((resolve, reject) => {
CollectionsApi.post(
url,
resourceId,
idList.dialogId,
{},
angular.toJson({
action: 'refresh_dialog_fields',
resource: {
dialog_fields: dialogData,
fields: dialogField
fields: dialogField,
resource_action_id: idList.resourceActionId,
target_id: idList.targetId,
target_type: idList.targetType
}
})
).then((response) => {
Expand Down
131 changes: 74 additions & 57 deletions client/app/core/dialog-field-refresh.service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,93 @@ describe('DialogFieldRefresh', () => {
bard.inject('CollectionsApi', 'Notifications', 'DialogFieldRefresh')
})

it('allows a dialog field to be refreshed', () => {
const successResponse = {
result: {
dialog1: {
data_type: 'string',
options: 'options',
read_only: false,
required: false,
values: 'Text'
describe('#refreshDialogField', () => {
it('allows a dialog field to be refreshed', () => {
const successResponse = {
result: {
dialog1: {
data_type: 'string',
options: 'options',
read_only: false,
required: false,
values: 'Text'
}
}
}
}
const collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.resolve(successResponse))
const dialogData = {
'dialog1': 'value1',
'dialog2': 'value2'
}
DialogFieldRefresh.refreshDialogField(dialogData, ['dialog1'], '/test/1', 1234).then((response) => {
expect(collectionsApiSpy).to.have.been.calledWith('/test/1',
1234,
{},
'{"action":"refresh_dialog_fields","resource":{"dialog_fields":{"dialog1":"value1","dialog2":"value2"},"fields":["dialog1"]}}')
const collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.resolve(successResponse))
const dialogData = {
'dialog1': 'value1',
'dialog2': 'value2'
}
const idList = {
dialogId: 1234,
resourceActionId: 4321,
targetId: 3241,
targetType: 'targetType'
}
DialogFieldRefresh.refreshDialogField(dialogData, ['dialog1'], '/test/1', idList).then((response) => {
expect(collectionsApiSpy).to.have.been.calledWith('/test/1',
1234,
{},
'{"action":"refresh_dialog_fields","resource":{"dialog_fields":{"dialog1":"value1","dialog2":"value2"},"fields":["dialog1"], "resource_action_id": "4321", "target_id": "3241", "target_type": "targetType"}}')
})
})
})

it('reports back when a field fails to refresh', () => {
const dialogData = {
'dialog1': 'value1',
'dialog2': 'value2'
}
const failureResponse = {'status': 'failed'}
sinon.stub(CollectionsApi, 'post').returns(Promise.reject(failureResponse))
DialogFieldRefresh.refreshDialogField(dialogData, ['dialog1'], '/test/1', 1234).then((response) => {
it('reports back when a field fails to refresh', () => {
const dialogData = {
'dialog1': 'value1',
'dialog2': 'value2'
}
const idList = {
dialogId: 1234,
resourceActionId: 4321,
targetId: 3241,
targetType: 'targetType'
}
const failureResponse = {'status': 'failed'}
sinon.stub(CollectionsApi, 'post').returns(Promise.reject(failureResponse))
DialogFieldRefresh.refreshDialogField(dialogData, ['dialog1'], '/test/1', idList).then((response) => {

}).catch((err) => {
expect(err).to.eq(failureResponse)
}).catch((err) => {
expect(err).to.eq(failureResponse)
})
})
})
it('can override defaults values for a dialog', () => {
const testDialog = {
dialog_tabs: [
{

describe('#setFieldValueDefaults', () => {
it('can override defaults values for a dialog', () => {
const testDialog = {
dialog_tabs: [
{
dialog_groups: [
{
dialog_fields: [
{'name': 'test1', 'default_value': 'test1'},
{'name': 'test2', 'default_value': 'test2'}
]
}
dialog_fields: [
{'name': 'test1', 'default_value': 'test1'},
{'name': 'test2', 'default_value': 'test2'}
]
}
]
}
]
}
]
}

const testValues = {
dialog_test1: 'modifiedTest1',
dialog_test2: 'modifiedTest2'
}
const testValues = {
dialog_test1: 'modifiedTest1',
dialog_test2: 'modifiedTest2'
}

const expectedDialog = {
'dialog_tabs': [{
'dialog_groups': [{
'dialog_fields': [{
'name': 'test1',
'default_value': 'modifiedTest1'
}, {'name': 'test2', 'default_value': 'modifiedTest2'}]
const expectedDialog = {
'dialog_tabs': [{
'dialog_groups': [{
'dialog_fields': [{
'name': 'test1',
'default_value': 'modifiedTest1'
}, {'name': 'test2', 'default_value': 'modifiedTest2'}]
}]
}]
}]
}
const modifiedDialog = DialogFieldRefresh.setFieldValueDefaults(testDialog, testValues)
expect(modifiedDialog).to.deep.eq(expectedDialog)
}
const modifiedDialog = DialogFieldRefresh.setFieldValueDefaults(testDialog, testValues)
expect(modifiedDialog).to.deep.eq(expectedDialog)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,21 @@ function StateController ($state, $stateParams, dialog, service, CollectionsApi,
vm.dialogData = {}

function refreshField (field) {
return DialogFieldRefresh.refreshDialogField(vm.dialogData, [field.name], vm.dialogUrl, dialog.id)
let targetType = 'service'
let targetId = vm.serviceId
if (vm.vmId) {
targetType = 'vm'
targetId = vm.vmId
}

let idList = {
dialogId: dialog.id,
resourceActionId: $stateParams.button.resource_action.id,
targetId: targetId,
targetType: targetType
}

return DialogFieldRefresh.refreshDialogField(vm.dialogData, [field.name], vm.dialogUrl, idList)
}

function setDialogData (data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,50 @@
/* global $state, $controller, CollectionsApi, Notifications, DialogFieldRefresh */
/* eslint-disable no-unused-expressions */
describe('State: services.custom_button_details', () => {
let dialog, dialogData, dialogFields, button

beforeEach(() => {
module('app.states')
})

describe('controller', () => {
let collectionsApiSpy, controller, notificationsErrorSpy, notificationsSuccessSpy
const dialogFields = [{
dialogFields = [{
name: 'dialogField1',
default_value: '1'
}, {
name: 'dialogField2',
default_value: '2'
}]
const dialog = {

dialog = {
dialog_tabs: [{
dialog_groups: [{
dialog_fields: dialogFields
}]
}]
}
const button = {

button = {
name: 'buttonName',
applies_to_id: 456,
applies_to_class: 'servicetemplate'
applies_to_class: 'servicetemplate',
resource_action: {
id: 789
}
}

dialogData = {
'validations': {
'isValid': true
},
'data': {
'dialogField1': 1,
'dialogField2': 2
}
}
})

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

beforeEach(() => {
bard.inject('$controller', '$log', '$state', '$stateParams', '$rootScope', 'CollectionsApi', 'Notifications', 'DialogFieldRefresh')

Expand All @@ -41,15 +59,6 @@ describe('State: services.custom_button_details', () => {
serviceTemplateCatalogId: 321
}
})
const dialogData = {
'validations': {
'isValid': true
},
'data': {
'dialogField1': 1,
'dialogField2': 2
}
}
controller.setDialogData(dialogData)
})

Expand Down Expand Up @@ -123,26 +132,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,
applies_to_class: 'vm'
}

beforeEach(() => {
bard.inject('$controller', '$log', '$state', '$stateParams', '$rootScope', 'CollectionsApi', 'Notifications', 'DialogFieldRefresh')
sinon.stub(DialogFieldRefresh, 'refreshDialogField')
Expand All @@ -158,15 +147,7 @@ describe('State: services.custom_button_details', () => {
serviceTemplateCatalogId: 321
}
})
const dialogData = {
'validations': {
'isValid': true
},
'data': {
'dialogField1': 1,
'dialogField2': 2
}
}

controller.setDialogData(dialogData)
})
it('POSTs to the vms API', (done) => {
Expand All @@ -193,4 +174,69 @@ describe('State: services.custom_button_details', () => {
expect($state.is('services.resource-details')).to.be.true
})
})

describe('controller#refreshField', () => {
let controller

describe('when the vmId does not exist', () => {
beforeEach(() => {
bard.inject('$controller', '$state', '$stateParams', 'CollectionsApi', 'Notifications', 'DialogFieldRefresh')
sinon.stub(DialogFieldRefresh, 'refreshDialogField')

controller = $controller($state.get('services.custom_button_details').controller, {
dialog: {content: [dialog], id: 213},
service: {},
$stateParams: {
dialogId: 213,
button: button,
serviceId: 123,
serviceTemplateCatalogId: 321
}
})

controller.setDialogData(dialogData)
})

it('delegates to DialogFieldRefresh with the right id list', () => {
controller.refreshField({name: 'fieldName'})
expect(DialogFieldRefresh.refreshDialogField).to.have.been.calledWith(
dialogData.data,
['fieldName'],
'service_dialogs/',
{dialogId: 213, resourceActionId: 789, targetId: 123, targetType: 'service'}
)
})
})

describe('when the vmId exists', () => {
beforeEach(() => {
bard.inject('$controller', '$state', '$stateParams', 'CollectionsApi', 'Notifications', 'DialogFieldRefresh')
sinon.stub(DialogFieldRefresh, 'refreshDialogField')

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

controller.setDialogData(dialogData)
})

it('delegates to DialogFieldRefresh with the right id list', () => {
controller.refreshField({name: 'fieldName'})
expect(DialogFieldRefresh.refreshDialogField).to.have.been.calledWith(
dialogData.data,
['fieldName'],
'service_dialogs/',
{dialogId: 213, resourceActionId: 789, targetId: 456, targetType: 'vm'}
)
})
})
})
})

0 comments on commit 6acbcf5

Please sign in to comment.