]> git.openstreetmap.org Git - rails.git/blob - test/abilities/api_capability_test.rb
Remove unused redaction code from api old elements controller
[rails.git] / test / abilities / api_capability_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class ChangesetCommentApiCapabilityTest < ActiveSupport::TestCase
6   test "as a normal user without scopes" do
7     user = create(:user)
8     scopes = Set.new
9     ability = ApiAbility.new user, scopes
10
11     [:create, :destroy, :restore].each do |action|
12       assert ability.cannot? action, ChangesetComment
13     end
14   end
15
16   test "as a normal user with write_changeset_comments scope" do
17     user = create(:user)
18     scopes = Set.new %w[write_changeset_comments]
19     ability = ApiAbility.new user, scopes
20
21     [:destroy, :restore].each do |action|
22       assert ability.cannot? action, ChangesetComment
23     end
24
25     [:create].each do |action|
26       assert ability.can? action, ChangesetComment
27     end
28   end
29
30   test "as a moderator without scopes" do
31     user = create(:moderator_user)
32     scopes = Set.new
33     ability = ApiAbility.new user, scopes
34
35     [:create, :destroy, :restore].each do |action|
36       assert ability.cannot? action, ChangesetComment
37     end
38   end
39
40   test "as a moderator with write_changeset_comments scope" do
41     user = create(:moderator_user)
42     scopes = Set.new %w[write_changeset_comments]
43     ability = ApiAbility.new user, scopes
44
45     [:create, :destroy, :restore].each do |action|
46       assert ability.can? action, ChangesetComment
47     end
48   end
49 end
50
51 class NoteApiCapabilityTest < ActiveSupport::TestCase
52   test "as a normal user without scopes" do
53     user = create(:user)
54     scopes = Set.new
55     ability = ApiAbility.new user, scopes
56
57     [:create, :comment, :close, :reopen, :destroy].each do |action|
58       assert ability.cannot? action, Note
59     end
60   end
61
62   test "as a normal user with write_notes scope" do
63     user = create(:user)
64     scopes = Set.new %w[write_notes]
65     ability = ApiAbility.new user, scopes
66
67     [:destroy].each do |action|
68       assert ability.cannot? action, Note
69     end
70
71     [:create, :comment, :close, :reopen].each do |action|
72       assert ability.can? action, Note
73     end
74   end
75
76   test "as a moderator without scopes" do
77     user = create(:moderator_user)
78     scopes = Set.new
79     ability = ApiAbility.new user, scopes
80
81     [:destroy].each do |action|
82       assert ability.cannot? action, Note
83     end
84   end
85
86   test "as a moderator with write_notes scope" do
87     user = create(:moderator_user)
88     scopes = Set.new %w[write_notes]
89     ability = ApiAbility.new user, scopes
90
91     [:destroy].each do |action|
92       assert ability.can? action, Note
93     end
94   end
95 end
96
97 class UserApiCapabilityTest < ActiveSupport::TestCase
98   test "user preferences" do
99     user = create(:user)
100     scopes = Set.new
101     ability = ApiAbility.new user, scopes
102
103     [:index, :show, :update_all, :update, :destroy].each do |act|
104       assert ability.cannot? act, UserPreference
105     end
106
107     scopes = Set.new %w[read_prefs]
108     ability = ApiAbility.new user, scopes
109
110     [:update_all, :update, :destroy].each do |act|
111       assert ability.cannot? act, UserPreference
112     end
113
114     [:index, :show].each do |act|
115       assert ability.can? act, UserPreference
116     end
117
118     scopes = Set.new %w[write_prefs]
119     ability = ApiAbility.new user, scopes
120
121     [:index, :show].each do |act|
122       assert ability.cannot? act, UserPreference
123     end
124
125     [:update_all, :update, :destroy].each do |act|
126       assert ability.can? act, UserPreference
127     end
128   end
129 end