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

BZ#1518390-Pass additional information to the API when refreshing a dialog field #1324

Merged
merged 1 commit into from
Jan 17, 2018
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
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
135 changes: 74 additions & 61 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,89 @@ 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: [
{
dialog_groups: [
{
dialog_fields: [
{'name': 'test1', 'default_value': 'test1'},
{'name': 'test2', 'default_value': 'test2'}
]
}
]
}
]
}

const testValues = {
dialog_test1: 'modifiedTest1',
dialog_test2: 'modifiedTest2'
}
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'}
]
}]
}]
}

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 @@ -32,12 +32,13 @@ function getStates () {
function StateController ($state, $stateParams, CollectionsApi, EventNotifications, DialogFieldRefresh) {
var vm = this
vm.title = __('Custom button action')
vm.dialogId = ''
vm.dialogId = $stateParams.dialogId || ''
vm.dialogs = {}
vm.service = {}
vm.serviceId = $stateParams.serviceId
vm.vmId = $stateParams.vmId || null
vm.button = $stateParams.button
vm.resourceAction = vm.button.resource_action
vm.submitCustomButton = submitCustomButton
vm.submitButtonEnabled = false
vm.dialogUrl = 'service_dialogs/'
Expand All @@ -48,7 +49,7 @@ function StateController ($state, $stateParams, CollectionsApi, EventNotificatio

function init () {
const options = {expand: 'resources', attributes: 'content'}
const dialogId = vm.button.resource_action.dialog_id
const dialogId = vm.resourceAction.dialog_id
const resolveDialogs = CollectionsApi.query('service_dialogs/' + dialogId, options)
const resolveService = CollectionsApi.get('services', $stateParams.serviceId, {attributes: ['picture', 'picture.image_href']})

Expand All @@ -63,7 +64,21 @@ function StateController ($state, $stateParams, CollectionsApi, EventNotificatio
}
init()
function refreshField (field) {
return DialogFieldRefresh.refreshDialogField(vm.dialogData, [field.name], vm.dialogUrl, vm.dialogId)
let targetType = 'service'
let targetId = vm.serviceId
if (vm.vmId) {
targetType = 'vm'
targetId = vm.vmId
}

let idList = {
dialogId: vm.dialogId,
resourceActionId: vm.resourceAction.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,35 +1,51 @@
/* 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',
resource_action: {
dialog_id: 1
dialog_id: 1,
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 @@ -46,15 +62,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 @@ -147,7 +154,8 @@ describe('State: services.custom_button_details', () => {
applies_to_id: 456,
applies_to_class: 'vm',
resource_action: {
dialog_id: 1
dialog_id: 1,
id: 789
}
}

Expand All @@ -168,15 +176,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 @@ -203,4 +203,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'}
)
})
})
})
})