module RHC::Rest::Mock::Helpers

Public Instance Methods

credentials_for(with_auth) click to toggle source
# 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
define_exceptional_test_on_wizard() click to toggle source
# 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
empty_response_list(type) click to toggle source
# File lib/rhc/rest/mock.rb, line 228
def empty_response_list(type)
  {
    :body => {
      :type => type,
      :data => [],
    }.to_json
  }
end
expect_authorization(with_auth) click to toggle source
# File lib/rhc/rest/mock.rb, line 38
def expect_authorization(with_auth)
  username, password = credentials_for(with_auth)
  lambda{ |r|
    !username || (r.headers['Authorization'] == "Basic #{["#{username}:#{password}"].pack('m').tr("\n", '')}")
  }
end
mock_api_with_authorizations() click to toggle source
# File lib/rhc/rest/mock.rb, line 361
def mock_api_with_authorizations
  mock_real_client_links.concat([
    ['LIST_AUTHORIZATIONS', "broker/rest/user/authorizations", 'GET'],
    ['ADD_AUTHORIZATION',   "broker/rest/user/authorizations", 'POST'],
    ['SHOW_AUTHORIZATION',  "broker/rest/user/authorizations/:id", 'GET'],
  ])
end
mock_cartridge_response(cart_count=1) click to toggle source
# 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
mock_date_1() click to toggle source
# File lib/rhc/rest/mock.rb, line 18
def mock_date_1
  '2013-02-21T01:00:01Z'
end
mock_gear_groups_response() click to toggle source
# 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
mock_href(relative="", with_auth=false) click to toggle source

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
mock_pass() click to toggle source
# File lib/rhc/rest/mock.rb, line 298
def mock_pass
  "test pass"
end
mock_uri() click to toggle source
# File lib/rhc/rest/mock.rb, line 302
def mock_uri
  "test.domain.com"
end
mock_user() click to toggle source
# File lib/rhc/rest/mock.rb, line 22
def mock_user
  "test_user"
end
mock_user_auth() click to toggle source
# File lib/rhc/rest/mock.rb, line 26
def mock_user_auth
  respond_to?(:user_auth) ? self.user_auth : {:user => username, :password => password}
end
new_authorization(params) click to toggle source
# File lib/rhc/rest/mock.rb, line 281
def new_authorization(params)
  {
    :status => 201,
    :body => {
      :type => 'authorization',
      :data => {
        :note => params[:note],
        :token => 'a_token_value',
        :scopes => (params[:scope] || "userinfo").gsub(%r,/, ' '),
        :expires_in => (params[:expires_in] || 60).to_i,
        :expires_in_seconds => (params[:expires_in] || 60).to_i,
        :created_at => mock_date_1,
      },
    }.to_json
  }
end
new_domain(name) click to toggle source
# 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
no_domains() click to toggle source
# File lib/rhc/rest/mock.rb, line 224
def no_domains
  empty_response_list('domains')
end
no_keys() click to toggle source
# File lib/rhc/rest/mock.rb, line 221
def no_keys
  empty_response_list('keys')
end
simple_carts() click to toggle source
# 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
simple_user(login) click to toggle source
# 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
stub_add_authorization(params) click to toggle source
# File lib/rhc/rest/mock.rb, line 114
def stub_add_authorization(params)
  stub_api_request(:post, 'broker/rest/user/authorizations', mock_user_auth).
    with(:body => hash_including(params)).
    to_return(new_authorization(params))
end
stub_api(auth=false, authorizations=false) click to toggle source
# 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
stub_api_request(method, uri, with_auth=true) click to toggle source
# 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
stub_api_v12(auth=false) click to toggle source
# 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
stub_authorizations() click to toggle source
# File lib/rhc/rest/mock.rb, line 88
def stub_authorizations
  stub_api_request(:get, 'broker/rest/user/authorizations', mock_user_auth).
    to_return({
      :status => 200,
      :body => {
        :type => 'authorizations',
        :data => [
          {
            :note => 'an_authorization',
            :token => 'a_token_value',
            :created_at => mock_date_1,
            :expires_in_seconds => 60,
            :scopes => 'session read'
          }
        ]
      }.to_json
    })
end
stub_create_default_key() click to toggle source
# 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
stub_create_domain(name) click to toggle source
# 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
stub_delete_authorization(token) click to toggle source
# File lib/rhc/rest/mock.rb, line 110
def stub_delete_authorization(token)
  stub_api_request(:delete, "broker/rest/user/authorizations/#{token}", mock_user_auth).
    to_return(:status => 204)
end
stub_delete_authorizations() click to toggle source
# File lib/rhc/rest/mock.rb, line 106
def stub_delete_authorizations
  stub_api_request(:delete, 'broker/rest/user/authorizations', mock_user_auth).
    to_return(:status => 204)
end
stub_mock_ssh_keys(name='test') click to toggle source
# 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
stub_no_domains() click to toggle source
# 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
stub_no_keys() click to toggle source
# 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
stub_one_application(domain_name, name, *args) click to toggle source
# 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
stub_one_domain(name) click to toggle source
# 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
stub_one_key(name) click to toggle source
# 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
stub_relative_application(domain_name, app_name, body = {}, status = 200) click to toggle source
# 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
stub_simple_carts() click to toggle source
# 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
stub_update_key(name) click to toggle source
# 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
stub_user(auth=mock_user_auth) click to toggle source
# 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