|
| 1 | +require 'spec_helper' |
| 2 | +include Pundit |
| 3 | + |
| 4 | +class ApplicationPolicy |
| 5 | + attr_reader :user, :record |
| 6 | + |
| 7 | + def initialize(user, record) |
| 8 | + @user = user |
| 9 | + @record = record |
| 10 | + end |
| 11 | + |
| 12 | + def show |
| 13 | + user.roles.include? :admin |
| 14 | + end |
| 15 | + |
| 16 | + def destroy |
| 17 | + false |
| 18 | + end |
| 19 | + |
| 20 | + def history |
| 21 | + user.roles.include? :admin |
| 22 | + end |
| 23 | + |
| 24 | + def show_in_app |
| 25 | + user.roles.include? :admin |
| 26 | + end |
| 27 | + |
| 28 | + def dashboard |
| 29 | + user.roles.include? :admin |
| 30 | + end |
| 31 | + |
| 32 | + def index |
| 33 | + false |
| 34 | + end |
| 35 | + |
| 36 | + def new |
| 37 | + user.roles.include? :admin |
| 38 | + end |
| 39 | + |
| 40 | + def edit |
| 41 | + user.roles.include? :admin |
| 42 | + end |
| 43 | + |
| 44 | + def export |
| 45 | + user.roles.include? :admin |
| 46 | + end |
| 47 | +end |
| 48 | + |
| 49 | +class PlayerPolicy < ApplicationPolicy |
| 50 | + def new |
| 51 | + (user.roles.include?(:create_player) || user.roles.include?(:admin) || user.roles.include?(:manage_player)) |
| 52 | + end |
| 53 | + |
| 54 | + def edit |
| 55 | + (user.roles.include? :manage_player) |
| 56 | + end |
| 57 | + |
| 58 | + def destroy |
| 59 | + (user.roles.include? :manage_player) |
| 60 | + end |
| 61 | + |
| 62 | + def index |
| 63 | + user.roles.include? :admin |
| 64 | + end |
| 65 | +end |
| 66 | + |
| 67 | +describe 'RailsAdmin Pundit Authorization', type: :request do |
| 68 | + subject { page } |
| 69 | + |
| 70 | + before do |
| 71 | + RailsAdmin.config do |c| |
| 72 | + c.authorize_with(:pundit) |
| 73 | + c.authenticate_with { warden.authenticate! scope: :user } |
| 74 | + c.current_user_method(&:current_user) |
| 75 | + end |
| 76 | + @player_model = RailsAdmin::AbstractModel.new(Player) |
| 77 | + @user = FactoryGirl.create :user |
| 78 | + login_as @user |
| 79 | + end |
| 80 | + |
| 81 | + describe 'with no roles' do |
| 82 | + before do |
| 83 | + @user.update_attributes(roles: []) |
| 84 | + end |
| 85 | + |
| 86 | + it 'GET /admin should raise Pundit::NotAuthorizedError' do |
| 87 | + expect { visit dashboard_path }.to raise_error(Pundit::NotAuthorizedError) |
| 88 | + end |
| 89 | + |
| 90 | + it 'GET /admin/player should raise Pundit::NotAuthorizedError' do |
| 91 | + expect { visit index_path(model_name: 'player') }.to raise_error(Pundit::NotAuthorizedError) |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + describe 'with read player role' do |
| 96 | + before do |
| 97 | + @user.update_attributes(roles: [:admin, :read_player]) |
| 98 | + end |
| 99 | + |
| 100 | + it 'GET /admin should show Player but not League' do |
| 101 | + visit dashboard_path |
| 102 | + is_expected.to have_content('Player') |
| 103 | + is_expected.not_to have_content('League') |
| 104 | + is_expected.not_to have_content('Add new') |
| 105 | + end |
| 106 | + |
| 107 | + it 'GET /admin/team should raise Pundit::NotAuthorizedError' do |
| 108 | + expect { visit index_path(model_name: 'team') }.to raise_error(Pundit::NotAuthorizedError) |
| 109 | + end |
| 110 | + |
| 111 | + it 'GET /admin/player/1/edit should raise access denied' do |
| 112 | + @player = FactoryGirl.create :player |
| 113 | + expect { visit edit_path(model_name: 'player', id: @player.id) }.to raise_error(Pundit::NotAuthorizedError) |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + describe 'with admin role' do |
| 118 | + before do |
| 119 | + @user.update_attributes(roles: [:admin, :manage_player]) |
| 120 | + end |
| 121 | + |
| 122 | + it 'GET /admin should show Player but not League' do |
| 123 | + visit dashboard_path |
| 124 | + is_expected.to have_content('Player') |
| 125 | + end |
| 126 | + |
| 127 | + it 'GET /admin/player/new should render and create record upon submission' do |
| 128 | + visit new_path(model_name: 'player') |
| 129 | + |
| 130 | + is_expected.to have_content('Save and edit') |
| 131 | + is_expected.not_to have_content('Delete') |
| 132 | + |
| 133 | + is_expected.to have_content('Save and add another') |
| 134 | + fill_in 'player[name]', with: 'Jackie Robinson' |
| 135 | + fill_in 'player[number]', with: '42' |
| 136 | + fill_in 'player[position]', with: 'Second baseman' |
| 137 | + click_button 'Save' |
| 138 | + is_expected.not_to have_content('Edit') |
| 139 | + |
| 140 | + @player = RailsAdmin::AbstractModel.new('Player').first |
| 141 | + expect(@player.name).to eq('Jackie Robinson') |
| 142 | + expect(@player.number).to eq(42) |
| 143 | + expect(@player.position).to eq('Second baseman') |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + describe 'with all roles' do |
| 148 | + it 'shows links to all actions' do |
| 149 | + @user.update_attributes(roles: [:admin, :manage_player]) |
| 150 | + @player = FactoryGirl.create :player |
| 151 | + |
| 152 | + visit index_path(model_name: 'player') |
| 153 | + is_expected.to have_css('.show_member_link') |
| 154 | + is_expected.to have_css('.edit_member_link') |
| 155 | + is_expected.to have_css('.delete_member_link') |
| 156 | + is_expected.to have_css('.history_show_member_link') |
| 157 | + is_expected.to have_css('.show_in_app_member_link') |
| 158 | + |
| 159 | + visit show_path(model_name: 'player', id: @player.id) |
| 160 | + is_expected.to have_content('Show') |
| 161 | + is_expected.to have_content('Edit') |
| 162 | + is_expected.to have_content('Delete') |
| 163 | + is_expected.to have_content('History') |
| 164 | + is_expected.to have_content('Show in app') |
| 165 | + end |
| 166 | + end |
| 167 | +end |
0 commit comments