Skip to content

Commit

Permalink
Merge pull request #57 from ninoseki/refactoring-specs
Browse files Browse the repository at this point in the history
refactoring: refactoring specs
  • Loading branch information
ninoseki committed Jun 23, 2019
2 parents ef89fa4 + 39fe9d8 commit 53f2fb7
Show file tree
Hide file tree
Showing 24 changed files with 237 additions and 8,057 deletions.
3 changes: 2 additions & 1 deletion lib/ayashige/sources/ct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "certificate-transparency-client"
require "filecache"
require "http"
require "parallel"

module Ayashige
module Sources
Expand Down Expand Up @@ -58,7 +59,7 @@ def name
end

def store_newly_registered_domains
records.each { |record| store record }
Parallel.each(records) { |record| store record }
end

def get_domain_name(subject)
Expand Down
3 changes: 2 additions & 1 deletion lib/ayashige/sources/securitytrails.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "date"
require "parallel"
require "securitytrails"

module Ayashige
Expand All @@ -13,7 +14,7 @@ def initialize
end

def store_newly_registered_domains
records.each { |record| store record }
Parallel.each(records) { |record| store record }
end

def records
Expand Down
10 changes: 6 additions & 4 deletions spec/domain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
require "yaml"

RSpec.describe Ayashige::Domain do
subject { Ayashige::Domain }
subject { described_class }

describe "#score" do
let(:scores) { YAML.load_file File.expand_path("./fixtures/scores.yml", __dir__) }
it "should calculate a suspicious score of a given domain" do

it "calculates a suspicious score of a given domain" do
domains = scores.keys
domains.each do |domain|
d = subject.new(domain)
Expand All @@ -17,14 +19,14 @@

describe "#suspicious?" do
context "when given an unofficial suspicious domain" do
it "should return true" do
it "returns true" do
d = subject.new("pay.pay.pay.pay.paypal.com.cn")
expect(d.suspicious?).to eq(true)
end
end

context "when given an suspicious but official domain" do
it "should return false" do
it "returns false" do
d = subject.new("pay.pay.pay.pay.paypal.com")
expect(d.suspicious?).to eq(false)
end
Expand Down
Binary file removed spec/fixtures/archive.zip
Binary file not shown.

Large diffs are not rendered by default.

This file was deleted.

Large diffs are not rendered by default.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

8 changes: 5 additions & 3 deletions spec/jobs/job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

RSpec.describe Ayashige::Jobs::Job do
subject { Ayashige::Jobs::Job.new }
subject { described_class.new }

describe "#perform" do
context "when an error is raised in #perform" do
Expand All @@ -10,23 +10,25 @@
allow(source).to receive(:store_newly_registered_domains).and_raise(ArgumentError)
subject.instance_variable_set(:@source, source)
end

context "when Rollbar is enabled" do
before do
allow(Ayashige::Rollbar).to receive(:available?).and_return(true)
allow(Ayashige::Rollbar).to receive(:error).and_return(nil)
end

it "should not raise any error" do
it "does not raise any error" do
out = capture(:stdout) { expect { subject.perform }.not_to raise_error }
expect(out.empty?).to eq(true)
end
end

context "when Rollbar is disabled" do
before do
allow(Ayashige::Rollbar).to receive(:available?).and_return(false)
end

it "should not raise any error" do
it "does not raise any error" do
out = capture(:stdout) { expect { subject.perform }.not_to raise_error }
expect(out.chomp).to eq("ArgumentError")
end
Expand Down
7 changes: 4 additions & 3 deletions spec/record_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# frozen_string_literal: true

RSpec.describe Ayashige::Record do
subject { Ayashige::Record.new(updated: "2018/01/01", domain_name: "test.com") }
subject { described_class.new(updated: "2018/01/01", domain_name: "test.com") }

describe "#updated_on" do
context "when given a valid date" do
it "should return %Y-%m-%d format string" do
it "returns %Y-%m-%d format string" do
s = subject.send(:normalize_date, "2018/01/01")
expect(s).to eq("2018-01-01")
end
end

context "when given an invalid date" do
it "should raise an ArgumentError" do
it "raises an ArgumentError" do
expect { subject.send(:normalize_date, "invalid") }.to raise_error(ArgumentError)
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/rollbar_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# frozen_string_literal: true

RSpec.describe Ayashige::Rollbar do
subject { Ayashige::Rollbar }
subject { described_class }

describe "#available?" do
context "when ENV['ROLLBAR_ACCESS_TOKEN'] exists" do
before do
allow(ENV).to receive(:key?).with(subject::ROLLBAR_KEY).and_return(true)
end

it "should return true" do
it "returns true" do
expect(subject.available?).to eq(true)
end
end
Expand All @@ -19,7 +19,7 @@
allow(ENV).to receive(:key?).with(subject::ROLLBAR_KEY).and_return(false)
end

it "should return false" do
it "returns false" do
expect(subject.available?).to eq(false)
end
end
Expand Down
22 changes: 7 additions & 15 deletions spec/sources/ct_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
subject { Ayashige::Sources::CTLogServer.new("https://ct.googleapis.com/logs/argon2020", cache) }

