Skip to content
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: 9 additions & 0 deletions app/controllers/api/custom_button_sets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,14 @@ def reorder_resource(type, id, data)
action_result(true, "Button Group Reorder saved")
end
end

def add_custom_button_resource(type, id, data)
api_action(type, id) do
raise BadRequestError, "Must specify a button_id" unless data["button_id"]

resource_search(id, type).add_button(data["button_id"])
action_result(true, "Button assigned to group")
end
end
end
end
9 changes: 9 additions & 0 deletions app/controllers/api/service_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ def unarchive_resource(type, id, _data)
action_result(false, "Could not unarchive Service Template - #{err}")
end

def add_custom_button_resource(type, id, data)
api_action(type, id) do
raise BadRequestError, "Must specify a button_id" unless data["button_id"]

resource_search(id, type).add_custom_button(data["button_id"])
action_result(true, "Button added to Service Template order")
end
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As discussed in chat, we found that api_action handles the rescue and re-raise so it's actually a better pattern to follow. So, I'm 👍 on this.


private

def set_additional_attributes
Expand Down
4 changes: 4 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,8 @@
:identifier: ab_group_edit
- :name: delete
:identifier: ab_group_delete
- :name: add_custom_button
:identifier: ab_button_new
:custom_buttons:
:description: Custom Buttons
:identifier: ab_buttons_accord
Expand Down Expand Up @@ -4034,6 +4036,8 @@
:identifier: svc_catalog_archive
- :name: unarchive
:identifier: svc_catalog_unarchive
- :name: add_custom_button
:identifier: ab_button_new
- :name: set_ownership
:identifier: catalogitem_ownership
:subresource_actions:
Expand Down
49 changes: 49 additions & 0 deletions spec/requests/custom_button_sets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@
end

describe 'POST /api/custom_button_sets/:id' do
let(:button) { FactoryBot.create(:custom_button, :applies_to_class => 'Service') }

it 'can update a custom button set by id' do
api_basic_authorize action_identifier(:custom_button_sets, :edit)

Expand All @@ -144,6 +146,53 @@
expect(response.parsed_body).to include(request.except('action'))
end

it 'can add a custom button to a custom button set' do
api_basic_authorize action_identifier(:custom_button_sets, :add_custom_button, :resource_actions, :post)

post(api_custom_button_set_url(nil, cb_set), :params => {:action => 'add_custom_button', :button_id => button.id})

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include('message' => 'Button assigned to group')
expect(cb_set.reload.set_data[:button_order]).to include(button.id)
end

it 'is idempotent when adding a custom button to a custom button set' do
api_basic_authorize action_identifier(:custom_button_sets, :add_custom_button, :resource_actions, :post)

2.times do
post(api_custom_button_set_url(nil, cb_set), :params => {:action => 'add_custom_button', :button_id => button.id})
expect(response).to have_http_status(:ok)
end

expect(cb_set.reload.set_data[:button_order]).to eq([button.id])
end

it 'is forbidden to add a custom button to a custom button set without the appropriate role' do
api_basic_authorize

post(api_custom_button_set_url(nil, cb_set), :params => {:action => 'add_custom_button', :button_id => button.id})

expect(response).to have_http_status(:forbidden)
end

it 'fails for an invalid custom button set id when adding a custom button' do
api_basic_authorize action_identifier(:custom_button_sets, :add_custom_button, :resource_actions, :post)

post(api_custom_button_set_url(nil, 999_999), :params => {:action => 'add_custom_button', :button_id => button.id})

expect(response).to have_http_status(:not_found)
end

it 'fails when adding a custom button without a button_id' do
api_basic_authorize action_identifier(:custom_button_sets, :add_custom_button, :resource_actions, :post)

post(api_custom_button_set_url(nil, cb_set), :params => {:action => 'add_custom_button'})

expect(response).to have_http_status(:bad_request)
Comment thread
ohhAndy marked this conversation as resolved.
Resolved
expect(response.parsed_body['error']).to include('message' => a_string_including('Must specify a button_id'))
expect(cb_set.reload.set_data[:button_order]).to be_blank
end

it 'can delete a custom button set by id' do
api_basic_authorize action_identifier(:custom_button_sets, :delete)

Expand Down
52 changes: 52 additions & 0 deletions spec/requests/service_templates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,58 @@
end
end

describe "Service Templates add custom button" do
let(:service_template) { FactoryBot.create(:service_template, :with_provision_resource_action_and_dialog) }
let(:button) { FactoryBot.create(:custom_button, :applies_to => service_template, :userid => @user.userid) }

it "can add a custom button to a service template" do
api_basic_authorize action_identifier(:service_templates, :add_custom_button, :resource_actions, :post)

post(api_service_template_url(nil, service_template), :params => {:action => "add_custom_button", :button_id => button.id})

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include("message" => "Button added to Service Template order")
expect(service_template.reload.options[:button_order]).to include("cb-#{button.id}")
end

it "is idempotent when adding a custom button to a service template" do
api_basic_authorize action_identifier(:service_templates, :add_custom_button, :resource_actions, :post)

2.times do
post(api_service_template_url(nil, service_template), :params => {:action => "add_custom_button", :button_id => button.id})
expect(response).to have_http_status(:ok)
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's good we don't add it multiple times but I'm curious if subsequent "add button" requests should fail if it's already added. I'm not sure what we do with other API endpoints when adding something already added, so it might be better to review later on and see if it makes sense based on other patterns we have.


expect(service_template.reload.options[:button_order]).to eq(["cb-#{button.id}"])
end

it "is forbidden without appropriate role" do
api_basic_authorize

post(api_service_template_url(nil, service_template), :params => {:action => "add_custom_button", :button_id => button.id})

expect(response).to have_http_status(:forbidden)
end

it "fails for an invalid service template id" do
api_basic_authorize action_identifier(:service_templates, :add_custom_button, :resource_actions, :post)

post(api_service_template_url(nil, 999_999), :params => {:action => "add_custom_button", :button_id => button.id})

expect(response).to have_http_status(:not_found)
end

it "fails when adding a custom button without a button_id" do
api_basic_authorize action_identifier(:service_templates, :add_custom_button, :resource_actions, :post)

post(api_service_template_url(nil, service_template), :params => {:action => "add_custom_button"})

expect(response).to have_http_status(:bad_request)
expect(response.parsed_body["error"]).to include("message" => a_string_including("Must specify a button_id"))
expect(service_template.reload.options[:button_order]).to be_blank
end
end

context "schedules subcollection" do
let!(:service_template) { FactoryBot.create(:service_template, :with_provision_resource_action_and_dialog) }

Expand Down
Loading