Skip to content
This repository has been archived by the owner on Jul 13, 2022. It is now read-only.

Raise error so 404 is triggered when URL is wrong #92

Merged
merged 3 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/admin/meetings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions app/controllers/admin/police_districts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Admin::PoliceDistrictsController < Admin::ApplicationController
include GoogleCalendarable

before_action :set_district, except: [:new, :create, :index]

def new
@district = PoliceDistrict.new

Expand All @@ -19,17 +21,14 @@ def create
end

def show
@district = PoliceDistrict.find_by_slug(params[:id])
render :show
end

def edit
@district = PoliceDistrict.find_by_slug(params[:id])

render :edit
end

def update
@district = PoliceDistrict.find_by_slug(params[:id])
if @district.update(district_params)
redirect_to admin_police_districts_path
else
Expand All @@ -43,6 +42,10 @@ def index

private

def set_district
@district = PoliceDistrict.find_by_slug!(params[:id])
end

def district_params
params.require(:police_district).permit(
:name,
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/police_districts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def index
end

def show
@district = PoliceDistrict.find_by_slug(params[:slug])
@district = PoliceDistrict.find_by_slug!(params[:slug])
@meeting = @district.next_meeting.present? ? @district.next_meeting : @district.most_recent_meeting
end
end
23 changes: 19 additions & 4 deletions spec/controllers/admin/meetings_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 }
Expand All @@ -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 }
Expand Down
46 changes: 46 additions & 0 deletions spec/controllers/admin/police_districts_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'rails_helper'

RSpec.describe Admin::PoliceDistrictsController, type: :controller do
let(:user) { FactoryBot.create(:user) }
let!(:district) { FactoryBot.create(:police_district, slug: 'oakland', timezone: 'Pacific Time (US & Canada)') }

context 'when user is signed in' do
before do
sign_in user
end

describe '#update' do
it 'returns 404 if district not present' do
expect do
post :update, params: { id: 'asldkfjaslkfdj' }
end.to raise_exception(ActiveRecord::RecordNotFound)
end
end

describe '#edit' do
render_views

it 'returns 404 if district not present' do
expect do
post :edit, params: { id: 'asldkfjaslkfdj' }
end.to raise_exception(ActiveRecord::RecordNotFound)
end
end

describe '#destroy' do
it 'returns 404 if district not present' do
expect do
post :update, params: { id: 'asldkfjaslkfdj' }
end.to raise_exception(ActiveRecord::RecordNotFound)
end
end

describe '#show' do
it 'returns 404 if district not present' do
expect do
get :show, params: { id: 'asldkfjaslkfdj' }
end.to raise_exception(ActiveRecord::RecordNotFound)
end
end
end
end
11 changes: 11 additions & 0 deletions spec/controllers/police_districts_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rails_helper'

RSpec.describe PoliceDistrictsController, type: :controller do
describe '#show' do
it 'returns 404 if district not present' do
expect do
get :show, params: { slug: 'asldkfjaslkfdj' }
end.to raise_exception(ActiveRecord::RecordNotFound)
end
end
end