From 09ca262ab162b6c294e8a92963412e3119f9a2a2 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Sat, 20 Apr 2019 12:36:25 +0200 Subject: [PATCH 1/4] Move index specs into without parameters context --- .../tournaments_controller_spec.rb | 56 ++++++++++--------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/spec/controllers/tournaments_controller_spec.rb b/spec/controllers/tournaments_controller_spec.rb index 88d6a6b..5159f5c 100644 --- a/spec/controllers/tournaments_controller_spec.rb +++ b/spec/controllers/tournaments_controller_spec.rb @@ -12,37 +12,39 @@ RSpec.describe TournamentsController, type: :controller do end describe 'GET #index' do - it 'returns a success response' do - get :index - expect(response).to be_successful - end + context 'without parameters' do + it 'returns a success response' do + get :index + expect(response).to be_successful + end - it 'returns all public tournaments' do - get :index - tournaments = deserialize_response response - public_tournaments = tournaments.select { |t| t[:public] } - expect(public_tournaments.map { |t| t[:id] }).to match_array(Tournament.where(public: true).map { |t| t[:id] }) - end + it 'returns all public tournaments' do + get :index + tournaments = deserialize_response response + public_tournaments = tournaments.select { |t| t[:public] } + expect(public_tournaments.map { |t| t[:id] }).to match_array(Tournament.where(public: true).map { |t| t[:id] }) + end - it 'returns no private tournaments for unauthenticated users' do - get :index - tournaments = deserialize_response response - private_tournaments = tournaments.reject { |t| t[:public] } - expect(private_tournaments.size).to eq(0) - end + it 'returns no private tournaments for unauthenticated users' do + get :index + tournaments = deserialize_response response + private_tournaments = tournaments.reject { |t| t[:public] } + expect(private_tournaments.size).to eq(0) + end - it 'returns private tournaments owned by the authenticated user' do - apply_authentication_headers_for @user - get :index - tournaments = deserialize_response response - expect(tournaments.filter { |t| !t[:public] }).to match_array(Tournament.where(owner: @owner, public: false)) - end + it 'returns private tournaments owned by the authenticated user' do + apply_authentication_headers_for @user + get :index + tournaments = deserialize_response response + expect(tournaments.filter { |t| !t[:public] }).to match_array(Tournament.where(owner: @owner, public: false)) + end - it 'returns no private tournaments owned by another user' do - apply_authentication_headers_for @user - get :index - tournaments = deserialize_response response - expect(tournaments.map { |t| t[:id] }).not_to include(@private_tournament.id) + it 'returns no private tournaments owned by another user' do + apply_authentication_headers_for @user + get :index + tournaments = deserialize_response response + expect(tournaments.map { |t| t[:id] }).not_to include(@private_tournament.id) + end end end From c4d03b52c3300ded5512851dee67846b1bca3677 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Tue, 23 Apr 2019 11:09:25 +0200 Subject: [PATCH 2/4] Fix tournament index spec apply wrong auth header because the user @owner doesn't have any private tournaments, therefore the spec was pointless --- spec/controllers/tournaments_controller_spec.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/controllers/tournaments_controller_spec.rb b/spec/controllers/tournaments_controller_spec.rb index 5159f5c..58e9a62 100644 --- a/spec/controllers/tournaments_controller_spec.rb +++ b/spec/controllers/tournaments_controller_spec.rb @@ -33,10 +33,12 @@ RSpec.describe TournamentsController, type: :controller do end it 'returns private tournaments owned by the authenticated user' do - apply_authentication_headers_for @user + apply_authentication_headers_for @another_user get :index tournaments = deserialize_response response - expect(tournaments.filter { |t| !t[:public] }).to match_array(Tournament.where(owner: @owner, public: false)) + private_tournaments = Tournament.where(owner: @another_user, public: false).map { |t| t[:id] } + returned_private_tournaments = tournaments.filter { |t| !t[:public] }.map { |t| t[:id] } + expect(returned_private_tournaments).to match_array(private_tournaments) end it 'returns no private tournaments owned by another user' do From 6d40d091a457a3831e9b5bfa70af50190f59db7b Mon Sep 17 00:00:00 2001 From: Thor77 Date: Tue, 23 Apr 2019 13:08:58 +0200 Subject: [PATCH 3/4] Modify tournament index spec for "type" parameter --- .../tournaments_controller_spec.rb | 56 ++++++++++++++++--- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/spec/controllers/tournaments_controller_spec.rb b/spec/controllers/tournaments_controller_spec.rb index 58e9a62..7dc9d2e 100644 --- a/spec/controllers/tournaments_controller_spec.rb +++ b/spec/controllers/tournaments_controller_spec.rb @@ -24,29 +24,69 @@ RSpec.describe TournamentsController, type: :controller do public_tournaments = tournaments.select { |t| t[:public] } expect(public_tournaments.map { |t| t[:id] }).to match_array(Tournament.where(public: true).map { |t| t[:id] }) end + end - it 'returns no private tournaments for unauthenticated users' do - get :index - tournaments = deserialize_response response - private_tournaments = tournaments.reject { |t| t[:public] } - expect(private_tournaments.size).to eq(0) + context 'with type=private parameter' do + let(:params) do + { type: 'private' } end - it 'returns private tournaments owned by the authenticated user' do + it 'returns all private tournaments' do apply_authentication_headers_for @another_user - get :index + get :index, params: params tournaments = deserialize_response response private_tournaments = Tournament.where(owner: @another_user, public: false).map { |t| t[:id] } returned_private_tournaments = tournaments.filter { |t| !t[:public] }.map { |t| t[:id] } expect(returned_private_tournaments).to match_array(private_tournaments) end + it 'returns no private tournaments for unauthenticated users' do + get :index, params: params + tournaments = deserialize_response response + private_tournaments = tournaments.reject { |t| t[:public] } + expect(private_tournaments.size).to eq(0) + end + it 'returns no private tournaments owned by another user' do apply_authentication_headers_for @user - get :index + get :index, params: params tournaments = deserialize_response response expect(tournaments.map { |t| t[:id] }).not_to include(@private_tournament.id) end + + it 'returns no public tournaments' do + apply_authentication_headers_for @another_user + get :index, params: params + tournaments = deserialize_response response + expect(tournaments.filter { |t| t[:public] }.size).to eq(0) + end + end + + context 'with type=public parameter' do + let(:params) do + { type: 'public' } + end + + it 'returns all public tournaments' do + get :index, params: params + tournaments = deserialize_response response + public_tournaments = tournaments.select { |t| t[:public] } + expect(public_tournaments.map { |t| t[:id] }).to match_array(Tournament.where(public: true).map { |t| t[:id] }) + end + + it 'returns no private tournaments' do + apply_authentication_headers_for @another_user + get :index, params: params + tournaments = deserialize_response response + expect(tournaments.filter { |t| !t[:public] }.size).to eq(0) + end + end + + context 'with invalid type parameter' do + it 'renders a bad request error response' do + put :index, params: { type: 'invalid' } + expect(response).to have_http_status(:bad_request) + end end end From 2b72d0457e6ed32aa53e236f8d90119d4105d3fb Mon Sep 17 00:00:00 2001 From: Thor77 Date: Tue, 23 Apr 2019 13:09:29 +0200 Subject: [PATCH 4/4] Implement type parameter for tournaments index --- app/controllers/tournaments_controller.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 55143ab..e8ea5c0 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -9,7 +9,16 @@ class TournamentsController < ApplicationController # GET /tournaments def index - tournaments = Tournament.where(public: true).or(Tournament.where(owner: current_user)).order(:created_at) + type = index_params.fetch(:type, 'public') + if type == 'public' + tournaments = Tournament.where(public: true).order(:created_at) + elsif type == 'private' + tournaments = Tournament.where(owner: current_user, public: false).order(:created_at) + else + # invalid type specified + render json: { error: 'invalid type' }, status: :bad_request + return + end render json: tournaments, each_serializer: SimpleTournamentSerializer end @@ -69,6 +78,10 @@ class TournamentsController < ApplicationController @tournament = Tournament.find(params[:id]) end + def index_params + params.permit(:type) + end + def tournament_params params.slice(:name, :description, :public, :teams).permit! end