1 # frozen_string_literal: true
5 class ChangesetCommentApiCapabilityTest < ActiveSupport::TestCase
6 test "as a normal user with permissionless token" do
7 token = create(:access_token)
8 capability = ApiCapability.new token
10 [:create, :destroy, :restore].each do |action|
11 assert capability.cannot? action, ChangesetComment
15 test "as a normal user with allow_write_api token" do
16 token = create(:access_token, :allow_write_api => true)
17 capability = ApiCapability.new token
19 [:destroy, :restore].each do |action|
20 assert capability.cannot? action, ChangesetComment
23 [:create].each do |action|
24 assert capability.can? action, ChangesetComment
28 test "as a moderator with permissionless token" do
29 token = create(:access_token, :user => create(:moderator_user))
30 capability = ApiCapability.new token
32 [:create, :destroy, :restore].each do |action|
33 assert capability.cannot? action, ChangesetComment
37 test "as a moderator with allow_write_api token" do
38 token = create(:access_token, :user => create(:moderator_user), :allow_write_api => true)
39 capability = ApiCapability.new token
41 [:create, :destroy, :restore].each do |action|
42 assert capability.can? action, ChangesetComment
47 class NoteApiCapabilityTest < ActiveSupport::TestCase
48 test "as a normal user with permissionless token" do
49 token = create(:access_token)
50 capability = ApiCapability.new token
52 [:create, :comment, :close, :reopen, :destroy].each do |action|
53 assert capability.cannot? action, Note
57 test "as a normal user with allow_write_notes token" do
58 token = create(:access_token, :allow_write_notes => true)
59 capability = ApiCapability.new token
61 [:destroy].each do |action|
62 assert capability.cannot? action, Note
65 [:create, :comment, :close, :reopen].each do |action|
66 assert capability.can? action, Note
70 test "as a moderator with permissionless token" do
71 token = create(:access_token, :user => create(:moderator_user))
72 capability = ApiCapability.new token
74 [:destroy].each do |action|
75 assert capability.cannot? action, Note
79 test "as a moderator with allow_write_notes token" do
80 token = create(:access_token, :user => create(:moderator_user), :allow_write_notes => true)
81 capability = ApiCapability.new token
83 [:destroy].each do |action|
84 assert capability.can? action, Note
89 class UserApiCapabilityTest < ActiveSupport::TestCase
90 test "user preferences" do
91 # a user with no tokens
92 capability = ApiCapability.new nil
93 [:index, :show, :update_all, :update, :destroy].each do |act|
94 assert capability.cannot? act, UserPreference
97 # A user with empty tokens
98 token = create(:access_token)
99 capability = ApiCapability.new token
101 [:index, :show, :update_all, :update, :destroy].each do |act|
102 assert capability.cannot? act, UserPreference
105 token = create(:access_token, :allow_read_prefs => true)
106 capability = ApiCapability.new token
108 [:update_all, :update, :destroy].each do |act|
109 assert capability.cannot? act, UserPreference
112 [:index, :show].each do |act|
113 assert capability.can? act, UserPreference
116 token = create(:access_token, :allow_write_prefs => true)
117 capability = ApiCapability.new token
119 [:index, :show].each do |act|
120 assert capability.cannot? act, UserPreference
123 [:update_all, :update, :destroy].each do |act|
124 assert capability.can? act, UserPreference