diff --git a/app/controllers/schools_controller.rb b/app/controllers/schools_controller.rb index b47a693..54697a3 100644 --- a/app/controllers/schools_controller.rb +++ b/app/controllers/schools_controller.rb @@ -1,6 +1,7 @@ class SchoolsController < ApplicationController before_action :set_school, only: [:show, :edit, :update, :destroy] - before_filter :check_access, only: [:new,:create,:update,:destroy,:edit] + before_filter :check_access_director, only: [:new,:create,:destroy] + before_filter :check_access_delegate, only: [:update,:edit] # GET /schools/1 @@ -76,15 +77,21 @@ class SchoolsController < ApplicationController params.require(:school).permit(:name, :score, :tournament_id) end - def check_access + def check_access_director if params[:tournament] @tournament = Tournament.find(params[:tournament]) elsif params[:school] @tournament = Tournament.find(params[:school]["tournament_id"]) elsif @school @tournament = @school.tournament + elsif school_params + @tournament = Tournament.find(school_params[:tournament_id]) end authorize! :manage, @tournament end + + def check_access_delegate + authorize! :manage, @school + end end diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index 489a4c4..b350998 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -5,6 +5,7 @@ class StaticPagesController < ApplicationController tournaments_delegated = current_user.delegated_tournaments all_tournaments = tournaments_created + tournaments_delegated @tournaments = all_tournaments.sort_by{|t| t.daysUntil} + @schools = current_user.delegated_schools end def not_allowed diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 7fa29bd..4bbec15 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -1,10 +1,73 @@ class TournamentsController < ApplicationController - before_action :set_tournament, only: [:matches,:weigh_in,:weigh_in_weight,:create_custom_weights,:show,:edit,:update,:destroy,:up_matches,:no_matches,:team_scores,:brackets,:generate_matches,:bracket,:all_brackets] - before_filter :check_access_manage, only: [:weigh_in,:weigh_in_weight,:create_custom_weights,:update,:edit,:generate_matches,:matches] - before_filter :check_access_destroy, only: [:destroy] + before_action :set_tournament, only: [:remove_school_delegate,:remove_delegate,:school_delegate,:delegate,:matches,:weigh_in,:weigh_in_weight,:create_custom_weights,:show,:edit,:update,:destroy,:up_matches,:no_matches,:team_scores,:brackets,:generate_matches,:bracket,:all_brackets] + before_filter :check_access_manage, only: [:remove_school_delegate,:school_delegate,:weigh_in,:weigh_in_weight,:create_custom_weights,:update,:edit,:generate_matches,:matches] + before_filter :check_access_destroy, only: [:destroy,:delegate,:remove_delegate] before_filter :check_for_matches, only: [:up_matches,:bracket,:all_brackets] - + + def remove_delegate + if params[:delegate] + @delegate = TournamentDelegate.find(params[:delegate]) + @delegate.destroy + respond_to do |format| + format.html { redirect_to "/tournaments/#{@tournament.id}/delegate", notice: 'Delegated permissions removed successfully' } + end + end + end + + def remove_school_delegate + if params[:delegate] + @delegate = SchoolDelegate.find(params[:delegate]) + @delegate.destroy + respond_to do |format| + format.html { redirect_to "/tournaments/#{@tournament.id}/school_delegate", notice: 'Delegated permissions removed successfully' } + end + end + end + + def school_delegate + if params[:search] + @users = User.search(params[:search]) + elsif params[:school_delegate] + @delegate = SchoolDelegate.new + @delegate.user_id = params[:school_delegate]["user_id"] + @delegate.school_id = params[:school_delegate]["school_id"] + respond_to do |format| + if @delegate.save + format.html { redirect_to "/tournaments/#{@tournament.id}/school_delegate", notice: 'Delegated permissions added successfully' } + else + format.html { redirect_to "/tournaments/#{@tournament.id}/school_delegate", notice: 'There was an issue delegating permissions please try again' } + end + end + else + @users_delegates = [] + @tournament.schools.each do |s| + s.delegates.each do |d| + @users_delegates << d + end + end + end + end + + def delegate + if params[:search] + @users = User.search(params[:search]) + elsif params[:tournament_delegate] + @delegate = TournamentDelegate.new + @delegate.user_id = params[:tournament_delegate]["user_id"] + @delegate.tournament_id = @tournament.id + respond_to do |format| + if @delegate.save + format.html { redirect_to "/tournaments/#{@tournament.id}/delegate", notice: 'Delegated permissions added successfully' } + else + format.html { redirect_to "/tournaments/#{@tournament.id}/delegate", notice: 'There was an issue delegating permissions please try again' } + end + end + else + @users_delegates = @tournament.delegates + end + end + def matches @matches = @tournament.matches.sort_by{|m| m.bout_number} if @match diff --git a/app/controllers/wrestlers_controller.rb b/app/controllers/wrestlers_controller.rb index a76783f..ae055f5 100644 --- a/app/controllers/wrestlers_controller.rb +++ b/app/controllers/wrestlers_controller.rb @@ -92,13 +92,16 @@ class WrestlersController < ApplicationController def check_access if params[:school] @school = School.find(params[:school]) - @tournament = Tournament.find(@school.tournament.id) + #@tournament = Tournament.find(@school.tournament.id) elsif params[:wrestler] @school = School.find(params[:wrestler]["school_id"]) - @tournament = Tournament.find(@school.tournament.id) + #@tournament = Tournament.find(@school.tournament.id) elsif @wrestler - @tournament = @wrestler.tournament + @school = @wrestler.school + #@tournament = @wrestler.tournament + elsif wrestler_params + @school = School.find(wrestler_params[:school_id]) end - authorize! :manage, @tournament + authorize! :manage, @school end end diff --git a/app/models/ability.rb b/app/models/ability.rb index a6a5983..10b7574 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -40,7 +40,7 @@ class Ability end #Can manage school if tournament owner can :manage, School do |school| - school.tournament.map(&:user_id).include? user.id + school.tournament.user_id == user.id end #Can manage school if tournament delegate can :manage, School do |school| diff --git a/app/models/mat.rb b/app/models/mat.rb index 6b56c84..e699758 100644 --- a/app/models/mat.rb +++ b/app/models/mat.rb @@ -21,7 +21,7 @@ class Mat < ActiveRecord::Base end def assignNextMatch - t_matches = tournament.matches.select{|m| m.mat_id == nil} + t_matches = tournament.matches.select{|m| m.mat_id == nil && m.finished != 1} if t_matches.size > 0 match = t_matches.sort_by{|m| m.bout_number}.first match.mat_id = self.id diff --git a/app/models/match.rb b/app/models/match.rb index 95a37c4..a4f6d98 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -52,9 +52,11 @@ class Match < ActiveRecord::Base if self.w1 && self.w2 @w1 = wrestler1 @w2 = wrestler2 - @w1.advanceInBracket - @w2.advanceInBracket - self.mat.assignNextMatch + @w1.advanceInBracket(self) + @w2.advanceInBracket(self) + if self.mat + self.mat.assignNextMatch + end end end if Rails.env.production? diff --git a/app/models/pool_advance.rb b/app/models/pool_advance.rb index bc49607..177c292 100644 --- a/app/models/pool_advance.rb +++ b/app/models/pool_advance.rb @@ -1,7 +1,8 @@ class PoolAdvance - def initialize(wrestler) + def initialize(wrestler,previousMatch) @wrestler = wrestler + @previousMatch = previousMatch end def advanceWrestler @@ -29,10 +30,10 @@ class PoolAdvance end def bracketAdvancment - if @wrestler.winnerOfLastMatch? + if @previousMatch.winner_id == @wrestler.id winnerAdvance end - if !@wrestler.winnerOfLastMatch? + if @previousMatch.winner_id != @wrestler.id loserAdvance end end diff --git a/app/models/school_delegate.rb b/app/models/school_delegate.rb index 382079c..d85f2f6 100644 --- a/app/models/school_delegate.rb +++ b/app/models/school_delegate.rb @@ -1,2 +1,4 @@ class SchoolDelegate < ActiveRecord::Base + belongs_to :school + belongs_to :user end diff --git a/app/models/tournament.rb b/app/models/tournament.rb index b1c7933..e2a983a 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -79,7 +79,7 @@ class Tournament < ActiveRecord::Base end def resetMats - matchesToReset = matches.select{|m| m.finished != 1 && m.mat_id != nil} + matchesToReset = matches.select{|m| m.mat_id != nil} # matchesToReset.update_all( {:mat_id => nil } ) matchesToReset.each do |m| m.mat_id = nil diff --git a/app/models/tournament_delegate.rb b/app/models/tournament_delegate.rb index c0e197b..310998a 100644 --- a/app/models/tournament_delegate.rb +++ b/app/models/tournament_delegate.rb @@ -1,4 +1,4 @@ class TournamentDelegate < ActiveRecord::Base - # belongs_to :tournament - # has_one :user + belongs_to :tournament + belongs_to :user end diff --git a/app/models/user.rb b/app/models/user.rb index e9279e6..ea5745c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,10 +2,29 @@ class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable has_many :tournaments - has_many :delegated_tournaments, class_name: "TournamentDelegate" - has_many :delegated_schools, class_name: "SchoolDelegate" + has_many :delegated_tournament_permissions, class_name: "TournamentDelegate" + has_many :delegated_school_permissions, class_name: "SchoolDelegate" devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable - + + def delegated_tournaments + tournaments_delegated = [] + delegated_tournament_permissions.each do |t| + tournaments_delegated << t.tournament + end + tournaments_delegated + end + + def delegated_schools + schools_delegated = [] + delegated_school_permissions.each do |t| + schools_delegated << t.school + end + schools_delegated + end + + def self.search(search) + where("email LIKE ?", "%#{search}%") + end end diff --git a/app/models/wrestler.rb b/app/models/wrestler.rb index e37d1d0..00a8a21 100644 --- a/app/models/wrestler.rb +++ b/app/models/wrestler.rb @@ -201,7 +201,7 @@ class Wrestler < ActiveRecord::Base end end - def advanceInBracket - PoolAdvance.new(self).advanceWrestler + def advanceInBracket(match) + PoolAdvance.new(self,match).advanceWrestler end end diff --git a/app/views/layouts/_cdn.html.erb b/app/views/layouts/_cdn.html.erb index 3cedd27..18a559a 100644 --- a/app/views/layouts/_cdn.html.erb +++ b/app/views/layouts/_cdn.html.erb @@ -12,5 +12,8 @@ + + + diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 71977c8..4089ac7 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -7,9 +7,15 @@
<%= notice %>
<%= link_to "Back to #{@tournament.name}", "/tournaments/#{@tournament.id}",:class=>"btn btn-default" %> - <% if can? :manage, @tournament %> + <% if can? :manage, @school %> | <%= link_to "Edit #{@school.name}", edit_school_path(@school),:class=>"btn btn-primary" %> <% end %> @@ -27,7 +27,7 @@| Name | +Tournament Name | +Tournament Date | ++ |
|---|---|---|---|
| <%= school.name %> | +<%= school.tournament.name %> | +<%= school.tournament.date %> | +<%= link_to 'Show', school, :class=>"btn btn-default btn-sm" %> + <% if can? :manage, school %> + <%= link_to 'Edit', edit_school_path(school), :class=>"btn btn-primary btn-sm" %> + <% end %> + | +
| User Email | ++ |
|---|---|
| <%= user.email %> | ++ <%= form_for TournamentDelegate.new, :url => url_for(:controller => 'tournaments', :action => 'delegate', :method => "post") do |f| %> + <%= f.hidden_field :user_id, :value => user.id %> + <% if can? :manage, @tournament %> + <%= submit_tag "Give permissions", :class=>"btn btn-success"%> + <% end %> + <% end %> + | +
Search by email address
+ + +| User Email | ++ |
|---|---|
| <%= delegate.user.email %> | +<%= link_to 'Remove permissions', "/tournaments/#{@tournament.id}/#{delegate.id}/remove_delegate", method: :delete, confirm: 'Are you sure?', :class=>"btn btn-danger btn-sm" %> | +
Search by email address
+ + +| User Email | +School to delegate | ++ |
|---|---|---|
| <%= user.email %> | + <%= form_for SchoolDelegate.new, :url => url_for(:controller => 'tournaments', :action => 'school_delegate', :method => "post") do |f| %> +<%= f.collection_select :school_id, @tournament.schools, :id, :name %> | ++ + <%= f.hidden_field :user_id, :value => user.id %> + <% if can? :manage, @tournament %> + <%= submit_tag "Give permissions", :class=>"btn btn-success"%> + <% end %> + <% end %> + | +
Search by email address
+ + +| User Email | +School | ++ |
|---|---|---|
| <%= delegate.user.email %> | +<%= delegate.school.name %> | +<%= link_to 'Remove permissions', "/tournaments/#{@tournament.id}/#{delegate.id}/remove_school_delegate", method: :delete, confirm: 'Are you sure?', :class=>"btn btn-danger btn-sm" %> | +
<%= notice %>
<%= link_to "Back to #{@school.name}", "/schools/#{@school.id}", :class=>"btn btn-default" %> - <% if can? :manage, @tournament %> + <% if can? :manage, @school %> | <%= link_to "Edit #{@wrestler.name}", edit_wrestler_path(@wrestler), :class=>"btn btn-primary" %> <% end %> <% cache ["wrestlers", @wrestler] do %> diff --git a/config/routes.rb b/config/routes.rb index d98061e..78c826b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -22,7 +22,6 @@ Wrestling::Application.routes.draw do # You can have the root of your site routed with "root" root 'static_pages#home' - get 'admin/index' get 'static_pages/control_match' get 'static_pages/not_allowed' get 'static_pages/about' @@ -34,12 +33,18 @@ Wrestling::Application.routes.draw do get 'tournaments/:id/create_custom_weights' => 'tournaments#create_custom_weights' get 'tournaments/:id/all_brackets' => 'tournaments#all_brackets' get 'tournaments/:id/brackets' => 'tournaments#brackets' - get 'tournaments/:id/brackets/:weight' => 'tournaments#bracket' + get 'tournaments/:id/brackets/:weight' => 'tournaments#bracket', :as => :weight_bracket get 'tournaments/:id/generate_matches' => 'tournaments#generate_matches' get 'tournaments/:id/team_scores' => 'tournaments#team_scores' - get 'tournaments/:id/up_matches' => 'tournaments#up_matches' + get 'tournaments/:id/up_matches' => 'tournaments#up_matches', :as => :up_matches get 'tournaments/:id/no_matches' => 'tournaments#no_matches' get 'tournaments/:id/matches' => 'tournaments#matches' + get 'tournaments/:id/delegate' => 'tournaments#delegate', :as => :tournament_delegate + post 'tournaments/:id/delegate' => 'tournaments#delegate', :as => :set_tournament_delegate + delete 'tournaments/:id/:delegate/remove_delegate' => 'tournaments#remove_delegate', :as => :delete_delegate_path + get 'tournaments/:id/school_delegate' => 'tournaments#school_delegate', :as => :school_delegate + post 'tournaments/:id/school_delegate' => 'tournaments#school_delegate', :as => :set_school_delegate + delete 'tournaments/:id/:delegate/remove_school_delegate' => 'tournaments#remove_school_delegate', :as => :delete_school_delegate_path # Example of regular route: # get 'products/:id' => 'catalog#view' diff --git a/rails-prod-Dockerfile b/rails-prod-Dockerfile index 389cb5e..7fb4818 100644 --- a/rails-prod-Dockerfile +++ b/rails-prod-Dockerfile @@ -4,7 +4,7 @@ FROM ruby:2.2.3 RUN apt-get update RUN apt-get -y upgrade -RUN DEBIAN_FRONTEND=noninteractive apt-get -y install build-essential libssl-dev libyaml-dev libreadline-dev openssl curl git-core zlib1g-dev bison libxml2-dev libxslt1-dev libcurl4-openssl-dev libsqlite3-dev sqlite3 wget apache2 apt-transport-https nodejs mysql-client +RUN DEBIAN_FRONTEND=noninteractive apt-get -y install build-essential libssl-dev libyaml-dev libreadline-dev openssl curl git-core zlib1g-dev bison libxml2-dev libxslt1-dev libcurl4-openssl-dev libsqlite3-dev sqlite3 wget apache2 apt-transport-https nodejs mysql-client postfix #New Relic #RUN echo deb http://apt.newrelic.com/debian/ newrelic non-free >> /etc/apt/sources.list.d/newrelic.list diff --git a/test/controllers/matches_controller_test.rb b/test/controllers/matches_controller_test.rb index 52a58f0..d9bd562 100644 --- a/test/controllers/matches_controller_test.rb +++ b/test/controllers/matches_controller_test.rb @@ -28,6 +28,10 @@ class MatchesControllerTest < ActionController::TestCase def sign_in_tournament_delegate sign_in users(:three) end + + def sign_in_school_delegate + sign_in users(:four) + end def success assert_response :success @@ -52,6 +56,12 @@ class MatchesControllerTest < ActionController::TestCase get_edit redirect end + + test "logged school delegate should not get edit match page if not owner" do + sign_in_school_delegate + get_edit + redirect + end test "non logged in user should not get edit match page" do get_edit @@ -68,6 +78,12 @@ class MatchesControllerTest < ActionController::TestCase post_update assert_redirected_to '/static_pages/not_allowed' end + + test "logged school delegate should not post update match if not owner" do + sign_in_school_delegate + post_update + assert_redirected_to '/static_pages/not_allowed' + end test "logged in tournament delegate should get edit match page" do sign_in_tournament_delegate diff --git a/test/controllers/mats_controller_test.rb b/test/controllers/mats_controller_test.rb index 4f2f788..7ebf6c4 100644 --- a/test/controllers/mats_controller_test.rb +++ b/test/controllers/mats_controller_test.rb @@ -44,7 +44,11 @@ class MatsControllerTest < ActionController::TestCase def sign_in_tournament_delegate sign_in users(:three) end - + + def sign_in_school_delegate + sign_in users(:four) + end + def success assert_response :success end @@ -78,6 +82,12 @@ class MatsControllerTest < ActionController::TestCase get_edit redirect end + + test "logged school delegate should not get edit mat page if not owner" do + sign_in_school_delegate + get_edit + redirect + end test "non logged in user should not get edit mat page" do get_edit @@ -94,6 +104,12 @@ class MatsControllerTest < ActionController::TestCase post_update redirect end + + test "logged school delegate should not post update mat if not owner" do + sign_in_school_delegate + post_update + redirect + end test "logged in tournament owner should post update mat" do sign_in_owner @@ -130,6 +146,14 @@ class MatsControllerTest < ActionController::TestCase create redirect end + + test "logged school delegate not tournament owner cannot create a mat" do + sign_in_school_delegate + new + redirect + create + redirect + end test "logged in tournament owner can destroy a mat" do sign_in_owner @@ -149,11 +173,23 @@ class MatsControllerTest < ActionController::TestCase redirect end + test "logged school delegate not tournament owner cannot destroy mat" do + sign_in_school_delegate + destroy + redirect + end + test "logged in user should not get show mat" do sign_in_non_owner show redirect end + + test "logged school delegate should not get show mat" do + sign_in_school_delegate + show + redirect + end test "logged in tournament owner should get show mat" do sign_in_owner diff --git a/test/controllers/schools_controller_test.rb b/test/controllers/schools_controller_test.rb index 8421c6a..f788822 100644 --- a/test/controllers/schools_controller_test.rb +++ b/test/controllers/schools_controller_test.rb @@ -40,6 +40,10 @@ class SchoolsControllerTest < ActionController::TestCase def sign_in_tournament_delegate sign_in users(:three) end + + def sign_in_school_delegate + sign_in users(:four) + end def success assert_response :success @@ -60,6 +64,12 @@ class SchoolsControllerTest < ActionController::TestCase get_edit success end + + test "logged in school delegate should get edit school page" do + sign_in_school_delegate + get_edit + success + end test "logged in user should not get edit school page if not owner" do sign_in_non_owner @@ -94,6 +104,12 @@ class SchoolsControllerTest < ActionController::TestCase post_update assert_redirected_to tournament_path(@school.tournament_id) end + + test "logged in school delegate should post update school" do + sign_in_school_delegate + post_update + assert_redirected_to tournament_path(@school.tournament_id) + end test "logged in tournament owner can create a new school" do sign_in_owner @@ -110,6 +126,14 @@ class SchoolsControllerTest < ActionController::TestCase create assert_redirected_to tournament_path(@school.tournament_id) end + + test "logged in school delegate cannot create a new school" do + sign_in_school_delegate + new + redirect + create + redirect + end test "logged in user not tournament owner cannot create a school" do sign_in_non_owner @@ -130,6 +154,12 @@ class SchoolsControllerTest < ActionController::TestCase destroy assert_redirected_to tournament_path(@tournament.id) end + + test "logged in school delegate can destroy a school" do + sign_in_school_delegate + destroy + redirect + end test "logged in user not tournament owner cannot destroy school" do sign_in_non_owner diff --git a/test/controllers/tournaments_controller_test.rb b/test/controllers/tournaments_controller_test.rb index 4fc0e6d..d8fb7cf 100644 --- a/test/controllers/tournaments_controller_test.rb +++ b/test/controllers/tournaments_controller_test.rb @@ -29,6 +29,10 @@ include Devise::TestHelpers def sign_in_delegate sign_in users(:three) end + + def sign_in_school_delegate + sign_in users(:four) + end def success assert_response :success @@ -61,6 +65,12 @@ include Devise::TestHelpers get :generate_matches, id: 1 redirect end + + test "logged in school delegate cannot generate matches" do + sign_in_school_delegate + get :generate_matches, id: 1 + redirect + end test "logged in tournament owner can create custom weights" do sign_in_owner @@ -73,6 +83,12 @@ include Devise::TestHelpers get :create_custom_weights, id: 1, customValue: 'hs' redirect end + + test "logged in school delegate cannot create custom weights" do + sign_in_school_delegate + get :create_custom_weights, id: 1, customValue: 'hs' + redirect + end test "logged in tournament owner can access weigh_ins" do @@ -86,6 +102,12 @@ include Devise::TestHelpers get :weigh_in, id: 1 redirect end + + test "logged in school delegate cannot access weigh_ins" do + sign_in_school_delegate + get :weigh_in, id: 1 + redirect + end test "logged in tournament owner can access weigh_in_weight" do sign_in_owner @@ -99,6 +121,12 @@ include Devise::TestHelpers redirect end + test "logged in school delegate cannot access weigh_in_weight" do + sign_in_school_delegate + get :weigh_in_weight, id: 1, weight: 1 + redirect + end + test "logged in tournament owner can access post weigh_in_weight" do sign_in_owner post :weigh_in, id: 1, weight: 1, wrestler: @wrestlers @@ -109,6 +137,12 @@ include Devise::TestHelpers post :weigh_in_weight, id: 1, weight: 1, wrestler: @wrestlers redirect end + + test "logged in school delegate cannot access post weigh_in_weight" do + sign_in_school_delegate + post :weigh_in_weight, id: 1, weight: 1, wrestler: @wrestlers + redirect + end test "logged in tournament owner should get edit tournament page" do @@ -122,6 +156,12 @@ include Devise::TestHelpers get_edit redirect end + + test "logged in school delegate should not get edit tournament page if not owner" do + sign_in_school_delegate + get_edit + redirect + end test "non logged in user should not get edit tournament page" do get_edit @@ -138,6 +178,12 @@ include Devise::TestHelpers post_update assert_redirected_to '/static_pages/not_allowed' end + + test "logged in school delegate should not post update tournament if not owner" do + sign_in_school_delegate + post_update + assert_redirected_to '/static_pages/not_allowed' + end test "logged in tournament owner should post update tournament" do sign_in_owner @@ -158,6 +204,12 @@ include Devise::TestHelpers redirect end + test "logged in school delegate not tournament owner cannot destroy tournament" do + sign_in_school_delegate + destroy + redirect + end + #TESTS THAT NEED MATCHES PUT ABOVE THIS test "redirect up_matches if no matches" do @@ -221,5 +273,55 @@ include Devise::TestHelpers destroy redirect end + + test 'logged in tournament owner can delegate a user' do + sign_in_owner + get :delegate, id: 1 + success + end + + test 'logged in tournament delegate cannot delegate a user' do + sign_in_delegate + get :delegate, id: 1 + redirect + end + + test 'logged in tournament owner can delegate a school user' do + sign_in_owner + get :school_delegate, id: 1 + success + end + + test 'logged in tournament delegate can delegate a school user' do + sign_in_delegate + get :school_delegate, id: 1 + success + end + + test 'logged in tournament owner can delete a school delegate' do + sign_in_owner + patch :remove_school_delegate, id: 1, delegate: SchoolDelegate.find(1) + assert_redirected_to "/tournaments/#{@tournament.id}/school_delegate" + end + + test 'logged in tournament delegate can delete a school delegate' do + sign_in_delegate + patch :remove_school_delegate, id: 1, delegate: SchoolDelegate.find(1) + assert_redirected_to "/tournaments/#{@tournament.id}/school_delegate" + end + + test 'logged in tournament owner can delete a delegate' do + sign_in_owner + patch :remove_delegate, id: 1, delegate: TournamentDelegate.find(1) + assert_redirected_to "/tournaments/#{@tournament.id}/delegate" + end + + test 'logged in tournament delegate cannot delete a delegate' do + sign_in_delegate + patch :remove_delegate, id: 1, delegate: TournamentDelegate.find(1) + redirect + end + + end diff --git a/test/controllers/weights_controller_test.rb b/test/controllers/weights_controller_test.rb index dd7ea4a..ba2a152 100644 --- a/test/controllers/weights_controller_test.rb +++ b/test/controllers/weights_controller_test.rb @@ -40,6 +40,10 @@ class WeightsControllerTest < ActionController::TestCase def sign_in_tournament_delegate sign_in users(:three) end + + def sign_in_school_delegate + sign_in users(:four) + end def success assert_response :success @@ -66,6 +70,12 @@ class WeightsControllerTest < ActionController::TestCase get_edit redirect end + + test "logged school delegate should not get edit weight page if not owner" do + sign_in_school_delegate + get_edit + redirect + end test "non logged in user should not get edit weight page" do get_edit @@ -82,6 +92,12 @@ class WeightsControllerTest < ActionController::TestCase post_update redirect end + + test "logged school delegate should not post update weight if not owner" do + sign_in_school_delegate + post_update + redirect + end test "logged in tournament owner should post update weight" do sign_in_owner @@ -118,6 +134,14 @@ class WeightsControllerTest < ActionController::TestCase create redirect end + + test "logged school delegate not tournament owner cannot create a weight" do + sign_in_school_delegate + new + redirect + create + redirect + end test "logged in tournament owner can destroy a weight" do sign_in_owner @@ -136,6 +160,12 @@ class WeightsControllerTest < ActionController::TestCase destroy redirect end + + test "logged school delegate not tournament owner cannot destroy weight" do + sign_in_school_delegate + destroy + redirect + end test "view wegiht" do get :show, id: 1 diff --git a/test/controllers/wrestlers_controller_test.rb b/test/controllers/wrestlers_controller_test.rb index c3c3fa9..9ef496f 100644 --- a/test/controllers/wrestlers_controller_test.rb +++ b/test/controllers/wrestlers_controller_test.rb @@ -41,6 +41,10 @@ class WrestlersControllerTest < ActionController::TestCase def sign_in_tournament_delegate sign_in users(:three) end + + def sign_in_school_delegate + sign_in users(:four) + end def success assert_response :success @@ -61,6 +65,12 @@ class WrestlersControllerTest < ActionController::TestCase get_edit success end + + test "logged in school delegate should get edit wrestler page" do + sign_in_school_delegate + get_edit + success + end test "logged in user should not get edit wrestler page if not owner" do sign_in_non_owner @@ -95,6 +105,12 @@ class WrestlersControllerTest < ActionController::TestCase post_update assert_redirected_to school_path(@school.id) end + + test "logged in school delegate should post update wrestler" do + sign_in_school_delegate + post_update + assert_redirected_to school_path(@school.id) + end test "logged in tournament owner can create a new wrestler" do sign_in_owner @@ -111,6 +127,14 @@ class WrestlersControllerTest < ActionController::TestCase create assert_redirected_to school_path(@school.id) end + + test "logged in school delegate can create a new wrestler" do + sign_in_school_delegate + new + success + create + assert_redirected_to school_path(@school.id) + end test "logged in user not tournament owner cannot create a wrestler" do sign_in_non_owner @@ -131,6 +155,12 @@ class WrestlersControllerTest < ActionController::TestCase destroy assert_redirected_to school_path(@school.id) end + + test "logged in school delegate can destroy a wrestler" do + sign_in_school_delegate + destroy + assert_redirected_to school_path(@school.id) + end test "logged in user not tournament owner cannot destroy wrestler" do sign_in_non_owner diff --git a/test/fixtures/school_delegates.yml b/test/fixtures/school_delegates.yml index 143b056..a72b87c 100644 --- a/test/fixtures/school_delegates.yml +++ b/test/fixtures/school_delegates.yml @@ -7,3 +7,8 @@ # two: # user_id: 1 # school_id: 1 + +one: + id: 1 + user_id: 4 + school_id: 1 \ No newline at end of file diff --git a/test/fixtures/tournament_delegates.yml b/test/fixtures/tournament_delegates.yml index 37c6a1b..b9cf22c 100644 --- a/test/fixtures/tournament_delegates.yml +++ b/test/fixtures/tournament_delegates.yml @@ -9,5 +9,6 @@ # tournament_id: 1 one: + id: 1 user_id: 3 tournament_id: 1 \ No newline at end of file diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 6d8325a..da16999 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -19,3 +19,7 @@ two: three: email: test3@test.com id: 3 + +four: + email: test4@test.com + id: 4 diff --git a/test/integration/pool_advancement_test.rb b/test/integration/pool_advancement_test.rb index 32c6ff5..8dd52aa 100644 --- a/test/integration/pool_advancement_test.rb +++ b/test/integration/pool_advancement_test.rb @@ -222,9 +222,7 @@ class PoolAdvancementTest < ActionDispatch::IntegrationTest match.winner_id = translateNameToId(winner) match.win_type = "Decision" match.score = 1-0 - #Need to manually assign mat_id because thise weight class is not currently assigned a mat - mat = @tournament.mats.first - match.mat_id = mat.id + match.save end @@ -234,9 +232,7 @@ class PoolAdvancementTest < ActionDispatch::IntegrationTest match.winner_id = translateNameToId(winner) match.win_type = "Decision" match.score = 0-2 - #Need to manually assign mat_id because thise weight class is not currently assigned a mat - mat = @tournament.mats.first - match.mat_id = mat.id + match.save end @@ -246,9 +242,7 @@ class PoolAdvancementTest < ActionDispatch::IntegrationTest match.winner_id = translateNameToId(winner) match.win_type = "Major" match.score = 8-0 - #Need to manually assign mat_id because thise weight class is not currently assigned a mat - mat = @tournament.mats.first - match.mat_id = mat.id + match.save end @@ -257,9 +251,7 @@ class PoolAdvancementTest < ActionDispatch::IntegrationTest match.finished = 1 match.winner_id = translateNameToId(winner) match.win_type = "Tech Fall" - #Need to manually assign mat_id because thise weight class is not currently assigned a mat - mat = @tournament.mats.first - match.mat_id = mat.id + match.save end @@ -269,9 +261,7 @@ class PoolAdvancementTest < ActionDispatch::IntegrationTest match.winner_id = translateNameToId(winner) match.win_type = "Pin" match.score = "5:00" - #Need to manually assign mat_id because thise weight class is not currently assigned a mat - mat = @tournament.mats.first - match.mat_id = mat.id + match.save end @@ -281,9 +271,7 @@ class PoolAdvancementTest < ActionDispatch::IntegrationTest match.winner_id = translateNameToId(winner) match.win_type = "Pin" match.score = "0:20" - #Need to manually assign mat_id because thise weight class is not currently assigned a mat - mat = @tournament.mats.first - match.mat_id = mat.id + match.save end @@ -293,9 +281,7 @@ class PoolAdvancementTest < ActionDispatch::IntegrationTest match.winner_id = translateNameToId(winner) match.win_type = "Pin" match.score = "1:20" - #Need to manually assign mat_id because thise weight class is not currently assigned a mat - mat = @tournament.mats.first - match.mat_id = mat.id + match.save end @@ -550,6 +536,16 @@ class PoolAdvancementTest < ActionDispatch::IntegrationTest assert_equal 2, wrestler.teamPointsEarned end - + test "Test mat assignment when adding a mat and when destroying a mat" do + @mat2 = Mat.new + @mat2.name = "2" + @mat2.tournament_id = 1 + @mat2.save + assert_equal 4, @mat2.matches.size + elevenManBracketFinished + @mat2.destroy + @mat1 = Mat.find(1) + assert_equal 4, @mat1.matches.size + end end