describe "#x509_entries" do
it "should return an Array of CT log entries which have x509 entry" do
it "returns an Array of CT log entries which have x509 entry" do
subject.x509_entries.each do |entry|
expect(entry.leaf_input.timestamped_entry.x509_entry).to be_a(OpenSSL::X509::Certificate)
end
Expand All @@ -24,20 +24,12 @@
end

RSpec.describe Ayashige::Sources::CT, :vcr do
subject { Ayashige::Sources::CT.new }

let(:redis) { MockRedis.new }
include_context "source context"

before do
allow(Ayashige::Redis).to receive(:client).and_return(redis)
end

after do
redis.flushdb
end
subject { Ayashige::Sources::CT.new }

describe "#ct_log_servers" do
it "should return an Array of CTLServer" do
it "returns an Array of CTLServer" do
servers = subject.ct_log_servers
expect(servers).to be_an(Array)
expect(servers.first).to be_an(Ayashige::Sources::CTLogServer)
Expand All @@ -58,22 +50,22 @@
allow(subject).to receive(:ct_log_servers).and_return(ct_log_servers)
end

it "should return an Array of records" do
it "returns an Array of records" do
records = subject.records
expect(records).to be_an(Array)
expect(records).not_to be_empty

records.each do |record|
expect(record.domain).to be_a(Ayashige::Domain)
expect(record.updated_on).to be_a(String)
rescue ArgumentError => _
rescue ArgumentError => _e
next
end
end
end

describe "#name" do
it "should return a name of the class" do
it "returns a name of the class" do
expect(subject.name).to eq("CT log")
end
end
Expand Down
3 changes: 3 additions & 0 deletions spec/sources/securitytrails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
require "mock_redis"

RSpec.describe Ayashige::Sources::SecurityTrails do
include_context "source context"

subject { described_class.new }

let(:redis) { MockRedis.new }

before do
allow(Ayashige::Redis).to receive(:client).and_return(redis)
allow(Parallel).to receive(:processor_count).and_return(0)
end

after do
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
end

require_relative "./support/shared_contexts/job_context"
require_relative "./support/shared_contexts/source_context"
require_relative "./support/shared_examples/source_example"

RSpec.configure do |rspec|
rspec.include_context "job context", include_shared: true
rspec.include_context "source context", include_shared: true
end

VCR.configure do |config|
Expand Down
21 changes: 13 additions & 8 deletions spec/store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "mock_redis"

RSpec.describe Ayashige::Store do
subject { Ayashige::Store.new }
subject { described_class.new }

let(:redis) { MockRedis.new }

Expand All @@ -25,7 +25,7 @@
end

describe "#store" do
it "should store arguments as a Hash" do
it "stores arguments as a Hash" do
subject.store(sample)
expect(redis.hgetall("test.com")).to eq(
"score" => "80",
Expand All @@ -38,15 +38,16 @@
before do
allow(ENV).to receive(:[]).with("DEFAULT_TTL").and_return(100)
end
it "should store arguments as a Hash with TTL = 100" do

it "stores arguments as a Hash with TTL = 100" do
subject.store sample
expect(redis.ttl("test.com")).to be_between(1, 100)
end
end
end

describe "#get" do
it "should return a Hash value" do
it "returns a Hash value" do
redis.hmset "test.com", "score", 80, "updated_on", "2018-01-01", "source", "test"
redis.hmset "test2.com", "score", 80, "updated_on", "2018-01-02", "source", "test"
expect(subject.get("test.com")).to eq(
Expand All @@ -62,7 +63,8 @@
redis.set "key1", "test"
redis.set "key2", "test"
end
it "should return keys" do

it "returns keys" do
expect(subject.keys).to eq(["key1", "key2"])
end
end
Expand All @@ -72,7 +74,8 @@
redis.hmset "test.com", "score", 80, "updated_on", "2018-01-01", "source", "test"
redis.hmset "test2.com", "score", 80, "updated_on", "2018-01-02", "source", "test"
end
it "should return all data as a Hash" do

it "returns all data as a Hash" do
expect(subject.all).to eq(
"test.com" => { "score" => "80", "updated_on" => "2018-01-01", "source" => "test" },
"test2.com" => { "score" => "80", "updated_on" => "2018-01-02", "source" => "test" }
Expand All @@ -83,12 +86,14 @@
describe "#exists?" do
context "when a given key exists" do
before { redis.set "test", "value" }
it "should return true" do

it "returns true" do
expect(subject.exists?("test")).to eq(true)
end
end

context "when a given key not exists" do
it "should return false" do
it "returns false" do
expect(subject.exists?("test")).to eq(false)
end
end
Expand Down
14 changes: 14 additions & 0 deletions spec/support/shared_contexts/source_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

RSpec.shared_context "source context", shared_context: :metadata do
let(:redis) { MockRedis.new }

before do
allow(Ayashige::Redis).to receive(:client).and_return(redis)
allow(Parallel).to receive(:processor_count).and_return(0)
end

after do
redis.flushdb
end
end

0 comments on commit 53f2fb7

Please sign in to comment.