From 91917886a852e203dbefbcfa8f9927f403b91687 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Sun, 25 Sep 2016 14:27:33 +1000 Subject: [PATCH] Adding basic routing and templates for standing orders index --- .../admin/standing_orders_controller.rb | 4 + app/models/spree/ability_decorator.rb | 2 +- .../admin/standing_orders/index.html.haml | 4 + config/locales/en.yml | 1 + config/routes.rb | 2 +- .../admin/standing_orders_controller_spec.rb | 34 +++++ spec/features/admin/standing_orders_spec.rb | 131 ++++++++++-------- 7 files changed, 116 insertions(+), 62 deletions(-) create mode 100644 app/views/admin/standing_orders/index.html.haml diff --git a/app/controllers/admin/standing_orders_controller.rb b/app/controllers/admin/standing_orders_controller.rb index e28e0f10ea..28303e9f4f 100644 --- a/app/controllers/admin/standing_orders_controller.rb +++ b/app/controllers/admin/standing_orders_controller.rb @@ -23,6 +23,10 @@ module Admin private + def collection + StandingOrder.where("1=0") + end + def load_shop @shop = Enterprise.find(params[:shop_id]) end diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index d42a7ec041..fad7540cb6 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -252,7 +252,7 @@ class AbilityDecorator can [:create], Customer can [:admin, :index, :update, :destroy], Customer, enterprise_id: Enterprise.managed_by(user).pluck(:id) - can [:admin, :new], StandingOrder + can [:admin, :new, :index], StandingOrder can [:create], StandingOrder do |standing_order| user.enterprises.include?(standing_order.shop) end diff --git a/app/views/admin/standing_orders/index.html.haml b/app/views/admin/standing_orders/index.html.haml new file mode 100644 index 0000000000..84536a2679 --- /dev/null +++ b/app/views/admin/standing_orders/index.html.haml @@ -0,0 +1,4 @@ +- content_for :page_title do + = t('admin.standing_orders.standing_orders') + +Listing Standing Orders diff --git a/config/locales/en.yml b/config/locales/en.yml index a03c624683..92b9336875 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -790,6 +790,7 @@ en: name: Packing Reports standing_orders: + standing_orders: Standing Orders new: New Standing Order create: Create Standing Order steps: diff --git a/config/routes.rb b/config/routes.rb index 4b856e86c7..73184d903b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -177,7 +177,7 @@ Openfoodnetwork::Application.routes.draw do resources :schedules, only: [:index, :create, :update, :destroy], format: :json - resources :standing_orders, only: [:new, :create] + resources :standing_orders, only: [:index, :new, :create] resources :standing_line_items, only: [], format: :json do post :build, on: :collection diff --git a/spec/controllers/admin/standing_orders_controller_spec.rb b/spec/controllers/admin/standing_orders_controller_spec.rb index ed7fa7216d..2a53560351 100644 --- a/spec/controllers/admin/standing_orders_controller_spec.rb +++ b/spec/controllers/admin/standing_orders_controller_spec.rb @@ -3,6 +3,40 @@ require 'spec_helper' describe Admin::StandingOrdersController, type: :controller do include AuthenticationWorkflow + + describe 'index' do + context 'html' do + let(:params) { { format: :html } } + + context 'as an regular user' do + let!(:user) { create(:user) } + + before do + allow(controller).to receive(:spree_current_user) { user } + end + + it 'renders the index page' do + spree_get :index, params + expect(response).to redirect_to spree.unauthorized_path + end + end + + context 'as an enterprise user' do + let!(:user) { create(:user) } + let!(:enterprise) { create(:enterprise, owner: user) } + + before do + allow(controller).to receive(:spree_current_user) { user } + end + + it 'renders the index page' do + spree_get :index, params + expect(response).to render_template 'index' + end + end + end + end + describe 'new' do let!(:user) { create(:user) } let!(:shop) { create(:distributor_enterprise, owner: user) } diff --git a/spec/features/admin/standing_orders_spec.rb b/spec/features/admin/standing_orders_spec.rb index abf1f33b76..a4c919fcd2 100644 --- a/spec/features/admin/standing_orders_spec.rb +++ b/spec/features/admin/standing_orders_spec.rb @@ -7,75 +7,86 @@ feature 'Standing Orders' do context "as an enterprise user", js: true do let!(:user) { create_enterprise_user(enterprise_limit: 10) } let!(:shop) { create(:distributor_enterprise, owner: user) } - let!(:customer) { create(:customer, enterprise: shop) } - let!(:product) { create(:product, supplier: shop) } - let!(:variant) { create(:variant, product: product, unit_value: '100', price: 12.00, option_values: []) } - let!(:enterprise_fee) { create(:enterprise_fee, amount: 1.75) } - let!(:order_cycle) { create(:simple_order_cycle, coordinator: shop, orders_open_at: 2.days.from_now, orders_close_at: 7.days.from_now) } - let!(:outgoing_exchange) { order_cycle.exchanges.create(sender: shop, receiver: shop, variants: [variant], enterprise_fees: [enterprise_fee]) } - let!(:schedule) { create(:schedule, order_cycles: [order_cycle]) } - let!(:payment_method) { create(:payment_method, distributors: [shop]) } - let!(:shipping_method) { create(:shipping_method, distributors: [shop]) } before { quick_login_as user } - it "I can create a new standing order" do - visit new_admin_standing_order_path(shop_id: shop.id) + context 'listing standing orders' do + it "passes the smoke test" do + visit admin_standing_orders_path - select2_select customer.email, from: 'customer_id' - select2_select schedule.name, from: 'schedule_id' - select2_select payment_method.name, from: 'payment_method_id' - select2_select shipping_method.name, from: 'shipping_method_id' - - # No date filled out, so error returned - click_button('Next') - expect(page).to have_content 'can\'t be blank' - expect(page).to have_content 'Oops! Please fill in all of the required fields...' - fill_in 'begins_at', with: Date.today.strftime('%F') - - click_button('Next') - expect(page).to have_content 'NAME OR SKU' - click_button('Next') - expect(page).to have_content 'Please add at least one product' - - # Adding a product and getting a price estimate - targetted_select2_search product.name, from: '#add_variant_id', dropdown_css: '.select2-drop' - fill_in 'add_quantity', with: 2 - click_link 'Add' - within 'table#standing-line-items tr.item', match: :first do - expect(page).to have_selector 'td.description', text: "#{product.name} - #{variant.full_name}" - expect(page).to have_selector 'td.price', text: "$13.75" - expect(page).to have_input 'quantity', with: "2" - expect(page).to have_selector 'td.total', text: "$27.50" + expect(page).to have_content 'Listing Standing Orders' end + end - click_button('Next') + context 'creating a new standing order' do + let!(:customer) { create(:customer, enterprise: shop) } + let!(:product) { create(:product, supplier: shop) } + let!(:variant) { create(:variant, product: product, unit_value: '100', price: 12.00, option_values: []) } + let!(:enterprise_fee) { create(:enterprise_fee, amount: 1.75) } + let!(:order_cycle) { create(:simple_order_cycle, coordinator: shop, orders_open_at: 2.days.from_now, orders_close_at: 7.days.from_now) } + let!(:outgoing_exchange) { order_cycle.exchanges.create(sender: shop, receiver: shop, variants: [variant], enterprise_fees: [enterprise_fee]) } + let!(:schedule) { create(:schedule, order_cycles: [order_cycle]) } + let!(:payment_method) { create(:payment_method, distributors: [shop]) } + let!(:shipping_method) { create(:shipping_method, distributors: [shop]) } - expect{ - click_button('Create Standing Order') - expect(page).to have_content 'Saved' - }.to change(StandingOrder, :count).by(1) + it "passes the smoke test" do + visit new_admin_standing_order_path(shop_id: shop.id) - # Prices are shown - within 'table#standing-line-items tr.item', match: :first do - expect(page).to have_selector 'td.description', text: "#{product.name} - #{variant.full_name}" - expect(page).to have_selector 'td.price', text: "$13.75" - expect(page).to have_input 'quantity', with: "2" - expect(page).to have_selector 'td.total', text: "$27.50" + select2_select customer.email, from: 'customer_id' + select2_select schedule.name, from: 'schedule_id' + select2_select payment_method.name, from: 'payment_method_id' + select2_select shipping_method.name, from: 'shipping_method_id' + + # No date filled out, so error returned + click_button('Next') + expect(page).to have_content 'can\'t be blank' + expect(page).to have_content 'Oops! Please fill in all of the required fields...' + fill_in 'begins_at', with: Date.today.strftime('%F') + + click_button('Next') + expect(page).to have_content 'NAME OR SKU' + click_button('Next') + expect(page).to have_content 'Please add at least one product' + + # Adding a product and getting a price estimate + targetted_select2_search product.name, from: '#add_variant_id', dropdown_css: '.select2-drop' + fill_in 'add_quantity', with: 2 + click_link 'Add' + within 'table#standing-line-items tr.item', match: :first do + expect(page).to have_selector 'td.description', text: "#{product.name} - #{variant.full_name}" + expect(page).to have_selector 'td.price', text: "$13.75" + expect(page).to have_input 'quantity', with: "2" + expect(page).to have_selector 'td.total', text: "$27.50" + end + + click_button('Next') + + expect{ + click_button('Create Standing Order') + expect(page).to have_content 'Saved' + }.to change(StandingOrder, :count).by(1) + + # Prices are shown + within 'table#standing-line-items tr.item', match: :first do + expect(page).to have_selector 'td.description', text: "#{product.name} - #{variant.full_name}" + expect(page).to have_selector 'td.price', text: "$13.75" + expect(page).to have_input 'quantity', with: "2" + expect(page).to have_selector 'td.total', text: "$27.50" + end + + # Basic properties of standing order are set + standing_order = StandingOrder.last + expect(standing_order.customer).to eq customer + expect(standing_order.schedule).to eq schedule + expect(standing_order.payment_method).to eq payment_method + expect(standing_order.shipping_method).to eq shipping_method + + # Standing Line Items are created + expect(standing_order.standing_line_items.count).to eq 1 + standing_line_item = standing_order.standing_line_items.first + expect(standing_line_item.variant).to eq variant + expect(standing_line_item.quantity).to eq 2 end - - # Basic properties of standing order are set - standing_order = StandingOrder.last - expect(standing_order.customer).to eq customer - expect(standing_order.schedule).to eq schedule - expect(standing_order.payment_method).to eq payment_method - expect(standing_order.shipping_method).to eq shipping_method - - # Standing Line Items are created - expect(standing_order.standing_line_items.count).to eq 1 - standing_line_item = standing_order.standing_line_items.first - expect(standing_line_item.variant).to eq variant - expect(standing_line_item.quantity).to eq 2 end end end