1 # frozen_string_literal: true
5 class ChangesetCommentApiCapabilityTest < ActiveSupport::TestCase
6 test "as a normal user with permissionless token" do
8 token = create(:oauth_access_token, :user => user)
9 ability = ApiAbility.new user, token
11 [:create, :destroy, :restore].each do |action|
12 assert ability.cannot? action, ChangesetComment
16 test "as a normal user with write_api token" do
18 token = create(:oauth_access_token, :user => user, :scopes => %w[write_api])
19 ability = ApiAbility.new user, token
21 [:destroy, :restore].each do |action|
22 assert ability.cannot? action, ChangesetComment
25 [:create].each do |action|
26 assert ability.can? action, ChangesetComment
30 test "as a moderator with permissionless token" do
31 user = create(:moderator_user)
32 token = create(:oauth_access_token, :user => user)
33 ability = ApiAbility.new user, token
35 [:create, :destroy, :restore].each do |action|
36 assert ability.cannot? action, ChangesetComment
40 test "as a moderator with write_api token" do
41 user = create(:moderator_user)
42 token = create(:oauth_access_token, :user => user, :scopes => %w[write_api])
43 ability = ApiAbility.new user, token
45 [:create, :destroy, :restore].each do |action|
46 assert ability.can? action, ChangesetComment
51 class NoteApiCapabilityTest < ActiveSupport::TestCase
52 test "as a normal user with permissionless token" do
54 token = create(:oauth_access_token, :user => user)
55 ability = ApiAbility.new user, token
57 [:create, :comment, :close, :reopen, :destroy].each do |action|
58 assert ability.cannot? action, Note
62 test "as a normal user with write_notes token" do
64 token = create(:oauth_access_token, :user => user, :scopes => %w[write_notes])
65 ability = ApiAbility.new user, token
67 [:destroy].each do |action|
68 assert ability.cannot? action, Note
71 [:create, :comment, :close, :reopen].each do |action|
72 assert ability.can? action, Note
76 test "as a moderator with permissionless token" do
77 user = create(:moderator_user)
78 token = create(:oauth_access_token, :user => user)
79 ability = ApiAbility.new user, token
81 [:destroy].each do |action|
82 assert ability.cannot? action, Note
86 test "as a moderator with write_notes token" do
87 user = create(:moderator_user)
88 token = create(:oauth_access_token, :user => user, :scopes => %w[write_notes])
89 ability = ApiAbility.new user, token
91 [:destroy].each do |action|
92 assert ability.can? action, Note
97 class UserApiCapabilityTest < ActiveSupport::TestCase
98 test "user preferences" do
99 # A user with empty tokens
101 token = create(:oauth_access_token, :user => user)
102 ability = ApiAbility.new user, token
104 [:index, :show, :update_all, :update, :destroy].each do |act|
105 assert ability.cannot? act, UserPreference
108 token = create(:oauth_access_token, :user => user, :scopes => %w[read_prefs])
109 ability = ApiAbility.new user, token
111 [:update_all, :update, :destroy].each do |act|
112 assert ability.cannot? act, UserPreference
115 [:index, :show].each do |act|
116 assert ability.can? act, UserPreference
119 token = create(:oauth_access_token, :user => user, :scopes => %w[write_prefs])
120 ability = ApiAbility.new user, token
122 [:index, :show].each do |act|
123 assert ability.cannot? act, UserPreference
126 [:update_all, :update, :destroy].each do |act|
127 assert ability.can? act, UserPreference