]> git.openstreetmap.org Git - rails.git/blob - test/abilities/api_capability_test.rb
Create api changeset comment visibility resource
[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     assert ability.cannot? :create, ChangesetComment
12     assert ability.cannot? :create, :changeset_comment_visibility
13     assert ability.cannot? :destroy, :changeset_comment_visibility
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     assert ability.can? :create, ChangesetComment
22     assert ability.cannot? :create, :changeset_comment_visibility
23     assert ability.cannot? :destroy, :changeset_comment_visibility
24   end
25
26   test "as a moderator without scopes" do
27     user = create(:moderator_user)
28     scopes = Set.new
29     ability = ApiAbility.new user, scopes
30
31     assert ability.cannot? :create, ChangesetComment
32     assert ability.cannot? :create, :changeset_comment_visibility
33     assert ability.cannot? :destroy, :changeset_comment_visibility
34   end
35
36   test "as a moderator with write_changeset_comments scope" do
37     user = create(:moderator_user)
38     scopes = Set.new %w[write_changeset_comments]
39     ability = ApiAbility.new user, scopes
40
41     assert ability.can? :create, ChangesetComment
42     assert ability.can? :create, :changeset_comment_visibility
43     assert ability.can? :destroy, :changeset_comment_visibility
44   end
45 end
46
47 class NoteApiCapabilityTest < ActiveSupport::TestCase
48   test "as a normal user without scopes" do
49     user = create(:user)
50     scopes = Set.new
51     ability = ApiAbility.new user, scopes
52
53     [:create, :comment, :close, :reopen, :destroy].each do |action|
54       assert ability.cannot? action, Note
55     end
56   end
57
58   test "as a normal user with write_notes scope" do
59     user = create(:user)
60     scopes = Set.new %w[write_notes]
61     ability = ApiAbility.new user, scopes
62
63     [:destroy].each do |action|
64       assert ability.cannot? action, Note
65     end
66
67     [:create, :comment, :close, :reopen].each do |action|
68       assert ability.can? action, Note
69     end
70   end
71
72   test "as a moderator without scopes" do
73     user = create(:moderator_user)
74     scopes = Set.new
75     ability = ApiAbility.new user, scopes
76
77     [:destroy].each do |action|
78       assert ability.cannot? action, Note
79     end
80   end
81
82   test "as a moderator with write_notes scope" do
83     user = create(:moderator_user)
84     scopes = Set.new %w[write_notes]
85     ability = ApiAbility.new user, scopes
86
87     [:destroy].each do |action|
88       assert ability.can? action, Note
89     end
90   end
91 end
92
93 class UserApiCapabilityTest < ActiveSupport::TestCase
94   test "user preferences" do
95     user = create(:user)
96     scopes = Set.new
97     ability = ApiAbility.new user, scopes
98
99     [:index, :show, :update_all, :update, :destroy].each do |act|
100       assert ability.cannot? act, UserPreference
101     end
102
103     scopes = Set.new %w[read_prefs]
104     ability = ApiAbility.new user, scopes
105
106     [:update_all, :update, :destroy].each do |act|
107       assert ability.cannot? act, UserPreference
108     end
109
110     [:index, :show].each do |act|
111       assert ability.can? act, UserPreference
112     end
113
114     scopes = Set.new %w[write_prefs]
115     ability = ApiAbility.new user, scopes
116
117     [:index, :show].each do |act|
118       assert ability.cannot? act, UserPreference
119     end
120
121     [:update_all, :update, :destroy].each do |act|
122       assert ability.can? act, UserPreference
123     end
124   end
125 end