summaryrefslogtreecommitdiff
path: root/test/test_helper.rb
blob: afdd3f99ff5e0afd38b5d22a23b6ee034445bc25 (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
69
70
71
72
73
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')

require 'rubygems'
require 'kernel_ext'
require 'bundler/setup'
require 'minitest/autorun'
silence_warnings do
  require 'webmock/minitest'
end
require 'nickserver'
require 'minitest/pride'
require 'minitest/hell'

TESTING = true

class Minitest::Test
  # Add global extensions to the test case class here

  def setup
    Nickserver::Config.load

    # by default, mock all non-localhost network connections
    WebMock.disable_net_connect!(allow_localhost: true)
  end

  def file_content(filename)
    (@file_contents ||= {})[filename] ||= File.read(file_path(filename))
  end

  def file_path(filename)
    "%s/files/%s" % [File.dirname(__FILE__), filename]
  end

  def real_network
    unless ENV['ONLY_LOCAL'] == 'true'
      WebMock.allow_net_connect!
      yield
      WebMock.disable_net_connect!
    end
  end

  def stub_sks_vindex_reponse(uid, opts = {})
    options = {status: 200, body: ""}.merge(opts)
    stub_http_request(:get, Nickserver::Config.hkp_url).with(
      query: {op: 'vindex', search: uid, exact: 'on', options: 'mr', fingerprint: 'on'}
    ).to_return(options)
  end

  def stub_sks_get_reponse(key_id, opts = {})
    options = {status: 200, body: ""}.merge(opts)
    stub_http_request(:get, Nickserver::Config.hkp_url).with(
      query: {op: 'get', search: "0x"+key_id, exact: 'on', options: 'mr'}
    ).to_return(options)
  end

  def stub_couch_response(uid, opts = {})
    # can't stub localhost, so set couch_host to anything else
    Nickserver::Config.stub :couch_host, 'notlocalhost' do
      options = {status: 200, body: ""}.merge(opts)
      query = "\?key=#{"%22#{uid}%22"}&reduce=false"
      stub_http_request(:get, /#{Regexp.escape(Nickserver::Config.couch_url)}.*#{query}/).to_return(options)
      yield
    end
  end

  #
  # temporarily stubs the config property for the duration of the given block
  #
  def stub_config(property, value, &block)
    Nickserver::Config.stub(property, value, &block)
  end

end