1 # frozen_string_literal: true
5 class ApiCapabilityTest < ActiveSupport::TestCase
9 AccessToken.new do |token|
11 token.public_send("#{t}=", true)
17 class ChangesetCommentApiCapabilityTest < ApiCapabilityTest
18 test "as a normal user with permissionless token" do
19 token = create(:access_token)
20 capability = ApiCapability.new token
22 [:create, :destroy, :restore].each do |action|
23 assert capability.cannot? action, ChangesetComment
27 test "as a normal user with allow_write_api token" do
28 token = create(:access_token, :allow_write_api => true)
29 capability = ApiCapability.new token
31 [:destroy, :restore].each do |action|
32 assert capability.cannot? action, ChangesetComment
35 [:create].each do |action|
36 assert capability.can? action, ChangesetComment
40 test "as a moderator with permissionless token" do
41 token = create(:access_token, :user => create(:moderator_user))
42 capability = ApiCapability.new token
44 [:create, :destroy, :restore].each do |action|
45 assert capability.cannot? action, ChangesetComment
49 test "as a moderator with allow_write_api token" do
50 token = create(:access_token, :user => create(:moderator_user), :allow_write_api => true)
51 capability = ApiCapability.new token
53 [:create, :destroy, :restore].each do |action|
54 assert capability.can? action, ChangesetComment
59 class NoteApiCapabilityTest < ApiCapabilityTest
60 test "as a normal user with permissionless token" do
61 token = create(:access_token)
62 capability = ApiCapability.new token
64 [:create, :comment, :close, :reopen, :destroy].each do |action|
65 assert capability.cannot? action, Note
69 test "as a normal user with allow_write_notes token" do
70 token = create(:access_token, :allow_write_notes => true)
71 capability = ApiCapability.new token
73 [:destroy].each do |action|
74 assert capability.cannot? action, Note
77 [:create, :comment, :close, :reopen].each do |action|
78 assert capability.can? action, Note
82 test "as a moderator with permissionless token" do
83 token = create(:access_token, :user => create(:moderator_user))
84 capability = ApiCapability.new token
86 [:destroy].each do |action|
87 assert capability.cannot? action, Note
91 test "as a moderator with allow_write_notes token" do
92 token = create(:access_token, :user => create(:moderator_user), :allow_write_notes => true)
93 capability = ApiCapability.new token
95 [:destroy].each do |action|
96 assert capability.can? action, Note
101 class UserApiCapabilityTest < ApiCapabilityTest
102 test "user preferences" do
103 # a user with no tokens
104 capability = ApiCapability.new nil
105 [:index, :show, :update_all, :update, :destroy].each do |act|
106 assert capability.cannot? act, UserPreference
109 # A user with empty tokens
110 capability = ApiCapability.new tokens
112 [:index, :show, :update_all, :update, :destroy].each do |act|
113 assert capability.cannot? act, UserPreference
116 capability = ApiCapability.new tokens(:allow_read_prefs)
118 [:update_all, :update, :destroy].each do |act|
119 assert capability.cannot? act, UserPreference
122 [:index, :show].each do |act|
123 assert capability.can? act, UserPreference
126 capability = ApiCapability.new tokens(:allow_write_prefs)
127 [:index, :show].each do |act|
128 assert capability.cannot? act, UserPreference
131 [:update_all, :update, :destroy].each do |act|
132 assert capability.can? act, UserPreference