vault-test-tools 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/vault-test-tools/spec_helpers.rb +21 -15
- data/lib/vault-test-tools/version.rb +1 -1
- data/test/spec_helpers_test.rb +25 -5
- metadata +4 -4
@@ -6,34 +6,40 @@ module Vault::Test
|
|
6
6
|
extend self
|
7
7
|
|
8
8
|
def usage_json
|
9
|
-
|
9
|
+
vault_spec('usage.json')
|
10
10
|
end
|
11
11
|
|
12
12
|
def statement_json
|
13
|
-
|
13
|
+
vault_spec('statement.json')
|
14
|
+
end
|
15
|
+
|
16
|
+
# memoizes return value so we don't keep making a request
|
17
|
+
def vault_spec(filename)
|
18
|
+
var_name = "@#{filename.gsub(".",'_')}".to_sym
|
19
|
+
spec_value = instance_variable_get(var_name)
|
20
|
+
return spec_value if spec_value
|
21
|
+
instance_variable_set(var_name, read_spec(filename))
|
14
22
|
end
|
15
23
|
|
16
24
|
protected
|
17
|
-
def
|
18
|
-
"http://vault-specs.herokuapp.com/#{name}
|
25
|
+
def url(name)
|
26
|
+
"http://vault-specs.herokuapp.com/#{name}"
|
19
27
|
end
|
20
28
|
|
21
|
-
def
|
22
|
-
"./test/support/#{name}
|
29
|
+
def file(name)
|
30
|
+
"./test/support/#{name}"
|
23
31
|
end
|
24
32
|
|
25
33
|
# Uses JSON at URL when it can, but will use
|
26
34
|
# the cached statement when it can't
|
27
35
|
def read_spec(name)
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
File.read(json_file(name))
|
36
|
-
end
|
36
|
+
data = Net::HTTP.get(URI.parse(url(name)))
|
37
|
+
FileUtils.mkdir_p(File.dirname(file(name)))
|
38
|
+
File.open(file(name), 'w') { |f| f << data }
|
39
|
+
data
|
40
|
+
rescue => e
|
41
|
+
$stderr.puts "Using cached #{name}"
|
42
|
+
File.read(file(name))
|
37
43
|
end
|
38
44
|
end
|
39
45
|
end
|
data/test/spec_helpers_test.rb
CHANGED
@@ -4,6 +4,7 @@ class SpecHelpersTest < Vault::TestCase
|
|
4
4
|
include Vault::Test::SpecHelpers
|
5
5
|
include RR::Adapters::MiniTest
|
6
6
|
STATEMENT_FILE = './test/support/statement.json'
|
7
|
+
USAGE_FILE = './test/support/usage.json'
|
7
8
|
|
8
9
|
def setup
|
9
10
|
super
|
@@ -18,15 +19,34 @@ class SpecHelpersTest < Vault::TestCase
|
|
18
19
|
FakeWeb.allow_net_connect = true
|
19
20
|
# Using the filesystem - dirrrty
|
20
21
|
File.unlink(STATEMENT_FILE) if File.exists?(STATEMENT_FILE)
|
22
|
+
File.unlink(USAGE_FILE) if File.exists?(USAGE_FILE)
|
21
23
|
reset #for RR
|
22
24
|
end
|
23
25
|
|
26
|
+
# These are for canonical endpoints
|
27
|
+
def test_named_methods
|
28
|
+
FakeWeb.register_uri(:get, "#{@url}/statement.json", body: "STATEMENT")
|
29
|
+
FakeWeb.register_uri(:get, "#{@url}/usage.json", body: "USAGE")
|
30
|
+
assert_equal("STATEMENT", statement_json)
|
31
|
+
assert_equal("USAGE", usage_json)
|
32
|
+
end
|
33
|
+
|
24
34
|
# Happy path is to use the request from the service
|
25
35
|
def test_requests_spec_from_service
|
26
36
|
url = "#{@url}/statement.json"
|
27
37
|
FakeWeb.register_uri(:get, url, body: "USAGE JSON")
|
28
38
|
stub(File).open(STATEMENT_FILE, 'w')
|
29
|
-
assert_equal("USAGE JSON",
|
39
|
+
assert_equal("USAGE JSON", vault_spec('statement.json'))
|
40
|
+
end
|
41
|
+
|
42
|
+
# The statement_json wrapper
|
43
|
+
def test_generic_spec_method
|
44
|
+
stub(File).open
|
45
|
+
url = "#{@url}/usage2v2.json"
|
46
|
+
FakeWeb.register_uri(:get, url, body: "USAGE JSON")
|
47
|
+
capture_io do
|
48
|
+
assert_equal("USAGE JSON", vault_spec('usage2v2.json'))
|
49
|
+
end
|
30
50
|
end
|
31
51
|
|
32
52
|
# Call both methods with stubbed HTTP endpoints
|
@@ -62,17 +82,17 @@ class SpecHelpersTest < Vault::TestCase
|
|
62
82
|
# now no network
|
63
83
|
stub(URI).parse { raise RuntimeError }
|
64
84
|
capture_io do
|
65
|
-
assert_equal("USAGE JSON",
|
85
|
+
assert_equal("USAGE JSON", vault_spec('statement.json'))
|
66
86
|
end
|
67
87
|
end
|
68
88
|
|
69
89
|
# This is the real error scenario
|
70
90
|
def test_bombs_if_no_cached_json
|
71
91
|
stub(URI).parse { raise RuntimeError }
|
72
|
-
stub(File).read { raise RuntimeError }
|
92
|
+
stub(File).read { raise RuntimeError, 'boom' }
|
73
93
|
capture_io do
|
74
|
-
assert_raises RuntimeError do
|
75
|
-
|
94
|
+
assert_raises RuntimeError, 'boom' do
|
95
|
+
vault_spec('usage.json')
|
76
96
|
end
|
77
97
|
end
|
78
98
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vault-test-tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-05-
|
13
|
+
date: 2013-05-10 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: turn
|
@@ -204,7 +204,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
204
204
|
version: '0'
|
205
205
|
segments:
|
206
206
|
- 0
|
207
|
-
hash:
|
207
|
+
hash: 1618270359711124284
|
208
208
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
209
209
|
none: false
|
210
210
|
requirements:
|
@@ -213,7 +213,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
213
213
|
version: '0'
|
214
214
|
segments:
|
215
215
|
- 0
|
216
|
-
hash:
|
216
|
+
hash: 1618270359711124284
|
217
217
|
requirements: []
|
218
218
|
rubyforge_project:
|
219
219
|
rubygems_version: 1.8.23
|