about summary refs log tree commit diff
path: root/spec/controllers/admin/users/roles_controller.rb
blob: bd6a3fa67341bad529ef9533e59e6a5e2b88e626 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'rails_helper'

describe Admin::Users::RolesController do
  render_views

  let(:current_role) { UserRole.create(name: 'Foo', permissions: UserRole::FLAGS[:manage_roles], position: 10) }
  let(:current_user) { Fabricate(:user, role: current_role) }

  let(:previous_role) { nil }
  let(:user) { Fabricate(:user, role: previous_role) }

  before do
    sign_in current_user, scope: :user
  end

  describe 'GET #show' do
    before do
      get :show, params: { user_id: user.id }
    end

    it 'returns http success' do
      expect(response).to have_http_status(:success)
    end

    context 'when target user is higher ranked than current user' do
      let(:previous_role) { UserRole.create(name: 'Baz', permissions: UserRole::FLAGS[:administrator], position: 100) }

      it 'returns http forbidden' do
        expect(response).to have_http_status(:forbidden)
      end
    end
  end

  describe 'PUT #update' do
    let(:selected_role) { UserRole.create(name: 'Bar', permissions: permissions, position: position) }

    before do
      put :update, params: { user_id: user.id, user: { role_id: selected_role.id } }
    end

    context do
      let(:permissions) { UserRole::FLAGS[:manage_roles] }
      let(:position) { 1 }

      it 'updates user role' do
        expect(user.reload.role_id).to eq selected_role&.id
      end

      it 'redirects back to account page' do
        expect(response).to redirect_to(admin_account_path(user.account_id))
      end
    end

    context 'when selected role has higher position than current user\'s role' do
      let(:permissions) { UserRole::FLAGS[:administrator] }
      let(:position) { 100 }

      it 'does not update user role' do
        expect(user.reload.role_id).to eq previous_role&.id
      end

      it 'renders edit form' do
        expect(response).to render_template(:show)
      end
    end

    context 'when target user is higher ranked than current user' do
      let(:previous_role) { UserRole.create(name: 'Baz', permissions: UserRole::FLAGS[:administrator], position: 100) }
      let(:permissions) { UserRole::FLAGS[:manage_roles] }
      let(:position) { 1 }

      it 'does not update user role' do
        expect(user.reload.role_id).to eq previous_role&.id
      end

      it 'returns http forbidden' do
        expect(response).to have_http_status(:forbidden)
      end
    end
  end
end