From 7fd1dcb30e48df47f58df4171090a29dee8854da Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Thu, 5 Mar 2026 00:11:09 +0000 Subject: [PATCH] Fix new rubocop Style/OneClassPerFile warnings --- .rubocop_todo.yml | 13 -- config/initializers/i18n.rb | 10 +- ...120208122334_merge_acl_address_and_mask.rb | 12 +- test/abilities/abilities_test.rb | 165 ------------------ test/abilities/administrator_ability_test.rb | 24 +++ test/abilities/api_abilities_test.rb | 49 ------ test/abilities/api_capability_test.rb | 125 ------------- .../changeset_comment_api_capability_test.rb | 45 +++++ test/abilities/guest_ability_test.rb | 48 +++++ test/abilities/guest_api_ability_test.rb | 18 ++ test/abilities/moderator_ability_test.rb | 73 ++++++++ test/abilities/moderator_api_ability_test.rb | 15 ++ test/abilities/note_api_capability_test.rb | 49 ++++++ test/abilities/user_ability_test.rb | 26 +++ test/abilities/user_api_ability_test.rb | 19 ++ test/abilities/user_api_capability_test.rb | 37 ++++ test/system/embed_test.rb | 28 --- test/system/german_embed_test.rb | 17 ++ test/system/unknown_language_embed_test.rb | 17 ++ test/validators/characters_validator_test.rb | 24 +-- test/validators/whitespace_validator_test.rb | 24 +-- 21 files changed, 416 insertions(+), 422 deletions(-) delete mode 100644 test/abilities/abilities_test.rb create mode 100644 test/abilities/administrator_ability_test.rb delete mode 100644 test/abilities/api_abilities_test.rb delete mode 100644 test/abilities/api_capability_test.rb create mode 100644 test/abilities/changeset_comment_api_capability_test.rb create mode 100644 test/abilities/guest_ability_test.rb create mode 100644 test/abilities/guest_api_ability_test.rb create mode 100644 test/abilities/moderator_ability_test.rb create mode 100644 test/abilities/moderator_api_ability_test.rb create mode 100644 test/abilities/note_api_capability_test.rb create mode 100644 test/abilities/user_ability_test.rb create mode 100644 test/abilities/user_api_ability_test.rb create mode 100644 test/abilities/user_api_capability_test.rb create mode 100644 test/system/german_embed_test.rb create mode 100644 test/system/unknown_language_embed_test.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index e042fd036..bf88e164f 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -179,19 +179,6 @@ Style/IfUnlessModifier: Style/NumericLiterals: MinDigits: 15 -# Offense count: 17 -# Configuration parameters: AllowedClasses. -Style/OneClassPerFile: - Exclude: - - 'config/initializers/i18n.rb' - - 'db/migrate/20120208122334_merge_acl_address_and_mask.rb' - - 'test/abilities/abilities_test.rb' - - 'test/abilities/api_abilities_test.rb' - - 'test/abilities/api_capability_test.rb' - - 'test/system/embed_test.rb' - - 'test/validators/characters_validator_test.rb' - - 'test/validators/whitespace_validator_test.rb' - # Offense count: 705 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, AllowRBSInlineAnnotation, AllowCopDirectives, AllowedPatterns, SplitStrings. diff --git a/config/initializers/i18n.rb b/config/initializers/i18n.rb index daec88bcb..632712904 100644 --- a/config/initializers/i18n.rb +++ b/config/initializers/i18n.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -module I18n - module Backend +module OpenStreetMap + module I18n module PluralizationFallback def pluralize(locale, entry, count) super @@ -11,11 +11,7 @@ module I18n e.entry[:other] end end - end -end -module OpenStreetMap - module I18n module NormaliseLocales def store_translations(locale, data, options = {}) locale = ::I18n::Locale::Tag::Rfc4646.tag(locale).to_s @@ -28,7 +24,7 @@ end I18n::Backend::Simple.prepend(OpenStreetMap::I18n::NormaliseLocales) -I18n::Backend::Simple.include(I18n::Backend::PluralizationFallback) +I18n::Backend::Simple.include(OpenStreetMap::I18n::PluralizationFallback) I18n::Backend::Simple.include(I18n::Backend::Fallbacks) I18n.enforce_available_locales = false diff --git a/db/migrate/20120208122334_merge_acl_address_and_mask.rb b/db/migrate/20120208122334_merge_acl_address_and_mask.rb index 8a9eded66..cc3c75ca3 100644 --- a/db/migrate/20120208122334_merge_acl_address_and_mask.rb +++ b/db/migrate/20120208122334_merge_acl_address_and_mask.rb @@ -2,16 +2,6 @@ require "ipaddr" -class IPAddr - def address - _to_string(@addr) - end - - def netmask - _to_string(@mask_addr) - end -end - class MergeAclAddressAndMask < ActiveRecord::Migration[4.2] class Acl < ApplicationRecord end @@ -40,7 +30,7 @@ class MergeAclAddressAndMask < ActiveRecord::Migration[4.2] Acl.find_each do |acl| address = IPAddr.new(acl.address) - acl.address = address.address + acl.address = address.to_string acl.netmask = address.netmask acl.save! end diff --git a/test/abilities/abilities_test.rb b/test/abilities/abilities_test.rb deleted file mode 100644 index a72e4d792..000000000 --- a/test/abilities/abilities_test.rb +++ /dev/null @@ -1,165 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class AbilityTest < ActiveSupport::TestCase -end - -class GuestAbilityTest < AbilityTest - test "search permissions for a guest" do - ability = Ability.new nil - - [:create, :show].each do |action| - assert ability.can?(action, :search), "should be able to #{action} searches" - end - end - - test "diary permissions for a guest" do - ability = Ability.new nil - [:index, :rss, :show].each do |action| - assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries" - end - - [:index].each do |action| - assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComments" - end - - [:create, :edit, :subscribe, :unsubscribe, :hide, :unhide].each do |action| - assert ability.cannot?(action, DiaryEntry), "should not be able to #{action} DiaryEntries" - end - - [:create, :hide, :unhide].each do |action| - assert ability.cannot?(action, DiaryComment), "should not be able to #{action} DiaryComments" - end - end - - test "note permissions for a guest" do - ability = Ability.new nil - - [:index].each do |action| - assert ability.can?(action, Note), "should be able to #{action} Notes" - end - end - - test "user roles permissions for a guest" do - ability = Ability.new nil - - [:create, :destroy].each do |action| - assert ability.cannot?(action, UserRole), "should not be able to #{action} UserRoles" - end - end -end - -class UserAbilityTest < AbilityTest - test "Diary permissions" do - ability = Ability.new create(:user) - - [:index, :rss, :show, :create, :edit, :subscribe, :unsubscribe].each do |action| - assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries" - end - - [:index, :create].each do |action| - assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComments" - end - - [:hide, :unhide].each do |action| - assert ability.cannot?(action, DiaryEntry), "should not be able to #{action} DiaryEntries" - assert ability.cannot?(action, DiaryComment), "should not be able to #{action} DiaryComment" - end - - [:index, :show, :resolve, :ignore, :reopen].each do |action| - assert ability.cannot?(action, Issue), "should not be able to #{action} Issues" - end - end -end - -class ModeratorAbilityTest < AbilityTest - test "Issue permissions" do - ability = Ability.new create(:moderator_user) - - [:index, :show, :resolve, :ignore, :reopen].each do |action| - assert ability.can?(action, Issue), "should be able to #{action} Issues" - end - end - - test "User Roles permissions" do - ability = Ability.new create(:moderator_user) - - [:create, :destroy].each do |action| - assert ability.cannot?(action, UserRole), "should not be able to #{action} UserRoles" - end - - [:hide, :unhide].each do |action| - assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries" - assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComment" - end - end - - test "Active block update permissions" do - creator_user = create(:moderator_user) - other_moderator_user = create(:moderator_user) - block = create(:user_block, :creator => creator_user) - - creator_ability = Ability.new creator_user - assert creator_ability.can?(:edit, block) - assert creator_ability.can?(:update, block) - - other_moderator_ability = Ability.new other_moderator_user - assert other_moderator_ability.can?(:edit, block) - assert other_moderator_ability.can?(:update, block) - end - - test "Expired block update permissions" do - creator_user = create(:moderator_user) - other_moderator_user = create(:moderator_user) - block = create(:user_block, :expired, :creator => creator_user) - - creator_ability = Ability.new creator_user - assert creator_ability.can?(:edit, block) - assert creator_ability.can?(:update, block) - - other_moderator_ability = Ability.new other_moderator_user - assert other_moderator_ability.cannot?(:edit, block) - assert other_moderator_ability.cannot?(:update, block) - end - - test "Revoked block update permissions" do - creator_user = create(:moderator_user) - revoker_user = create(:moderator_user) - other_moderator_user = create(:moderator_user) - block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user) - - creator_ability = Ability.new creator_user - assert creator_ability.can?(:edit, block) - assert creator_ability.can?(:update, block) - - revoker_ability = Ability.new revoker_user - assert revoker_ability.can?(:edit, block) - assert revoker_ability.can?(:update, block) - - other_moderator_ability = Ability.new other_moderator_user - assert other_moderator_ability.cannot?(:edit, block) - assert other_moderator_ability.cannot?(:update, block) - end -end - -class AdministratorAbilityTest < AbilityTest - test "Diary for an administrator" do - ability = Ability.new create(:administrator_user) - [:index, :rss, :show, :create, :edit, :subscribe, :unsubscribe, :hide, :unhide].each do |action| - assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries" - end - - [:index, :create, :hide, :unhide].each do |action| - assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComments" - end - end - - test "User Roles permissions for an administrator" do - ability = Ability.new create(:administrator_user) - - [:create, :destroy].each do |action| - assert ability.can?(action, UserRole), "should be able to #{action} UserRoles" - end - end -end diff --git a/test/abilities/administrator_ability_test.rb b/test/abilities/administrator_ability_test.rb new file mode 100644 index 000000000..27ba4fb95 --- /dev/null +++ b/test/abilities/administrator_ability_test.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require "test_helper" + +class AdministratorAbilityTest < ActiveSupport::TestCase + test "Diary for an administrator" do + ability = Ability.new create(:administrator_user) + [:index, :rss, :show, :create, :edit, :subscribe, :unsubscribe, :hide, :unhide].each do |action| + assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries" + end + + [:index, :create, :hide, :unhide].each do |action| + assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComments" + end + end + + test "User Roles permissions for an administrator" do + ability = Ability.new create(:administrator_user) + + [:create, :destroy].each do |action| + assert ability.can?(action, UserRole), "should be able to #{action} UserRoles" + end + end +end diff --git a/test/abilities/api_abilities_test.rb b/test/abilities/api_abilities_test.rb deleted file mode 100644 index 38154174c..000000000 --- a/test/abilities/api_abilities_test.rb +++ /dev/null @@ -1,49 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class ApiAbilityTest < ActiveSupport::TestCase -end - -class GuestApiAbilityTest < ApiAbilityTest - test "note permissions for a guest" do - scopes = Set.new - ability = ApiAbility.new nil, scopes - - [:index, :create, :feed, :show, :search].each do |action| - assert ability.can?(action, Note), "should be able to #{action} Notes" - end - - [:comment, :close, :reopen, :destroy].each do |action| - assert ability.cannot?(action, Note), "should not be able to #{action} Notes" - end - end -end - -class UserApiAbilityTest < ApiAbilityTest - test "Note permissions" do - user = create(:user) - scopes = Set.new %w[write_notes] - ability = ApiAbility.new user, scopes - - [:index, :create, :comment, :feed, :show, :search, :close, :reopen].each do |action| - assert ability.can?(action, Note), "should be able to #{action} Notes" - end - - [:destroy].each do |action| - assert ability.cannot?(action, Note), "should not be able to #{action} Notes" - end - end -end - -class ModeratorApiAbilityTest < ApiAbilityTest - test "Note permissions" do - user = create(:moderator_user) - scopes = Set.new %w[write_notes] - ability = ApiAbility.new user, scopes - - [:index, :create, :comment, :feed, :show, :search, :close, :reopen, :destroy].each do |action| - assert ability.can?(action, Note), "should be able to #{action} Notes" - end - end -end diff --git a/test/abilities/api_capability_test.rb b/test/abilities/api_capability_test.rb deleted file mode 100644 index 0945b6290..000000000 --- a/test/abilities/api_capability_test.rb +++ /dev/null @@ -1,125 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class ChangesetCommentApiCapabilityTest < ActiveSupport::TestCase - test "as a normal user without scopes" do - user = create(:user) - scopes = Set.new - ability = ApiAbility.new user, scopes - - assert ability.cannot? :create, ChangesetComment - assert ability.cannot? :create, :changeset_comment_visibility - assert ability.cannot? :destroy, :changeset_comment_visibility - end - - test "as a normal user with write_changeset_comments scope" do - user = create(:user) - scopes = Set.new %w[write_changeset_comments] - ability = ApiAbility.new user, scopes - - assert ability.can? :create, ChangesetComment - assert ability.cannot? :create, :changeset_comment_visibility - assert ability.cannot? :destroy, :changeset_comment_visibility - end - - test "as a moderator without scopes" do - user = create(:moderator_user) - scopes = Set.new - ability = ApiAbility.new user, scopes - - assert ability.cannot? :create, ChangesetComment - assert ability.cannot? :create, :changeset_comment_visibility - assert ability.cannot? :destroy, :changeset_comment_visibility - end - - test "as a moderator with write_changeset_comments scope" do - user = create(:moderator_user) - scopes = Set.new %w[write_changeset_comments] - ability = ApiAbility.new user, scopes - - assert ability.can? :create, ChangesetComment - assert ability.can? :create, :changeset_comment_visibility - assert ability.can? :destroy, :changeset_comment_visibility - end -end - -class NoteApiCapabilityTest < ActiveSupport::TestCase - test "as a normal user without scopes" do - user = create(:user) - scopes = Set.new - ability = ApiAbility.new user, scopes - - [:create, :comment, :close, :reopen, :destroy].each do |action| - assert ability.cannot? action, Note - end - end - - test "as a normal user with write_notes scope" do - user = create(:user) - scopes = Set.new %w[write_notes] - ability = ApiAbility.new user, scopes - - [:destroy].each do |action| - assert ability.cannot? action, Note - end - - [:create, :comment, :close, :reopen].each do |action| - assert ability.can? action, Note - end - end - - test "as a moderator without scopes" do - user = create(:moderator_user) - scopes = Set.new - ability = ApiAbility.new user, scopes - - [:destroy].each do |action| - assert ability.cannot? action, Note - end - end - - test "as a moderator with write_notes scope" do - user = create(:moderator_user) - scopes = Set.new %w[write_notes] - ability = ApiAbility.new user, scopes - - [:destroy].each do |action| - assert ability.can? action, Note - end - end -end - -class UserApiCapabilityTest < ActiveSupport::TestCase - test "user preferences" do - user = create(:user) - scopes = Set.new - ability = ApiAbility.new user, scopes - - [:index, :show, :update_all, :update, :destroy].each do |act| - assert ability.cannot? act, UserPreference - end - - scopes = Set.new %w[read_prefs] - ability = ApiAbility.new user, scopes - - [:update_all, :update, :destroy].each do |act| - assert ability.cannot? act, UserPreference - end - - [:index, :show].each do |act| - assert ability.can? act, UserPreference - end - - scopes = Set.new %w[write_prefs] - ability = ApiAbility.new user, scopes - - [:index, :show].each do |act| - assert ability.cannot? act, UserPreference - end - - [:update_all, :update, :destroy].each do |act| - assert ability.can? act, UserPreference - end - end -end diff --git a/test/abilities/changeset_comment_api_capability_test.rb b/test/abilities/changeset_comment_api_capability_test.rb new file mode 100644 index 000000000..f15b29a5e --- /dev/null +++ b/test/abilities/changeset_comment_api_capability_test.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require "test_helper" + +class ChangesetCommentApiCapabilityTest < ActiveSupport::TestCase + test "as a normal user without scopes" do + user = create(:user) + scopes = Set.new + ability = ApiAbility.new user, scopes + + assert ability.cannot? :create, ChangesetComment + assert ability.cannot? :create, :changeset_comment_visibility + assert ability.cannot? :destroy, :changeset_comment_visibility + end + + test "as a normal user with write_changeset_comments scope" do + user = create(:user) + scopes = Set.new %w[write_changeset_comments] + ability = ApiAbility.new user, scopes + + assert ability.can? :create, ChangesetComment + assert ability.cannot? :create, :changeset_comment_visibility + assert ability.cannot? :destroy, :changeset_comment_visibility + end + + test "as a moderator without scopes" do + user = create(:moderator_user) + scopes = Set.new + ability = ApiAbility.new user, scopes + + assert ability.cannot? :create, ChangesetComment + assert ability.cannot? :create, :changeset_comment_visibility + assert ability.cannot? :destroy, :changeset_comment_visibility + end + + test "as a moderator with write_changeset_comments scope" do + user = create(:moderator_user) + scopes = Set.new %w[write_changeset_comments] + ability = ApiAbility.new user, scopes + + assert ability.can? :create, ChangesetComment + assert ability.can? :create, :changeset_comment_visibility + assert ability.can? :destroy, :changeset_comment_visibility + end +end diff --git a/test/abilities/guest_ability_test.rb b/test/abilities/guest_ability_test.rb new file mode 100644 index 000000000..d6bd12728 --- /dev/null +++ b/test/abilities/guest_ability_test.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require "test_helper" + +class GuestAbilityTest < ActiveSupport::TestCase + test "search permissions for a guest" do + ability = Ability.new nil + + [:create, :show].each do |action| + assert ability.can?(action, :search), "should be able to #{action} searches" + end + end + + test "diary permissions for a guest" do + ability = Ability.new nil + [:index, :rss, :show].each do |action| + assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries" + end + + [:index].each do |action| + assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComments" + end + + [:create, :edit, :subscribe, :unsubscribe, :hide, :unhide].each do |action| + assert ability.cannot?(action, DiaryEntry), "should not be able to #{action} DiaryEntries" + end + + [:create, :hide, :unhide].each do |action| + assert ability.cannot?(action, DiaryComment), "should not be able to #{action} DiaryComments" + end + end + + test "note permissions for a guest" do + ability = Ability.new nil + + [:index].each do |action| + assert ability.can?(action, Note), "should be able to #{action} Notes" + end + end + + test "user roles permissions for a guest" do + ability = Ability.new nil + + [:create, :destroy].each do |action| + assert ability.cannot?(action, UserRole), "should not be able to #{action} UserRoles" + end + end +end diff --git a/test/abilities/guest_api_ability_test.rb b/test/abilities/guest_api_ability_test.rb new file mode 100644 index 000000000..b0d76f35b --- /dev/null +++ b/test/abilities/guest_api_ability_test.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require "test_helper" + +class GuestApiAbilityTest < ActiveSupport::TestCase + test "note permissions for a guest" do + scopes = Set.new + ability = ApiAbility.new nil, scopes + + [:index, :create, :feed, :show, :search].each do |action| + assert ability.can?(action, Note), "should be able to #{action} Notes" + end + + [:comment, :close, :reopen, :destroy].each do |action| + assert ability.cannot?(action, Note), "should not be able to #{action} Notes" + end + end +end diff --git a/test/abilities/moderator_ability_test.rb b/test/abilities/moderator_ability_test.rb new file mode 100644 index 000000000..b93c0c5f9 --- /dev/null +++ b/test/abilities/moderator_ability_test.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +require "test_helper" + +class ModeratorAbilityTest < ActiveSupport::TestCase + test "Issue permissions" do + ability = Ability.new create(:moderator_user) + + [:index, :show, :resolve, :ignore, :reopen].each do |action| + assert ability.can?(action, Issue), "should be able to #{action} Issues" + end + end + + test "User Roles permissions" do + ability = Ability.new create(:moderator_user) + + [:create, :destroy].each do |action| + assert ability.cannot?(action, UserRole), "should not be able to #{action} UserRoles" + end + + [:hide, :unhide].each do |action| + assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries" + assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComment" + end + end + + test "Active block update permissions" do + creator_user = create(:moderator_user) + other_moderator_user = create(:moderator_user) + block = create(:user_block, :creator => creator_user) + + creator_ability = Ability.new creator_user + assert creator_ability.can?(:edit, block) + assert creator_ability.can?(:update, block) + + other_moderator_ability = Ability.new other_moderator_user + assert other_moderator_ability.can?(:edit, block) + assert other_moderator_ability.can?(:update, block) + end + + test "Expired block update permissions" do + creator_user = create(:moderator_user) + other_moderator_user = create(:moderator_user) + block = create(:user_block, :expired, :creator => creator_user) + + creator_ability = Ability.new creator_user + assert creator_ability.can?(:edit, block) + assert creator_ability.can?(:update, block) + + other_moderator_ability = Ability.new other_moderator_user + assert other_moderator_ability.cannot?(:edit, block) + assert other_moderator_ability.cannot?(:update, block) + end + + test "Revoked block update permissions" do + creator_user = create(:moderator_user) + revoker_user = create(:moderator_user) + other_moderator_user = create(:moderator_user) + block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user) + + creator_ability = Ability.new creator_user + assert creator_ability.can?(:edit, block) + assert creator_ability.can?(:update, block) + + revoker_ability = Ability.new revoker_user + assert revoker_ability.can?(:edit, block) + assert revoker_ability.can?(:update, block) + + other_moderator_ability = Ability.new other_moderator_user + assert other_moderator_ability.cannot?(:edit, block) + assert other_moderator_ability.cannot?(:update, block) + end +end diff --git a/test/abilities/moderator_api_ability_test.rb b/test/abilities/moderator_api_ability_test.rb new file mode 100644 index 000000000..b4ec509f5 --- /dev/null +++ b/test/abilities/moderator_api_ability_test.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require "test_helper" + +class ModeratorApiAbilityTest < ActiveSupport::TestCase + test "Note permissions" do + user = create(:moderator_user) + scopes = Set.new %w[write_notes] + ability = ApiAbility.new user, scopes + + [:index, :create, :comment, :feed, :show, :search, :close, :reopen, :destroy].each do |action| + assert ability.can?(action, Note), "should be able to #{action} Notes" + end + end +end diff --git a/test/abilities/note_api_capability_test.rb b/test/abilities/note_api_capability_test.rb new file mode 100644 index 000000000..1f00b0572 --- /dev/null +++ b/test/abilities/note_api_capability_test.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require "test_helper" + +class NoteApiCapabilityTest < ActiveSupport::TestCase + test "as a normal user without scopes" do + user = create(:user) + scopes = Set.new + ability = ApiAbility.new user, scopes + + [:create, :comment, :close, :reopen, :destroy].each do |action| + assert ability.cannot? action, Note + end + end + + test "as a normal user with write_notes scope" do + user = create(:user) + scopes = Set.new %w[write_notes] + ability = ApiAbility.new user, scopes + + [:destroy].each do |action| + assert ability.cannot? action, Note + end + + [:create, :comment, :close, :reopen].each do |action| + assert ability.can? action, Note + end + end + + test "as a moderator without scopes" do + user = create(:moderator_user) + scopes = Set.new + ability = ApiAbility.new user, scopes + + [:destroy].each do |action| + assert ability.cannot? action, Note + end + end + + test "as a moderator with write_notes scope" do + user = create(:moderator_user) + scopes = Set.new %w[write_notes] + ability = ApiAbility.new user, scopes + + [:destroy].each do |action| + assert ability.can? action, Note + end + end +end diff --git a/test/abilities/user_ability_test.rb b/test/abilities/user_ability_test.rb new file mode 100644 index 000000000..f1c69567c --- /dev/null +++ b/test/abilities/user_ability_test.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require "test_helper" + +class UserAbilityTest < ActiveSupport::TestCase + test "Diary permissions" do + ability = Ability.new create(:user) + + [:index, :rss, :show, :create, :edit, :subscribe, :unsubscribe].each do |action| + assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries" + end + + [:index, :create].each do |action| + assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComments" + end + + [:hide, :unhide].each do |action| + assert ability.cannot?(action, DiaryEntry), "should not be able to #{action} DiaryEntries" + assert ability.cannot?(action, DiaryComment), "should not be able to #{action} DiaryComment" + end + + [:index, :show, :resolve, :ignore, :reopen].each do |action| + assert ability.cannot?(action, Issue), "should not be able to #{action} Issues" + end + end +end diff --git a/test/abilities/user_api_ability_test.rb b/test/abilities/user_api_ability_test.rb new file mode 100644 index 000000000..d4a59480e --- /dev/null +++ b/test/abilities/user_api_ability_test.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require "test_helper" + +class UserApiAbilityTest < ActiveSupport::TestCase + test "Note permissions" do + user = create(:user) + scopes = Set.new %w[write_notes] + ability = ApiAbility.new user, scopes + + [:index, :create, :comment, :feed, :show, :search, :close, :reopen].each do |action| + assert ability.can?(action, Note), "should be able to #{action} Notes" + end + + [:destroy].each do |action| + assert ability.cannot?(action, Note), "should not be able to #{action} Notes" + end + end +end diff --git a/test/abilities/user_api_capability_test.rb b/test/abilities/user_api_capability_test.rb new file mode 100644 index 000000000..e18d8d927 --- /dev/null +++ b/test/abilities/user_api_capability_test.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require "test_helper" + +class UserApiCapabilityTest < ActiveSupport::TestCase + test "user preferences" do + user = create(:user) + scopes = Set.new + ability = ApiAbility.new user, scopes + + [:index, :show, :update_all, :update, :destroy].each do |act| + assert ability.cannot? act, UserPreference + end + + scopes = Set.new %w[read_prefs] + ability = ApiAbility.new user, scopes + + [:update_all, :update, :destroy].each do |act| + assert ability.cannot? act, UserPreference + end + + [:index, :show].each do |act| + assert ability.can? act, UserPreference + end + + scopes = Set.new %w[write_prefs] + ability = ApiAbility.new user, scopes + + [:index, :show].each do |act| + assert ability.cannot? act, UserPreference + end + + [:update_all, :update, :destroy].each do |act| + assert ability.can? act, UserPreference + end + end +end diff --git a/test/system/embed_test.rb b/test/system/embed_test.rb index e906866ea..7bd188ff5 100644 --- a/test/system/embed_test.rb +++ b/test/system/embed_test.rb @@ -8,31 +8,3 @@ class EmbedTest < ApplicationSystemTestCase assert_link "Report a problem" end end - -class GermanEmbedTest < ApplicationSystemTestCase - driven_by_selenium( - "de", - :preferences => { - "intl.accept_languages" => "de" - } - ) - - test "shows localized report link" do - visit export_embed_path - assert_link "Ein Problem melden" - end -end - -class UnknownLanguageEmbedTest < ApplicationSystemTestCase - driven_by_selenium( - "nolang", - :preferences => { - "intl.accept_languages" => "unknown-language" - } - ) - - test "shows report link in fallback language" do - visit export_embed_path - assert_link "Report a problem" - end -end diff --git a/test/system/german_embed_test.rb b/test/system/german_embed_test.rb new file mode 100644 index 000000000..ed59640da --- /dev/null +++ b/test/system/german_embed_test.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require "application_system_test_case" + +class GermanEmbedTest < ApplicationSystemTestCase + driven_by_selenium( + "de", + :preferences => { + "intl.accept_languages" => "de" + } + ) + + test "shows localized report link" do + visit export_embed_path + assert_link "Ein Problem melden" + end +end diff --git a/test/system/unknown_language_embed_test.rb b/test/system/unknown_language_embed_test.rb new file mode 100644 index 000000000..9f5c3964c --- /dev/null +++ b/test/system/unknown_language_embed_test.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require "application_system_test_case" + +class UnknownLanguageEmbedTest < ApplicationSystemTestCase + driven_by_selenium( + "nolang", + :preferences => { + "intl.accept_languages" => "unknown-language" + } + ) + + test "shows report link in fallback language" do + visit export_embed_path + assert_link "Report a problem" + end +end diff --git a/test/validators/characters_validator_test.rb b/test/validators/characters_validator_test.rb index 8d2decff8..6ce640fa2 100644 --- a/test/validators/characters_validator_test.rb +++ b/test/validators/characters_validator_test.rb @@ -2,22 +2,22 @@ require "test_helper" -class InvalidCharsValidatable - include ActiveModel::Validations +class CharactersValidatorTest < ActiveSupport::TestCase + include Rails::Dom::Testing::Assertions::SelectorAssertions - validates :chars, :characters => true - attr_accessor :chars -end + class InvalidCharsValidatable + include ActiveModel::Validations -class InvalidUrlCharsValidatable - include ActiveModel::Validations + validates :chars, :characters => true + attr_accessor :chars + end - validates :chars, :characters => { :url_safe => true } - attr_accessor :chars -end + class InvalidUrlCharsValidatable + include ActiveModel::Validations -class CharactersValidatorTest < ActiveSupport::TestCase - include Rails::Dom::Testing::Assertions::SelectorAssertions + validates :chars, :characters => { :url_safe => true } + attr_accessor :chars + end def test_with_valid_chars c = InvalidCharsValidatable.new diff --git a/test/validators/whitespace_validator_test.rb b/test/validators/whitespace_validator_test.rb index fa8eadcf8..ba0df5982 100644 --- a/test/validators/whitespace_validator_test.rb +++ b/test/validators/whitespace_validator_test.rb @@ -2,22 +2,22 @@ require "test_helper" -class LeadingWhitespaceValidatable - include ActiveModel::Validations +class WhitespaceValidatorTest < ActiveSupport::TestCase + include Rails::Dom::Testing::Assertions::SelectorAssertions - validates :string, :whitespace => { :leading => false } - attr_accessor :string -end + class LeadingWhitespaceValidatable + include ActiveModel::Validations -class TrailingWhitespaceValidatable - include ActiveModel::Validations + validates :string, :whitespace => { :leading => false } + attr_accessor :string + end - validates :string, :whitespace => { :trailing => false } - attr_accessor :string -end + class TrailingWhitespaceValidatable + include ActiveModel::Validations -class WhitespaceValidatorTest < ActiveSupport::TestCase - include Rails::Dom::Testing::Assertions::SelectorAssertions + validates :string, :whitespace => { :trailing => false } + attr_accessor :string + end def test_with_leading_whitespace validator = LeadingWhitespaceValidatable.new -- 2.39.5