1 # frozen_string_literal: true
5 class ChangesetCommentApiCapabilityTest < ActiveSupport::TestCase
6 test "as a normal user with permissionless token" do
7 token = create(:oauth_access_token)
8 ability = ApiAbility.new token
10 [:create, :destroy, :restore].each do |action|
11 assert ability.cannot? action, ChangesetComment
15 test "as a normal user with write_api token" do
16 token = create(:oauth_access_token, :scopes => %w[write_api])
17 ability = ApiAbility.new token
19 [:destroy, :restore].each do |action|
20 assert ability.cannot? action, ChangesetComment
23 [:create].each do |action|
24 assert ability.can? action, ChangesetComment
28 test "as a moderator with permissionless token" do
29 token = create(:oauth_access_token, :resource_owner_id => create(:moderator_user).id)
30 ability = ApiAbility.new token
32 [:create, :destroy, :restore].each do |action|
33 assert ability.cannot? action, ChangesetComment
37 test "as a moderator with write_api token" do
38 token = create(:oauth_access_token, :resource_owner_id => create(:moderator_user).id, :scopes => %w[write_api])
39 ability = ApiAbility.new token
41 [:create, :destroy, :restore].each do |action|
42 assert ability.can? action, ChangesetComment
47 class NoteApiCapabilityTest < ActiveSupport::TestCase
48 test "as a normal user with permissionless token" do
49 token = create(:oauth_access_token)
50 ability = ApiAbility.new token
52 [:create, :comment, :close, :reopen, :destroy].each do |action|
53 assert ability.cannot? action, Note
57 test "as a normal user with write_notes token" do
58 token = create(:oauth_access_token, :scopes => %w[write_notes])
59 ability = ApiAbility.new token
61 [:destroy].each do |action|
62 assert ability.cannot? action, Note
65 [:create, :comment, :close, :reopen].each do |action|
66 assert ability.can? action, Note
70 test "as a moderator with permissionless token" do
71 token = create(:oauth_access_token, :resource_owner_id => create(:moderator_user).id)
72 ability = ApiAbility.new token
74 [:destroy].each do |action|
75 assert ability.cannot? action, Note
79 test "as a moderator with write_notes token" do
80 token = create(:oauth_access_token, :resource_owner_id => create(:moderator_user).id, :scopes => %w[write_notes])
81 ability = ApiAbility.new token
83 [:destroy].each do |action|
84 assert ability.can? action, Note
89 class UserApiCapabilityTest < ActiveSupport::TestCase
90 test "user preferences" do
91 # A user with empty tokens
92 token = create(:oauth_access_token)
93 ability = ApiAbility.new token
95 [:index, :show, :update_all, :update, :destroy].each do |act|
96 assert ability.cannot? act, UserPreference
99 token = create(:oauth_access_token, :scopes => %w[read_prefs])
100 ability = ApiAbility.new token
102 [:update_all, :update, :destroy].each do |act|
103 assert ability.cannot? act, UserPreference
106 [:index, :show].each do |act|
107 assert ability.can? act, UserPreference
110 token = create(:oauth_access_token, :scopes => %w[write_prefs])
111 ability = ApiAbility.new token
113 [:index, :show].each do |act|
114 assert ability.cannot? act, UserPreference
117 [:update_all, :update, :destroy].each do |act|
118 assert ability.can? act, UserPreference