diff --git a/app/controllers/mod_warning_controller.rb b/app/controllers/mod_warning_controller.rb index 7c8b44397461837564510a11eff0e4d0ddc5b25a..bbdc1f5b2ce4ce57927e880648085c805ae4ecae 100644 --- a/app/controllers/mod_warning_controller.rb +++ b/app/controllers/mod_warning_controller.rb @@ -1,5 +1,4 @@ class ModWarningController < ApplicationController - before_action :authenticate_user! before_action :verify_moderator, only: [:log, :new, :create] before_action :set_warning, only: [:current, :approve] @@ -10,7 +9,7 @@ class ModWarningController < ApplicationController end def approve - return not_found if @warning.is_suspension + return not_found if @warning.suspension_active? if params[:approve_checkbox].nil? @failed_to_click_checkbox = true diff --git a/test/controllers/mod_warning_controller_test.rb b/test/controllers/mod_warning_controller_test.rb index 9f5a45b8e6870b5137ef2bb70d1df734c915fe11..fc129f4e40fce4471b7a40c719109a133dfb001e 100644 --- a/test/controllers/mod_warning_controller_test.rb +++ b/test/controllers/mod_warning_controller_test.rb @@ -1,7 +1,61 @@ require 'test_helper' -class ModWarningControllerTest < ActionDispatch::IntegrationTest - # test "the truth" do - # assert true - # end +class ModWarningControllerTest < ActionController::TestCase + include Devise::Test::ControllerHelpers + + test 'should require authentication to access pages' do + sign_out :user + [:log, :new].each do |path| + get path, params: { user_id: users(:standard_user).id } + assert_response(404) + end + end + + test 'should require moderator status to access pages' do + sign_in users(:standard_user) + [:log, :new].each do |path| + get path, params: { user_id: users(:standard_user).id } + assert_response(404) + end + end + + test 'suspended user should redirect to current warning page' do + sign_in users(:standard_user) + mod_warnings(:first_warning).update(active: true) + + current_controller = @controller + @controller = CategoriesController.new + get :homepage + @controller = current_controller + + assert_redirected_to '/warning' + mod_warnings(:first_warning).update(active: false) + end + + test 'warned user should be able to accept warning' do + sign_in users(:standard_user) + @warning = mod_warnings(:first_warning) + @warning.update(active: true) + post :approve, params: { approve_checkbox: true } + @warning.reload + assert !@warning.active + end + + test 'suspended user should not be able to accept pending suspension' do + sign_in users(:standard_user) + @warning = mod_warnings(:third_warning) + @warning.update(active: true) + post :approve, params: { approve_checkbox: true } + @warning.reload + assert @warning.active + end + + test 'suspended user should be able to accept outdated suspension' do + sign_in users(:standard_user) + @warning = mod_warnings(:second_warning) + @warning.update(active: true) + post :approve, params: { approve_checkbox: true } + @warning.reload + assert !@warning.active + end end diff --git a/test/fixtures/mod_warnings.yml b/test/fixtures/mod_warnings.yml index 80aed36e30b2598726b55a90c65850a8f9aeb609..11a1dff2febf6eb909f03385eefce2574b70c6c7 100644 --- a/test/fixtures/mod_warnings.yml +++ b/test/fixtures/mod_warnings.yml @@ -1,11 +1,24 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html -# This model initially had no columns defined. If you add columns to the -# model remove the '{}' from the fixture names and add the columns immediately -# below each fixture, per the syntax in the comments below -# -one: {} -# column: value -# -two: {} -# column: value +first_warning: + community_user: sample_standard_user + body: NOBODY EXPECTS THE SPANISH INQUISITION + is_suspension: false + active: false + author: moderator + +second_warning: + community_user: sample_standard_user + body: NOBODY EXPECTS THE SPANISH INQUISITION! + is_suspension: true + suspension_end: 2000-01-01T00:00:00.000000Z + active: false + author: moderator + +third_warning: + community_user: sample_standard_user + body: NOBODY EXPECTS THE SPANISH INQUISITION!!! + is_suspension: true + suspension_end: 5000-01-01T00:00:00.000000Z + active: false + author: moderator \ No newline at end of file