Merge pull request #32 from turniere/ticket/TURNIERE-146

Implement url parameter for tournament list filter
This commit is contained in:
Daniel Schädler 2019-04-24 23:10:09 +02:00 committed by GitHub
commit 189e611175
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 26 deletions

View File

@ -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

View File

@ -12,6 +12,7 @@ RSpec.describe TournamentsController, type: :controller do
end
describe 'GET #index' do
context 'without parameters' do
it 'returns a success response' do
get :index
expect(response).to be_successful
@ -23,27 +24,70 @@ 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
context 'with type=private parameter' do
let(:params) do
{ type: 'private' }
end
it 'returns all private tournaments' do
apply_authentication_headers_for @another_user
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
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 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
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
describe 'GET #show' do