1
0

invite_policy_spec.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. require 'pundit/rspec'
  4. RSpec.describe InvitePolicy do
  5. let(:subject) { described_class }
  6. let(:admin) { Fabricate(:user, admin: true).account }
  7. let(:john) { Fabricate(:user).account }
  8. permissions :index? do
  9. context 'staff?' do
  10. it 'permits' do
  11. expect(subject).to permit(admin, Invite)
  12. end
  13. end
  14. end
  15. permissions :create? do
  16. context 'min_required_role?' do
  17. it 'permits' do
  18. allow_any_instance_of(described_class).to receive(:min_required_role?) { true }
  19. expect(subject).to permit(john, Invite)
  20. end
  21. end
  22. context 'not min_required_role?' do
  23. it 'denies' do
  24. allow_any_instance_of(described_class).to receive(:min_required_role?) { false }
  25. expect(subject).to_not permit(john, Invite)
  26. end
  27. end
  28. end
  29. permissions :deactivate_all? do
  30. context 'admin?' do
  31. it 'permits' do
  32. expect(subject).to permit(admin, Invite)
  33. end
  34. end
  35. context 'not admin?' do
  36. it 'denies' do
  37. expect(subject).to_not permit(john, Invite)
  38. end
  39. end
  40. end
  41. permissions :destroy? do
  42. context 'owner?' do
  43. it 'permits' do
  44. expect(subject).to permit(john, Fabricate(:invite, user: john.user))
  45. end
  46. end
  47. context 'not owner?' do
  48. context 'Setting.min_invite_role == "admin"' do
  49. before do
  50. Setting.min_invite_role = 'admin'
  51. end
  52. context 'admin?' do
  53. it 'permits' do
  54. expect(subject).to permit(admin, Fabricate(:invite))
  55. end
  56. end
  57. context 'not admin?' do
  58. it 'denies' do
  59. expect(subject).to_not permit(john, Fabricate(:invite))
  60. end
  61. end
  62. end
  63. context 'Setting.min_invite_role != "admin"' do
  64. before do
  65. Setting.min_invite_role = 'else'
  66. end
  67. context 'staff?' do
  68. it 'permits' do
  69. expect(subject).to permit(admin, Fabricate(:invite))
  70. end
  71. end
  72. context 'not staff?' do
  73. it 'denies' do
  74. expect(subject).to_not permit(john, Fabricate(:invite))
  75. end
  76. end
  77. end
  78. end
  79. end
  80. end