summaryrefslogtreecommitdiff
path: root/test/remote/nicknym_source_test.rb
blob: 7840e10eee2cf89f9a5bab73f97882aae3c297b4 (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
require 'test_helper'
require 'support/celluloid_test'
require 'nickserver/nicknym/source'
require 'nickserver/email_address'

#
# Please note the Readme.md file in this directory
#
class RemoteNicknymSourceTest < CelluloidTest

  def test_availablility_check
    source.available_for? 'mail.bitmask.net'
    refute source.available_for? 'dl.bitmask.net'   # not a provider
  rescue HTTP::ConnectionError => e
    skip e.to_s
  end

  def test_successful_query
    response = source.query(email_with_key)
    skip if response.status == 404
    assert_pgp_key_in response
  rescue HTTP::ConnectionError => e
    skip e.to_s
  end

  def test_not_found
    response = source.query(email_without_key)
    skip if response.status == 200
    assert_equal 404, response.status
  rescue HTTP::ConnectionError => e
    skip e.to_s
  end

  protected

  def assert_pgp_key_in(response)
    json = JSON.parse response.content
    assert_equal email_with_key.to_s, json["address"]
    refute_empty json["openpgp"]
  rescue JSON::ParserError
    skip "invalid json response: #{response.content}"
  end

  def source
    @source ||= Nickserver::Nicknym::Source.new
  end

  def email_with_key
    Nickserver::EmailAddress.new('test@mail.bitmask.net')
  end

  def email_without_key
    Nickserver::EmailAddress.new('pleaseneverusethisemailweuseittotest@mail.bitmask.net')
  end

end