Skip to content

Commit e2354da

Browse files
authored
Merge pull request #210 from internetee/add-tests-improve-coverage
Upgrade test coverage from 24.6% to 94.12%
2 parents 2403173 + 3249098 commit e2354da

File tree

8 files changed

+465
-24
lines changed

8 files changed

+465
-24
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
require 'test_helper'
2+
require 'simpleidn'
3+
require_relative '../../lib/whois_server'
4+
require_relative '../../lib/whois_server_core'
5+
require_relative '../../app/models/whois_record'
6+
require_relative '../../app/validators/unicode_validator'
7+
8+
TEST_DOMAIN = 'test-domain.ee'
9+
INTEGRATION_TEST_DOMAIN = 'integration-test.ee'
10+
11+
class WhoisServerIntegrationTest < Minitest::Test
12+
include WhoisServer
13+
14+
def setup
15+
super
16+
@sent_data = []
17+
@connection_closed = false
18+
@connection_closed_after_writing = false
19+
@logger_output = StringIO.new
20+
@logger = Logger.new(@logger_output)
21+
ENV['WHOIS_ENV'] = 'test'
22+
end
23+
24+
def stub_connection_and_ip(ip = ['12345', '127.0.0.1'])
25+
stubs(:connection).returns(nil)
26+
stubs(:extract_ip).returns(ip)
27+
end
28+
29+
def mock_whois_record(body: 'Whois body', id: 1)
30+
mock_record = Minitest::Mock.new
31+
mock_record.expect(:unix_body, body)
32+
mock_record.expect(:id, id)
33+
WhoisRecord.stubs(:find_by).returns(mock_record)
34+
mock_record
35+
end
36+
37+
def test_receive_data_rejects_invalid_encoding
38+
invalid_payload = "\xFF\xFE".dup.force_encoding('ASCII-8BIT')
39+
receive_data(invalid_payload)
40+
41+
assert_equal 1, @sent_data.length
42+
assert_includes @sent_data.first.downcase, 'invalid encoding'
43+
assert @connection_closed_after_writing
44+
end
45+
46+
def test_receive_data_returns_valid_whois_body
47+
stub_connection_and_ip
48+
mock_record = mock_whois_record(body: 'This is a valid WHOIS record')
49+
50+
receive_data(TEST_DOMAIN)
51+
52+
assert_equal 1, @sent_data.length
53+
assert_includes @sent_data.first, 'This is a valid WHOIS record'
54+
assert @connection_closed_after_writing
55+
56+
mock_record.verify
57+
end
58+
59+
def test_receive_data_domain_not_found
60+
stub_connection_and_ip
61+
WhoisRecord.stubs(:find_by).returns(nil)
62+
63+
receive_data(TEST_DOMAIN)
64+
65+
assert_equal 1, @sent_data.length
66+
assert_includes @sent_data.first.downcase, 'domain not found'
67+
assert @connection_closed_after_writing
68+
end
69+
70+
def test_receive_data_with_no_ip
71+
stubs(:extract_ip).returns(nil)
72+
stubs(:connection).returns(nil)
73+
74+
receive_data(TEST_DOMAIN)
75+
76+
assert_equal 0, @sent_data.length
77+
end
78+
79+
def send_data(data)
80+
@sent_data << data
81+
end
82+
83+
def close_connection_after_writing
84+
@connection_closed_after_writing = true
85+
end
86+
87+
def close_connection
88+
@connection_closed = true
89+
end
90+
91+
def get_peername
92+
Socket.pack_sockaddr_in(12345, '127.0.0.1')
93+
end
94+
95+
def logger
96+
@logger
97+
end
98+
end

test/logging_test.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@ class LoggingTest < Minitest::Test
99

1010
def setup
1111
@output = StringIO.new
12-
@logger = Logger.new(@output)
13-
@logger.formatter = proc do |severity, datetime, progname, msg|
14-
"[#{progname}] #{msg}\n"
12+
@logger = nil
13+
end
14+
15+
def logger
16+
@logger ||= begin
17+
original_logger = super
18+
io_logger = Logger.new(@output, progname: 'whois')
19+
io_logger.formatter = proc do |severity, datetime, progname, msg|
20+
"[#{progname}] #{msg}\n"
21+
end
22+
@logger = io_logger
1523
end
1624
end
1725

@@ -73,5 +81,5 @@ def test_log_record_not_found
7381

7482
assert_includes log_output, '"status":"not_found"'
7583
assert_includes log_output, '"record_found":false'
76-
end
84+
end
7785
end

