From 4a745e0aba2f0fcdade69974d3a7975b02fa9b9f Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Thu, 22 Sep 2016 17:26:00 -0500 Subject: [PATCH] admin test complete --- app/controllers/admin/result_controller.rb | 1 + .../controllers/admin/auth_controller_test.rb | 7 ++ .../admin/dashboard_controller_test.rb | 28 +++++++ .../controllers/admin/user_controller_test.rb | 21 +++-- test/controllers/admin_controller_test.rb | 7 ++ test/models/user_test.rb | 80 ++++++++++++++++++- test/policies/admin_policy_test.rb | 10 --- test/policies/dashboard_policy_test.rb | 8 ++ 8 files changed, 144 insertions(+), 18 deletions(-) delete mode 100644 test/policies/admin_policy_test.rb create mode 100644 test/policies/dashboard_policy_test.rb diff --git a/app/controllers/admin/result_controller.rb b/app/controllers/admin/result_controller.rb index 9d14ded..e5c9564 100644 --- a/app/controllers/admin/result_controller.rb +++ b/app/controllers/admin/result_controller.rb @@ -8,6 +8,7 @@ module Admin after_action :skip_authorization # + # TODO: Limit results to the quizzes current_user has access to def index @candidates = Candidate.where(completed: true).includes(:recruiter) end diff --git a/test/controllers/admin/auth_controller_test.rb b/test/controllers/admin/auth_controller_test.rb index c3cb40c..f99bfaa 100644 --- a/test/controllers/admin/auth_controller_test.rb +++ b/test/controllers/admin/auth_controller_test.rb @@ -26,6 +26,13 @@ module Admin assert_redirected_to admin_url end + test "should FAIL auth" do + post admin_auth_url, params: { auth: + { email: 'alan.admin@mailinator.com', password: 'b@d9a$$werD' } } + assert_redirected_to admin_login_url + assert_match(/incorrect.*password/i, flash[:error]) + end + test "recruiter should auth to dashboard" do post admin_auth_url, params: { auth: { email: 'pdr.recruiter@mailinator.com', password: 'password' } } diff --git a/test/controllers/admin/dashboard_controller_test.rb b/test/controllers/admin/dashboard_controller_test.rb index 10a1474..a0cc4e7 100644 --- a/test/controllers/admin/dashboard_controller_test.rb +++ b/test/controllers/admin/dashboard_controller_test.rb @@ -14,5 +14,33 @@ module Admin get admin_users_url assert_response :success end + + test "admin should redirect to users" do + auth_admin + get admin_url + + assert_redirected_to admin_users_path + end + + test "manager should redirect to quizzes" do + auth_manager + get admin_url + + assert_redirected_to admin_quizzes_path + end + + test "reviewer should redirect to results" do + auth_reviewer + get admin_url + + assert_redirected_to admin_results_path + end + + test "recruiter should redirect to candidates" do + auth_recruiter + get admin_url + + assert_redirected_to admin_candidates_path + end end end diff --git a/test/controllers/admin/user_controller_test.rb b/test/controllers/admin/user_controller_test.rb index c11ffbb..8498ffc 100644 --- a/test/controllers/admin/user_controller_test.rb +++ b/test/controllers/admin/user_controller_test.rb @@ -5,24 +5,22 @@ module Admin class UserControllerTest < ActionDispatch::IntegrationTest include ActiveJob::TestHelper - def setup - post admin_auth_url, params: { auth: - { email: 'alan.admin@mailinator.com', password: 'password' } } - end - test "should get index" do + auth_admin get admin_users_url assert_response :success assert assigns :users end test "should get new" do + auth_admin get admin_new_user_url assert_response :success assert assigns :user end test "should fail create" do + auth_admin assert_difference("User.count", 0) do post admin_create_user_url, params: { user: { name: 'New User' } } end @@ -31,6 +29,7 @@ module Admin end test "should post create" do + auth_admin assert_enqueued_jobs 1 do assert_difference("User.count", 1) do post admin_create_user_url, params: { user: @@ -41,6 +40,7 @@ module Admin end test "should get view" do + auth_admin user = users(:recruiter) get admin_user_url user.to_i assert_response :success @@ -48,6 +48,7 @@ module Admin end test "should get edit" do + auth_admin user = users(:recruiter) get admin_edit_user_url user.to_i assert_response :success @@ -55,6 +56,7 @@ module Admin end test "should post update user" do + auth_admin user = users(:recruiter) post admin_update_user_url(user.to_i), params: { user: { name: 'new name' } } assert_redirected_to admin_user_path(user.to_i) @@ -64,10 +66,19 @@ module Admin end test "should fail to update user" do + auth_admin user = users(:recruiter) post admin_update_user_url(user.to_i), params: { user: { name: nil } } assert :success assert_match(/failed/i, session[:flash].values.join) end + + test 'non admin can not change own role' do + auth_recruiter + user = users(:recruiter) + post admin_update_user_url(user.to_i), params: { user: { name: 'new name', role: 'foobar' } } + + assert_equal user.role, User.find_by(id: user.id).role + end end end diff --git a/test/controllers/admin_controller_test.rb b/test/controllers/admin_controller_test.rb index 60da372..6666e23 100644 --- a/test/controllers/admin_controller_test.rb +++ b/test/controllers/admin_controller_test.rb @@ -2,4 +2,11 @@ require 'test_helper' class AdminControllerTest < ActionDispatch::IntegrationTest + test 'should provide flash mesage when access is denied' do + auth_reviewer + get admin_candidates_path + + assert_redirected_to admin_login_path + assert_match 'not authorized', flash[:error] + end end diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 2a7931b..bc372e4 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -2,7 +2,81 @@ require 'test_helper' class UserTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + test 'admin should act as any role' do + user = users(:admin) + + assert user.acts_as_admin? + assert user.acts_as_manager? + assert user.acts_as_recruiter? + assert user.acts_as_reviewer? + end + + test 'admin should only be admin' do + user = users(:admin) + + assert user.admin? + refute user.manager? + refute user.recruiter? + refute user.reviewer? + end + + test 'manager should act as manager' do + user = users(:manager) + + assert user.acts_as_manager? + + refute user.acts_as_admin? + refute user.acts_as_recruiter? + refute user.acts_as_reviewer? + end + + test 'manager should only be manager' do + user = users(:manager) + + assert user.manager? + + refute user.admin? + refute user.recruiter? + refute user.reviewer? + end + + test 'recruiter should act as recruiter' do + user = users(:recruiter) + + assert user.acts_as_recruiter? + + refute user.acts_as_manager? + refute user.acts_as_admin? + refute user.acts_as_reviewer? + end + + test 'recruiter should only be recruiter' do + user = users(:recruiter) + + assert user.recruiter? + + refute user.admin? + refute user.manager? + refute user.reviewer? + end + + test 'reviewer should act as reviewer' do + user = users(:reviewer) + + assert user.acts_as_reviewer? + + refute user.acts_as_manager? + refute user.acts_as_admin? + refute user.acts_as_recruiter? + end + + test 'reviewer should only be reviewer' do + user = users(:reviewer) + + assert user.reviewer? + + refute user.admin? + refute user.manager? + refute user.recruiter? + end end diff --git a/test/policies/admin_policy_test.rb b/test/policies/admin_policy_test.rb deleted file mode 100644 index 00f8494..0000000 --- a/test/policies/admin_policy_test.rb +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: true -require 'test_helper' - -# TODO: How the heck to you test a headless policy?... -# -# class AdminPolicyTest < PolicyAssertions::Test -# def test_dashboard -# assert_permit users(:recruiter), Admin -# end -# end diff --git a/test/policies/dashboard_policy_test.rb b/test/policies/dashboard_policy_test.rb new file mode 100644 index 0000000..93d0b78 --- /dev/null +++ b/test/policies/dashboard_policy_test.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true +require 'test_helper' + +class DashboardPolicyTest < PolicyAssertions::Test + def test_show + assert_permit users(:recruiter), :dashboard + end +end