From f4788979c8ab9195b5eb9989be9928b6c43f6924 Mon Sep 17 00:00:00 2001 From: Manabu Niseki Date: Tue, 11 Jun 2024 19:54:39 +0900 Subject: [PATCH] refactor: renew rubocop & rspec (#1091) --- .github/workflows/ruby.yml | 2 +- .rubocop.yml | 30 ++++++- Rakefile | 4 +- lefthook.yml | 4 +- mihari.gemspec | 73 ++++++++-------- spec/analyzers/dnstwister_spec.rb | 2 + spec/analyzers/otx_spec.rb | 8 +- spec/analyzers/securitytrails_spec.rb | 2 +- spec/cli/alert_spec.rb | 15 ++-- spec/cli/rule_spec.rb | 2 +- .../autonomous_system_normalizable_spec.rb | 4 +- spec/concerns/error_unwrappable_spec.rb | 14 ++-- .../falsepositive_validatable_spec.rb | 6 +- spec/concerns/refangable_spec.rb | 4 +- spec/concerns/retriable_spec.rb | 22 ++--- spec/config_spec.rb | 63 -------------- spec/emitters/misp_spec.rb | 2 +- spec/emitters/slack_spec.rb | 2 +- spec/emitters/the_hive_spec.rb | 2 +- spec/emitters/yeti_spec.rb | 2 +- spec/factories/rules.rb | 2 +- spec/mihari_spec.rb | 70 +++++++++++++++- spec/models/artifact_spec.rb | 8 +- spec/rule_spec.rb | 8 +- spec/schemas/rule_spec.rb | 2 +- spec/services/artifact_enricher_spec.rb | 2 +- spec/sidekiq/search_job_spec.rb | 2 +- spec/support/httpbin.rb | 83 ------------------- .../shared_contexts/httpbin_context.rb | 5 +- 29 files changed, 196 insertions(+), 249 deletions(-) delete mode 100644 spec/config_spec.rb delete mode 100644 spec/support/httpbin.rb diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 697089a5b..60c462f93 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -35,7 +35,7 @@ jobs: bundler: latest bundler-cache: true - name: Lint - run: bundle exec standardrb + run: bundle exec rake rubocop test: runs-on: ubuntu-latest strategy: diff --git a/.rubocop.yml b/.rubocop.yml index e6011d5e8..0d978c433 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,5 +1,6 @@ -Style/StringLiterals: - EnforcedStyle: double_quotes +AllCops: + TargetRubyVersion: 3.2 + NewCops: enable Metrics/BlockLength: Max: 150 Exclude: @@ -12,13 +13,36 @@ Metrics/MethodLength: Metrics/AbcSize: Max: 50 RSpec/MultipleMemoizedHelpers: - Max: 10 + Max: 15 RSpec/ExampleLength: Max: 20 RSpec/FilePath: SpecSuffixOnly: true +RSpec/NestedGroups: + Max: 5 +RSpec/RepeatedExampleGroupDescription: + Enabled: false +RSpec/ReceiveMessages: + Enabled: false +RSpec/MultipleExpectations: + Enabled: false +RSpec/SpecFilePathFormat: + Enabled: false +RSpecRails: + Enabled: false +FactoryBot/SyntaxMethods: + Enabled: false require: + - rubocop-capybara - rubocop-factory_bot + - rubocop-performance - rubocop-rake - rubocop-rspec - rubocop-yard + - standard + - standard-custom + - standard-performance +inherit_gem: + standard: config/base.yml + standard-custom: config/base.yml + standard-performance: config/base.yml diff --git a/Rakefile b/Rakefile index 3eb473146..d52e078fa 100644 --- a/Rakefile +++ b/Rakefile @@ -3,8 +3,9 @@ require "time" require "rspec/core/rake_task" -require "standard/rake" +require "rubocop/rake_task" +RuboCop::RakeTask.new RSpec::Core::RakeTask.new(:spec) task default: :spec @@ -67,6 +68,7 @@ namespace :build do end end +desc "Build including Swagger doc and frontend assets" task :build do Rake::Task["build:swagger"].invoke Rake::Task["build:frontend"].invoke diff --git a/lefthook.yml b/lefthook.yml index 83de6a986..1d7d50b90 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -1,8 +1,8 @@ pre-commit: commands: - standard: + rubocop: glob: "*.rb" - run: bundle exec standardrb --fix {staged_files} + run: bundle exec rubocop --fix {staged_files} stage_fixed: true eslint: root: "frontend/" diff --git a/mihari.gemspec b/mihari.gemspec index b7c764d26..00082f607 100644 --- a/mihari.gemspec +++ b/mihari.gemspec @@ -36,76 +36,77 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] - spec.add_development_dependency "better_errors", "~> 2.10" - spec.add_development_dependency "binding_of_caller", "~> 1.0" + spec.add_development_dependency "better_errors", "~> 2.10.1" + spec.add_development_dependency "binding_of_caller", "~> 1.0.1" spec.add_development_dependency "bundler", "~> 2.5" spec.add_development_dependency "capybara", "~> 3.40" spec.add_development_dependency "coveralls_reborn", "~> 0.28" - spec.add_development_dependency "factory_bot", "~> 6.4" + spec.add_development_dependency "factory_bot", "~> 6.4.6" spec.add_development_dependency "fakefs", "~> 2.5" - spec.add_development_dependency "faker", "~> 3.3" - spec.add_development_dependency "fuubar", "~> 2.5" - spec.add_development_dependency "mysql2", "~> 0.5" - spec.add_development_dependency "pg", "~> 1.5" + spec.add_development_dependency "faker", "~> 3.4.1" + spec.add_development_dependency "fuubar", "~> 2.5.1" + spec.add_development_dependency "mysql2", "~> 0.5.6" + spec.add_development_dependency "pg", "~> 1.5.6" spec.add_development_dependency "rack-test", "~> 2.1" - spec.add_development_dependency "rake", "~> 13.2" - spec.add_development_dependency "rb-fsevent", "~> 0.11" + spec.add_development_dependency "rake", "~> 13.2.1" + spec.add_development_dependency "rb-fsevent", "~> 0.11.2" spec.add_development_dependency "rerun", "~> 0.14" + spec.add_development_dependency "rspec-httpbin", "~> 0.1.0" + spec.add_development_dependency "rspec-parameterized", "~> 1.0.2" spec.add_development_dependency "rspec", "~> 3.13" - spec.add_development_dependency "rspec-parameterized", "~> 1.0" - spec.add_development_dependency "rubocop-factory_bot", "~> 2.25" + spec.add_development_dependency "rubocop-factory_bot", "~> 2.26.0" spec.add_development_dependency "rubocop-rake", "~> 0.6" - spec.add_development_dependency "rubocop-rspec", "~> 2.29" - spec.add_development_dependency "rubocop-yard", "~> 0.9" + spec.add_development_dependency "rubocop-rspec", "~> 2.31.0" + spec.add_development_dependency "rubocop-yard", "~> 0.9.3" spec.add_development_dependency "simplecov-lcov", "~> 0.8" - spec.add_development_dependency "standard", "~> 1.35" - spec.add_development_dependency "test-prof", "~> 1.3" - spec.add_development_dependency "timecop", "~> 0.9" + spec.add_development_dependency "standard", "~> 1.36.0" + spec.add_development_dependency "test-prof", "~> 1.3.3" + spec.add_development_dependency "timecop", "~> 0.9.9" spec.add_development_dependency "vcr", "~> 6.2" - spec.add_development_dependency "webmock", "~> 3.23" + spec.add_development_dependency "webmock", "~> 3.23.1" unless ci_env? - spec.add_development_dependency "lefthook", "~> 1.6" - spec.add_development_dependency "ruby-lsp-rspec", "~> 0.1" + spec.add_development_dependency "lefthook", "~> 1.6.15" + spec.add_development_dependency "ruby-lsp-rspec", "~> 0.1.12" spec.add_development_dependency "solargraph", "~> 0.50" end - spec.add_dependency "activerecord", "7.1.3.2" + spec.add_dependency "activerecord", "7.1.3.4" spec.add_dependency "addressable", "~> 2.8" - spec.add_dependency "anyway_config", "2.6.3" - spec.add_dependency "awrence", "2.0.1" + spec.add_dependency "anyway_config", "2.6.4" + spec.add_dependency "awrence", "3.0.0" spec.add_dependency "csv", "~> 3.3" - spec.add_dependency "dotenv", "3.1.0" + spec.add_dependency "dotenv", "3.1.2" spec.add_dependency "dry-container", "0.11.0" spec.add_dependency "dry-files", "1.1.0" spec.add_dependency "dry-monads", "1.6.0" - spec.add_dependency "dry-schema", "1.13.3" + spec.add_dependency "dry-schema", "1.13.4" spec.add_dependency "dry-struct", "1.6.0" spec.add_dependency "dry-validation", "1.10.0" spec.add_dependency "email_address", "0.2.4" spec.add_dependency "grape", "2.0.0" spec.add_dependency "grape-entity", "1.0.1" - spec.add_dependency "grape-swagger", "2.0.2" - spec.add_dependency "grape-swagger-entity", "0.5.3" + spec.add_dependency "grape-swagger", "2.1.0" + spec.add_dependency "grape-swagger-entity", "0.5.4" spec.add_dependency "http", "5.2.0" - spec.add_dependency "jbuilder", "2.11.5" + spec.add_dependency "jbuilder", "2.12.0" spec.add_dependency "jr-cli", "0.6.0" - spec.add_dependency "launchy", "3.0.0" - spec.add_dependency "memo_wise", "1.8.0" + spec.add_dependency "launchy", "3.0.1" + spec.add_dependency "memo_wise", "1.9.0" spec.add_dependency "normalize_country", "0.3.2" - spec.add_dependency "parallel", "1.24.0" - spec.add_dependency "plissken", "2.0.1" + spec.add_dependency "parallel", "1.25.1" + spec.add_dependency "plissken", "3.0.0" spec.add_dependency "public_suffix", "5.0.5" spec.add_dependency "puma", "6.4.2" - spec.add_dependency "rack", "3.0.10" + spec.add_dependency "rack", "3.0.11" spec.add_dependency "rack-cors", "2.0.2" spec.add_dependency "rack-session", "2.0.0" spec.add_dependency "rackup", "2.1.0" - spec.add_dependency "search_cop", "1.3.0" + spec.add_dependency "search_cop", "1.4.0" spec.add_dependency "semantic_logger", "4.15.0" - spec.add_dependency "sentry-ruby", "~> 5.17" - spec.add_dependency "sentry-sidekiq", "~> 5.17" - spec.add_dependency "sidekiq", "7.2.2" + spec.add_dependency "sentry-ruby", "~> 5.17.3" + spec.add_dependency "sentry-sidekiq", "~> 5.17.3" + spec.add_dependency "sidekiq", "7.2.4" spec.add_dependency "slack-notifier", "2.4.0" spec.add_dependency "sqlite3", "~> 1.7" spec.add_dependency "thor", "1.3.1" diff --git a/spec/analyzers/dnstwister_spec.rb b/spec/analyzers/dnstwister_spec.rb index 55fcc12b8..a16b77872 100644 --- a/spec/analyzers/dnstwister_spec.rb +++ b/spec/analyzers/dnstwister_spec.rb @@ -7,7 +7,9 @@ describe "#artifacts" do before do + # rubocop:disable RSpec/SubjectStub allow(analyzer).to receive(:resolvable?).and_return(true) + # rubocop:enable RSpec/SubjectStub end it do diff --git a/spec/analyzers/otx_spec.rb b/spec/analyzers/otx_spec.rb index ce10f9fd9..b333dac7a 100644 --- a/spec/analyzers/otx_spec.rb +++ b/spec/analyzers/otx_spec.rb @@ -8,7 +8,7 @@ describe "#artifacts" do it do - expect(subject.artifacts).to be_an(Array) + expect(analyzer.artifacts).to be_an(Array) end end end @@ -18,7 +18,7 @@ describe "#artifacts" do it do - expect(subject.artifacts).to be_an(Array) + expect(analyzer.artifacts).to be_an(Array) end end end @@ -28,7 +28,7 @@ describe "#artifacts" do it do - expect { subject.artifacts }.to raise_error(Mihari::ValueError) + expect { analyzer.artifacts }.to raise_error(Mihari::ValueError) end end end @@ -41,7 +41,7 @@ end it do - expect { subject.artifacts }.to raise_error(ArgumentError) + expect { analyzer.artifacts }.to raise_error(ArgumentError) end end end diff --git a/spec/analyzers/securitytrails_spec.rb b/spec/analyzers/securitytrails_spec.rb index e2ca19c85..a898f4e6a 100644 --- a/spec/analyzers/securitytrails_spec.rb +++ b/spec/analyzers/securitytrails_spec.rb @@ -44,7 +44,7 @@ end context "without API credentials" do - let!(:query) { "1.1.1.1" } + let(:query) { "1.1.1.1" } before do allow(Mihari.config).to receive(:securitytrails_api_key).and_return(nil) diff --git a/spec/cli/alert_spec.rb b/spec/cli/alert_spec.rb index 1199711ee..651333f26 100644 --- a/spec/cli/alert_spec.rb +++ b/spec/cli/alert_spec.rb @@ -27,18 +27,19 @@ end describe "#create" do - before do - @file = Tempfile.new("dummy") - @file.write(YAML.dump({rule_id: rule.id, artifacts: %w[1.1.1.1]})) - @file.rewind - end + let(:file) { + file = Tempfile.new("dummy") + file.write(YAML.dump({rule_id: rule.id, artifacts: %w[1.1.1.1]})) + file.rewind + file + } after do - @file.unlink + file.unlink end it do - expect { described_class.new.invoke(:create, [@file.path]) }.to output(include(rule.id)).to_stdout + expect { described_class.new.invoke(:create, [file.path]) }.to output(include(rule.id)).to_stdout end end end diff --git a/spec/cli/rule_spec.rb b/spec/cli/rule_spec.rb index 54334dba8..ce9a68aee 100644 --- a/spec/cli/rule_spec.rb +++ b/spec/cli/rule_spec.rb @@ -11,7 +11,7 @@ it do expect do described_class.new.invoke(:init, [path]) - end.to output("").to_stdout + end.not_to output.to_stdout end end diff --git a/spec/concerns/autonomous_system_normalizable_spec.rb b/spec/concerns/autonomous_system_normalizable_spec.rb index 21063fe48..9a6d89c1e 100644 --- a/spec/concerns/autonomous_system_normalizable_spec.rb +++ b/spec/concerns/autonomous_system_normalizable_spec.rb @@ -5,7 +5,7 @@ class AutonomousSystemTest end RSpec.describe Mihari::Concerns::AutonomousSystemNormalizable do - subject(:subject) { AutonomousSystemTest.new } + subject(:test) { AutonomousSystemTest.new } describe "#normalize_asn" do where(:value, :expected) do @@ -18,7 +18,7 @@ class AutonomousSystemTest with_them do it do - expect(subject.normalize_asn(value)).to eq(expected) + expect(test.normalize_asn(value)).to eq(expected) end end end diff --git a/spec/concerns/error_unwrappable_spec.rb b/spec/concerns/error_unwrappable_spec.rb index 1c5549883..3d74a0f00 100644 --- a/spec/concerns/error_unwrappable_spec.rb +++ b/spec/concerns/error_unwrappable_spec.rb @@ -21,28 +21,28 @@ def raise_error end RSpec.describe Mihari::Concerns::ErrorUnwrappable do - subject(:subject) { ErrorTest.new } + subject(:test) { ErrorTest.new } describe "#unwrap_error" do it do - subject.raise_try_error + test.raise_try_error rescue => e expect(e).not_to be_a ZeroDivisionError - expect(subject.unwrap_error(e)).to be_a ZeroDivisionError + expect(test.unwrap_error(e)).to be_a ZeroDivisionError end it do - subject.raise_result_error + test.raise_result_error rescue => e expect(e).not_to be_a ZeroDivisionError - expect(subject.unwrap_error(e)).to be_a ZeroDivisionError + expect(test.unwrap_error(e)).to be_a ZeroDivisionError end it do - subject.raise_error + test.raise_error rescue => e expect(e).to be_a ZeroDivisionError - expect(subject.unwrap_error(e)).to be_a ZeroDivisionError + expect(test.unwrap_error(e)).to be_a ZeroDivisionError end end end diff --git a/spec/concerns/falsepositive_validatable_spec.rb b/spec/concerns/falsepositive_validatable_spec.rb index 12421d46e..5d4d9fdea 100644 --- a/spec/concerns/falsepositive_validatable_spec.rb +++ b/spec/concerns/falsepositive_validatable_spec.rb @@ -5,7 +5,7 @@ class FalsePositiveTest end RSpec.describe Mihari::Concerns::FalsePositiveValidatable do - subject(:subject) { FalsePositiveTest.new } + subject(:test) { FalsePositiveTest.new } describe "#normalize_falsepositive" do where(:value, :expected) do @@ -19,7 +19,7 @@ class FalsePositiveTest with_them do it do - expect(subject.normalize_falsepositive(value)).to eq(expected) + expect(test.normalize_falsepositive(value)).to eq(expected) end end end @@ -36,7 +36,7 @@ class FalsePositiveTest with_them do it do - expect(subject.valid_falsepositive?(value)).to be expected + expect(test.valid_falsepositive?(value)).to be expected end end end diff --git a/spec/concerns/refangable_spec.rb b/spec/concerns/refangable_spec.rb index f22d1847a..51eb5555a 100644 --- a/spec/concerns/refangable_spec.rb +++ b/spec/concerns/refangable_spec.rb @@ -6,7 +6,7 @@ class RefangTest RSpec.describe Mihari::Concerns::Refangable do describe "#refang" do - subject(:subject) { RefangTest.new } + subject(:test) { RefangTest.new } where(:value, :expected) do [ @@ -18,7 +18,7 @@ class RefangTest with_them do it do - expect(subject.refang(value)).to eq(expected) + expect(test.refang(value)).to eq(expected) end end end diff --git a/spec/concerns/retriable_spec.rb b/spec/concerns/retriable_spec.rb index 32558d431..2cd3e4c2b 100644 --- a/spec/concerns/retriable_spec.rb +++ b/spec/concerns/retriable_spec.rb @@ -28,40 +28,42 @@ def http end RSpec.describe Mihari::Concerns::Retriable do - subject(:subject) { RetriableTest.new } + subject(:test) { RetriableTest.new } include_context "with fake HTTPBin" describe "#retry_on_error" do context "with 404" do it do - expect { subject.retriable_get("#{server.base_url}/status/404") }.to raise_error(Mihari::StatusError) - expect(subject.count).to eq(1) + expect { test.retriable_get("#{server.base_url}/status/404") }.to raise_error(Mihari::StatusError) + expect(test.count).to eq(1) end end context "with 401" do it do - expect { subject.retriable_get("#{server.base_url}/status/401") }.to raise_error(Mihari::StatusError) - expect(subject.count).to eq(1) + expect { test.retriable_get("#{server.base_url}/status/401") }.to raise_error(Mihari::StatusError) + expect(test.count).to eq(1) end end context "with non-404" do it do - expect { subject.retriable_get("#{server.base_url}/status/500") }.to raise_error(Mihari::StatusError) - expect(subject.count).to eq(subject.times) + expect { test.retriable_get("#{server.base_url}/status/500") }.to raise_error(Mihari::StatusError) + expect(test.count).to eq(test.times) end end context "with HTTP::TimeoutError" do before do - allow(subject).to receive(:http).and_return(Mihari::HTTP::Factory.build(timeout: -1)) + # rubocop:disable RSpec/SubjectStub + allow(test).to receive(:http).and_return(Mihari::HTTP::Factory.build(timeout: -1)) + # rubocop:enable RSpec/SubjectStub end it do - expect { subject.retriable_get("#{server.base_url}/get") }.to raise_error(HTTP::TimeoutError) - expect(subject.count).to eq(subject.times) + expect { test.retriable_get("#{server.base_url}/get") }.to raise_error(HTTP::TimeoutError) + expect(test.count).to eq(test.times) end end end diff --git a/spec/config_spec.rb b/spec/config_spec.rb deleted file mode 100644 index 85a52d8de..000000000 --- a/spec/config_spec.rb +++ /dev/null @@ -1,63 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe Mihari.config do - describe "#retry_exponential_backoff" do - context "with lowercase value" do - before do - ENV["RETRY_EXPONENTIAL_BACKOFF"] = "true" - Mihari.config.load - end - - it do - expect(Mihari.config.retry_exponential_backoff).to be true - end - end - - context "with non-lowercase value" do - before do - ENV["RETRY_EXPONENTIAL_BACKOFF"] = "False" - Mihari.config.load - end - - it do - expect(Mihari.config.retry_exponential_backoff).to be false - end - end - end - - context "with analyzer configuration keys" do - where(:key) do - Mihari.analyzers.map(&:configuration_keys).flatten - end - - with_them do - it do - expect(Mihari.config.respond_to?(key)).to be true - end - end - end - - context "with emitter configuration keys" do - where(:key) do - Mihari.emitters.map(&:configuration_keys).flatten - end - - with_them do - it do - expect(Mihari.config.respond_to?(key)).to be true - end - end - end - - context "with enricher configuration keys" do - where(:key) do - Mihari.enrichers.map(&:configuration_keys).flatten - end - - with_them do - it do - expect(Mihari.config.respond_to?(key)).to be true - end - end - end -end diff --git a/spec/emitters/misp_spec.rb b/spec/emitters/misp_spec.rb index 57bc42aa8..fc55c8860 100644 --- a/spec/emitters/misp_spec.rb +++ b/spec/emitters/misp_spec.rb @@ -23,7 +23,7 @@ describe "#call" do it do - emitter.call artifacts + expect(emitter.call(artifacts)).not_to be_nil end end diff --git a/spec/emitters/slack_spec.rb b/spec/emitters/slack_spec.rb index 0d4184f0b..26912c773 100644 --- a/spec/emitters/slack_spec.rb +++ b/spec/emitters/slack_spec.rb @@ -60,7 +60,7 @@ end describe "#call" do - let!(:mock) { instance_double("notifier") } + let!(:mock) { instance_double(Slack::Notifier) } before do allow(::Slack::Notifier).to receive(:new).and_return(mock) diff --git a/spec/emitters/the_hive_spec.rb b/spec/emitters/the_hive_spec.rb index 696100225..9c1b9534a 100644 --- a/spec/emitters/the_hive_spec.rb +++ b/spec/emitters/the_hive_spec.rb @@ -29,7 +29,7 @@ end describe "#call" do - let!(:mock_client) { instance_double("client") } + let!(:mock_client) { instance_double(Mihari::Clients::TheHive) } let!(:mocked_emitter) { described_class.new(rule:) } before do diff --git a/spec/emitters/yeti_spec.rb b/spec/emitters/yeti_spec.rb index fbdeeadd8..ce12ac0e7 100644 --- a/spec/emitters/yeti_spec.rb +++ b/spec/emitters/yeti_spec.rb @@ -25,7 +25,7 @@ end describe "#call" do - let!(:mock_client) { instance_double("client") } + let!(:mock_client) { instance_double(Mihari::Clients::Yeti) } let!(:mocked_emitter) { described_class.new(rule:) } before do diff --git a/spec/factories/rules.rb b/spec/factories/rules.rb index 2541aa05d..5e2207e1d 100644 --- a/spec/factories/rules.rb +++ b/spec/factories/rules.rb @@ -5,7 +5,7 @@ id { Faker::Internet.unique.uuid } title { Faker::Internet.unique.slug } description { Faker::Internet.unique.slug } - tags { build_list :tag, 1 } + tags { build_list(:tag, 1) } data do { id:, diff --git a/spec/mihari_spec.rb b/spec/mihari_spec.rb index a36136f88..019c8e2b5 100644 --- a/spec/mihari_spec.rb +++ b/spec/mihari_spec.rb @@ -2,22 +2,84 @@ RSpec.describe Mihari do it "has a version number" do - expect(Mihari::VERSION).not_to be nil + expect(Mihari::VERSION).not_to be_nil end it "has a config" do - expect(described_class.config).not_to be nil + expect(described_class.config).not_to be_nil end describe "#sidekiq?" do it "returns false in RSpec" do - expect(described_class.sidekiq?).to eq(false) + expect(described_class.sidekiq?).to be(false) end end describe "#puma?" do it "returns false in RSpec" do - expect(described_class.puma?).to eq(false) + expect(described_class.puma?).to be(false) + end + end + + describe "#config" do + describe "#retry_exponential_backoff" do + context "with lowercase value" do + before do + ENV["RETRY_EXPONENTIAL_BACKOFF"] = "true" + described_class.config.load + end + + it do + expect(described_class.config.retry_exponential_backoff).to be true + end + end + + context "with non-lowercase value" do + before do + ENV["RETRY_EXPONENTIAL_BACKOFF"] = "False" + described_class.config.load + end + + it do + expect(described_class.config.retry_exponential_backoff).to be false + end + end + end + + context "with analyzer configuration keys" do + where(:key) do + described_class.analyzers.map(&:configuration_keys).flatten + end + + with_them do + it do + expect(described_class.config.respond_to?(key)).to be true + end + end + end + + context "with emitter configuration keys" do + where(:key) do + described_class.emitters.map(&:configuration_keys).flatten + end + + with_them do + it do + expect(described_class.config.respond_to?(key)).to be true + end + end + end + + context "with enricher configuration keys" do + where(:key) do + described_class.enrichers.map(&:configuration_keys).flatten + end + + with_them do + it do + expect(described_class.config.respond_to?(key)).to be true + end + end end end end diff --git a/spec/models/artifact_spec.rb b/spec/models/artifact_spec.rb index f320cedc1..243db8580 100644 --- a/spec/models/artifact_spec.rb +++ b/spec/models/artifact_spec.rb @@ -65,14 +65,14 @@ describe "#enrichable?" do it do - expect(artifact.enrichable?).to eq(true) + expect(artifact.enrichable?).to be(true) end context "with unenrichable artifact" do let(:artifact) { FactoryBot.build(:artifact, :unenrichable) } it do - expect(artifact.enrichable?).to eq(false) + expect(artifact.enrichable?).to be(false) end end end @@ -116,12 +116,12 @@ let(:alert) { artifact.alert } it do - expect(Mihari::Models::Alert.exists?(alert.id)).to eq(true) + expect(Mihari::Models::Alert.exists?(alert.id)).to be(true) end it do artifact.destroy - expect(Mihari::Models::Alert.exists?(alert.id)).to eq(false) + expect(Mihari::Models::Alert.exists?(alert.id)).to be(false) end end end diff --git a/spec/rule_spec.rb b/spec/rule_spec.rb index 5c5e6ed80..9eff318fd 100644 --- a/spec/rule_spec.rb +++ b/spec/rule_spec.rb @@ -134,25 +134,25 @@ end it do - expect(rule.diff?).to eq(false) + expect(rule.diff?).to be(false) end context "with modified integer" do it do rule.tap { |rule| rule.data[:artifact_ttl] = -1 } - expect(rule.diff?).to eq(true) + expect(rule.diff?).to be(true) end end context "with modified dates" do it do rule.tap { |rule| rule.data[:created_on] = Date.today.prev_day } - expect(rule.diff?).to eq(true) + expect(rule.diff?).to be(true) end it do rule.tap { |rule| rule.data[:updated_on] = Date.today.prev_day } - expect(rule.diff?).to eq(true) + expect(rule.diff?).to be(true) end end end diff --git a/spec/schemas/rule_spec.rb b/spec/schemas/rule_spec.rb index cf8452d93..9af16852f 100644 --- a/spec/schemas/rule_spec.rb +++ b/spec/schemas/rule_spec.rb @@ -45,7 +45,7 @@ it do result = contract.call(**data) - expect(result.errors.empty?).to eq true + expect(result.errors.empty?).to be true end end diff --git a/spec/services/artifact_enricher_spec.rb b/spec/services/artifact_enricher_spec.rb index 71d52585a..0f41d18f9 100644 --- a/spec/services/artifact_enricher_spec.rb +++ b/spec/services/artifact_enricher_spec.rb @@ -10,7 +10,7 @@ end it do - expect(described_class.call(artifact.id)).to eq(true) + expect(described_class.call(artifact.id)).to be(true) end end diff --git a/spec/sidekiq/search_job_spec.rb b/spec/sidekiq/search_job_spec.rb index 59e9282b6..aa6b9c1ab 100644 --- a/spec/sidekiq/search_job_spec.rb +++ b/spec/sidekiq/search_job_spec.rb @@ -8,7 +8,7 @@ it do Sidekiq::Testing.inline! do res = described_class.perform_async(rule.id) - expect(res).not_to be nil + expect(res).not_to be_nil end end end diff --git a/spec/support/httpbin.rb b/spec/support/httpbin.rb deleted file mode 100644 index d4b17a21c..000000000 --- a/spec/support/httpbin.rb +++ /dev/null @@ -1,83 +0,0 @@ -# frozen_string_literal: true - -require "forwardable" - -class HTTPBin - extend Forwardable - - class << self - def call(env) - new(env).call - end - end - - attr_reader :req - - def initialize(env) - @req = Rack::Request.new(env) - end - - def_delegators :req, :content_type, :form_data?, :get?, :post?, :delete?, :url, :query_string, :path, :path_info - - def call - case path_info - when "/get" - get? ? ok_response : error_405 - when "/post" - post? ? ok_response : error_405 - when "/delete" - delete? ? ok_response : error_405 - when %r{^/status/[0-9]+$} - status = path_info.split("/").last - status_response status - else - error_404 - end - end - - def headers - http_headers = req.env.select { |k, _v| k.start_with?("HTTP_") } - http_headers.transform_keys do |k| - k.sub(/^HTTP_/, "").downcase.gsub(/(^|_)\w/, &:upcase).tr("_", "-") - end - end - - def json? - content_type == "application/json" - end - - def origin - req.env["REMOTE_ADDR"] - end - - def body - @body ||= req.body.read - end - - def body_payload - return {} if body == "" - return {data: body, files: {}, form: {}, json: JSON.parse(body)} if json? - return {data: "", files: {}, form: Rack::Utils.parse_nested_query(body), json: nil} if form_data? - - {data: body, files: {}, form: {}, json: nil} - end - - def ok_response - payload = body_payload.merge(args: query_string, headers:, origin:, url:) - - ["200", {"Content-Type" => "application/json"}, [JSON.generate(payload)]] - ["200", {"Content-Type" => "text/plain"}, [JSON.generate(payload)]] - end - - def status_response(status) - [status, {"Content-Type" => "text/plain"}, [JSON.generate({})]] - end - - def error_404 - ["404", {"Content-Type" => "application/json"}, [JSON.generate({})]] - end - - def error_405 - ["405", {"Content-Type" => "application/json"}, [JSON.generate({})]] - end -end diff --git a/spec/support/shared_contexts/httpbin_context.rb b/spec/support/shared_contexts/httpbin_context.rb index 2bb12ec02..d610bce59 100644 --- a/spec/support/shared_contexts/httpbin_context.rb +++ b/spec/support/shared_contexts/httpbin_context.rb @@ -1,12 +1,11 @@ # frozen_string_literal: true require "capybara" - -require_relative "../httpbin" +require "rspec/httpbin" RSpec.shared_context "with fake HTTPBin" do let_it_be(:server) do - server = Capybara::Server.new(HTTPBin) + server = Capybara::Server.new(RSpec::HTTPBin) server.boot server end