|
| 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 |
0 commit comments