diff --git a/.rubocop_todo.rspec.yml b/.rubocop_todo.rspec.yml index eab54855d..28bb3b226 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 b81bda651..d23e06fca 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 @@ RSpec.describe Issue1219ZhtwLocaleTypo, type: :db_migration do 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 55213d7b0..c1514f6a4 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 @@ RSpec.describe Issue1905ExchangeLoginFromRemoteId, type: :db_migration do context 'blank config' do let(:config) { nil } + it_behaves_like 'irrelevant config' end @@ -53,6 +54,7 @@ RSpec.describe Issue1905ExchangeLoginFromRemoteId, type: :db_migration do some: 'config' } end + it_behaves_like 'irrelevant config' end @@ -64,6 +66,7 @@ RSpec.describe Issue1905ExchangeLoginFromRemoteId, type: :db_migration do } } end + it_behaves_like 'irrelevant config' end @@ -76,6 +79,7 @@ RSpec.describe Issue1905ExchangeLoginFromRemoteId, type: :db_migration do } } 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 977694090..1c009da28 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 @@ RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do 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 @@ RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do 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 @@ RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do 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 bc09e56b2..f939062c7 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 3542081c0..244469a01 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 @@ RSpec.describe Issue2541FixNotificationEmailWithoutBody, type: :db_migration do 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 @@ RSpec.describe Issue2541FixNotificationEmailWithoutBody, type: :db_migration do 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 f6a9cdde8..c279d6f31 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 @@ RSpec.describe ObjectManagerAttributeDateRemoveFuturePast, type: :db_migration d 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 551c677b5..b79a8c113 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 @@ RSpec.describe UpdateCtiLogsByCallerJob, type: :job do 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 @@ RSpec.describe UpdateCtiLogsByCallerJob, type: :job do 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 1f8255aca..972fef046 100644 --- a/spec/lib/application_handle_info_spec.rb +++ b/spec/lib/application_handle_info_spec.rb @@ -3,30 +3,30 @@ require 'rails_helper' 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 92e2c73d3..daabb3f81 100644 --- a/spec/lib/cache_spec.rb +++ b/spec/lib/cache_spec.rb @@ -5,14 +5,14 @@ RSpec.describe Cache do 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 @@ RSpec.describe Cache do 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 @@ RSpec.describe Cache do 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 30862abb3..08276ef79 100644 --- a/spec/lib/core_ext/string_spec.rb +++ b/spec/lib/core_ext/string_spec.rb @@ -25,8 +25,8 @@ RSpec.describe String do 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 @@ RSpec.describe String do 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 @@ RSpec.describe String do 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 ee214e8eb..cbf0a69ab 100644 --- a/spec/lib/html_sanitizer_spec.rb +++ b/spec/lib/html_sanitizer_spec.rb @@ -4,8 +4,8 @@ require 'rails_helper' 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 @@ RSpec.describe HtmlSanitizer do 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 @@ RSpec.describe HtmlSanitizer do 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 @@ RSpec.describe HtmlSanitizer do 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 @@ RSpec.describe HtmlSanitizer do 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 @@ RSpec.describe HtmlSanitizer do 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 @@ RSpec.describe HtmlSanitizer do 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 11d5cb3de..ef2ea0c1f 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 'rails_helper' 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 @@ RSpec.describe Import::OTRS::Article::AttachmentFactory do 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 @@ RSpec.describe Import::OTRS::Article::AttachmentFactory do 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 0645271b0..4e6771b5f 100644 --- a/spec/lib/import/otrs/article_customer_spec.rb +++ b/spec/lib/import/otrs/article_customer_spec.rb @@ -32,18 +32,18 @@ RSpec.describe Import::OTRS::ArticleCustomer do 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 60b5d3bcc..533a3905f 100644 --- a/spec/lib/import/otrs/customer_user_spec.rb +++ b/spec/lib/import/otrs/customer_user_spec.rb @@ -104,7 +104,7 @@ RSpec.describe Import::OTRS::CustomerUser do } 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 98a1e3b51..8ad3d3a07 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/factory_examples' 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 b61145df2..411f4af37 100644 --- a/spec/lib/import/otrs/dynamic_field_spec.rb +++ b/spec/lib/import/otrs/dynamic_field_spec.rb @@ -2,10 +2,10 @@ require 'rails_helper' 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 812117732..ad2aa3c55 100644 --- a/spec/lib/import/otrs/user_factory_spec.rb +++ b/spec/lib/import/otrs/user_factory_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Import::OTRS::UserFactory do 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 5291ad522..451b9ba70 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 @@ RSpec.shared_examples Import::Zendesk::ObjectAttribute::Base do 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 fc8e9f3f3..9d66c301e 100644 --- a/spec/lib/ldap/group_spec.rb +++ b/spec/lib/ldap/group_spec.rb @@ -5,6 +5,11 @@ require 'ldap/group' 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 @@ RSpec.describe Ldap::Group do 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 @@ RSpec.describe Ldap::Group do 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 ad567050e..087e70d53 100644 --- a/spec/lib/ldap/user_spec.rb +++ b/spec/lib/ldap/user_spec.rb @@ -6,6 +6,8 @@ require 'tcr/net/ldap' RSpec.describe Ldap::User do + let(:mocked_ldap) { double() } + context '.uid_attribute' do it 'responds to .uid_attribute' do @@ -32,13 +34,12 @@ RSpec.describe Ldap::User do # 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 @@ RSpec.describe Ldap::User do 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 79666a30b..7bad22db7 100644 --- a/spec/lib/ldap_spec.rb +++ b/spec/lib/ldap_spec.rb @@ -233,13 +233,13 @@ RSpec.describe Ldap do 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 @@ RSpec.describe Ldap do 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 d498af65e..4b8450df3 100644 --- a/spec/lib/mail/encodings_spec.rb +++ b/spec/lib/mail/encodings_spec.rb @@ -6,12 +6,12 @@ RSpec.describe Mail::Encodings do # 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 fe9558ae1..9c91ff20a 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 @@ require 'rails_helper' 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 c59d25d20..3b7aae850 100644 --- a/spec/lib/notification_factory/slack_spec.rb +++ b/spec/lib/notification_factory/slack_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' 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 @@ RSpec.describe NotificationFactory::Slack do 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 @@ RSpec.describe NotificationFactory::Slack do ) 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 094f5ca46..0326cbd8c 100644 --- a/spec/lib/notification_factory/template_spec.rb +++ b/spec/lib/notification_factory/template_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' 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 e146d7d05..11288f57f 100644 --- a/spec/lib/notification_factory_spec.rb +++ b/spec/lib/notification_factory_spec.rb @@ -47,6 +47,7 @@ RSpec.describe NotificationFactory do 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 @@ RSpec.describe NotificationFactory do 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 70d5573a4..821e01665 100644 --- a/spec/lib/password_hash_spec.rb +++ b/spec/lib/password_hash_spec.rb @@ -57,12 +57,12 @@ RSpec.describe PasswordHash do 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 586bc7e33..514dc28d0 100644 --- a/spec/lib/report/ticket_generic_time_spec.rb +++ b/spec/lib/report/ticket_generic_time_spec.rb @@ -34,7 +34,7 @@ returns 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 708f9a955..bcb808503 100644 --- a/spec/lib/search_index_backend_spec.rb +++ b/spec/lib/search_index_backend_spec.rb @@ -2,7 +2,8 @@ require 'rails_helper' 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 @@ RSpec.describe SearchIndexBackend do 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 @@ RSpec.describe SearchIndexBackend do 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 @@ RSpec.describe SearchIndexBackend do 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 4ac5b1912..0ded1b0d8 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 @@ RSpec.describe ::Sequencer::Sequence::Import::Ldap::Users, sequencer: :sequence 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 @@ RSpec.describe ::Sequencer::Sequence::Import::Ldap::Users, sequencer: :sequence 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 @@ RSpec.describe ::Sequencer::Sequence::Import::Ldap::Users, sequencer: :sequence 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 @@ RSpec.describe ::Sequencer::Sequence::Import::Ldap::Users, sequencer: :sequence 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 979e66131..48f56fdbc 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 @@ RSpec.describe Sequencer::Unit::Import::Ldap::Users::Lost::Deactivate, sequencer 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 34a8b32cf..db33664d1 100644 --- a/spec/lib/signature_detection_spec.rb +++ b/spec/lib/signature_detection_spec.rb @@ -21,7 +21,7 @@ RSpec.describe SignatureDetection do 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 @@ RSpec.describe SignatureDetection do 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 @@ RSpec.describe SignatureDetection do 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 @@ RSpec.describe SignatureDetection do 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 @@ RSpec.describe SignatureDetection do 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 @@ RSpec.describe SignatureDetection do 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 ed0e2b20e..838dfab23 100644 --- a/spec/lib/stats/ticket_waiting_time_spec.rb +++ b/spec/lib/stats/ticket_waiting_time_spec.rb @@ -7,19 +7,19 @@ RSpec.describe Stats::TicketWaitingTime do 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 @@ RSpec.describe Stats::TicketWaitingTime do 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 @@ RSpec.describe Stats::TicketWaitingTime do 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 @@ RSpec.describe Stats::TicketWaitingTime do 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 @@ RSpec.describe Stats::TicketWaitingTime do 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 @@ RSpec.describe Stats::TicketWaitingTime do 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 @@ RSpec.describe Stats::TicketWaitingTime do 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 @@ RSpec.describe Stats::TicketWaitingTime do 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 @@ RSpec.describe Stats::TicketWaitingTime do 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 @@ RSpec.describe Stats::TicketWaitingTime do 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 5a74f7d3b..256a6fdbd 100644 --- a/spec/lib/stats_spec.rb +++ b/spec/lib/stats_spec.rb @@ -10,19 +10,19 @@ RSpec.describe Stats do 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 eeb424c17..f34667830 100644 --- a/spec/lib/upload_cache_spec.rb +++ b/spec/lib/upload_cache_spec.rb @@ -27,7 +27,7 @@ RSpec.describe UploadCache do 'Content-Disposition' => 'attached', }, ) - end.to change { Store.count }.by(1) + end.to change(Store, :count).by(1) end end @@ -72,7 +72,7 @@ RSpec.describe UploadCache do 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 @@ RSpec.describe UploadCache do 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 0325a9c86..aedaa36cc 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 @@ RSpec.shared_examples 'ApplicationModel::CanAssets' do |associations: [], select 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 @@ RSpec.shared_examples 'ApplicationModel::CanAssets' do |associations: [], select 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 @@ RSpec.shared_examples 'ApplicationModel::CanAssets' do |associations: [], select 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 973a027fb..30688a2fc 100644 --- a/spec/models/application_model/checks_import_examples.rb +++ b/spec/models/application_model/checks_import_examples.rb @@ -19,7 +19,7 @@ RSpec.shared_examples 'ApplicationModel::ChecksImport' do 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 @@ RSpec.shared_examples 'ApplicationModel::ChecksImport' do 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 @@ RSpec.shared_examples 'ApplicationModel::ChecksImport' do 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 6983f7ba7..abbce6972 100644 --- a/spec/models/calendar_spec.rb +++ b/spec/models/calendar_spec.rb @@ -11,17 +11,18 @@ RSpec.describe Calendar, type: :model do 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 @@ RSpec.describe Calendar, type: :model do 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 @@ RSpec.describe Calendar, type: :model 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 expect { calendar.sync } @@ -135,7 +136,7 @@ RSpec.describe Calendar, type: :model do 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 @@ RSpec.describe Calendar, type: :model do 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 17019be4a..4202812ba 100644 --- a/spec/models/channel/email_parser_spec.rb +++ b/spec/models/channel/email_parser_spec.rb @@ -40,7 +40,7 @@ RSpec.describe Channel::EmailParser, type: :model do 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 @@ RSpec.describe Channel::EmailParser, type: :model do 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 @@ RSpec.describe Channel::EmailParser, type: :model do 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 @@ RSpec.describe Channel::EmailParser, type: :model do 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 @@ RSpec.describe Channel::EmailParser, type: :model do 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 @@ RSpec.describe Channel::EmailParser, type: :model 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(agent) end @@ -959,7 +959,7 @@ RSpec.describe Channel::EmailParser, type: :model do 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 @@ RSpec.describe Channel::EmailParser, type: :model do 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 88319319c..722c9efbb 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 @@ require 'rails_helper' 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 740d68819..55aef7337 100644 --- a/spec/models/channel_spec.rb +++ b/spec/models/channel_spec.rb @@ -49,7 +49,7 @@ RSpec.describe Channel do 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 d55e975a2..4dc14d541 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 @@ RSpec.shared_examples 'HasGroups' do |group_access_factory:| 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 @@ RSpec.shared_examples 'HasGroups' do |group_access_factory:| 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 @@ RSpec.shared_examples 'HasGroups' do |group_access_factory:| 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 @@ RSpec.shared_examples 'HasGroups' do |group_access_factory:| 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 489c67df0..600e221fa 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 0a4e70556..4ab61ffa8 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 @@ RSpec.shared_examples 'HasRoles' do |group_access_factory:| end context 'active Role' do - before(:each) do + before do role.group_names_access_map = { group_role.name => 'read', } @@ -58,7 +59,7 @@ RSpec.shared_examples 'HasRoles' do |group_access_factory:| context '.role_access_ids' do - before(:each) do + before do role.group_names_access_map = { group_role.name => 'read', } @@ -101,7 +102,7 @@ RSpec.shared_examples 'HasRoles' do |group_access_factory:| context 'group' do - before(:each) do + before do role.group_names_access_map = { group_role.name => 'read', } @@ -129,7 +130,7 @@ RSpec.shared_examples 'HasRoles' do |group_access_factory:| 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 8bb1c6a9e..8bd4f6e87 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 @@ RSpec.shared_examples 'HasSearchIndexBackend' do |indexed_factory:| 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 8af57b840..9415adbf7 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 @@ RSpec.shared_examples 'HasXssSanitizedNote' do |model_factory:| describe 'XSS prevention' do context 'with injected JS' do subject { create(model_factory, note: 'test 123 some text') } + it 'strips out