Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions test/integration/whois_server_integration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
require 'test_helper'
require 'simpleidn'
require_relative '../../lib/whois_server'
require_relative '../../lib/whois_server_core'
require_relative '../../app/models/whois_record'
require_relative '../../app/validators/unicode_validator'

TEST_DOMAIN = 'test-domain.ee'
INTEGRATION_TEST_DOMAIN = 'integration-test.ee'

class WhoisServerIntegrationTest < Minitest::Test
include WhoisServer

def setup
super
@sent_data = []
@connection_closed = false
@connection_closed_after_writing = false
@logger_output = StringIO.new
@logger = Logger.new(@logger_output)
ENV['WHOIS_ENV'] = 'test'
end

def stub_connection_and_ip(ip = ['12345', '127.0.0.1'])
stubs(:connection).returns(nil)
stubs(:extract_ip).returns(ip)
end

def mock_whois_record(body: 'Whois body', id: 1)
mock_record = Minitest::Mock.new
mock_record.expect(:unix_body, body)
mock_record.expect(:id, id)
WhoisRecord.stubs(:find_by).returns(mock_record)
mock_record
end

def test_receive_data_rejects_invalid_encoding
invalid_payload = "\xFF\xFE".dup.force_encoding('ASCII-8BIT')
receive_data(invalid_payload)

assert_equal 1, @sent_data.length
assert_includes @sent_data.first.downcase, 'invalid encoding'
assert @connection_closed_after_writing
end

def test_receive_data_returns_valid_whois_body
stub_connection_and_ip
mock_record = mock_whois_record(body: 'This is a valid WHOIS record')

receive_data(TEST_DOMAIN)

assert_equal 1, @sent_data.length
assert_includes @sent_data.first, 'This is a valid WHOIS record'
assert @connection_closed_after_writing

mock_record.verify
end

def test_receive_data_domain_not_found
stub_connection_and_ip
WhoisRecord.stubs(:find_by).returns(nil)

receive_data(TEST_DOMAIN)

assert_equal 1, @sent_data.length
assert_includes @sent_data.first.downcase, 'domain not found'
assert @connection_closed_after_writing
end

def test_receive_data_with_no_ip
stubs(:extract_ip).returns(nil)
stubs(:connection).returns(nil)

receive_data(TEST_DOMAIN)

assert_equal 0, @sent_data.length
end

def send_data(data)
@sent_data << data
end

def close_connection_after_writing
@connection_closed_after_writing = true
end

def close_connection
@connection_closed = true
end

def get_peername
Socket.pack_sockaddr_in(12345, '127.0.0.1')
end

def logger
@logger
end
end
16 changes: 12 additions & 4 deletions test/logging_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ class LoggingTest < Minitest::Test

def setup
@output = StringIO.new
@logger = Logger.new(@output)
@logger.formatter = proc do |severity, datetime, progname, msg|
"[#{progname}] #{msg}\n"
@logger = nil
end

def logger
@logger ||= begin
original_logger = super
io_logger = Logger.new(@output, progname: 'whois')
io_logger.formatter = proc do |severity, datetime, progname, msg|
"[#{progname}] #{msg}\n"
end
@logger = io_logger
end
end

Expand Down Expand Up @@ -73,5 +81,5 @@ def test_log_record_not_found

assert_includes log_output, '"status":"not_found"'
assert_includes log_output, '"record_found":false'
end
end
end
26 changes: 26 additions & 0 deletions test/models/contact_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,30 @@ def test_attribute_concealed
contact = Contact.new(disclosed_attributes: %w[other])
refute contact.attribute_disclosed?(:name)
end

def test_attribute_disclosed_with_string_attribute
contact = Contact.new(disclosed_attributes: %w[name email])
assert contact.attribute_disclosed?('name')
assert contact.attribute_disclosed?(:email)
end

def test_attribute_disclosed_with_nil_disclosed_attributes
contact = Contact.new(disclosed_attributes: nil)
refute contact.attribute_disclosed?(:name)
end

def test_publishable_returns_true
contact = Contact.new(registrant_publishable: true)
assert contact.publishable?
end

def test_publishable_returns_false
contact = Contact.new(registrant_publishable: false)
refute contact.publishable?
end

def test_publishable_with_nil
contact = Contact.new(registrant_publishable: nil)
refute contact.publishable?
end
end
31 changes: 25 additions & 6 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
require 'simplecov'
SimpleCov.start 'rails'

require 'minitest/autorun'
require 'mocha/minitest'
require 'active_record'
require 'yaml'

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

require 'simplecov'
SimpleCov.start 'rails'
# --- Prevent EventMachine from starting in tests ---
# This monkey-patch must run before whois server files are required.
require 'eventmachine'
module EventMachine
class << self
def run(*)
yield if block_given?
end

def start_server(*)
# no-op in tests to avoid binding real ports
end

def set_effective_user(*)
# no-op in tests
end
end
end
# ---------------------------------------------------

class Minitest::Test
def dbconfig
return @dbconfig unless @dbconfig.nil?

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

Expand All @@ -26,7 +46,6 @@ def connection

def setup
super

connection
end
end
40 changes: 40 additions & 0 deletions test/unit/whois_server_core_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'test_helper'
require_relative '../../lib/whois_server'
require_relative '../../lib/whois_server_core'

class WhoisServerCoreTest < Minitest::Test
include WhoisServer

def test_allows_one_letter_domain
assert_match WhoisServerCore::DOMAIN_NAME_REGEXP, 'a.ee'
assert_match WhoisServerCore::DOMAIN_NAME_REGEXP, 'õ.ee'
assert_match WhoisServerCore::DOMAIN_NAME_REGEXP, '1.ee'
refute_match WhoisServerCore::DOMAIN_NAME_REGEXP, 'a..ee'
assert_match WhoisServerCore::DOMAIN_NAME_REGEXP, 'ab.ee'
end

def test_domain_valid_format_valid_domains
assert send(:domain_valid_format?, 'example.ee')
assert send(:domain_valid_format?, 'test-domain.ee')
assert send(:domain_valid_format?, 'õ.ee')
assert send(:domain_valid_format?, 'example.ee'.downcase)
end

def test_domain_valid_format_invalid_domains
refute send(:domain_valid_format?, 'invalid..ee')
refute send(:domain_valid_format?, '')
refute send(:domain_valid_format?, 'no-tld')
refute send(:domain_valid_format?, '.ee')
refute send(:domain_valid_format?, 'example.')
end

def test_domain_valid_format_with_whitespace
assert send(:domain_valid_format?, ' example.ee ')
assert send(:domain_valid_format?, "\texample.ee\n")
end

def test_domain_valid_format_with_mixed_case
assert send(:domain_valid_format?, 'EXAMPLE.EE')
assert send(:domain_valid_format?, 'ExAmPlE.Ee')
end
end
Loading