test/models/contact_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,30 @@ def test_attribute_concealed
1111
contact = Contact.new(disclosed_attributes: %w[other])
1212
refute contact.attribute_disclosed?(:name)
1313
end
14+
15+
def test_attribute_disclosed_with_string_attribute
16+
contact = Contact.new(disclosed_attributes: %w[name email])
17+
assert contact.attribute_disclosed?('name')
18+
assert contact.attribute_disclosed?(:email)
19+
end
20+
21+
def test_attribute_disclosed_with_nil_disclosed_attributes
22+
contact = Contact.new(disclosed_attributes: nil)
23+
refute contact.attribute_disclosed?(:name)
24+
end
25+
26+
def test_publishable_returns_true
27+
contact = Contact.new(registrant_publishable: true)
28+
assert contact.publishable?
29+
end
30+
31+
def test_publishable_returns_false
32+
contact = Contact.new(registrant_publishable: false)
33+
refute contact.publishable?
34+
end
35+
36+
def test_publishable_with_nil
37+
contact = Contact.new(registrant_publishable: nil)
38+
refute contact.publishable?
39+
end
1440
end

test/test_helper.rb

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,42 @@
1+
require 'simplecov'
2+
SimpleCov.start 'rails'
3+
14
require 'minitest/autorun'
25
require 'mocha/minitest'
36
require 'active_record'
47
require 'yaml'
58

69
ENV['WHOIS_ENV'] ||= 'test'
710

8-
require 'simplecov'
9-
SimpleCov.start 'rails'
11+
# --- Prevent EventMachine from starting in tests ---
12+
# This monkey-patch must run before whois server files are required.
13+
require 'eventmachine'
14+
module EventMachine
15+
class << self
16+
def run(*)
17+
yield if block_given?
18+
end
19+
20+
def start_server(*)
21+
# no-op in tests to avoid binding real ports
22+
end
23+
24+
def set_effective_user(*)
25+
# no-op in tests
26+
end
27+
end
28+
end
29+
# ---------------------------------------------------
1030

1131
class Minitest::Test
1232
def dbconfig
1333
return @dbconfig unless @dbconfig.nil?
1434

1535
begin
16-
dbconf = YAML.load_file(File.open(File.expand_path('../config/database.yml', __dir__)), aliases: true)
17-
@dbconfig = dbconf[(ENV['WHOIS_ENV'])]
36+
dbconf = YAML.load_file(File.expand_path('../config/database.yml', __dir__), aliases: true)
37+
@dbconfig = dbconf[ENV['WHOIS_ENV']]
1838
rescue NoMethodError => e
19-
logger.fatal "\n----> Please inspect config/database.yml for issues! Error: #{e}\n\n"
39+
warn "\n----> Please inspect config/database.yml for issues! Error: #{e}\n\n"
2040
end
2141
end
2242

@@ -26,7 +46,6 @@ def connection
2646

2747
def setup
2848
super
29-
3049
connection
3150
end
3251
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'test_helper'
2+
require_relative '../../lib/whois_server'
3+
require_relative '../../lib/whois_server_core'
4+
5+
class WhoisServerCoreTest < Minitest::Test
6+
include WhoisServer
7+
8+
def test_allows_one_letter_domain
9+
assert_match WhoisServerCore::DOMAIN_NAME_REGEXP, 'a.ee'
10+
assert_match WhoisServerCore::DOMAIN_NAME_REGEXP, 'õ.ee'
11+
assert_match WhoisServerCore::DOMAIN_NAME_REGEXP, '1.ee'
12+
refute_match WhoisServerCore::DOMAIN_NAME_REGEXP, 'a..ee'
13+
assert_match WhoisServerCore::DOMAIN_NAME_REGEXP, 'ab.ee'
14+
end
15+
16+
def test_domain_valid_format_valid_domains
17+
assert send(:domain_valid_format?, 'example.ee')
18+
assert send(:domain_valid_format?, 'test-domain.ee')
19+
assert send(:domain_valid_format?, 'õ.ee')
20+
assert send(:domain_valid_format?, 'example.ee'.downcase)
21+
end
22+
23+
def test_domain_valid_format_invalid_domains
24+
refute send(:domain_valid_format?, 'invalid..ee')
25+
refute send(:domain_valid_format?, '')
26+
refute send(:domain_valid_format?, 'no-tld')
27+
refute send(:domain_valid_format?, '.ee')
28+
refute send(:domain_valid_format?, 'example.')
29+
end
30+
31+
def test_domain_valid_format_with_whitespace
32+
assert send(:domain_valid_format?, ' example.ee ')
33+
assert send(:domain_valid_format?, "\texample.ee\n")
34+
end
35+
36+
def test_domain_valid_format_with_mixed_case
37+
assert send(:domain_valid_format?, 'EXAMPLE.EE')
38+
assert send(:domain_valid_format?, 'ExAmPlE.Ee')
39+
end
40+
end

0 commit comments

Comments
 (0)