From 2ac28bf906e3c7e12bcfb386fc690523a9064655 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Mon, 15 Apr 2019 09:41:17 +0800 Subject: [PATCH] Fix auto-correctable rubocop offenses in test suite --- .rubocop_todo.rspec.yml | 182 +------------ .../issue_1219_zhtw_locale_typo_spec.rb | 2 +- ...1905_exchange_login_from_remote_id_spec.rb | 4 + ...7_remove_invalid_user_foreign_keys_spec.rb | 12 +- ...2333_object_country_already_exists_spec.rb | 21 +- ...ix_notification_email_without_body_spec.rb | 2 + ..._attribute_date_remove_future_past_spec.rb | 2 +- .../update_cti_logs_by_caller_job_spec.rb | 4 +- spec/lib/application_handle_info_spec.rb | 16 +- spec/lib/cache_spec.rb | 62 ++--- spec/lib/core_ext/string_spec.rb | 10 +- spec/lib/html_sanitizer_spec.rb | 18 +- .../otrs/article/attachment_factory_spec.rb | 22 +- spec/lib/import/otrs/article_customer_spec.rb | 6 +- spec/lib/import/otrs/customer_user_spec.rb | 2 +- .../import/otrs/dynamic_field_factory_spec.rb | 6 +- spec/lib/import/otrs/dynamic_field_spec.rb | 6 +- spec/lib/import/otrs/user_factory_spec.rb | 2 +- .../zendesk/object_attribute/base_examples.rb | 1 + spec/lib/ldap/group_spec.rb | 15 +- spec/lib/ldap/user_spec.rb | 8 +- spec/lib/ldap_spec.rb | 10 +- spec/lib/mail/encodings_spec.rb | 4 +- .../ldap_samaccountname_to_uid_spec.rb | 4 +- spec/lib/notification_factory/slack_spec.rb | 11 +- .../lib/notification_factory/template_spec.rb | 2 +- spec/lib/notification_factory_spec.rb | 2 + spec/lib/password_hash_spec.rb | 6 +- spec/lib/report/ticket_generic_time_spec.rb | 2 +- spec/lib/search_index_backend_spec.rb | 9 +- .../sequence/import/ldap/users_spec.rb | 16 +- .../import/ldap/users/lost/deactivate_spec.rb | 2 +- spec/lib/signature_detection_spec.rb | 12 +- spec/lib/stats/ticket_waiting_time_spec.rb | 32 +-- spec/lib/stats_spec.rb | 6 +- spec/lib/upload_cache_spec.rb | 6 +- .../application_model/can_assets_examples.rb | 4 + .../checks_import_examples.rb | 8 +- spec/models/calendar_spec.rb | 13 +- spec/models/channel/email_parser_spec.rb | 18 +- .../channel/filter/match/email_regex_spec.rb | 3 +- spec/models/channel_spec.rb | 2 +- spec/models/concerns/has_groups_examples.rb | 33 +-- .../has_groups_permissions_examples.rb | 3 +- spec/models/concerns/has_roles_examples.rb | 9 +- .../has_search_index_backend_examples.rb | 4 +- .../has_xss_sanitized_note_examples.rb | 1 + spec/models/cti/caller_id_spec.rb | 54 ++-- spec/models/cti/log_spec.rb | 43 ++-- spec/models/email_address_spec.rb | 1 + spec/models/job_spec.rb | 7 +- spec/models/object_lookup_spec.rb | 2 +- .../attribute/validation/backend_spec.rb | 6 +- .../attribute/validation/future_past_spec.rb | 5 +- .../attribute/validation/required_spec.rb | 5 +- .../attribute/validation_spec.rb | 5 +- spec/models/object_manager/attribute_spec.rb | 6 +- spec/models/recent_view_spec.rb | 10 +- spec/models/role_spec.rb | 38 +-- spec/models/setting_spec.rb | 3 +- spec/models/tag_spec.rb | 2 +- spec/models/taskbar_spec.rb | 12 +- spec/models/ticket/article_spec.rb | 10 +- spec/models/ticket/overviews_spec.rb | 2 +- spec/models/ticket/time_accounting_spec.rb | 2 +- spec/models/ticket_spec.rb | 50 ++-- spec/models/token_spec.rb | 7 +- spec/models/translation_spec.rb | 118 ++++----- spec/models/trigger/sms_spec.rb | 4 +- spec/models/trigger_spec.rb | 27 +- spec/models/type_lookup_spec.rb | 2 +- spec/models/user_spec.rb | 86 ++++--- spec/requests/api_auth_on_behalf_of_spec.rb | 22 +- spec/requests/api_auth_spec.rb | 100 ++++---- spec/requests/basic_spec.rb | 28 +- spec/requests/calendar_spec.rb | 16 +- spec/requests/external_credentials_spec.rb | 52 ++-- spec/requests/form_spec.rb | 42 +-- spec/requests/integration/check_mk_spec.rb | 30 +-- spec/requests/integration/cti_spec.rb | 54 ++-- spec/requests/integration/idoit_spec.rb | 26 +- spec/requests/integration/monitoring_spec.rb | 154 +++++------ .../object_manager_attributes_spec.rb | 50 ++-- spec/requests/integration/placetel_spec.rb | 54 ++-- spec/requests/integration/sipgate_spec.rb | 48 ++-- spec/requests/integration/telegram_spec.rb | 52 ++-- spec/requests/integration/twilio_sms_spec.rb | 20 +- .../integration/twitter_webhook_spec.rb | 41 +-- spec/requests/integration/user_device_spec.rb | 52 ++-- spec/requests/long_polling_spec.rb | 30 +-- spec/requests/o_auth_spec.rb | 6 +- spec/requests/organization_spec.rb | 90 +++---- spec/requests/overview_spec.rb | 10 +- spec/requests/package_spec.rb | 12 +- spec/requests/report_spec.rb | 34 +-- spec/requests/search_spec.rb | 46 ++-- spec/requests/settings_spec.rb | 38 +-- spec/requests/sla_spec.rb | 8 +- spec/requests/taskbar_spec.rb | 10 +- spec/requests/text_module_spec.rb | 20 +- .../ticket/article_attachments_spec.rb | 22 +- spec/requests/ticket/article_spec.rb | 40 +-- spec/requests/ticket/escalation_spec.rb | 4 +- spec/requests/ticket_spec.rb | 215 ++++++++-------- spec/requests/time_accounting_spec.rb | 4 +- spec/requests/upload_cache_spec.rb | 4 +- spec/requests/user/organization_spec.rb | 34 +-- spec/requests/user/permission_spec.rb | 16 +- spec/requests/user_spec.rb | 242 +++++++++--------- spec/scripts/websocket_server_spec.rb | 4 + spec/system/basic/authentication_spec.rb | 4 +- spec/system/basic/redirects_spec.rb | 8 +- spec/system/basic/richtext_spec.rb | 8 +- spec/system/js/q_unit_spec.rb | 38 +-- spec/system/setup/auto_wizard_spec.rb | 2 +- spec/system/setup/mail_accounts_spec.rb | 4 +- spec/system/setup/system_spec.rb | 2 +- spec/system/ticket/create_spec.rb | 2 +- spec/system/ticket/update_spec.rb | 6 +- 119 files changed, 1405 insertions(+), 1531 deletions(-) diff --git a/.rubocop_todo.rspec.yml b/.rubocop_todo.rspec.yml index eab54855d8e9..28bb3b226bde 100644 --- a/.rubocop_todo.rspec.yml +++ b/.rubocop_todo.rspec.yml @@ -6,41 +6,6 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 36 -# Cop supports --auto-correct. -# Configuration parameters: EnabledMethods. -Capybara/FeatureMethods: - Exclude: - - 'spec/system/basic/authentication_spec.rb' - - 'spec/system/basic/redirects_spec.rb' - - 'spec/system/basic/richtext_spec.rb' - - 'spec/system/js/q_unit_spec.rb' - - 'spec/system/setup/auto_wizard_spec.rb' - - 'spec/system/setup/mail_accounts_spec.rb' - - 'spec/system/setup/system_spec.rb' - - 'spec/system/ticket/create_spec.rb' - - 'spec/system/ticket/update_spec.rb' - -# Offense count: 39 -# Cop supports --auto-correct. -# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment. -Layout/ExtraSpacing: - Exclude: - - 'spec/factories/job.rb' - - 'spec/factories/postmaster_filter.rb' - - 'spec/factories/ticket.rb' - - 'spec/factories/ticket/article.rb' - - 'spec/lib/sequencer/unit/import/zendesk/sub_sequence/base_examples.rb' - - 'spec/models/calendar_spec.rb' - - 'spec/models/channel/email_parser_spec.rb' - - 'spec/models/cti/caller_id_spec.rb' - - 'spec/models/job_spec.rb' - - 'spec/models/recent_view_spec.rb' - - 'spec/models/role_spec.rb' - - 'spec/models/ticket_spec.rb' - - 'spec/models/trigger_spec.rb' - - 'spec/requests/ticket_spec.rb' - # Offense count: 43 Lint/UselessAssignment: Enabled: false @@ -53,7 +18,7 @@ Lint/UselessAssignment: # Configuration parameters: CountComments, ExcludedMethods. # ExcludedMethods: refine Metrics/BlockLength: - Max: 1969 + Max: 1987 # Offense count: 16 RSpec/AnyInstance: @@ -84,48 +49,6 @@ RSpec/BeforeAfterAll: RSpec/ContextWording: Enabled: false -# Offense count: 1 -RSpec/DescribeClass: - Exclude: - - 'spec/scripts/websocket_server_spec.rb' - -# Offense count: 207 -# Cop supports --auto-correct. -# Configuration parameters: SkipBlocks, EnforcedStyle. -# SupportedStyles: described_class, explicit -RSpec/DescribedClass: - Enabled: false - -# Offense count: 15 -# Cop supports --auto-correct. -RSpec/EmptyLineAfterFinalLet: - Exclude: - - 'spec/db/migrate/issue_1905_exchange_login_from_remote_id_spec.rb' - - 'spec/db/migrate/issue_2541_fix_notification_email_without_body_spec.rb' - - 'spec/lib/import/zendesk/object_attribute/base_examples.rb' - - 'spec/models/cti/log_spec.rb' - - 'spec/models/object_manager/attribute/validation/future_past_spec.rb' - - 'spec/models/object_manager/attribute/validation/required_spec.rb' - - 'spec/models/ticket_spec.rb' - - 'spec/models/user_spec.rb' - - 'spec/requests/integration/twitter_webhook_spec.rb' - -# Offense count: 25 -# Cop supports --auto-correct. -RSpec/EmptyLineAfterHook: - Exclude: - - 'spec/lib/notification_factory/slack_spec.rb' - - 'spec/lib/notification_factory_spec.rb' - - 'spec/models/role_spec.rb' - - 'spec/models/ticket_spec.rb' - - 'spec/models/trigger_spec.rb' - - 'spec/models/user_spec.rb' - -# Offense count: 34 -# Cop supports --auto-correct. -RSpec/EmptyLineAfterSubject: - Enabled: false - # Offense count: 540 # Configuration parameters: Max. RSpec/ExampleLength: @@ -144,13 +67,6 @@ RSpec/ExpectActual: - 'spec/requests/user/organization_spec.rb' - 'spec/requests/user_spec.rb' -# Offense count: 99 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: method_call, block -RSpec/ExpectChange: - Enabled: false - # Offense count: 3 RSpec/ExpectInHook: Exclude: @@ -175,20 +91,6 @@ RSpec/FilePath: - 'spec/db/migrate/issue_2541_fix_notification_email_without_body_spec.rb' - 'spec/lib/import/base_factory_spec.rb' -# Offense count: 30 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: implicit, each, example -RSpec/HookArgument: - Enabled: false - -# Offense count: 4 -# Cop supports --auto-correct. -RSpec/HooksBeforeExamples: - Exclude: - - 'spec/models/concerns/has_groups_examples.rb' - - 'spec/models/trigger_spec.rb' - # Offense count: 60 # Configuration parameters: AssignmentOnly. RSpec/InstanceVariable: @@ -205,36 +107,6 @@ RSpec/InstanceVariable: - 'spec/requests/ticket/article_attachments_spec.rb' - 'spec/requests/user_spec.rb' -# Offense count: 2 -RSpec/IteratedExpectation: - Exclude: - - 'spec/jobs/update_cti_logs_by_caller_job_spec.rb' - -# Offense count: 10 -# Cop supports --auto-correct. -RSpec/LeadingSubject: - Exclude: - - 'spec/lib/notification_factory/slack_spec.rb' - - 'spec/models/object_manager/attribute/validation/backend_spec.rb' - - 'spec/models/object_manager/attribute/validation/future_past_spec.rb' - - 'spec/models/object_manager/attribute/validation/required_spec.rb' - - 'spec/models/role_spec.rb' - - 'spec/models/ticket/article_spec.rb' - - 'spec/models/trigger_spec.rb' - - 'spec/models/user_spec.rb' - -# Offense count: 15 -# Cop supports --auto-correct. -RSpec/LetBeforeExamples: - Exclude: - - 'spec/lib/import/otrs/article/attachment_factory_spec.rb' - - 'spec/lib/import/otrs/dynamic_field_factory_spec.rb' - - 'spec/lib/import/otrs/dynamic_field_spec.rb' - - 'spec/lib/ldap/group_spec.rb' - - 'spec/lib/ldap/user_spec.rb' - - 'spec/lib/ldap_spec.rb' - - 'spec/models/user_spec.rb' - # Offense count: 34 RSpec/LetSetup: Enabled: false @@ -274,40 +146,6 @@ RSpec/NamedSubject: RSpec/NestedGroups: Max: 8 -# Offense count: 28 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: not_to, to_not -RSpec/NotToNot: - Exclude: - - 'spec/db/migrate/object_manager_attribute_date_remove_future_past_spec.rb' - - 'spec/lib/import/otrs/user_factory_spec.rb' - - 'spec/lib/migration_job/ldap_samaccountname_to_uid_spec.rb' - - 'spec/lib/report/ticket_generic_time_spec.rb' - - 'spec/lib/stats_spec.rb' - - 'spec/models/object_manager/attribute/validation_spec.rb' - - 'spec/models/object_manager/attribute_spec.rb' - - 'spec/requests/integration/idoit_spec.rb' - - 'spec/requests/integration/twilio_sms_spec.rb' - - 'spec/requests/integration/user_device_spec.rb' - - 'spec/requests/search_spec.rb' - - 'spec/requests/text_module_spec.rb' - - 'spec/requests/ticket/article_spec.rb' - -# Offense count: 81 -# Cop supports --auto-correct. -# Configuration parameters: Strict, EnforcedStyle. -# SupportedStyles: inflected, explicit -RSpec/PredicateMatcher: - Exclude: - - 'spec/lib/password_hash_spec.rb' - - 'spec/models/trigger/sms_spec.rb' - - 'spec/requests/api_auth_on_behalf_of_spec.rb' - - 'spec/requests/api_auth_spec.rb' - - 'spec/requests/integration/monitoring_spec.rb' - - 'spec/requests/organization_spec.rb' - - 'spec/requests/user_spec.rb' - # Offense count: 12 RSpec/RepeatedDescription: Exclude: @@ -315,11 +153,6 @@ RSpec/RepeatedDescription: - 'spec/requests/form_spec.rb' - 'spec/requests/ticket_spec.rb' -# Offense count: 2 -RSpec/RepeatedExample: - Exclude: - - 'spec/models/translation_spec.rb' - # Offense count: 3 RSpec/ScatteredLet: Exclude: @@ -347,16 +180,3 @@ RSpec/SubjectStub: # Configuration parameters: IgnoreNameless, IgnoreSymbolicNames. RSpec/VerifiedDoubles: Enabled: false - -# Offense count: 2 -RSpec/VoidExpect: - Exclude: - - 'spec/lib/ldap/group_spec.rb' - - 'spec/lib/ldap/user_spec.rb' - -# Offense count: 741 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: numeric, symbolic -Rails/HttpStatus: - Enabled: false diff --git a/spec/db/migrate/issue_1219_zhtw_locale_typo_spec.rb b/spec/db/migrate/issue_1219_zhtw_locale_typo_spec.rb index b81bda65123c..d23e06fca29f 100644 --- a/spec/db/migrate/issue_1219_zhtw_locale_typo_spec.rb +++ b/spec/db/migrate/issue_1219_zhtw_locale_typo_spec.rb @@ -5,7 +5,7 @@ let(:translation) { create(:translation, locale: premigrate_locale) } let(:user) { create(:user, preferences: { locale: premigrate_locale }) } - before(:each) do + before do Locale.find_by(name: 'Chinese (Tradi.) (正體中文)')&.destroy stub_const("#{described_class}::CURRENT_VERSION", version) end diff --git a/spec/db/migrate/issue_1905_exchange_login_from_remote_id_spec.rb b/spec/db/migrate/issue_1905_exchange_login_from_remote_id_spec.rb index 55213d7b0fe6..c1514f6a4521 100644 --- a/spec/db/migrate/issue_1905_exchange_login_from_remote_id_spec.rb +++ b/spec/db/migrate/issue_1905_exchange_login_from_remote_id_spec.rb @@ -44,6 +44,7 @@ context 'blank config' do let(:config) { nil } + it_behaves_like 'irrelevant config' end @@ -53,6 +54,7 @@ some: 'config' } end + it_behaves_like 'irrelevant config' end @@ -64,6 +66,7 @@ } } end + it_behaves_like 'irrelevant config' end @@ -76,6 +79,7 @@ } } end + it_behaves_like 'irrelevant config' end end diff --git a/spec/db/migrate/issue_1977_remove_invalid_user_foreign_keys_spec.rb b/spec/db/migrate/issue_1977_remove_invalid_user_foreign_keys_spec.rb index 977694090437..1c009da28c6b 100644 --- a/spec/db/migrate/issue_1977_remove_invalid_user_foreign_keys_spec.rb +++ b/spec/db/migrate/issue_1977_remove_invalid_user_foreign_keys_spec.rb @@ -16,9 +16,7 @@ expect do migrate - end.to change { - OnlineNotification.count - }.by(-1) + end.to change(OnlineNotification, :count).by(-1) end it 'cleans up RecentView#created_by_id', db_strategy: :reset do @@ -30,9 +28,7 @@ expect do migrate - end.to change { - RecentView.count - }.by(-1) + end.to change(RecentView, :count).by(-1) end it 'cleans up Avatar#o_id', db_strategy: :reset do @@ -44,9 +40,7 @@ expect do migrate - end.to change { - Avatar.count - }.by(-1) + end.to change(Avatar, :count).by(-1) end end diff --git a/spec/db/migrate/issue_2333_object_country_already_exists_spec.rb b/spec/db/migrate/issue_2333_object_country_already_exists_spec.rb index bc09e56b271b..f939062c79fe 100644 --- a/spec/db/migrate/issue_2333_object_country_already_exists_spec.rb +++ b/spec/db/migrate/issue_2333_object_country_already_exists_spec.rb @@ -1,23 +1,20 @@ require 'rails_helper' RSpec.describe AddCountryAttributeToUsers, type: :db_migration do - context 'AddCountryAttributeToUsers migration' do - - def country_attribute - ObjectManager::Attribute.find_by(object_lookup_id: ObjectLookup.by_name('User'), name: 'country') - end - it 'preserves the existing country attribute' do expect { migrate } - .not_to(change { country_attribute.present? }) + .not_to change { ObjectManager::Attribute.find_by(object_lookup_id: ObjectLookup.by_name('User'), name: 'country') } end - it 'adds the country attribute when it is not present' do - country_attribute.delete - expect { migrate } - .to change { country_attribute.present? } - .from( false ).to( true ) + context 'when country attribute is not present' do + before { ObjectManager::Attribute.find_by(object_lookup_id: ObjectLookup.by_name('User'), name: 'country').delete } + + it 'adds the country attribute when it is not present' do + expect { migrate } + .to change { ObjectManager::Attribute.exists?(object_lookup_id: ObjectLookup.by_name('User'), name: 'country') } + .from(false).to(true) + end end end end diff --git a/spec/db/migrate/issue_2541_fix_notification_email_without_body_spec.rb b/spec/db/migrate/issue_2541_fix_notification_email_without_body_spec.rb index 3542081c0a55..244469a01ab9 100644 --- a/spec/db/migrate/issue_2541_fix_notification_email_without_body_spec.rb +++ b/spec/db/migrate/issue_2541_fix_notification_email_without_body_spec.rb @@ -38,6 +38,7 @@ context 'when migrating Jobs' do subject(:job) { create(:job) } + let(:type) { 'notification.email' } it "updates empty perform['notification.email']['body'] attribute" do @@ -48,6 +49,7 @@ describe 'scheduler management' do let(:scheduler) { Scheduler.find_by(method: 'Job.run') } + before { scheduler.update!(active: false) } it "re-enables 'Job.run' Scheduler" do diff --git a/spec/db/migrate/object_manager_attribute_date_remove_future_past_spec.rb b/spec/db/migrate/object_manager_attribute_date_remove_future_past_spec.rb index f6a9cdde873d..c279d6f31b7d 100644 --- a/spec/db/migrate/object_manager_attribute_date_remove_future_past_spec.rb +++ b/spec/db/migrate/object_manager_attribute_date_remove_future_past_spec.rb @@ -19,7 +19,7 @@ migrate - expect(subject.data_option).to_not include(:past, :future) + expect(subject.data_option).not_to include(:past, :future) end context 'when incomplete data_option is given' do diff --git a/spec/jobs/update_cti_logs_by_caller_job_spec.rb b/spec/jobs/update_cti_logs_by_caller_job_spec.rb index 551c677b5dab..b79a8c1139b1 100644 --- a/spec/jobs/update_cti_logs_by_caller_job_spec.rb +++ b/spec/jobs/update_cti_logs_by_caller_job_spec.rb @@ -14,7 +14,7 @@ it 'updates Cti::Logs from that number with "preferences" => {}' do described_class.perform_now(phone) - log_prefs.each { |p| expect(p).to be_empty } + expect(log_prefs).to all(be_empty) end end @@ -24,7 +24,7 @@ it 'updates Cti::Logs from that number with valid "preferences" hash' do described_class.perform_now(phone) - log_prefs.each { |p| expect(p).to include('from' => a_kind_of(Array)) } + expect(log_prefs).to all(include('from' => a_kind_of(Array))) end end end diff --git a/spec/lib/application_handle_info_spec.rb b/spec/lib/application_handle_info_spec.rb index 1f8255aca5ba..972fef04652a 100644 --- a/spec/lib/application_handle_info_spec.rb +++ b/spec/lib/application_handle_info_spec.rb @@ -3,30 +3,30 @@ RSpec.describe ApplicationHandleInfo do describe '.use' do it 'requires a block' do - expect { ApplicationHandleInfo.use('foo') } + expect { described_class.use('foo') } .to raise_error(ArgumentError) end context 'for a given starting ApplicationHandleInfo' do - before { ApplicationHandleInfo.current = 'foo' } + before { described_class.current = 'foo' } it 'runs the block using the given ApplicationHandleInfo' do - ApplicationHandleInfo.use('bar') do - expect(ApplicationHandleInfo.current).to eq('bar') + described_class.use('bar') do + expect(described_class.current).to eq('bar') end end it 'resets ApplicationHandleInfo to its original value' do - ApplicationHandleInfo.use('bar') {} + described_class.use('bar') {} - expect(ApplicationHandleInfo.current).to eq('foo') + expect(described_class.current).to eq('foo') end context 'when an error is raised in the given block' do it 'does not rescue the error, and still resets ApplicationHandleInfo' do - expect { ApplicationHandleInfo.use('bar') { raise } } + expect { described_class.use('bar') { raise } } .to raise_error(StandardError) - .and not_change { ApplicationHandleInfo.current } + .and not_change { described_class.current } end end end diff --git a/spec/lib/cache_spec.rb b/spec/lib/cache_spec.rb index 92e2c73d363d..daabb3f81370 100644 --- a/spec/lib/cache_spec.rb +++ b/spec/lib/cache_spec.rb @@ -5,14 +5,14 @@ before { allow(Rails.cache).to receive(:read) } it 'wraps Rails.cache.read' do - Cache.get('foo') + described_class.get('foo') expect(Rails.cache).to have_received(:read).with('foo') end context 'with a non-string argument' do it 'passes a string' do - Cache.get(:foo) + described_class.get(:foo) expect(Rails.cache).to have_received(:read).with('foo') end @@ -21,25 +21,25 @@ describe '.write' do it 'stores string values' do - expect { Cache.write('123', 'some value') } - .to change { Cache.get('123') }.to('some value') + expect { described_class.write('123', 'some value') } + .to change { described_class.get('123') }.to('some value') end it 'stores hash values' do - expect { Cache.write('123', { key: 'some value' }) } - .to change { Cache.get('123') }.to({ key: 'some value' }) + expect { described_class.write('123', { key: 'some value' }) } + .to change { described_class.get('123') }.to({ key: 'some value' }) end it 'overwrites previous values' do - Cache.write('123', 'some value') + described_class.write('123', 'some value') - expect { Cache.write('123', { key: 'some value' }) } - .to change { Cache.get('123') }.to({ key: 'some value' }) + expect { described_class.write('123', { key: 'some value' }) } + .to change { described_class.get('123') }.to({ key: 'some value' }) end it 'stores hash values with non-ASCII content' do - expect { Cache.write('123', { key: 'some valueöäüß' }) } - .to change { Cache.get('123') }.to({ key: 'some valueöäüß' }) + expect { described_class.write('123', { key: 'some valueöäüß' }) } + .to change { described_class.get('123') }.to({ key: 'some valueöäüß' }) end context 'when expiring' do @@ -51,58 +51,58 @@ end it 'defaults to expires_in: 7.days' do - Cache.write('123', 'some value') + described_class.write('123', 'some value') - expect { travel 7.days - 1.second }.not_to change { Cache.get('123') } - expect { travel 2.seconds }.to change { Cache.get('123') }.to(nil) + expect { travel 7.days - 1.second }.not_to change { described_class.get('123') } + expect { travel 2.seconds }.to change { described_class.get('123') }.to(nil) end it 'accepts a custom :expires_in option' do - Cache.write('123', 'some value', expires_in: 3.seconds) + described_class.write('123', 'some value', expires_in: 3.seconds) - expect { travel 4.seconds }.to change { Cache.get('123') }.to(nil) + expect { travel 4.seconds }.to change { described_class.get('123') }.to(nil) end end end describe '.delete' do it 'deletes stored values' do - Cache.write('123', 'some value') + described_class.write('123', 'some value') - expect { Cache.delete('123') } - .to change { Cache.get('123') }.to(nil) + expect { described_class.delete('123') } + .to change { described_class.get('123') }.to(nil) end it 'is idempotent' do - Cache.write('123', 'some value') - Cache.delete('123') + described_class.write('123', 'some value') + described_class.delete('123') - expect { Cache.delete('123') }.not_to raise_error + expect { described_class.delete('123') }.not_to raise_error end end describe '.clear' do it 'deletes all stored values' do - Cache.write('123', 'some value') - Cache.write('456', 'some value') + described_class.write('123', 'some value') + described_class.write('456', 'some value') - expect { Cache.clear } - .to change { Cache.get('123') }.to(nil) - .and change { Cache.get('456') }.to(nil) + expect { described_class.clear } + .to change { described_class.get('123') }.to(nil) + .and change { described_class.get('456') }.to(nil) end it 'is idempotent' do - Cache.write('123', 'some value') - Cache.clear + described_class.write('123', 'some value') + described_class.clear - expect { Cache.clear }.not_to raise_error + expect { described_class.clear }.not_to raise_error end context 'when cache directory is not present on disk' do before { FileUtils.rm_rf(Rails.cache.cache_path) } it 'does not raise an error' do - expect { Cache.clear }.not_to raise_error + expect { described_class.clear }.not_to raise_error end end end diff --git a/spec/lib/core_ext/string_spec.rb b/spec/lib/core_ext/string_spec.rb index 30862abb317f..08276ef79c50 100644 --- a/spec/lib/core_ext/string_spec.rb +++ b/spec/lib/core_ext/string_spec.rb @@ -25,8 +25,8 @@ end it 'does not break on non-unicode strings' do - expect(String.new("\xC2\xA92011 Z ", encoding: 'ASCII-8BIT').strip) - .to eq(String.new("\xC2\xA92011 Z", encoding: 'ASCII-8BIT')) + expect(described_class.new("\xC2\xA92011 Z ", encoding: 'ASCII-8BIT').strip) + .to eq(described_class.new("\xC2\xA92011 Z", encoding: 'ASCII-8BIT')) end end end @@ -56,9 +56,9 @@ end it 'does not break on invalid-unicode strings (in place)' do - str = String.new("\xC2\xA92011 Z ", encoding: 'ASCII-8BIT') + str = described_class.new("\xC2\xA92011 Z ", encoding: 'ASCII-8BIT') expect(str.strip!) - .to be(str).and eq(String.new("\xC2\xA92011 Z", encoding: 'ASCII-8BIT')) + .to be(str).and eq(described_class.new("\xC2\xA92011 Z", encoding: 'ASCII-8BIT')) end end end @@ -1844,7 +1844,7 @@ end context 'which are incorrectly set to other, technically valid encodings' do - let(:subject) { String.new('ö', encoding: 'tis-620') } + let(:subject) { described_class.new('ö', encoding: 'tis-620') } it 'sets input encoding to UTF-8 instead of attempting conversion' do expect(subject.utf8_encode).to eq(subject.dup.force_encoding('utf-8')) diff --git a/spec/lib/html_sanitizer_spec.rb b/spec/lib/html_sanitizer_spec.rb index ee214e8ebfd0..cbf0a69ab18c 100644 --- a/spec/lib/html_sanitizer_spec.rb +++ b/spec/lib/html_sanitizer_spec.rb @@ -4,8 +4,8 @@ RSpec.describe HtmlSanitizer do describe '.replace_inline_images' do - let(:body) { HtmlSanitizer.replace_inline_images(html).first } - let(:inline_attachments) { HtmlSanitizer.replace_inline_images(html).last } + let(:body) { described_class.replace_inline_images(html).first } + let(:inline_attachments) { described_class.replace_inline_images(html).last } context 'for image at absolute path' do let(:html) { 'abc' } @@ -142,7 +142,7 @@ context 'for image at absolute path' do context 'with src attr last' do it 'add max-width: 100% rule to style attr' do - expect(HtmlSanitizer.dynamic_image_size(<<~HTML.chomp)).to match(Regexp.new(<<~REGEX.chomp)) + expect(described_class.dynamic_image_size(<<~HTML.chomp)).to match(Regexp.new(<<~REGEX.chomp)) HTML @@ -152,7 +152,7 @@ context 'with src attr first' do it 'add max-width: 100% rule to style attr' do - expect(HtmlSanitizer.dynamic_image_size(<<~HTML.chomp)).to match(Regexp.new(<<~REGEX.chomp)) + expect(described_class.dynamic_image_size(<<~HTML.chomp)).to match(Regexp.new(<<~REGEX.chomp)) abc HTML abc @@ -164,7 +164,7 @@ context 'for base64-encoded inline images' do context 'with src attr last' do it 'add max-width: 100% rule to style attr' do - expect(HtmlSanitizer.dynamic_image_size(<<~HTML.chomp)).to match(Regexp.new(<<~REGEX.chomp)) + expect(described_class.dynamic_image_size(<<~HTML.chomp)).to match(Regexp.new(<<~REGEX.chomp)) abc HTML abc @@ -174,7 +174,7 @@ context 'with src attr first' do it 'add max-width: 100% rule to style attr' do - expect(HtmlSanitizer.dynamic_image_size(<<~HTML.chomp)).to match(Regexp.new(<<~REGEX.chomp)) + expect(described_class.dynamic_image_size(<<~HTML.chomp)).to match(Regexp.new(<<~REGEX.chomp)) abc HTML abc @@ -190,14 +190,14 @@ before { allow(Timeout).to receive(:timeout).and_raise(Timeout::Error) } it 'returns a timeout error message for the user' do - expect(HtmlSanitizer.strict(+'', true)) + expect(described_class.strict(+'', true)) .to match(HtmlSanitizer::UNPROCESSABLE_HTML_MSG) end end context 'with href links that contain square brackets' do it 'correctly URL encodes them' do - expect(HtmlSanitizer.strict(+'example', true)) + expect(described_class.strict(+'example', true)) .to eq('example') end end @@ -208,7 +208,7 @@ before { allow(Timeout).to receive(:timeout).and_raise(Timeout::Error) } it 'returns a timeout error message for the user' do - expect(HtmlSanitizer.cleanup(+'')) + expect(described_class.cleanup(+'')) .to match(HtmlSanitizer::UNPROCESSABLE_HTML_MSG) end end diff --git a/spec/lib/import/otrs/article/attachment_factory_spec.rb b/spec/lib/import/otrs/article/attachment_factory_spec.rb index 11d5cb3de134..ef2ea0c1fbb7 100644 --- a/spec/lib/import/otrs/article/attachment_factory_spec.rb +++ b/spec/lib/import/otrs/article/attachment_factory_spec.rb @@ -2,13 +2,13 @@ require 'lib/import/import_factory_examples' RSpec.describe Import::OTRS::Article::AttachmentFactory do - it_behaves_like 'Import factory' - - def load_attachment_json(file) - json_fixture("import/otrs/article/attachment/#{file}") + let(:start_import) do + described_class.import( + attachments: attachments, + local_article: local_article + ) end - let(:local_article) { instance_double(Ticket::Article, ticket_id: 1337, id: 42) } let(:attachments) do [ load_attachment_json('default'), @@ -16,11 +16,11 @@ def load_attachment_json(file) load_attachment_json('default') ] end - let(:start_import) do - described_class.import( - attachments: attachments, - local_article: local_article - ) + + let(:local_article) { instance_double(Ticket::Article, ticket_id: 1337, id: 42) } + + def load_attachment_json(file) + json_fixture("import/otrs/article/attachment/#{file}") end def import_expectations @@ -34,6 +34,8 @@ def article_attachment_expectations(article_attachments) expect(local_article).to receive(:attachments).and_return(article_attachments) end + it_behaves_like 'Import factory' + it 'imports' do article_attachment_expectations([]) import_expectations diff --git a/spec/lib/import/otrs/article_customer_spec.rb b/spec/lib/import/otrs/article_customer_spec.rb index 0645271b0205..4e6771b5f0ac 100644 --- a/spec/lib/import/otrs/article_customer_spec.rb +++ b/spec/lib/import/otrs/article_customer_spec.rb @@ -32,18 +32,18 @@ def load_article_json(file) end it 'creates customers with special encoding in name' do - expect { described_class.new(load_article_json('customer_special_chars')) }.to change { User.count }.by(1) + expect { described_class.new(load_article_json('customer_special_chars')) }.to change(User, :count).by(1) expect(User.last.login).to eq('user.hernandez@example.com') end it 'creates customers with special from email syntax' do - expect { described_class.new(load_article_json('from_bracket_email_syntax')) }.to change { User.count }.by(1) + expect { described_class.new(load_article_json('from_bracket_email_syntax')) }.to change(User, :count).by(1) expect(User.last.login).to eq('user@example.com') end it 'converts emails to downcase' do Setting.set('import_mode', true) - expect { described_class.new(load_article_json('from_capital_case')) }.to change { User.count }.by(1) + expect { described_class.new(load_article_json('from_capital_case')) }.to change(User, :count).by(1) expect(User.last.email).to eq('user@example.com') expect(User.last.login).to eq('user@example.com') end diff --git a/spec/lib/import/otrs/customer_user_spec.rb b/spec/lib/import/otrs/customer_user_spec.rb index 60b5d3bcc608..533a3905f84c 100644 --- a/spec/lib/import/otrs/customer_user_spec.rb +++ b/spec/lib/import/otrs/customer_user_spec.rb @@ -104,7 +104,7 @@ def load_customer_json(file) } end - before(:each) do + before do travel_to DateTime.current end diff --git a/spec/lib/import/otrs/dynamic_field_factory_spec.rb b/spec/lib/import/otrs/dynamic_field_factory_spec.rb index 98a1e3b51406..8ad3d3a07501 100644 --- a/spec/lib/import/otrs/dynamic_field_factory_spec.rb +++ b/spec/lib/import/otrs/dynamic_field_factory_spec.rb @@ -3,10 +3,10 @@ require 'lib/import/otrs/dynamic_field_examples' RSpec.describe Import::OTRS::DynamicFieldFactory do - it_behaves_like 'Import::Factory' - - let(:start_import_test) { described_class.import(object_structure) } let(:object_structure) { [load_dynamic_field_json('text/default')] } + let(:start_import_test) { described_class.import(object_structure) } + + it_behaves_like 'Import::Factory' it 'responds to skip_field?' do expect(described_class).to respond_to('skip_field?') diff --git a/spec/lib/import/otrs/dynamic_field_spec.rb b/spec/lib/import/otrs/dynamic_field_spec.rb index b61145df265a..411f4af37716 100644 --- a/spec/lib/import/otrs/dynamic_field_spec.rb +++ b/spec/lib/import/otrs/dynamic_field_spec.rb @@ -2,10 +2,10 @@ require 'lib/import/otrs/dynamic_field_examples' RSpec.describe Import::OTRS::DynamicField do - it_behaves_like 'Import::OTRS::DynamicField' - - let(:start_import_test) { described_class.new(object_structure) } let(:object_structure) { load_dynamic_field_json('text/default') } + let(:start_import_test) { described_class.new(object_structure) } + + it_behaves_like 'Import::OTRS::DynamicField' it 'requires an implementation of init_callback' do expect(ObjectManager::Attribute).to receive(:get).and_return(false) diff --git a/spec/lib/import/otrs/user_factory_spec.rb b/spec/lib/import/otrs/user_factory_spec.rb index 812117732432..ad2aa3c558d4 100644 --- a/spec/lib/import/otrs/user_factory_spec.rb +++ b/spec/lib/import/otrs/user_factory_spec.rb @@ -7,7 +7,7 @@ it 'skips root@localhost' do root_data = json_fixture('import/otrs/user/default') - expect(Import::OTRS::User).to_not receive(:new) + expect(Import::OTRS::User).not_to receive(:new) described_class.import([root_data]) end diff --git a/spec/lib/import/zendesk/object_attribute/base_examples.rb b/spec/lib/import/zendesk/object_attribute/base_examples.rb index 5291ad522cbe..451b9ba70445 100644 --- a/spec/lib/import/zendesk/object_attribute/base_examples.rb +++ b/spec/lib/import/zendesk/object_attribute/base_examples.rb @@ -19,6 +19,7 @@ describe 'exception handling' do let(:error_text) { Faker::Lorem.sentence } + it 'extends ObjectManager Attribute exception message' do expect(ObjectManager::Attribute).to receive(:add).and_raise(RuntimeError, error_text) diff --git a/spec/lib/ldap/group_spec.rb b/spec/lib/ldap/group_spec.rb index fc8e9f3f355a..9d66c301e290 100644 --- a/spec/lib/ldap/group_spec.rb +++ b/spec/lib/ldap/group_spec.rb @@ -5,6 +5,11 @@ RSpec.describe Ldap::Group do + # required as 'let' to perform test based + # expectations and reuse it in 'let' instance + # as additional parameter + let(:mocked_ldap) { double() } + context '.uid_attribute' do it 'responds to .uid_attribute' do @@ -16,17 +21,12 @@ end end - # required as 'let' to perform test based - # expectations and reuse it in 'let' instance - # as additional parameter - let(:mocked_ldap) { double() } - context 'initialization config parameters' do it 'reuses given Ldap instance if given' do config = {} expect(Ldap).not_to receive(:new).with(config) - instance = described_class.new(config, ldap: mocked_ldap) + described_class.new(config, ldap: mocked_ldap) end it 'takes optional filter' do @@ -55,7 +55,8 @@ it 'creates own Ldap instance if none given' do expect(Ldap).to receive(:new) - expect(described_class.new()) + + described_class.new end end diff --git a/spec/lib/ldap/user_spec.rb b/spec/lib/ldap/user_spec.rb index ad567050e750..087e70d539af 100644 --- a/spec/lib/ldap/user_spec.rb +++ b/spec/lib/ldap/user_spec.rb @@ -6,6 +6,8 @@ RSpec.describe Ldap::User do + let(:mocked_ldap) { double() } + context '.uid_attribute' do it 'responds to .uid_attribute' do @@ -32,13 +34,12 @@ # required as 'let' to perform test based # expectations and reuse it in 'let' instance # as additional parameter - let(:mocked_ldap) { double() } context 'initialization config parameters' do it 'reuses given Ldap instance if given' do expect(Ldap).not_to receive(:new) - instance = described_class.new(ldap: mocked_ldap) + described_class.new(ldap: mocked_ldap) end it 'takes optional filter' do @@ -67,7 +68,8 @@ it 'creates own Ldap instance if none given' do expect(Ldap).to receive(:new) - expect(described_class.new()) + + described_class.new() end end diff --git a/spec/lib/ldap_spec.rb b/spec/lib/ldap_spec.rb index 79666a30bec1..7bad22db7476 100644 --- a/spec/lib/ldap_spec.rb +++ b/spec/lib/ldap_spec.rb @@ -233,13 +233,13 @@ def mock_initialization(given:, expected:) context '#search' do + let(:base) { 'DC=domain,DC=tld' } + let(:filter) { '(objectClass=user)' } + it 'responds to #search' do expect(instance).to respond_to(:search) end - let(:filter) { '(objectClass=user)' } - let(:base) { 'DC=domain,DC=tld' } - it 'performs search for a filter, base and scope and yields of returned entries' do scope = Net::LDAP::SearchScope_BaseObject @@ -309,12 +309,12 @@ def mock_initialization(given:, expected:) context '#entries?' do + let(:filter) { '(objectClass=user)' } + it 'responds to #entries?' do expect(instance).to respond_to(:entries?) end - let(:filter) { '(objectClass=user)' } - it 'returns true if entries are present' do params = { diff --git a/spec/lib/mail/encodings_spec.rb b/spec/lib/mail/encodings_spec.rb index d498af65e2c2..4b8450df3a45 100644 --- a/spec/lib/mail/encodings_spec.rb +++ b/spec/lib/mail/encodings_spec.rb @@ -6,12 +6,12 @@ # Then, upstream was fixed, whereas our patch broke.) describe '.value_decode' do it 'decodes us-ascii encoded strings' do - expect(Mail::Encodings.value_decode('=?us-ascii?Q?Test?=')) + expect(described_class.value_decode('=?us-ascii?Q?Test?=')) .to eql('Test') end it 'decodes utf-8 encoded strings' do - expect(Mail::Encodings.value_decode('=?UTF-8?Q? Personal=C3=A4nderung?=')) + expect(described_class.value_decode('=?UTF-8?Q? Personal=C3=A4nderung?=')) .to eql(' Personaländerung') end end diff --git a/spec/lib/migration_job/ldap_samaccountname_to_uid_spec.rb b/spec/lib/migration_job/ldap_samaccountname_to_uid_spec.rb index fe9558ae12f5..9c91ff20a0b1 100644 --- a/spec/lib/migration_job/ldap_samaccountname_to_uid_spec.rb +++ b/spec/lib/migration_job/ldap_samaccountname_to_uid_spec.rb @@ -3,14 +3,14 @@ RSpec.describe MigrationJob::LdapSamaccountnameToUid do it 'performs no changes if no LDAP config present' do - expect(Setting).to_not receive(:set) + expect(Setting).not_to receive(:set) expect(Import::Ldap).to receive(:config).and_return(nil) described_class.new.perform end it 'performs no changes if uid attributes equals' do - expect(Setting).to_not receive(:set) + expect(Setting).not_to receive(:set) ldap_config = { 'user_uid' => 'samaccountname' diff --git a/spec/lib/notification_factory/slack_spec.rb b/spec/lib/notification_factory/slack_spec.rb index c59d25d20b3f..3b7aae850860 100644 --- a/spec/lib/notification_factory/slack_spec.rb +++ b/spec/lib/notification_factory/slack_spec.rb @@ -3,7 +3,7 @@ RSpec.describe NotificationFactory::Slack do describe '.template' do subject(:template) do - NotificationFactory::Slack.template( + described_class.template( template: action, locale: 'en-us', timezone: 'Europe/Berlin', @@ -64,11 +64,8 @@ end context 'for "ticket_escalate"' do - before { ticket.escalation_at = escalation_time } - let(:escalation_time) { Time.zone.parse('2019-04-01T10:00:00Z') } - subject(:template) do - NotificationFactory::Slack.template( + described_class.template( template: 'ticket_escalation', locale: 'en-us', timezone: 'Europe/Berlin', @@ -80,6 +77,10 @@ ) end + before { ticket.escalation_at = escalation_time } + + let(:escalation_time) { Time.zone.parse('2019-04-01T10:00:00Z') } + it 'returns a hash with subject: (as Markdown heading)' do expect(template).to include(subject: "# #{ticket.title}") end diff --git a/spec/lib/notification_factory/template_spec.rb b/spec/lib/notification_factory/template_spec.rb index 094f5ca46d32..0326cbd8ce78 100644 --- a/spec/lib/notification_factory/template_spec.rb +++ b/spec/lib/notification_factory/template_spec.rb @@ -2,7 +2,7 @@ RSpec.describe NotificationFactory::Template do subject(:template) do - NotificationFactory::Template.new(template_string, escape) + described_class.new(template_string, escape) end describe '#to_s' do diff --git a/spec/lib/notification_factory_spec.rb b/spec/lib/notification_factory_spec.rb index e146d7d05d1b..11288f57f65b 100644 --- a/spec/lib/notification_factory_spec.rb +++ b/spec/lib/notification_factory_spec.rb @@ -47,6 +47,7 @@ context 'if no locale given in arguments, but default locale is set' do before { Setting.set('locale_default', 'de-de') } + let(:rendered_locale) { 'de' } it 'tries template for default locale' do @@ -56,6 +57,7 @@ context 'and no such template exists' do before { Setting.set('locale_default', 'xx') } + let(:rendered_locale) { 'en' } it 'falls back to en template' do diff --git a/spec/lib/password_hash_spec.rb b/spec/lib/password_hash_spec.rb index 70d5573a4ce7..821e01665479 100644 --- a/spec/lib/password_hash_spec.rb +++ b/spec/lib/password_hash_spec.rb @@ -57,12 +57,12 @@ let(:zammad_sha2) { '{sha2}dd9c764fa7ea18cd992c8600006d3dc3ac983d1ba22e9ba2d71f6207456be0ba' } it 'requires hash to be not blank' do - expect(described_class.legacy?(nil, pw_plain)).to be_falsy - expect(described_class.legacy?('', pw_plain)).to be_falsy + expect(described_class).not_to be_legacy(nil, pw_plain) + expect(described_class).not_to be_legacy('', pw_plain) end it 'requires password to be not nil' do - expect(described_class.legacy?(zammad_sha2, nil)).to be_falsy + expect(described_class).not_to be_legacy(zammad_sha2, nil) end it 'detects sha2 hashes' do diff --git a/spec/lib/report/ticket_generic_time_spec.rb b/spec/lib/report/ticket_generic_time_spec.rb index 586bc7e33a66..514dc28d011e 100644 --- a/spec/lib/report/ticket_generic_time_spec.rb +++ b/spec/lib/report/ticket_generic_time_spec.rb @@ -34,7 +34,7 @@ selector: {}, # ticket selector to get only a collection of tickets params: { field: 'created_at' }, ) - end.to_not raise_error + end.not_to raise_error end end end diff --git a/spec/lib/search_index_backend_spec.rb b/spec/lib/search_index_backend_spec.rb index 708f9a955e07..bcb808503322 100644 --- a/spec/lib/search_index_backend_spec.rb +++ b/spec/lib/search_index_backend_spec.rb @@ -2,7 +2,8 @@ RSpec.describe SearchIndexBackend do describe '.build_query' do - subject(:query) { SearchIndexBackend.build_query('', query_extension: params) } + subject(:query) { described_class.build_query('', query_extension: params) } + let(:params) { { 'bool' => { 'filter' => { 'term' => { 'a' => 'b' } } } } } it 'coerces :query_extension hash keys to symbols' do @@ -11,7 +12,7 @@ end describe '.search' do - subject(:search) { SearchIndexBackend.search(query, index, limit: 3000) } + subject(:search) { described_class.search(query, index, limit: 3000) } context 'for query with no results' do let(:query) { 'preferences.notification_sound.enabled:*' } @@ -51,7 +52,7 @@ QUERIES it 'appends a * to the original query' do - expect(queries.map(&SearchIndexBackend.method(:append_wildcard_to_simple_query))) + expect(queries.map(&described_class.method(:append_wildcard_to_simple_query))) .to eq(queries.map { |q| "#{q}*" }) end end @@ -93,7 +94,7 @@ QUERIES it 'returns the original query verbatim' do - expect(queries.map(&SearchIndexBackend.method(:append_wildcard_to_simple_query))) + expect(queries.map(&described_class.method(:append_wildcard_to_simple_query))) .to eq(queries) end end diff --git a/spec/lib/sequencer/sequence/import/ldap/users_spec.rb b/spec/lib/sequencer/sequence/import/ldap/users_spec.rb index 4ac5b1912d47..0ded1b0d8ebc 100644 --- a/spec/lib/sequencer/sequence/import/ldap/users_spec.rb +++ b/spec/lib/sequencer/sequence/import/ldap/users_spec.rb @@ -54,9 +54,7 @@ ldap_connection: connection, import_job: import_job, ) - end.to change { - User.count - }.by(1) + end.to change(User, :count).by(1) imported_user = User.last @@ -87,9 +85,7 @@ ldap_connection: connection, import_job: import_job, ) - end.not_to change { - User.count - } + end.not_to change(User, :count) imported_user.reload @@ -148,9 +144,7 @@ ldap_connection: connection, import_job: import_job, ) - end.to change { - User.count - }.by(1) + end.to change(User, :count).by(1) imported_user = User.last @@ -181,9 +175,7 @@ ldap_connection: connection, import_job: import_job, ) - end.not_to change { - User.count - } + end.not_to change(User, :count) imported_user.reload diff --git a/spec/lib/sequencer/unit/import/ldap/users/lost/deactivate_spec.rb b/spec/lib/sequencer/unit/import/ldap/users/lost/deactivate_spec.rb index 979e661317e6..48f56fdbcda5 100644 --- a/spec/lib/sequencer/unit/import/ldap/users/lost/deactivate_spec.rb +++ b/spec/lib/sequencer/unit/import/ldap/users/lost/deactivate_spec.rb @@ -20,7 +20,7 @@ it 'enforces created_by_id => 1 in newly created History logs' do expect { process(lost_ids: lost_users.pluck(:id), dry_run: false) } - .to change { History.count }.by(sample_length) + .to change(History, :count).by(sample_length) expect(History.last(sample_length).pluck(:created_by_id)) .to eq(Array.new(sample_length, 1)) diff --git a/spec/lib/signature_detection_spec.rb b/spec/lib/signature_detection_spec.rb index 34a8b32cf157..db33664d160e 100644 --- a/spec/lib/signature_detection_spec.rb +++ b/spec/lib/signature_detection_spec.rb @@ -21,7 +21,7 @@ end it 'returns the first 5–10-line substring they share in common' do - expect(SignatureDetection.find_signature(messages)).to eq(<<~SIG.chomp) + expect(described_class.find_signature(messages)).to eq(<<~SIG.chomp) Mit freundlichen Grüßen @@ -48,7 +48,7 @@ end it 'returns the first 5–10-line substring they share in common' do - expect(SignatureDetection.find_signature(messages)).to eq(<<~SIG.chomp) + expect(described_class.find_signature(messages)).to eq(<<~SIG.chomp) Freundliche Grüße @@ -75,7 +75,7 @@ end it 'converts messages (via #html2text) then returns the first 5–10-line substring they share in common' do - expect(SignatureDetection.find_signature(messages)).to eq(<<~SIG.chomp) + expect(described_class.find_signature(messages)).to eq(<<~SIG.chomp) ChristianSmith Technik @@ -112,7 +112,7 @@ SIG it 'returns the line of the message where the signature begins' do - expect(SignatureDetection.find_signature_line(signature, content, content_type)).to eq(10) + expect(described_class.find_signature_line(signature, content, content_type)).to eq(10) end end end @@ -135,7 +135,7 @@ SIG it 'converts messages (via #html2text) then returns the line of the message where the signature begins' do - expect(SignatureDetection.find_signature_line(signature, content, content_type)).to eq(11) + expect(described_class.find_signature_line(signature, content, content_type)).to eq(11) end end end @@ -152,7 +152,7 @@ end it 'updates the signature-line data of all articles' do - expect { SignatureDetection.rebuild_all_articles } + expect { described_class.rebuild_all_articles } .to change { articles.first.reload.preferences[:signature_detection] }.to(3) .and change { articles.second.reload.preferences[:signature_detection] }.to(2) end diff --git a/spec/lib/stats/ticket_waiting_time_spec.rb b/spec/lib/stats/ticket_waiting_time_spec.rb index ed0e2b20e99f..838dfab233ab 100644 --- a/spec/lib/stats/ticket_waiting_time_spec.rb +++ b/spec/lib/stats/ticket_waiting_time_spec.rb @@ -7,19 +7,19 @@ context 'when given an agent with no tickets' do it 'returns a hash with 1-day average ticket wait time for user (in minutes)' do - expect(Stats::TicketWaitingTime.generate(user)).to include(handling_time: 0) + expect(described_class.generate(user)).to include(handling_time: 0) end it 'returns a hash with 1-day average ticket wait time across user’s groups (in minutes)' do - expect(Stats::TicketWaitingTime.generate(user)).to include(average_per_agent: 0) + expect(described_class.generate(user)).to include(average_per_agent: 0) end it 'returns a hash with verbal grade for average ticket wait time' do - expect(Stats::TicketWaitingTime.generate(user)).to include(state: 'supergood') + expect(described_class.generate(user)).to include(state: 'supergood') end it 'returns a hash with decimal score (0–1) of user’s risk of falling to a lower grade' do - expect(Stats::TicketWaitingTime.generate(user)).to include(percent: 0.0) + expect(described_class.generate(user)).to include(percent: 0.0) end context 'and who belongs to a group with other tickets' do @@ -31,7 +31,7 @@ end it 'returns a hash with 1-day average ticket wait time across user’s groups (in minutes)' do - expect(Stats::TicketWaitingTime.generate(user)).to include(average_per_agent: 60) + expect(described_class.generate(user)).to include(average_per_agent: 60) end end end @@ -45,19 +45,19 @@ end it 'returns a hash with 1-day average ticket wait time for user (in minutes)' do - expect(Stats::TicketWaitingTime.generate(user)).to include(handling_time: 60) + expect(described_class.generate(user)).to include(handling_time: 60) end it 'returns a hash with 1-day average ticket wait time across user’s groups (in minutes)' do - expect(Stats::TicketWaitingTime.generate(user)).to include(average_per_agent: 60) + expect(described_class.generate(user)).to include(average_per_agent: 60) end it 'returns a hash with verbal grade for average ticket wait time' do - expect(Stats::TicketWaitingTime.generate(user)).to include(state: 'supergood') + expect(described_class.generate(user)).to include(state: 'supergood') end it 'returns a hash with decimal score (0–1) of user’s risk of falling to a lower grade' do - expect(Stats::TicketWaitingTime.generate(user)).to include(percent: 1.0) + expect(described_class.generate(user)).to include(percent: 1.0) end context 'and who belongs to a group with other tickets' do @@ -69,7 +69,7 @@ end it 'returns a hash with 1-day average ticket wait time across user’s groups (in minutes)' do - expect(Stats::TicketWaitingTime.generate(user)).to include(average_per_agent: 90) + expect(described_class.generate(user)).to include(average_per_agent: 90) end end end @@ -81,7 +81,7 @@ context 'with empty tickets (no articles)' do it 'returns 0' do - expect(Stats::TicketWaitingTime.calculate_average(ticket.id, start_time)).to eq(0) + expect(described_class.calculate_average(ticket.id, start_time)).to eq(0) end end @@ -92,7 +92,7 @@ end it 'returns 0' do - expect(Stats::TicketWaitingTime.calculate_average(ticket.id, start_time)).to eq(0) + expect(described_class.calculate_average(ticket.id, start_time)).to eq(0) end end @@ -103,7 +103,7 @@ end it 'returns elapsed time' do - expect(Stats::TicketWaitingTime.calculate_average(ticket.id, start_time)).to eq(1.minute) + expect(described_class.calculate_average(ticket.id, start_time)).to eq(1.minute) end end @@ -115,7 +115,7 @@ end it 'ignores them (and measures time to actual response)' do - expect(Stats::TicketWaitingTime.calculate_average(ticket.id, start_time)).to eq(2.minutes) + expect(described_class.calculate_average(ticket.id, start_time)).to eq(2.minutes) end end @@ -128,7 +128,7 @@ end it 'returns average of elapsed times' do - expect(Stats::TicketWaitingTime.calculate_average(ticket.id, start_time)).to eq(3.minutes) + expect(described_class.calculate_average(ticket.id, start_time)).to eq(3.minutes) end end @@ -152,7 +152,7 @@ end it 'ignores all edge cases and returns only specified average response time' do - expect(Stats::TicketWaitingTime.calculate_average(ticket.id, start_time)).to eq(3.minutes) + expect(described_class.calculate_average(ticket.id, start_time)).to eq(3.minutes) end end end diff --git a/spec/lib/stats_spec.rb b/spec/lib/stats_spec.rb index 5a74f7d3bbde..256a6fdbdc15 100644 --- a/spec/lib/stats_spec.rb +++ b/spec/lib/stats_spec.rb @@ -10,19 +10,19 @@ end it 'generates stats' do - expect { Stats.generate }.to_not raise_error + expect { described_class.generate }.not_to raise_error end context 'when backend registration is invalid' do it 'fails for empty registration' do Setting.set('Stats::TicketWaitingTime', nil) - expect { Stats.generate }.to raise_error(RuntimeError) + expect { described_class.generate }.to raise_error(RuntimeError) end it 'fails for unknown backend' do Setting.set('Stats::TicketWaitingTime', 'Stats::UNKNOWN') - expect { Stats.generate }.to raise_error(LoadError) + expect { described_class.generate }.to raise_error(LoadError) end end end diff --git a/spec/lib/upload_cache_spec.rb b/spec/lib/upload_cache_spec.rb index eeb424c1706f..f34667830db6 100644 --- a/spec/lib/upload_cache_spec.rb +++ b/spec/lib/upload_cache_spec.rb @@ -27,7 +27,7 @@ 'Content-Disposition' => 'attached', }, ) - end.to change { Store.count }.by(1) + end.to change(Store, :count).by(1) end end @@ -72,7 +72,7 @@ end it 'removes all added Store items' do - expect { subject.destroy }.to change { Store.count }.by(-2) + expect { subject.destroy }.to change(Store, :count).by(-2) end end @@ -89,7 +89,7 @@ end it 'removes the Store item matching the given ID' do - expect { subject.remove_item(Store.last.id) }.to change { Store.count }.by(-1) + expect { subject.remove_item(Store.last.id) }.to change(Store, :count).by(-1) end it 'prevents removage of non UploadCache Store items' do diff --git a/spec/models/application_model/can_assets_examples.rb b/spec/models/application_model/can_assets_examples.rb index 0325a9c8668f..aedaa36ccfcd 100644 --- a/spec/models/application_model/can_assets_examples.rb +++ b/spec/models/application_model/can_assets_examples.rb @@ -1,5 +1,6 @@ RSpec.shared_examples 'ApplicationModel::CanAssets' do |associations: [], selectors: [], own_attributes: true| subject { create(described_class.name.underscore, updated_by_id: admin.id) } + let(:admin) { create(:admin_user) } describe '#assets (for supplying model data to front-end framework)' do @@ -37,6 +38,7 @@ shared_examples 'single association' do subject { create(described_class.name.underscore, association => single) } + let(:single) { create(reflection.class_name.underscore) } it 'returns a hash with its asset attributes' do @@ -56,6 +58,7 @@ shared_examples 'collection association' do subject { create(described_class.name.underscore, association => collection) } + let(:collection) { create_list(reflection.class_name.underscore, 5) } let(:collection_assets) { collection.reduce({}) { |assets_hash, single| single.assets(assets_hash) } } @@ -84,6 +87,7 @@ Array(selectors).each do |s| subject { create(described_class.name.underscore, s => selector) } + let(:selector) { { 'ticket.priority_id' => { operator: 'is', value: [1, 2] } } } let(:priorities_assets) { Ticket::Priority.first(2).reduce({}) { |asset_hash, priority| priority.assets(asset_hash) } } diff --git a/spec/models/application_model/checks_import_examples.rb b/spec/models/application_model/checks_import_examples.rb index 973a027fbdeb..30688a2fc0bd 100644 --- a/spec/models/application_model/checks_import_examples.rb +++ b/spec/models/application_model/checks_import_examples.rb @@ -19,7 +19,7 @@ before { Setting.set('system_init_done', false) } it 'allows explicit setting of #id attribute' do - expect { subject.save }.not_to change { subject.id } + expect { subject.save }.not_to change(subject, :id) end end @@ -30,7 +30,7 @@ before { Setting.set('import_mode', false) } it 'prevents explicit setting of #id attribute' do - expect { subject.save }.to change { subject.id } + expect { subject.save }.to change(subject, :id) end end @@ -39,13 +39,13 @@ shared_examples 'importable classes' do it 'allows explicit setting of #id attribute' do - expect { subject.save }.not_to change { subject.id } + expect { subject.save }.not_to change(subject, :id) end end shared_examples 'non-importable classes' do it 'prevents explicit setting of #id attribute' do - expect { subject.save }.to change { subject.id } + expect { subject.save }.to change(subject, :id) end end diff --git a/spec/models/calendar_spec.rb b/spec/models/calendar_spec.rb index 6983f7ba7fb0..abbce6972e6f 100644 --- a/spec/models/calendar_spec.rb +++ b/spec/models/calendar_spec.rb @@ -11,17 +11,18 @@ subject(:calendar) { build(:calendar, default: true) } it 'stays true and sets all other calendars to default: false' do - expect { calendar.tap(&:save).reload }.not_to change { calendar.default } + expect { calendar.tap(&:save).reload }.not_to change(calendar, :default) expect(Calendar.where(default: true) - [calendar]).to be_empty end end context 'when set to true on update' do subject(:calendar) { create(:calendar, default: false) } + before { calendar.default = true } it 'stays true and sets all other calendars to default: false' do - expect { calendar.tap(&:save).reload }.not_to change { calendar.default } + expect { calendar.tap(&:save).reload }.not_to change(calendar, :default) expect(Calendar.where(default: true) - [calendar]).to be_empty end end @@ -103,7 +104,7 @@ context 'and neither current date nor iCal URL have changed' do it 'is idempotent' do expect { calendar.sync } - .not_to change { calendar.public_holidays } + .not_to change(calendar, :public_holidays) end it 'does not create a background job for escalation rebuild' do calendar # create and sync (1 inital background job is created) @@ -120,7 +121,7 @@ it 'is idempotent' do expect { calendar.sync } - .not_to change { calendar.public_holidays } + .not_to change(calendar, :public_holidays) end it 'does not create a background job for escalation rebuild' do expect { calendar.sync } @@ -135,7 +136,7 @@ end it 'appends newly computed event data to #public_holidays' do - expect { calendar.sync }.to change { calendar.public_holidays }.to( + expect { calendar.sync }.to change(calendar, :public_holidays).to( '2016-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) }, '2017-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) }, '2018-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) }, @@ -154,7 +155,7 @@ it 'replaces #public_holidays with event data computed from new iCal URL' do expect { calendar.save } - .to change { calendar.public_holidays }.to( + .to change(calendar, :public_holidays).to( '2016-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) }, '2016-12-25' => { 'active' => true, 'summary' => 'Christmas2', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) }, '2017-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) }, diff --git a/spec/models/channel/email_parser_spec.rb b/spec/models/channel/email_parser_spec.rb index 17019be4a35e..4202812ba4a0 100644 --- a/spec/models/channel/email_parser_spec.rb +++ b/spec/models/channel/email_parser_spec.rb @@ -40,7 +40,7 @@ describe 'auto-creating new users' do context 'with one unrecognized email address' do it 'creates one new user' do - expect { Channel::EmailParser.new.process({}, <<~RAW) }.to change { User.count }.by(1) + expect { Channel::EmailParser.new.process({}, <<~RAW) }.to change(User, :count).by(1) From: #{Faker::Internet.unique.email} RAW end @@ -48,7 +48,7 @@ context 'with a large number of unrecognized recipient addresses' do it 'never creates more than 40 users' do - expect { Channel::EmailParser.new.process({}, <<~RAW) }.to change { User.count }.by(40) + expect { Channel::EmailParser.new.process({}, <<~RAW) }.to change(User, :count).by(40) From: nicole.braun@zammad.org To: #{Array.new(20) { Faker::Internet.unique.email }.join(', ')} Cc: #{Array.new(21) { Faker::Internet.unique.email }.join(', ')} @@ -99,8 +99,8 @@ it 'creates a ticket and article' do expect { Channel::EmailParser.new.process({}, raw_mail) } - .to change { Ticket.count }.by(1) - .and change { Ticket::Article.count }.by_at_least(1) + .to change(Ticket, :count).by(1) + .and change(Ticket::Article, :count).by_at_least(1) end it 'sets #title to email subject' do @@ -260,7 +260,7 @@ shared_examples 'creates a new ticket' do it 'creates a new ticket' do expect { described_class.new.process({}, raw_mail) } - .to change { Ticket.count }.by(1) + .to change(Ticket, :count).by(1) .and not_change { ticket.articles.length } end end @@ -656,7 +656,7 @@ context 'when "postmaster_sender_is_agent_search_for_customer" setting is true (default)' do it 'sets ticket.customer to user with To: email' do expect { Channel::EmailParser.new.process({}, raw_mail) } - .to change { Ticket.count }.by(1) + .to change(Ticket, :count).by(1) expect(Ticket.last.customer).to eq(customer) end @@ -667,7 +667,7 @@ it 'sets ticket.customer to user with To: email' do expect { Channel::EmailParser.new.process({}, raw_mail) } - .to change { Ticket.count }.by(1) + .to change(Ticket, :count).by(1) expect(Ticket.last.customer).to eq(agent) end @@ -959,7 +959,7 @@ it 're-opens a closed ticket' do expect { described_class.new.process({}, raw_mail) } - .to not_change { Ticket.count } + .to not_change(Ticket, :count) .and change { ticket.reload.state.name }.to('open') end end @@ -972,7 +972,7 @@ it 'does not re-open a closed ticket' do expect { described_class.new.process({}, raw_mail) } - .to not_change { Ticket.count } + .to not_change(Ticket, :count) .and not_change { ticket.reload.state.name } end end diff --git a/spec/models/channel/filter/match/email_regex_spec.rb b/spec/models/channel/filter/match/email_regex_spec.rb index 88319319cc32..722c9efbbd1c 100644 --- a/spec/models/channel/filter/match/email_regex_spec.rb +++ b/spec/models/channel/filter/match/email_regex_spec.rb @@ -2,7 +2,8 @@ RSpec.describe Channel::Filter::Match::EmailRegex do describe '.match' do - subject(:match) { Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: check_mode) } + subject(:match) { described_class.match(value: from, match_rule: sender, check_mode: check_mode) } + let(:from) { 'foobar@foo.bar' } context 'in normal (error-suppressing) mode (default)' do diff --git a/spec/models/channel_spec.rb b/spec/models/channel_spec.rb index 740d68819221..55aef7337594 100644 --- a/spec/models/channel_spec.rb +++ b/spec/models/channel_spec.rb @@ -49,7 +49,7 @@ it 'adds tickets as appropriate' do expect { twitter_channel.fetch(true) } - .to change { Ticket.count }.by(26) + .to change(Ticket, :count).by(26) expect(Ticket.last.attributes).to include( 'title' => 'Wir haben unsere DMs deaktiviert. ' \ diff --git a/spec/models/concerns/has_groups_examples.rb b/spec/models/concerns/has_groups_examples.rb index d55e975a22d1..4dc14d541ef5 100644 --- a/spec/models/concerns/has_groups_examples.rb +++ b/spec/models/concerns/has_groups_examples.rb @@ -1,6 +1,7 @@ RSpec.shared_examples 'HasGroups' do |group_access_factory:| context 'group' do subject { create(group_access_factory) } + let(:group_full) { create(:group) } let(:group_read) { create(:group) } let(:group_inactive) { create(:group, active: false) } @@ -45,7 +46,7 @@ context 'result' do - before(:each) do + before do subject.group_names_access_map = { group_full.name => 'full', group_read.name => 'read', @@ -78,16 +79,16 @@ context '#group_access?' do - it 'responds to group_access?' do - expect(subject).to respond_to(:group_access?) - end - - before(:each) do + before do subject.group_names_access_map = { group_read.name => 'read', } end + it 'responds to group_access?' do + expect(subject).to respond_to(:group_access?) + end + context 'Group ID parameter' do include_examples '#group_access? call' do let(:group_parameter) { group_read.id } @@ -121,16 +122,16 @@ context '#group_ids_access' do - it 'responds to group_ids_access' do - expect(subject).to respond_to(:group_ids_access) - end - - before(:each) do + before do subject.group_names_access_map = { group_read.name => 'read', } end + it 'responds to group_ids_access' do + expect(subject).to respond_to(:group_ids_access) + end + it 'lists only active Group IDs' do subject.group_names_access_map = { group_read.name => 'read', @@ -531,16 +532,16 @@ context '.group_access' do - it 'responds to group_access' do - expect(described_class).to respond_to(:group_access) - end - - before(:each) do + before do subject.group_names_access_map = { group_read.name => 'read', } end + it 'responds to group_access' do + expect(described_class).to respond_to(:group_access) + end + it 'lists only active instances' do subject.update!(active: false) diff --git a/spec/models/concerns/has_groups_permissions_examples.rb b/spec/models/concerns/has_groups_permissions_examples.rb index 489c67df005c..600e221fae0f 100644 --- a/spec/models/concerns/has_groups_permissions_examples.rb +++ b/spec/models/concerns/has_groups_permissions_examples.rb @@ -1,9 +1,10 @@ RSpec.shared_examples 'HasGroups and Permissions' do |group_access_no_permission_factory:| context 'group' do subject { build(group_access_no_permission_factory) } + let(:group_read) { create(:group) } - before(:each) do + before do subject.group_names_access_map = { group_read.name => 'read', } diff --git a/spec/models/concerns/has_roles_examples.rb b/spec/models/concerns/has_roles_examples.rb index 0a4e70556328..4ab61ffa8a84 100644 --- a/spec/models/concerns/has_roles_examples.rb +++ b/spec/models/concerns/has_roles_examples.rb @@ -1,6 +1,7 @@ RSpec.shared_examples 'HasRoles' do |group_access_factory:| context 'role' do subject { create(group_access_factory) } + let(:role) { create(:role) } let(:group_instance) { create(:group) } let(:group_role) { create(:group) } @@ -13,7 +14,7 @@ end context 'active Role' do - before(:each) do + before do role.group_names_access_map = { group_role.name => 'read', } @@ -58,7 +59,7 @@ context '.role_access_ids' do - before(:each) do + before do role.group_names_access_map = { group_role.name => 'read', } @@ -101,7 +102,7 @@ context 'group' do - before(:each) do + before do role.group_names_access_map = { group_role.name => 'read', } @@ -129,7 +130,7 @@ context '#group_ids_access' do - before(:each) do + before do role.group_names_access_map = { group_role.name => 'read', } diff --git a/spec/models/concerns/has_search_index_backend_examples.rb b/spec/models/concerns/has_search_index_backend_examples.rb index 8bb1c6a9e5f2..8bd4f6e872e6 100644 --- a/spec/models/concerns/has_search_index_backend_examples.rb +++ b/spec/models/concerns/has_search_index_backend_examples.rb @@ -3,13 +3,13 @@ context '#search_index_update', performs_jobs: true do subject { create(indexed_factory) } - before(:each) do + before do allow(SearchIndexBackend).to receive(:enabled?).and_return(true) end context 'record indexing' do - before(:each) do + before do expect(subject).to be_present end diff --git a/spec/models/concerns/has_xss_sanitized_note_examples.rb b/spec/models/concerns/has_xss_sanitized_note_examples.rb index 8af57b840007..9415adbf7c4e 100644 --- a/spec/models/concerns/has_xss_sanitized_note_examples.rb +++ b/spec/models/concerns/has_xss_sanitized_note_examples.rb @@ -2,6 +2,7 @@ describe 'XSS prevention' do context 'with injected JS' do subject { create(model_factory, note: 'test 123 some text') } + it 'strips out