roles_controller.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. require 'rails_helper'
  2. describe Admin::Users::RolesController do
  3. render_views
  4. let(:current_role) { UserRole.create(name: 'Foo', permissions: UserRole::FLAGS[:manage_roles], position: 10) }
  5. let(:current_user) { Fabricate(:user, role: current_role) }
  6. let(:previous_role) { nil }
  7. let(:user) { Fabricate(:user, role: previous_role) }
  8. before do
  9. sign_in current_user, scope: :user
  10. end
  11. describe 'GET #show' do
  12. before do
  13. get :show, params: { user_id: user.id }
  14. end
  15. it 'returns http success' do
  16. expect(response).to have_http_status(:success)
  17. end
  18. context 'when target user is higher ranked than current user' do
  19. let(:previous_role) { UserRole.create(name: 'Baz', permissions: UserRole::FLAGS[:administrator], position: 100) }
  20. it 'returns http forbidden' do
  21. expect(response).to have_http_status(:forbidden)
  22. end
  23. end
  24. end
  25. describe 'PUT #update' do
  26. let(:selected_role) { UserRole.create(name: 'Bar', permissions: permissions, position: position) }
  27. before do
  28. put :update, params: { user_id: user.id, user: { role_id: selected_role.id } }
  29. end
  30. context do
  31. let(:permissions) { UserRole::FLAGS[:manage_roles] }
  32. let(:position) { 1 }
  33. it 'updates user role' do
  34. expect(user.reload.role_id).to eq selected_role&.id
  35. end
  36. it 'redirects back to account page' do
  37. expect(response).to redirect_to(admin_account_path(user.account_id))
  38. end
  39. end
  40. context 'when selected role has higher position than current user\'s role' do
  41. let(:permissions) { UserRole::FLAGS[:administrator] }
  42. let(:position) { 100 }
  43. it 'does not update user role' do
  44. expect(user.reload.role_id).to eq previous_role&.id
  45. end
  46. it 'renders edit form' do
  47. expect(response).to render_template(:show)
  48. end
  49. end
  50. context 'when target user is higher ranked than current user' do
  51. let(:previous_role) { UserRole.create(name: 'Baz', permissions: UserRole::FLAGS[:administrator], position: 100) }
  52. let(:permissions) { UserRole::FLAGS[:manage_roles] }
  53. let(:position) { 1 }
  54. it 'does not update user role' do
  55. expect(user.reload.role_id).to eq previous_role&.id
  56. end
  57. it 'returns http forbidden' do
  58. expect(response).to have_http_status(:forbidden)
  59. end
  60. end
  61. end
  62. end