Merge pull request #32 from turniere/ticket/TURNIERE-146
Implement url parameter for tournament list filter
This commit is contained in:
commit
189e611175
|
|
@ -9,7 +9,16 @@ class TournamentsController < ApplicationController
|
||||||
|
|
||||||
# GET /tournaments
|
# GET /tournaments
|
||||||
def index
|
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
|
render json: tournaments, each_serializer: SimpleTournamentSerializer
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -69,6 +78,10 @@ class TournamentsController < ApplicationController
|
||||||
@tournament = Tournament.find(params[:id])
|
@tournament = Tournament.find(params[:id])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def index_params
|
||||||
|
params.permit(:type)
|
||||||
|
end
|
||||||
|
|
||||||
def tournament_params
|
def tournament_params
|
||||||
params.slice(:name, :description, :public, :teams).permit!
|
params.slice(:name, :description, :public, :teams).permit!
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -12,37 +12,81 @@ RSpec.describe TournamentsController, type: :controller do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'GET #index' do
|
describe 'GET #index' do
|
||||||
it 'returns a success response' do
|
context 'without parameters' do
|
||||||
get :index
|
it 'returns a success response' do
|
||||||
expect(response).to be_successful
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns all public tournaments' do
|
context 'with type=private parameter' do
|
||||||
get :index
|
let(:params) do
|
||||||
tournaments = deserialize_response response
|
{ type: 'private' }
|
||||||
public_tournaments = tournaments.select { |t| t[:public] }
|
end
|
||||||
expect(public_tournaments.map { |t| t[:id] }).to match_array(Tournament.where(public: true).map { |t| t[:id] })
|
|
||||||
|
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, 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, 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
|
end
|
||||||
|
|
||||||
it 'returns no private tournaments for unauthenticated users' do
|
context 'with type=public parameter' do
|
||||||
get :index
|
let(:params) do
|
||||||
tournaments = deserialize_response response
|
{ type: 'public' }
|
||||||
private_tournaments = tournaments.reject { |t| t[:public] }
|
end
|
||||||
expect(private_tournaments.size).to eq(0)
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
it 'returns private tournaments owned by the authenticated user' do
|
context 'with invalid type parameter' do
|
||||||
apply_authentication_headers_for @user
|
it 'renders a bad request error response' do
|
||||||
get :index
|
put :index, params: { type: 'invalid' }
|
||||||
tournaments = deserialize_response response
|
expect(response).to have_http_status(:bad_request)
|
||||||
expect(tournaments.filter { |t| !t[:public] }).to match_array(Tournament.where(owner: @owner, public: false))
|
end
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue