class Deis::Client::Mock

Attributes

password[R]
token[R]
url[R]
username[R]

Public Class Methods

data() click to toggle source
# File lib/deis/client/mock.rb, line 6
def self.data
  @data ||= Hash.new { |h, url|
    h[url] = {
      :users => {},
    }
  }
end
new(options={}) click to toggle source
# File lib/deis/client/mock.rb, line 32
def initialize(options={})
  @url                 = options[:url]
  @username, @password = options[:username], options[:password]
  @auth_token          = options[:token]
end
reset!() click to toggle source
# File lib/deis/client/mock.rb, line 14
def self.reset!
  @data = nil
end
serial_id() click to toggle source
# File lib/deis/client/mock.rb, line 18
def self.serial_id
  @current_id ||= 0
  @current_id += 1
  @current_id.to_s
end

Public Instance Methods

data() click to toggle source
# File lib/deis/client/mock.rb, line 24
def data
  self.class.data[@url]
end
register_user(params={}) click to toggle source
# File lib/deis/client/requests/register_user.rb, line 13
def register_user(params={})
  id = self.serial_id

  unless params["username"]
    response(
      :body   => {"username" => "This field is required."},
      :status => 400,
    )
  end

  unless params["password"]
    response(
      :body   => {"password" => "This field is required."},
      :status => 400,
    )
  end

  user_params = Cistern::Hash.slice(params, "username", "password", "email", "first_name", "last_name").merge(
    "id"               => id,
    "is_active"        => true,
    "is_staff"         => false,
    "groups"           => [],
    "user_permissions" => [],
    "is_superuser"     => false,
  )

  if self.data[:users].keys.empty?
    user_params.merge!("is_staff" => true, "is_superuser" => true)
  end

  if self.data[:users].values.find { |u| u["username"] == user_params["username"] }
    response(
      :body   => {"username" => "User with this Username already exists."},
      :status => 400,
    )
  end

  self.data[:users][id] = user_params

  response(
    :path   => "/auth/register",
    :method => :post,
    :body   => user_params,
    :status => 201,
  )
end
response(options={}) click to toggle source
# File lib/deis/client/mock.rb, line 38
def response(options={})
  method = options[:method] || :get
  status = options[:status] || 200
  path   = options[:path]
  body   = options[:body]
  url    = options[:url] || File.join(@url, "api/v1", path)

  request_headers  = {
    "Accept"       => "application/json",
    "Content-Type" => "application/json",
  }

  response_headers = {
    "Content-Type"            => "application/json",
    "x_deis_api_version"      => "1.1",
    "x_deis_platform_version" => "1.2.2",
  }

  # request phase
  # * :method - :get, :post, ...
  # * :url    - URI for the current request; also contains GET parameters
  # * :body   - POST parameters for :post/:put requests
  # * :request_headers

  # response phase
  # * :status - HTTP response status code, such as 200
  # * :body   - the response body
  # * :response_headers

  env = Faraday::Env.from(
    :method           => method,
    :url              => URI.parse(url),
    :body             => body,
    :request_headers  => request_headers,
    :response_headers => response_headers,
    :status           => status,
  )

  Faraday::Response::RaiseError.new.on_complete(env) ||
    Faraday::Response.new(env)
end
serial_id() click to toggle source
# File lib/deis/client/mock.rb, line 28
def serial_id
  self.class.serial_id
end