summaryrefslogtreecommitdiff
path: root/test/unit/nicknym/source_test.rb
blob: b17f22bd9a1f96c90ad02b44de23f05e40620873 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'test_helper'
require 'http'
require 'json'
require 'nickserver/nicknym/source'
require 'nickserver/email_address'

class NicknymSourceTest < Minitest::Test

  def test_initialization
    assert source
  end

  def test_available_for_domain_with_service_mx
    assert available_on?(200, '{"services": ["mx"]}')
  end

  def test_no_provider_json_means_no_nicknym
    refute available_on?(404, 'blablabla')
  end

  def test_invalid_provider_json_means_no_nicknym
    refute available_on?(200, 'blablabla')
  end

  # adapter rescues name resolution errors and returns nothing
  def test_not_available_without_response
    refute available_on?
  end

  def test_proxy_successful_query
    assert proxies_query_response?(200, 'dummy body')
  end

  def test_proxy_query_not_found
    assert proxies_query_response?(404, 'dummy body')
  end

  protected

  def proxies_query_response?(status = 0, body = nil)
    adapter.expect :get, [status, body],
      ['https://nicknym.leap_powered.tld:6425', query: {address: email_stub.to_s}]
    response = source.query(email_stub)
    assert_equal status, response.status
    assert_equal body, response.content
    adapter.verify
  end

  def available_on?(*args)
    adapter.expect :get, args,
      ['https://remote.tld/provider.json', Hash]
    available = source.available_for?('remote.tld')
    adapter.verify
    return available
  end

  def source
    Nickserver::Nicknym::Source.new(adapter)
  end

  def adapter
    @adapter ||= Minitest::Mock.new
  end

  def email_stub
    @email_stub ||= Nickserver::EmailAddress.new 'test@leap_powered.tld'
  end
end