1 # frozen_string_literal: true
5 class CapabilityTest < ActiveSupport::TestCase
7 AccessToken.new do |token|
9 token.public_send("#{t}=", true)
15 class ChangesetCommentCapabilityTest < CapabilityTest
16 test "as a normal user with permissionless token" do
17 token = create(:access_token)
18 capability = Capability.new token
20 [:create, :destroy, :restore].each do |action|
21 assert capability.cannot? action, ChangesetComment
25 test "as a normal user with allow_write_api token" do
26 token = create(:access_token, :allow_write_api => true)
27 capability = Capability.new token
29 [:destroy, :restore].each do |action|
30 assert capability.cannot? action, ChangesetComment
33 [:create].each do |action|
34 assert capability.can? action, ChangesetComment
38 test "as a moderator with permissionless token" do
39 token = create(:access_token, :user => create(:moderator_user))
40 capability = Capability.new token
42 [:create, :destroy, :restore].each do |action|
43 assert capability.cannot? action, ChangesetComment
47 test "as a moderator with allow_write_api token" do
48 token = create(:access_token, :user => create(:moderator_user), :allow_write_api => true)
49 capability = Capability.new token
51 [:create, :destroy, :restore].each do |action|
52 assert capability.can? action, ChangesetComment
57 class NoteCapabilityTest < CapabilityTest
58 test "as a normal user with permissionless token" do
59 token = create(:access_token)
60 capability = Capability.new token
62 [:create, :comment, :close, :reopen, :destroy].each do |action|
63 assert capability.cannot? action, Note
67 test "as a normal user with allow_write_notes token" do
68 token = create(:access_token, :allow_write_notes => true)
69 capability = Capability.new token
71 [:destroy].each do |action|
72 assert capability.cannot? action, Note
75 [:create, :comment, :close, :reopen].each do |action|
76 assert capability.can? action, Note
80 test "as a moderator with permissionless token" do
81 token = create(:access_token, :user => create(:moderator_user))
82 capability = Capability.new token
84 [:destroy].each do |action|
85 assert capability.cannot? action, Note
89 test "as a moderator with allow_write_notes token" do
90 token = create(:access_token, :user => create(:moderator_user), :allow_write_notes => true)
91 capability = Capability.new token
93 [:destroy].each do |action|
94 assert capability.can? action, Note
99 class UserCapabilityTest < CapabilityTest
100 test "user preferences" do
101 # a user with no tokens
102 capability = Capability.new nil
103 [:read, :read_one, :update, :update_one, :delete_one].each do |act|
104 assert capability.cannot? act, UserPreference
107 # A user with empty tokens
108 capability = Capability.new tokens
110 [:read, :read_one, :update, :update_one, :delete_one].each do |act|
111 assert capability.cannot? act, UserPreference
114 capability = Capability.new tokens(:allow_read_prefs)
116 [:update, :update_one, :delete_one].each do |act|
117 assert capability.cannot? act, UserPreference
120 [:read, :read_one].each do |act|
121 assert capability.can? act, UserPreference
124 capability = Capability.new tokens(:allow_write_prefs)
125 [:read, :read_one].each do |act|
126 assert capability.cannot? act, UserPreference
129 [:update, :update_one, :delete_one].each do |act|
130 assert capability.can? act, UserPreference