# File lib/rhc/rest/mock.rb, line 30 def credentials_for(with_auth) if with_auth == true [respond_to?(:username) ? self.username : mock_user, respond_to?(:password) ? self.password : mock_pass] elsif with_auth with_auth.values_at(:user, :password) end end
# File lib/rhc/rest/mock.rb, line 212 def define_exceptional_test_on_wizard RHC::Wizard.module_eval " private def test_and_raise raise end " end
# File lib/rhc/rest/mock.rb, line 228 def empty_response_list(type) { :body => { :type => type, :data => [], }.to_json } end
# File lib/rhc/rest/mock.rb, line 386 def mock_alias_links(domain_id='test_domain',app_id='test_app',alias_id='test.foo.com') [['DELETE', "domains/#{domain_id}/apps/#{app_id}/aliases/#{alias_id}/delete", 'post'], ['GET', "domains/#{domain_id}/apps/#{app_id}/aliases/#{alias_id}", 'get' ], ['UPDATE', "domains/#{domain_id}/apps/#{app_id}/aliases/#{alias_id}/update", 'post' ]] end
# File lib/rhc/rest/mock.rb, line 327 def mock_app_links(domain_id='test_domain',app_id='test_app') [['ADD_CARTRIDGE', "domains/#{domain_id}/apps/#{app_id}/carts/add", 'post'], ['LIST_CARTRIDGES', "domains/#{domain_id}/apps/#{app_id}/carts/", 'get' ], ['GET_GEAR_GROUPS', "domains/#{domain_id}/apps/#{app_id}/gear_groups", 'get' ], ['START', "domains/#{domain_id}/apps/#{app_id}/start", 'post'], ['STOP', "domains/#{domain_id}/apps/#{app_id}/stop", 'post'], ['RESTART', "domains/#{domain_id}/apps/#{app_id}/restart", 'post'], ['THREAD_DUMP', "domains/#{domain_id}/apps/#{app_id}/event", 'post'], ['ADD_ALIAS', "domains/#{domain_id}/apps/#{app_id}/event", 'post'], ['REMOVE_ALIAS', "domains/#{domain_id}/apps/#{app_id}/event", 'post'], ['LIST_ALIASES', "domains/#{domain_id}/apps/#{app_id}/aliases", 'get'], ['DELETE', "domains/#{domain_id}/apps/#{app_id}/delete", 'post']] end
# File lib/rhc/rest/mock.rb, line 341 def mock_cart_links(domain_id='test_domain',app_id='test_app',cart_id='test_cart') [['START', "domains/#{domain_id}/apps/#{app_id}/carts/#{cart_id}/start", 'post'], ['STOP', "domains/#{domain_id}/apps/#{app_id}/carts/#{cart_id}/stop", 'post'], ['RESTART', "domains/#{domain_id}/apps/#{app_id}/carts/#{cart_id}/restart", 'post'], ['DELETE', "domains/#{domain_id}/apps/#{app_id}/carts/#{cart_id}/delete", 'post']] end
# File lib/rhc/rest/mock.rb, line 392 def mock_cartridge_response(cart_count=1) carts = [] while carts.length < cart_count carts << { :name => "mock_cart_#{carts.length}", :type => "mock_cart_#{carts.length}_type", :links => mock_response_links(mock_cart_links('mock_domain','mock_app',"mock_cart_#{carts.length}")) } end carts = carts[0] if cart_count == 1 type = cart_count == 1 ? 'cartridge' : 'cartridges' return { :body => { :type => type, :data => carts }.to_json, :status => 200 } end
# File lib/rhc/rest/mock.rb, line 348 def mock_client_links [['GET_USER', 'user/', 'get' ], ['ADD_DOMAIN', 'domains/add', 'post'], ['LIST_DOMAINS', 'domains/', 'get' ], ['LIST_CARTRIDGES', 'cartridges/', 'get' ]] end
# File lib/rhc/rest/mock.rb, line 18 def mock_date_1 '2013-02-21T01:00:01Z' end
# File lib/rhc/rest/mock.rb, line 369 def mock_domain_links(domain_id='test_domain') [['ADD_APPLICATION', "domains/#{domain_id}/apps/add", 'post'], ['LIST_APPLICATIONS', "domains/#{domain_id}/apps/", 'get' ], ['UPDATE', "domains/#{domain_id}/update", 'post'], ['DELETE', "domains/#{domain_id}/delete", 'post']] end
# File lib/rhc/rest/mock.rb, line 414 def mock_gear_groups_response() groups = [{}] type = 'gear_groups' return { :body => { :type => type, :data => groups }.to_json, :status => 200 } end
Creates consistent hrefs for testing
# File lib/rhc/rest/mock.rb, line 307 def mock_href(relative="", with_auth=false) server = respond_to?(:server) ? self.server : mock_uri uri_string = server "https://#{uri_string}/#{relative}" end
# File lib/rhc/rest/mock.rb, line 376 def mock_key_links(key_id='test_key') [['UPDATE', "user/keys/#{key_id}/update", 'post'], ['DELETE', "user/keys/#{key_id}/delete", 'post']] end
# File lib/rhc/rest/mock.rb, line 298 def mock_pass "test pass" end
# File lib/rhc/rest/mock.rb, line 354 def mock_real_client_links [['GET_USER', "broker/rest/user", 'GET'], ['LIST_DOMAINS', "broker/rest/domains", 'GET'], ['ADD_DOMAIN', "broker/rest/domains", 'POST'], ['LIST_CARTRIDGES', "broker/rest/cartridges", 'GET'], ] end
This formats link lists for JSONification
# File lib/rhc/rest/mock.rb, line 314 def mock_response_links(links) link_set = {} links.each do |link| operation = link[0] href = link[1] method = link[2] # Note that the 'relative' key/value pair below is a convenience for testing; # this is not used by the API classes. link_set[operation] = { 'href' => mock_href(href), 'method' => method, 'relative' => href } end link_set end
# File lib/rhc/rest/mock.rb, line 302 def mock_uri "test.domain.com" end
# File lib/rhc/rest/mock.rb, line 22 def mock_user "test_user" end
# File lib/rhc/rest/mock.rb, line 26 def mock_user_auth respond_to?(:user_auth) ? self.user_auth : {:user => username, :password => password} end
# File lib/rhc/rest/mock.rb, line 381 def mock_user_links [['ADD_KEY', 'user/keys/add', 'post'], ['LIST_KEYS', 'user/keys/', 'get' ]] end
# File lib/rhc/rest/mock.rb, line 237 def new_domain(name) { :status => 201, :body => { :type => 'domain', :data => { :id => name, :links => mock_response_links([ ]) }, }.to_json } end
# File lib/rhc/rest/mock.rb, line 224 def no_domains empty_response_list('domains') end
# File lib/rhc/rest/mock.rb, line 221 def no_keys empty_response_list('keys') end
# File lib/rhc/rest/mock.rb, line 250 def simple_carts { :body => { :type => 'cartridges', :data => [ {:name => 'mock_standalone_cart-1', :type => 'standalone', :tags => ['cartridge'], :display_name => 'Mock1 Cart'}, {:name => 'mock_standalone_cart-2', :type => 'standalone', :description => 'Mock2 description'}, {:name => 'mock_embedded_cart-1', :type => 'embedded', :tags => ['scheduled'], :display_name => 'Mock1 Embedded Cart'}, {:name => 'premium_cart-1', :type => 'standalone', :tags => ['premium'], :display_name => 'Premium Cart', :usage_rate_usd => '0.02'}, ], }.to_json } end
# File lib/rhc/rest/mock.rb, line 263 def simple_user(login) { :body => { :type => 'user', :data => { :login => login, :plan_id => respond_to?(:user_plan_id) ? self.user_plan_id : nil, :consumed_gears => respond_to?(:user_consumed_gears) ? self.user_consumed_gears : 0, :max_gears => respond_to?(:user_max_gears) ? self.user_max_gears : 3, :capabilities => respond_to?(:user_capabilities) ? self.user_capabilities : {:gear_sizes => ['small', 'medium']}, :links => mock_response_links([ ['ADD_KEY', "broker/rest/user/keys", 'POST'], ['LIST_KEYS', "broker/rest/user/keys", 'GET'], ]) }, }.to_json } end
# File lib/rhc/rest/mock.rb, line 52 def stub_api(auth=false, authorizations=false) stub_api_request(:get, 'broker/rest/api', auth). to_return({ :body => { :data => mock_response_links(authorizations ? mock_api_with_authorizations : mock_real_client_links), :supported_api_versions => [1.0, 1.1, 1.2, 1.3, 1.4], }.to_json }) end
# File lib/rhc/rest/mock.rb, line 45 def stub_api_request(method, uri, with_auth=true) api = stub_request(method, mock_href(uri, with_auth)) api.with(&lambda{ |r| request.headers['Authorization'] == "Bearer #{with_auth[:token]}" }) if with_auth.respond_to?(:[]) && with_auth[:token] api.with(&expect_authorization(with_auth)) api.with(&user_agent_header) end
# File lib/rhc/rest/mock.rb, line 61 def stub_api_v12(auth=false) stub_api_request(:get, 'broker/rest/api', auth). to_return({ :body => { :data => mock_response_links(mock_real_client_links), :supported_api_versions => [1.0, 1.1, 1.2], }.to_json }) end
# File lib/rhc/rest/mock.rb, line 73 def stub_create_default_key stub_api_request(:post, 'broker/rest/user/keys', mock_user_auth). with(:body => hash_including({:name => 'default', :type => 'ssh-rsa'})). to_return({:status => 201, :body => {}.to_json}) end
# File lib/rhc/rest/mock.rb, line 83 def stub_create_domain(name) stub_api_request(:post, 'broker/rest/domains', mock_user_auth). with(:body => hash_including({:id => name})). to_return(new_domain(name)) end
# File lib/rhc/rest/mock.rb, line 122 def stub_mock_ssh_keys(name='test') stub_api_request(:get, 'broker/rest/user/keys', mock_user_auth). to_return({ :body => { :type => 'keys', :data => [ { :name => name, :type => pub_key.split[0], :content => pub_key.split[1], # :links => mock_response_links([ # ['UPDATE', "broker/rest/user/keys/#{name}", 'put'] # ]), } ], }.to_json }) end
# File lib/rhc/rest/mock.rb, line 158 def stub_no_domains stub_api_request(:get, 'broker/rest/domains', mock_user_auth).to_return(no_domains) end
# File lib/rhc/rest/mock.rb, line 119 def stub_no_keys stub_api_request(:get, 'broker/rest/user/keys', mock_user_auth).to_return(no_keys) end
# File lib/rhc/rest/mock.rb, line 173 def stub_one_application(domain_name, name, *args) stub_api_request(:get, "broker/rest/domains/#{domain_name}/applications", mock_user_auth). to_return({ :body => { :type => 'applications', :data => [{ :domain_id => domain_name, :id => 1, :name => name, :app_url => "http://#{name}-#{domain_name}.rhcloud.com", :links => mock_response_links([ ]), }], }.to_json }) stub_relative_application(domain_name,name, *args) end
# File lib/rhc/rest/mock.rb, line 161 def stub_one_domain(name) stub_api_request(:get, 'broker/rest/domains', mock_user_auth). to_return({ :body => { :type => 'domains', :data => [{:id => name, :links => mock_response_links([ ['LIST_APPLICATIONS', "broker/rest/domains/#{name}/applications", 'get'], ['ADD_APPLICATION', "broker/rest/domains/#{name}/applications", 'post'], ])}], }.to_json }) end
# File lib/rhc/rest/mock.rb, line 140 def stub_one_key(name) stub_api_request(:get, 'broker/rest/user/keys', mock_user_auth). to_return({ :body => { :type => 'keys', :data => [ { :name => name, :type => 'ssh-rsa', :content => rsa_key_content_public, :links => mock_response_links([ ['UPDATE', "broker/rest/user/keys/#{name}", 'put'] ]), } ], }.to_json }) end
# File lib/rhc/rest/mock.rb, line 191 def stub_relative_application(domain_name, app_name, body = {}, status = 200) url = client_links['LIST_DOMAINS']['relative'] rescue "broker/rest/domains" stub_api_request(:any, "#{url}/#{domain_name}/applications/#{app_name}"). to_return({ :body => { :type => 'application', :data => { :domain_id => domain_name, :name => app_name, :id => 1, :links => mock_response_links(mock_app_links(domain_name,app_name)), } }.merge(body).to_json, :status => status }) end
# File lib/rhc/rest/mock.rb, line 208 def stub_simple_carts stub_api_request(:get, 'broker/rest/cartridges', mock_user_auth).to_return(simple_carts) end
# File lib/rhc/rest/mock.rb, line 78 def stub_update_key(name) stub_api_request(:put, "broker/rest/user/keys/#{name}", mock_user_auth). with(:body => hash_including({:type => 'ssh-rsa'})). to_return({:status => 200, :body => {}.to_json}) end
# File lib/rhc/rest/mock.rb, line 70 def stub_user(auth=mock_user_auth) stub_api_request(:get, 'broker/rest/user', auth).to_return(simple_user(username)) end