Remove unrequired #cards and #addresses actions from Admin::CustomerController

This commit is contained in:
Rob Harrington
2018-05-10 18:35:41 +10:00
committed by Maikel Linke
parent e0d46aa105
commit 21c3f7d21c
4 changed files with 2 additions and 118 deletions

View File

@@ -59,22 +59,6 @@ module Admin
end
end
# GET /admin/customers/:id/addresses
# Used by subscriptions form to load details for selected customer
def addresses
finder = OpenFoodNetwork::AddressFinder.new(@customer, @customer.email)
bill_address = Api::AddressSerializer.new(finder.bill_address).serializable_hash
ship_address = Api::AddressSerializer.new(finder.ship_address).serializable_hash
render json: { bill_address: bill_address, ship_address: ship_address }
end
# GET /admin/customers/:id/cards
# Used by subscriptions form to load details for selected customer
def cards
cards = Spree::CreditCard.where(user_id: @customer.user_id)
render json: ActiveModel::ArraySerializer.new(cards, each_serializer: Api::CreditCardSerializer)
end
private
def collection

View File

@@ -257,7 +257,7 @@ class AbilityDecorator
can [:admin, :index, :customers, :group_buys, :bulk_coop, :sales_tax, :payments, :orders_and_distributors, :orders_and_fulfillment, :products_and_inventory, :order_cycle_management, :xero_invoices], :report
can [:create], Customer
can [:admin, :index, :update, :destroy, :addresses, :cards, :show], Customer, enterprise_id: Enterprise.managed_by(user).pluck(:id)
can [:admin, :index, :update, :destroy, :show], Customer, enterprise_id: Enterprise.managed_by(user).pluck(:id)
can [:admin, :new, :index], Subscription
can [:create, :edit, :update, :cancel, :pause, :unpause], Subscription do |subscription|
user.enterprises.include?(subscription.shop)

View File

@@ -147,10 +147,7 @@ Openfoodnetwork::Application.routes.draw do
resources :inventory_items, only: [:create, :update]
resources :customers, only: [:index, :create, :update, :destroy, :show] do
get :addresses, on: :member
get :cards, on: :member
end
resources :customers, only: [:index, :create, :update, :destroy, :show]
resources :tag_rules, only: [], format: :json do
get :map_by_tag, on: :collection

View File

@@ -139,103 +139,6 @@ describe Admin::CustomersController, type: :controller do
end
end
describe "#addresses" do
let!(:enterprise) { create(:enterprise) }
let(:bill_address) { create(:address, firstname: "Dominic", address1: "123 Lala Street" ) }
let(:ship_address) { create(:address, firstname: "Dom", address1: "123 Sesame Street") }
let(:managed_customer) { create(:customer, enterprise: enterprise, bill_address: bill_address, ship_address: ship_address) }
let(:unmanaged_customer) { create(:customer) }
let(:params) { { format: :json } }
before { login_as_enterprise_user [enterprise] }
context "when I manage the customer" do
before { params.merge!(id: managed_customer.id) }
it "returns with serialized addresses for the customer" do
spree_get :addresses, params
json_response = JSON.parse(response.body)
expect(json_response.keys).to include "bill_address", "ship_address"
expect(json_response["bill_address"]["firstname"]).to eq "Dominic"
expect(json_response["bill_address"]["address1"]).to eq "123 Lala Street"
expect(json_response["ship_address"]["firstname"]).to eq "Dom"
expect(json_response["ship_address"]["address1"]).to eq "123 Sesame Street"
end
end
context "when I don't manage the customer" do
before { params.merge!(customer_id: unmanaged_customer.id) }
it "redirects to unauthorised" do
spree_get :addresses, params
expect(response).to redirect_to spree.unauthorized_path
end
end
context "when no customer with a matching id exists" do
before { params.merge!(customer_id: 1) }
it "redirects to unauthorised" do
spree_get :addresses, params
expect(response).to redirect_to spree.unauthorized_path
end
end
end
describe "#cards" do
let(:user) { create(:user) }
let!(:enterprise) { create(:enterprise) }
let!(:credit_card1) { create(:credit_card, user: user) }
let!(:credit_card2) { create(:credit_card) }
let(:managed_customer) { create(:customer, enterprise: enterprise) }
let(:unmanaged_customer) { create(:customer) }
let(:params) { { format: :json } }
before { login_as_enterprise_user [enterprise] }
context "when I manage the customer" do
before { params.merge!(id: managed_customer.id) }
context "when the customer is not associated with a user" do
it "returns with an empty array" do
spree_get :cards, params
json_response = JSON.parse(response.body)
expect(json_response).to eq []
end
end
context "when the customer is associated with a user" do
before { managed_customer.update_attributes(user_id: user.id) }
it "returns with serialized cards for the customer" do
spree_get :cards, params
json_response = JSON.parse(response.body)
expect(json_response).to be_an Array
expect(json_response.length).to be 1
expect(json_response.first["id"]).to eq credit_card1.id
end
end
end
context "when I don't manage the customer" do
before { params.merge!(customer_id: unmanaged_customer.id) }
it "redirects to unauthorised" do
spree_get :cards, params
expect(response).to redirect_to spree.unauthorized_path
end
end
context "when no customer with a matching id exists" do
before { params.merge!(customer_id: 1) }
it "redirects to unauthorised" do
spree_get :cards, params
expect(response).to redirect_to spree.unauthorized_path
end
end
end
describe "show" do
let(:enterprise) { create(:distributor_enterprise) }
let(:another_enterprise) { create(:distributor_enterprise) }