-
Notifications
You must be signed in to change notification settings - Fork 99
/
api_expectations.rb
206 lines (174 loc) · 6.17 KB
/
api_expectations.rb
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
module HammerCLIForeman
module Testing
module APIExpectations
def self.api_calls
@api_calls ||= []
end
class APICallMatcher < Mocha::ParameterMatchers::Base
attr_accessor :expected_params, :expected_resource, :expected_action, :block
def initialize(resource=nil, action=nil, &block)
@expected_resource = resource
@expected_action = action
@block = block
@expected_params = {}
end
def matches?(actual_parameters)
action, params, _headers, _options = actual_parameters.shift(4)
action_name = action.name.to_s
resource_name = action.resource.to_s
result = true
result &&= (resource_name == @expected_resource.to_s) unless @expected_resource.nil?
result &&= (action_name == @expected_action.to_s) unless @expected_action.nil?
result &&= @block.call(params) if @block
result &&= assert_params(params)
result
end
def mocha_inspect
res = @expected_resource.nil? ? 'any_resource' : ":#{@expected_resource}"
act = @expected_action.nil? ? 'any_action' : ":#{@expected_action}"
blk = @block ? '&block' : '*any_argument'
"#{res}, #{act}, #{blk}"
end
protected
def assert_params(params)
stringify_keys(params) == deep_merge_hash(stringify_keys(params), stringify_keys(@expected_params))
end
def deep_merge_hash(h, other_h)
h = h.clone
h.merge!(other_h) do |key, old_val, new_val|
if old_val.is_a?(Hash) && new_val.is_a?(Hash)
deep_merge_hash(old_val, new_val)
else
new_val
end
end
end
def stringify_keys(hash)
hash.inject({}) do |stringified, (key, value)|
if value.is_a?(Hash)
value = stringify_keys(value)
end
stringified.update(key.to_s => value)
end
end
end
module ExpectationExtensions
def method_signature
signature = "#{@note}\n #{super}"
if @api_call_matcher && !@api_call_matcher.expected_params.empty?
signature += "\n expected params to include: " + params_signature(@api_call_matcher.expected_params)
end
if @api_call_matcher && !@api_call_matcher.block.nil?
signature += "\n expected params to match block at: " + block_signature(@api_call_matcher.block)
end
signature
end
def params_signature(hash)
JSON.pretty_generate(hash).split("\n").join("\n ")
end
def block_signature(block)
block.source_location.join(':')
end
def set_note(note)
@note = note
end
def with_params(expected_params = {}, &block)
api_call_matcher.expected_params = expected_params
api_call_matcher.block = block if block_given?
self.with(api_call_matcher)
self
end
def with_action(resource, action)
api_call_matcher.expected_resource = resource
api_call_matcher.expected_action = action
self.with(api_call_matcher)
self
end
def api_call_matcher
@api_call_matcher ||= APICallMatcher.new
end
end
class APIExpectationsDecorator < SimpleDelegator
def initialize(api_instance = ApipieBindings::API.any_instance)
@api_instance = api_instance
super
end
def expects_call(resource=nil, action=nil, note=nil, &block)
ex = @api_instance.expects(:call_action)
ex.extend(ExpectationExtensions)
ex.with_action(resource, action).with_params(&block)
ex.set_note(note)
ex
end
def expects_no_call
@api_instance.expects(:call_action).never
end
def expects_search(resource=nil, search_options={}, note=nil)
note ||= "Find #{resource}"
if search_options.is_a?(Hash)
search_query = search_options.map{|k, v| "#{k} = \"#{v}\"" }.join(" or ")
else
search_query = search_options
end
expects_call(resource, :index, note).with_params(:search => search_query)
end
end
class TestAuthenticator < ApipieBindings::Authenticators::BasicAuth
def user(ask=nil)
@user
end
def password(ask=nil)
@password
end
end
class FakeApiConnection < HammerCLI::Apipie::ApiConnection
attr_reader :authenticator
def initialize(params, options = {})
@authenticator = params[:authenticator]
super
end
end
def api_connection(options={}, version = FOREMAN_VERSION)
FakeApiConnection.new({
:uri => 'https://test.org',
:apidoc_cache_dir => "test/data/#{version}",
:apidoc_cache_name => 'foreman_api',
:authenticator => TestAuthenticator.new('admin', 'changeme'),
:dry_run => true
}.merge(options))
end
def api_calls
HammerCLIForeman::Testing::APIExpectations.api_calls
end
def api_expects(resource=nil, action=nil, note=nil, &block)
api_calls << [resource, action]
APIExpectationsDecorator.new.expects_call(resource, action, note, &block)
end
def api_expects_no_call
APIExpectationsDecorator.new.expects_no_call
end
def api_expects_search(resource=nil, search_options={}, note=nil)
APIExpectationsDecorator.new.expects_search(resource, search_options, note)
end
def index_response(items, options={})
cnt = if items.is_a?(Hash)
items.keys.count
else
items.length
end
{
"total" => options.fetch(:total, cnt),
"subtotal" => options.fetch(:subtotal, cnt),
"page" => options.fetch(:page, 1),
"per_page" => options.fetch(:per_page, cnt),
"search" => "",
"sort" => {
"by" => nil,
"order" => nil
},
"results" => items
}
end
end
end
end