user_role_spec.rb 4.9 KB

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