Skip to content

Commit

Permalink
Merge pull request #202 from mberlanda/reservation-cc-1
Browse files Browse the repository at this point in the history
test: increase reservation controller code coverage
  • Loading branch information
mberlanda committed Nov 9, 2021
2 parents 491dbb6 + 356a9b0 commit 0cec267
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/controllers/reservations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def model_scope
).select(%w[id] | datatable_columns, '"users"."email" AS "user_email"')
end

# FIXME: scope the access on this endpoint
def status
@reservation = Reservation.find(params[:id])
end
Expand Down
58 changes: 58 additions & 0 deletions spec/requests/reservations_status_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'ReservationsController#status', type: :request do
let(:competition1) { FactoryBot.create(:competition) }
let(:team_a) { FactoryBot.create(:team) }
let(:team_b) { FactoryBot.create(:team) }
let(:fan) { FactoryBot.create(:user, role: :fan, status: :active) }
let(:another_fan) { FactoryBot.create(:user, role: :fan, status: :active) }
let(:admin) { FactoryBot.create(:user, role: :admin, status: :active) }
let(:eventa) do
FactoryBot.create(
:event, requested_seats: 0, confirmed_seats: 0,
competition: competition1, home_team: team_a, away_team: team_b
)
end
let!(:reservation1) do
FactoryBot.create(:reservation, user: fan, event: eventa, fan_names: %w[Foo|Bar])
end

before(:each) do
Rails.cache.clear
end

context 'when a fan is logged in' do
it 'shows the fan reservation status' do
sign_in fan

get "/reservations/#{reservation1.id}/status"

expect(status).to eq(200)
expect(response).to render_template(:status)
end
end

context 'when an admin is logged in' do
it 'shows the fan reservation status' do
sign_in admin

get "/reservations/#{reservation1.id}/status"

expect(status).to eq(200)
expect(response).to render_template(:status)
end
end

context 'when another fan is logged in' do
it 'shows the fan reservation status' do
sign_in another_fan

get "/reservations/#{reservation1.id}/status"

expect(status).to eq(200)
expect(response).to render_template(:status)
end
end
end
93 changes: 93 additions & 0 deletions spec/requests/reservations_user_form_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'ReservationsController#status', type: :request do
let(:fan) { FactoryBot.create(:user, role: :fan, status: :active) }
let(:another_fan) { FactoryBot.create(:user, role: :fan, status: :active) }
let(:admin) { FactoryBot.create(:user, role: :admin, status: :active) }
let(:event) do
FactoryBot.create(:event, requested_seats: 0, confirmed_seats: 0)
end
before(:each) do
Rails.cache.clear
end

context 'when a user is rejected' do
it 'renders rejected static page' do
fan.rejected!
sign_in fan

get '/reservations/user_form', params: { event_id: event.id }

expect(status).to eq(200)
expect(response).to render_template('reservations/rejected_user')
end
end

context 'when a user is pending' do
it 'renders pending static page' do
fan.pending!
sign_in fan

get '/reservations/user_form', params: { event_id: event.id }

expect(status).to eq(200)
expect(response).to render_template('reservations/pending_user')
end
end

context 'when the event is not bookable' do
it 'renders not bookable static page' do
allow(event).to receive(:book_range?).and_return(false)
sign_in fan

get '/reservations/user_form', params: { event_id: event.id }

expect(status).to eq(200)
expect(response).to render_template('reservations/no_user_form')
end
end

context 'when a user has already booked' do
it 'redirects to reservation status' do
allow(event).to receive(:book_range?).and_return(true)
reservation = FactoryBot.create(:reservation, user_id: fan.id, event_id: event.id,
fan_names: %w[Foo|Bar])
sign_in fan

get '/reservations/user_form', params: { event_id: event.id }

expect(status).to eq(302)
expect(response).to redirect_to("/reservations/#{reservation.id}/status")
end
end

context 'when a user cannot book an available event' do
it 'renders not bookable static page' do
allow(event).to receive(:book_range?).and_return(true)
allow(fan).to receive(:can_book?).with(event).and_return(false)

sign_in fan

get '/reservations/user_form', params: { event_id: event.id }

expect(status).to eq(200)
expect(response).to render_template('reservations/no_user_form')
end
end

context 'when a user can book an available event' do
it 'renders not bookable static page' do
allow(event).to receive(:book_range?).and_return(true)
allow(fan).to receive(:can_book?).with(event).and_return(true)

sign_in fan

get '/reservations/user_form', params: { event_id: event.id }

expect(status).to eq(200)
expect(response).to render_template('reservations/user_form')
end
end
end

0 comments on commit 0cec267

Please sign in to comment.