From d1437a56af0db3e86cf5e283685d655b2c5a3e50 Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Thu, 29 Oct 2015 07:53:47 -0400 Subject: [PATCH] Added tests for matches controller --- app/controllers/matches_controller.rb | 4 +- app/controllers/static_pages_controller.rb | 4 ++ app/views/static_pages/not_allowed.html.erb | 1 + config/routes.rb | 2 +- test/controllers/matches_controller_test.rb | 72 +++++++++++++++++++-- test/fixtures/tournaments.yml | 2 +- test/fixtures/users.yml | 6 ++ 7 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 app/views/static_pages/not_allowed.html.erb diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index b4d345f..916da12 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -25,7 +25,7 @@ class MatchesController < ApplicationController def update respond_to do |format| if @match.update(match_params) - format.html { redirect_to root_path, notice: 'Match was successfully updated.' } + format.html { redirect_to tournament_path(@match.tournament_id), notice: 'Match was successfully updated.' } format.json { head :no_content } else format.html { render action: 'edit' } @@ -48,7 +48,7 @@ class MatchesController < ApplicationController def check_access if current_user != @match.tournament.user - redirect_to root_path + redirect_to '/static_pages/not_allowed' end end end diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index 4cb1fe4..7a890d5 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -116,6 +116,10 @@ class StaticPagesController < ApplicationController end end + + def not_allowed + end + private def check_access if params[:tournament] diff --git a/app/views/static_pages/not_allowed.html.erb b/app/views/static_pages/not_allowed.html.erb new file mode 100644 index 0000000..50a03f8 --- /dev/null +++ b/app/views/static_pages/not_allowed.html.erb @@ -0,0 +1 @@ +You do not have permissions to go there. diff --git a/config/routes.rb b/config/routes.rb index 92c5bb2..f333cbf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -36,7 +36,7 @@ Wrestling::Application.routes.draw do get 'static_pages/generate_matches' get 'static_pages/weigh_in' post 'static_pages/weigh_in' - + get 'static_pages/not_allowed' # Example of regular route: # get 'products/:id' => 'catalog#view' diff --git a/test/controllers/matches_controller_test.rb b/test/controllers/matches_controller_test.rb index 5a35692..8ee41d7 100644 --- a/test/controllers/matches_controller_test.rb +++ b/test/controllers/matches_controller_test.rb @@ -1,11 +1,75 @@ require 'test_helper' class MatchesControllerTest < ActionController::TestCase - # setup do - # @match = matches(:one) - # end + include Devise::TestHelpers - test "the truth" do + setup do + @tournament = Tournament.find(1) + @tournament.generateMatchups + @match = @tournament.matches.first + end + + def post_update + patch :update, id: @match.id, match: {w1: @match.w1, w2: @match.w2} + end + + def get_edit + get :edit, id: @match.id + end + + def sign_in_owner + sign_in users(:one) + end + + def sign_in_non_owner + sign_in users(:two) + end + + def success + assert_response :success + end + + def redirect + assert_response :redirect + end + + test "the truth" do assert true end + + test "logged in tournament owner should get edit match page" do + sign_in_owner + get_edit + success + end + + test "logged in user should not get edit match page if not owner" do + sign_in_non_owner + get_edit + redirect + end + + test "non logged in user should not get edit match page" do + get_edit + redirect + end + + test "non logged in user should get post update match" do + post_update + assert_redirected_to '/static_pages/not_allowed' + end + + test "logged in user should not post update match if not owner" do + sign_in_non_owner + post_update + assert_redirected_to '/static_pages/not_allowed' + end + + test "logged in tournament owner should post update match" do + sign_in_owner + post_update + assert_redirected_to tournament_path(@match.tournament_id) + end + + end diff --git a/test/fixtures/tournaments.yml b/test/fixtures/tournaments.yml index fca58d5..099200b 100644 --- a/test/fixtures/tournaments.yml +++ b/test/fixtures/tournaments.yml @@ -7,6 +7,6 @@ one: director: Jacob Cody Wimer director_email: jacob.wimer@gmail.com tournament_type: Pool to bracket - + user_id: 1 diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index a73a4cc..f96654e 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -9,3 +9,9 @@ # # # two: {} # # column: value +one: + email: test@test.com + id: 1 +two: + email: test2@test.com + id: 2