Skip to content

Commit

Permalink
Merge 9fc3f13 into 0e12f05
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed May 25, 2019
2 parents 0e12f05 + 9fc3f13 commit 40ede53
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 43 deletions.
2 changes: 1 addition & 1 deletion lib/ayashige/sources/ct.rb
Expand Up @@ -29,7 +29,7 @@ def x509_entries
@cache.set(url, sth.tree_size)

entries
rescue StandardError => _
rescue StandardError => _e
[]
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ayashige/sources/source.rb
Expand Up @@ -36,13 +36,13 @@ def store(record)

def html2doc(html)
Oga.parse_html html
rescue StandardError => _
rescue StandardError => e
nil
end

def xml2doc(xml)
Oga.parse_xml xml
rescue StandardError => _
rescue StandardError => e
nil
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/application_spec.rb
Expand Up @@ -11,7 +11,7 @@ def app
end

describe "GET /" do
it "should return 200 OK" do
it "returns 200 OK" do
get "/"
expect(last_response).to be_ok
end
Expand All @@ -25,7 +25,7 @@ def app
)
end

it "should return 200 OK and JSON response" do
it "returns 200 OK and JSON response" do
get "/feed"
expect(last_response).to be_ok
array = JSON.parse(last_response.body.to_s)
Expand Down
10 changes: 6 additions & 4 deletions spec/domain_spec.rb
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
8 changes: 5 additions & 3 deletions spec/jobs/job_spec.rb
@@ -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
@@ -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
@@ -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
14 changes: 7 additions & 7 deletions spec/sources/ct_spec.rb
Expand Up @@ -5,17 +5,17 @@
require "mock_redis"

RSpec.describe Ayashige::Sources::CTLogServer, :vcr do
subject { Ayashige::Sources::CTLogServer.new("https://ct.googleapis.com/logs/argon2020", cache) }

let(:cache) { double("cache") }

before do
allow(cache).to receive(:get).and_return(0)
allow(cache).to receive(:set)
end

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 @@ -37,7 +37,7 @@
end

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 +58,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
2 changes: 1 addition & 1 deletion spec/sources/domain_watch_spec.rb
Expand Up @@ -16,7 +16,7 @@
end

describe "#get_records_from_doc" do
it "should return domains in a page as an Array" do
it "returns domains in a page as an Array" do
page = subject.get_page(1)
records = subject.get_records_from_doc(page)

Expand Down
6 changes: 3 additions & 3 deletions spec/sources/web_analyzer_spec.rb
Expand Up @@ -16,13 +16,13 @@
end

describe "#latest_indexed_date" do
it "should return the latest indexed date as a String" do
it "returns the latest indexed date as a String" do
expect(subject.latest_indexed_date).to be_a(String)
end
end

describe "#get_records_from_doc" do
it "should return domains in a page as an Array" do
it "returns domains in a page as an Array" do
page = subject.get_page("2018-11-22", "com", "1")
records = subject.get_records_from_doc(page)

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

describe "#get_links_from_doc" do
it "should return links in a page as an Array" do
it "returns links in a page as an Array" do
page = subject.get_page("2018-11-22", "com", "1")
links = subject.get_links_from_doc(page)

Expand Down
6 changes: 3 additions & 3 deletions spec/sources/whoisds_spec.rb
Expand Up @@ -16,20 +16,20 @@
end

describe "#latest_zip_file_link" do
it "should return a link of the latest zip file as a String" do
it "returns a link of the latest zip file as a String" do
expect(subject.latest_zip_file_link).to be_a(String)
expect(subject.latest_zip_file_link).to include "https://whoisds.com//whois-database/newly-registered-domains"
end
end

describe "#latest_indexed_date" do
it "should return the latest indexed date as a String" do
it "returns the latest indexed date as a String" do
expect(subject.latest_indexed_date).to be_a(String)
end
end

describe "#unzip" do
it "should unzip a given zip file and extract its contents" do
it "unzips a given zip file and extract its contents" do
data = File.read(File.expand_path("../fixtures/archive.zip", __dir__))
lines = subject.unzip(data)
expect(lines).to eq(["test.com", "test2.com", "test3.com"])
Expand Down
21 changes: 13 additions & 8 deletions spec/store_spec.rb
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
5 changes: 3 additions & 2 deletions spec/support/shared_contexts/job_context.rb
Expand Up @@ -9,19 +9,20 @@
subject.instance_variable_set(:@source, mock)
end

it "should return the latest indexed date as a String" do
it "returns the latest indexed date as a String" do
output = capture(:stdout){ subject.perform }
expect(output.empty?).to eq(true)
end
end

context "when an error is raised" do
before do
mock = double
allow(mock).to receive(:store_newly_registered_domains).and_raise(ArgumentError, "argument error")
subject.instance_variable_set(:@source, mock)
end

it "should return the latest indexed date as a String" do
it "returns the latest indexed date as a String" do
output = capture(:stdout) { subject.perform }
expect(output).to include("argument error")
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/shared_examples/source_example.rb
Expand Up @@ -4,7 +4,7 @@
let(:updated_on) { "2018-01-01" }
let(:domain_name) { "paypal.pay.pay.world" }

it "should store parsed domains into Redis" do
it "stores parsed domains into Redis" do
output = capture(:stdout) { subject.store_newly_registered_domains }
expect(output.include?(domain_name)).to eq(true)
expect(redis.exists(domain_name)).to eq(true)
Expand Down

0 comments on commit 40ede53

Please sign in to comment.