-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_steps.rb
More file actions
157 lines (142 loc) · 4.91 KB
/
Copy pathrequest_steps.rb
File metadata and controls
157 lines (142 loc) · 4.91 KB
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
Given /^I dump the contents of the search index$/ do
rest.get_rest("/search/").each do |index_name, index_url|
puts "INDEX NAME: `#{index_name}'"
pp rest.get_rest(index_url.sub("http://127.0.0.1:4000", ''))
end
end
When /^I '([^']*)' (?:to )?the path '([^']*)'$/ do |http_method, request_uri|
begin
self.api_response = rest.send("#{http_method}_rest".downcase.to_sym, request_uri)
self.inflated_response = self.api_response
rescue
Chef::Log.debug("Caught exception in request: #{$!.message}")
self.exception = $!
end
end
When /^I '(.+)' the path '(.+)' using a wrong private key$/ do |http_method, request_uri|
key = OpenSSL::PKey::RSA.generate(2048)
File.open(File.join(tmpdir, 'false_key.pem'), "w") { |f| f.print key }
@rest = Chef::REST.new(Chef::Config[:chef_server_url], 'snakebite' , File.join(tmpdir, 'false_key.pem'))
When "I '#{http_method}' the path '#{request_uri}'"
end
When /^I (.+) the client$/ do |action|
raise ArgumentError, "You can only create or save clients" unless action =~ /^(create|save)$/
client = stash['client']
request_body = {:name => client.name, :admin => client.admin}
begin
self.inflated_response = @rest.post_rest("clients", request_body) if action == 'create'
self.inflated_response = @rest.put_rest("clients/#{client.name}", request_body) if action == 'save'
rescue
self.exception = $!
end
end
When /^I '(.+)' the '(.+)' to the path '(.+)'$/ do |http_method, stash_key, request_uri|
begin
self.api_response = rest.send("#{http_method.to_s.downcase}_rest".downcase.to_sym, request_uri, stash[stash_key])
self.inflated_response = self.api_response
rescue
self.exception = $!
end
end
When /^I '(.+)' the '(.+)' to the path '(.+)' using a wrong private key$/ do |http_method, stash_key, request_uri|
key = OpenSSL::PKey::RSA.generate(2048)
File.open(File.join(tmpdir, 'false_key.pem'), "w") { |f| f.print key }
@rest = Chef::REST.new(Chef::Config[:chef_server_url], 'snakebite' , File.join(tmpdir, 'false_key.pem'))
When "I '#{http_method}' the '#{stash_key}' to the path '#{request_uri}'"
end
When /^I delete local private key/ do
Chef::FileCache.delete("private_key.pem")
end
When /^I register '(.+)'$/ do |user|
begin
rest = Chef::REST.new(Chef::Config[:registration_url])
rest.register("bobo")
rescue
self.exception = $!
end
end
When /^I authenticate as '(.+)'$/ do |reg|
begin
rest.authenticate(reg, 'tclown')
rescue
self.exception = $!
end
end
When "I edit the '$not_admin' client" do |client|
stash['client'] = @rest.get_rest("/clients/not_admin")
end
When "I set '$property' to true" do |property|
stash['client'].send(property.to_sym, true)
end
def call_as_admin(&block)
orig_rest = @rest
orig_node_name = Chef::Config[:node_name]
orig_client_key = Chef::Config[:client_key]
begin
@rest = admin_rest
Chef::Config[:node_name] = @rest.auth_credentials.client_name
Chef::Config[:client_key] = @rest.auth_credentials.key_file
yield
ensure
@rest = orig_rest
Chef::Config[:node_name] = orig_node_name
Chef::Config[:client_key] = orig_client_key
end
end
#When /^I dump the contents of the search index$/ do
# Given "I dump the contents of the search index"
#end
#
# When /^I '(.+)' the path '(.+)'$/ do |http_method, request_uri|
# begin
# #if http_method.downcase == 'get'
# # self.api_response = @rest.get_rest(request_uri)
# #else
# #puts "test test test \n\n\n\n\n\n\n"
# @response = @rest.send("#{http_method}_rest".downcase.to_sym, request_uri)
# #end
# puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
# puts @response
# puts @response['content-type']
# #puts self.api_response
# #puts self.api_response.inspect
# #self.inflated_response = self.api_response
# @inflated_response = @response#JSON.parse(response.body.to_s)
# puts "~~~~~~~~INFLATED RESPONSE~~~~~~~~~~~~"
# puts @inflated_response
# rescue
# self.exception = $!
# end
# end
#
# When /^I '(.+)' the '(.+)' to the path '(.+)'$/ do |http_method, stash_key, request_uri|
# begin
# #if http_method.downcase == 'post'
# # puts "post request"
# # self.api_response = @rest.post_rest(request_uri, @stash[stash_key])
# # puts self.api_response
# #else
# puts "This is the request -- @stash[stash_key]:"
# puts @stash[stash_key].to_s
# @response = @rest.send("#{http_method}_rest".downcase.to_sym, request_uri, @stash[stash_key])
# #end
# puts "This is the response:"
# #puts self.api_response.body.to_s
# puts @response
# #self.inflated_response = response
# @inflated_response = @response#JSON.parse(self.api_response.body.to_s)
# puts "~~~~~~~~INFLATED RESPONSE~~~~~~~~~~~~"
# puts @inflated_response
# rescue
# self.exception = $!
# end
# end
#
# When /^I authenticate as '(.+)'$/ do |reg|
# begin
# rest.authenticate(reg, 'tclown')
# rescue
# self.exception = $!
# end
# end
#