user_role_spec.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe UserRole do
  4. subject { described_class.create(name: 'Foo', position: 1) }
  5. describe '#can?' do
  6. context 'with a single flag' do
  7. it 'returns true if any of them are present' do
  8. subject.permissions = UserRole::FLAGS[:manage_reports]
  9. expect(subject.can?(:manage_reports)).to be true
  10. end
  11. it 'returns false if it is not set' do
  12. expect(subject.can?(:manage_reports)).to be false
  13. end
  14. end
  15. context 'with multiple flags' do
  16. it 'returns true if any of them are present' do
  17. subject.permissions = UserRole::FLAGS[:manage_users]
  18. expect(subject.can?(:manage_reports, :manage_users)).to be true
  19. end
  20. it 'returns false if none of them are present' do
  21. expect(subject.can?(:manage_reports, :manage_users)).to be false
  22. end
  23. end
  24. context 'with an unknown flag' do
  25. it 'raises an error' do
  26. expect { subject.can?(:foo) }.to raise_error ArgumentError
  27. end
  28. end
  29. end
  30. describe '#overrides?' do
  31. it 'returns true if other role has lower position' do
  32. expect(subject.overrides?(described_class.new(position: subject.position - 1))).to be true
  33. end
  34. it 'returns true if other role is nil' do
  35. expect(subject.overrides?(nil)).to be true
  36. end
  37. it 'returns false if other role has higher position' do
  38. expect(subject.overrides?(described_class.new(position: subject.position + 1))).to be false
  39. end
  40. end
  41. describe '#permissions_as_keys' do
  42. before do
  43. subject.permissions = UserRole::FLAGS[:invite_users] | UserRole::FLAGS[:view_dashboard] | UserRole::FLAGS[:manage_reports]
  44. end
  45. it 'returns an array' do
  46. expect(subject.permissions_as_keys).to match_array %w(invite_users view_dashboard manage_reports)
  47. end
  48. end
  49. describe '#permissions_as_keys=' do
  50. let(:input) {}
  51. before do
  52. subject.permissions_as_keys = input
  53. end
  54. context 'with a single value' do
  55. let(:input) { %w(manage_users) }
  56. it 'sets permission flags' do
  57. expect(subject.permissions).to eq UserRole::FLAGS[:manage_users]
  58. end
  59. end
  60. context 'with multiple values' do
  61. let(:input) { %w(manage_users manage_reports) }
  62. it 'sets permission flags' do
  63. expect(subject.permissions).to eq UserRole::FLAGS[:manage_users] | UserRole::FLAGS[:manage_reports]
  64. end
  65. end
  66. context 'with an unknown value' do
  67. let(:input) { %w(foo) }
  68. it 'does not set permission flags' do
  69. expect(subject.permissions).to eq UserRole::Flags::NONE
  70. end
  71. end
  72. end
  73. describe '#computed_permissions' do
  74. context 'when the role is nobody' do
  75. subject { described_class.nobody }
  76. it 'returns none' do
  77. expect(subject.computed_permissions).to eq UserRole::Flags::NONE
  78. end
  79. end
  80. context 'when the role is everyone' do
  81. subject { described_class.everyone }
  82. it 'returns permissions' do
  83. expect(subject.computed_permissions).to eq subject.permissions
  84. end
  85. end
  86. context 'when role has the administrator flag' do
  87. before do
  88. subject.permissions = UserRole::FLAGS[:administrator]
  89. end
  90. it 'returns all permissions' do
  91. expect(subject.computed_permissions).to eq UserRole::Flags::ALL
  92. end
  93. end
  94. it 'returns permissions combined with the everyone role' do
  95. expect(subject.computed_permissions).to eq described_class.everyone.permissions
  96. end
  97. end
  98. describe '.everyone' do
  99. subject { described_class.everyone }
  100. it 'returns a role' do
  101. expect(subject).to be_a(described_class)
  102. end
  103. it 'is identified as the everyone role' do
  104. expect(subject.everyone?).to be true
  105. end
  106. it 'has default permissions' do
  107. expect(subject.permissions).to eq UserRole::FLAGS[:invite_users]
  108. end
  109. it 'has negative position' do
  110. expect(subject.position).to eq(-1)
  111. end
  112. end
  113. describe '.nobody' do
  114. subject { described_class.nobody }
  115. it 'returns a role' do
  116. expect(subject).to be_a(described_class)
  117. end
  118. it 'is identified as the nobody role' do
  119. expect(subject.nobody?).to be true
  120. end
  121. it 'has no permissions' do
  122. expect(subject.permissions).to eq UserRole::Flags::NONE
  123. end
  124. it 'has negative position' do
  125. expect(subject.position).to eq(-1)
  126. end
  127. end
  128. describe '#everyone?' do
  129. it 'returns true when id is -99' do
  130. subject.id = -99
  131. expect(subject.everyone?).to be true
  132. end
  133. it 'returns false when id is not -99' do
  134. subject.id = 123
  135. expect(subject.everyone?).to be false
  136. end
  137. end
  138. describe '#nobody?' do
  139. it 'returns true when id is nil' do
  140. subject.id = nil
  141. expect(subject.nobody?).to be true
  142. end
  143. it 'returns false when id is not nil' do
  144. subject.id = 123
  145. expect(subject.nobody?).to be false
  146. end
  147. end
  148. end