1 # frozen_string_literal: true
5 class CapabilityTest < ActiveSupport::TestCase
7 AccessToken.new do |token|
9 token.public_send("#{t}=", true)
15 class ChangesetCommentCapabilityTest < CapabilityTest
16 test "as a normal user with permissionless token" do
17 token = create(:access_token)
18 capability = Capability.new token
20 [:create, :destroy, :restore].each do |action|
21 assert capability.cannot? action, ChangesetComment
25 test "as a normal user with allow_write_api token" do
26 token = create(:access_token, :allow_write_api => true)
27 capability = Capability.new token
29 [:destroy, :restore].each do |action|
30 assert capability.cannot? action, ChangesetComment
33 [:create].each do |action|
34 assert capability.can? action, ChangesetComment
38 test "as a moderator with permissionless token" do
39 token = create(:access_token, :user => create(:moderator_user))
40 capability = Capability.new token
42 [:create, :destroy, :restore].each do |action|
43 assert capability.cannot? action, ChangesetComment
47 test "as a moderator with allow_write_api token" do
48 token = create(:access_token, :user => create(:moderator_user), :allow_write_api => true)
49 capability = Capability.new token
51 [:create, :destroy, :restore].each do |action|
52 assert capability.can? action, ChangesetComment
57 class UserCapabilityTest < CapabilityTest
58 test "user preferences" do
59 # a user with no tokens
60 capability = Capability.new nil
61 [:read, :read_one, :update, :update_one, :delete_one].each do |act|
62 assert capability.cannot? act, UserPreference
65 # A user with empty tokens
66 capability = Capability.new tokens
68 [:read, :read_one, :update, :update_one, :delete_one].each do |act|
69 assert capability.cannot? act, UserPreference
72 capability = Capability.new tokens(:allow_read_prefs)
74 [:update, :update_one, :delete_one].each do |act|
75 assert capability.cannot? act, UserPreference
78 [:read, :read_one].each do |act|
79 assert capability.can? act, UserPreference
82 capability = Capability.new tokens(:allow_write_prefs)
83 [:read, :read_one].each do |act|
84 assert capability.cannot? act, UserPreference
87 [:update, :update_one, :delete_one].each do |act|
88 assert capability.can? act, UserPreference