-
-
Notifications
You must be signed in to change notification settings - Fork 731
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
6. Add webhook endpoints to user developer settings screen
Allowing creation and deleting via the user association. It probably won't be much effort to allow editing and multiple records, but I cut it down to the minimum needed to start with. I couldn't find a way to test a failure in the destroy method, but decided to keep the condition because I thought it was worth having.
- Loading branch information
Showing
10 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
class WebhookEndpointsController < ::BaseController | ||
before_action :load_resource, only: :destroy | ||
|
||
def create | ||
webhook_endpoint = spree_current_user.webhook_endpoints.create(webhook_endpoint_params) | ||
|
||
if webhook_endpoint.save | ||
flash[:success] = t('.success') | ||
else | ||
flash[:error] = t('.error') | ||
end | ||
respond_to do |format| | ||
format.html { redirect_to redirect_path } | ||
end | ||
end | ||
|
||
def destroy | ||
if @webhook_endpoint.destroy | ||
flash[:success] = t('.success') | ||
else | ||
flash[:error] = t('.error') | ||
end | ||
respond_to do |format| | ||
format.html { redirect_to redirect_path } | ||
end | ||
end | ||
|
||
def load_resource | ||
@webhook_endpoint = WebhookEndpoint.where(user_id: spree_current_user.id).find(params[:id]) | ||
end | ||
|
||
def webhook_endpoint_params | ||
params.require(:webhook_endpoint).permit(::PermittedAttributes::WebhookEndpoint.attributes) | ||
end | ||
|
||
def redirect_path | ||
if request.referer.blank? || request.referer.include?(spree.account_path) | ||
developer_settings_path | ||
else | ||
request.referer | ||
end | ||
end | ||
|
||
def developer_settings_path | ||
"#{spree.account_path}#/developer_settings" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module PermittedAttributes | ||
class WebhookEndpoint | ||
def self.attributes | ||
[ | ||
:url | ||
] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
%script{ type: "text/ng-template", id: "account/developer_settings.html" } | ||
%h3= t('.title') | ||
= render partial: 'api_keys' | ||
= render partial: 'webhook_endpoints' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
%section{ id: "webhook_endpoints" } | ||
%hr | ||
%h3= t('.title') | ||
%p= t('.description') | ||
|
||
%table{width: "100%"} | ||
%thead | ||
%tr | ||
%th= t('.event_type.header') | ||
%th= t('.url.header') | ||
%tbody | ||
- @user.webhook_endpoints.each do |webhook_endpoint| | ||
%tr | ||
%td= t('.event_types.order_cycle_opened') # For now, we only support one type. | ||
%td= webhook_endpoint.url | ||
%td.actions | ||
= button_to I18n.t(:delete), account_webhook_endpoint_path(webhook_endpoint), method: :delete, | ||
data: { confirm: I18n.t(:are_you_sure)} if webhook_endpoint.persisted? | ||
|
||
-# Create new | ||
- if @user.webhook_endpoints.empty? | ||
%tr | ||
%td= t('.event_types.order_cycle_opened') # For now, we only support one type. | ||
%td | ||
= form_for(@user.webhook_endpoints.build, url: account_webhook_endpoints_path) do |f| | ||
= f.url_field :url, placeholder: t('.url.create_placeholder'), required: true | ||
= f.submit t(:create), :class => 'button primary' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: false | ||
|
||
require 'spec_helper' | ||
require 'open_food_network/order_cycle_permissions' | ||
|
||
describe WebhookEndpointsController, type: :controller do | ||
let(:user) { create(:admin_user) } | ||
|
||
before { allow(controller).to receive(:spree_current_user) { user } } | ||
|
||
describe "#create" do | ||
it "creates a webhook_endpoint" do | ||
expect { | ||
spree_post :create, { url: "https://url" } | ||
}.to change { | ||
user.webhook_endpoints.count | ||
}.by(1) | ||
|
||
expect(flash[:success]).to be_present | ||
expect(flash[:error]).to be_blank | ||
expect(user.webhook_endpoints.first.url).to eq "https://url" | ||
end | ||
|
||
it "shows error if parameters not specified" do | ||
expect { | ||
spree_post :create, { url: "" } | ||
}.to_not change { | ||
user.webhook_endpoints.count | ||
} | ||
|
||
expect(flash[:success]).to be_blank | ||
expect(flash[:error]).to be_present | ||
end | ||
|
||
it "redirects back to referrer" do | ||
spree_post :create, { url: "https://url" } | ||
|
||
expect(response).to redirect_to "/account#/developer_settings" | ||
end | ||
end | ||
|
||
describe "#destroy" do | ||
let!(:webhook_endpoint) { user.webhook_endpoints.create(url: "https://url") } | ||
|
||
it "destroys a webhook_endpoint" do | ||
webhook_endpoint2 = user.webhook_endpoints.create!(url: "https://url2") | ||
|
||
expect { | ||
spree_delete :destroy, { id: webhook_endpoint.id } | ||
}.to change { | ||
user.webhook_endpoints.count | ||
}.by(-1) | ||
|
||
expect(flash[:success]).to be_present | ||
expect(flash[:error]).to be_blank | ||
|
||
expect{ webhook_endpoint.reload }.to raise_error(ActiveRecord::RecordNotFound) | ||
expect(webhook_endpoint2.reload).to be_present | ||
end | ||
|
||
it "redirects back to developer settings tab" do | ||
spree_delete :destroy, id: webhook_endpoint.id | ||
|
||
expect(response).to redirect_to "/account#/developer_settings" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters