From d3a542727cec9bee80a64b94512fa4ee966bab75 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Fri, 15 Feb 2019 08:40:19 +1300 Subject: [PATCH] Fixing crop deleting and adding tests --- app/controllers/crops_controller.rb | 2 + spec/controllers/crops_controller_spec.rb | 51 +++++++++++++++-------- spec/features/crops/delete_crop_spec.rb | 23 ++++++++++ spec/features/crops/show_spec.rb | 4 +- 4 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 spec/features/crops/delete_crop_spec.rb diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 73019a827..a0232f8a6 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -104,6 +104,8 @@ class CropsController < ApplicationController end def destroy + @crop = Crop.find_by!(slug: params[:slug]) + authorize! :destroy, @crop @crop.destroy respond_with @crop end diff --git a/spec/controllers/crops_controller_spec.rb b/spec/controllers/crops_controller_spec.rb index ad011da74..112d768cd 100644 --- a/spec/controllers/crops_controller_spec.rb +++ b/spec/controllers/crops_controller_spec.rb @@ -1,34 +1,36 @@ require 'rails_helper' describe CropsController do - login_member(:crop_wrangling_member) - - def valid_attributes - { - name: "Tomato", - en_wikipedia_url: 'http://en.wikipedia.org/wiki/Tomato', - approval_status: 'approved' - } + shared_context 'login as wrangler' do + login_member(:crop_wrangling_member) end subject { response } describe "GET crop wrangler homepage" do describe 'fetches the crop wrangler homepage' do - before { get :wrangle } - - it { is_expected.to be_success } - it { is_expected.to render_template("crops/wrangle") } - it { expect(assigns[:crop_wranglers]).to eq(Role.crop_wranglers) } + context 'anonymous' do + before { get :wrangle } + it { is_expected.not_to be_success } + end + context 'wrangler' do + include_context 'login as wrangler' + before { get :wrangle } + it { is_expected.to be_success } + it { is_expected.to render_template("crops/wrangle") } + it { expect(assigns[:crop_wranglers]).to eq(Role.crop_wranglers) } + end end end describe "GET crop hierarchy " do describe 'fetches the crop hierarchy page' do - before { get :hierarchy } - - it { is_expected.to be_success } - it { is_expected.to render_template("crops/hierarchy") } + context 'wrangler' do + include_context 'login as wrangler' + before { get :hierarchy } + it { is_expected.to be_success } + it { is_expected.to render_template("crops/hierarchy") } + end end end @@ -50,4 +52,19 @@ describe CropsController do it { expect(response.content_type).to eq("application/rss+xml") } end end + + describe 'DELETE destroy' do + let!(:crop) { FactoryBot.create :crop } + subject { delete :destroy, params: { slug: crop.to_param } } + context 'not logged in' do + it { expect { subject }.not_to change { Crop.count } } + end + context 'logged in as member' do + it { expect { subject }.not_to change { Crop.count } } + end + context 'wrangler' do + include_context 'login as wrangler' + it { expect { subject }.to change { Crop.count }.by -1 } + end + end end diff --git a/spec/features/crops/delete_crop_spec.rb b/spec/features/crops/delete_crop_spec.rb new file mode 100644 index 000000000..6890c07f8 --- /dev/null +++ b/spec/features/crops/delete_crop_spec.rb @@ -0,0 +1,23 @@ +require 'rails_helper' + +feature "Delete crop spec" do + context "As a crop wrangler" do + let(:wrangler) { FactoryBot.create :crop_wrangling_member } + let!(:pending_crop) { FactoryBot.create :crop_request } + let!(:approved_crop) { FactoryBot.create :crop } + + background { login_as wrangler } + + scenario "Delete approved crop" do + visit crop_path(approved_crop) + click_link 'Delete' + expect(page).to have_content "crop was successfully destroyed" + end + + scenario "Delete pending crop" do + visit crop_path(pending_crop) + click_link 'Delete' + expect(page).to have_content "crop was successfully destroyed" + end + end +end diff --git a/spec/features/crops/show_spec.rb b/spec/features/crops/show_spec.rb index 245e60b00..e7bfc0358 100644 --- a/spec/features/crops/show_spec.rb +++ b/spec/features/crops/show_spec.rb @@ -15,7 +15,7 @@ feature "browse crops" do before { FactoryBot.create_list :harvest, 20, crop: tomato, harvested_at: 1.year.ago, created_at: 1.minute.ago } let!(:most_recent_harvest) do - FactoryBot.create :harvest, crop: tomato, harvested_at: 60.minutes.ago, created_at: 10.minute.ago + FactoryBot.create :harvest, crop: tomato, harvested_at: 60.minutes.ago, created_at: 10.minutes.ago end scenario "Shows most recently harvested harvest" do @@ -28,7 +28,7 @@ feature "browse crops" do before { FactoryBot.create_list :planting, 20, crop: tomato, planted_at: 1.year.ago, created_at: 1.minute.ago } let!(:most_recent_planting) do - FactoryBot.create :planting, crop: tomato, planted_at: 60.minutes.ago, created_at: 10.minute.ago + FactoryBot.create :planting, crop: tomato, planted_at: 60.minutes.ago, created_at: 10.minutes.ago end scenario "Shows most recently planted planting" do