From 5f73f2853a9d6b1fe701ad9c32f571cde4f7c426 Mon Sep 17 00:00:00 2001 From: Christa Hartsock Date: Tue, 30 Jun 2020 22:12:49 -0700 Subject: [PATCH] Raise 404 if no district for meeting --- app/controllers/admin/meetings_controller.rb | 2 +- .../admin/meetings_controller_spec.rb | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin/meetings_controller.rb b/app/controllers/admin/meetings_controller.rb index 850020a..4a5d2b4 100644 --- a/app/controllers/admin/meetings_controller.rb +++ b/app/controllers/admin/meetings_controller.rb @@ -55,7 +55,7 @@ def destroy attr_reader :district def set_police_district - @district = PoliceDistrict.find_by_slug(params[:police_district_id]) + @district = PoliceDistrict.find_by_slug!(params[:police_district_id]) end def meeting_params diff --git a/spec/controllers/admin/meetings_controller_spec.rb b/spec/controllers/admin/meetings_controller_spec.rb index 3818763..0db9d2a 100644 --- a/spec/controllers/admin/meetings_controller_spec.rb +++ b/spec/controllers/admin/meetings_controller_spec.rb @@ -3,6 +3,7 @@ RSpec.describe Admin::MeetingsController, type: :controller do let(:user) { FactoryBot.create(:user) } let!(:district) { FactoryBot.create(:police_district, slug: 'oakland', timezone: 'Pacific Time (US & Canada)') } + let!(:meeting) { FactoryBot.create(:meeting, police_district: district, event_datetime: DateTime.new(2025,1,20,11,0,0,0)) } context 'when user is signed in' do before do @@ -63,6 +64,12 @@ } end + it 'returns 404 if district not present' do + expect do + post :update, params: { police_district_id: 'asldkfjaslkfdj', id: meeting.id } + end.to raise_exception(ActiveRecord::RecordNotFound) + end + it 'converts datetime from district timezone to UTC before storing' do travel_to Date.parse('2020-06-03') do expect do @@ -73,10 +80,14 @@ end describe '#edit' do - let!(:meeting) { FactoryBot.create(:meeting, police_district: district, event_datetime: DateTime.new(2025,1,20,11,0,0,0)) } - render_views + it 'returns 404 if district not present' do + expect do + post :edit, params: { police_district_id: 'asldkfjaslkfdj', id: meeting.id } + end.to raise_exception(ActiveRecord::RecordNotFound) + end + it 'converts time to district timezone, but strips timezone' do travel_to Date.parse('2020-06-03') do get :edit, params: { id: meeting.id, police_district_id: district.slug } @@ -87,9 +98,13 @@ end describe '#destroy' do - let!(:meeting) { FactoryBot.create(:meeting, police_district: district) } - context 'js request' do + it 'returns 404 if district not present' do + expect do + post :update, format: :js, params: { police_district_id: 'asldkfjaslkfdj', id: meeting.id } + end.to raise_exception(ActiveRecord::RecordNotFound) + end + it 'deletes the meeting' do expect do delete :destroy, format: :js, params: { police_district_id: district.slug, id: meeting.id }