Class: Fog::RiakCS::Provisioning::Mock

Inherits:
Object
  • Object
show all
Includes:
UserUtils, Utils
Defined in:
lib/fog/riakcs/provisioning.rb,
lib/fog/riakcs/requests/provisioning/get_user.rb,
lib/fog/riakcs/requests/provisioning/list_users.rb,
lib/fog/riakcs/requests/provisioning/create_user.rb,
lib/fog/riakcs/requests/provisioning/enable_user.rb,
lib/fog/riakcs/requests/provisioning/update_user.rb,
lib/fog/riakcs/requests/provisioning/disable_user.rb,
lib/fog/riakcs/requests/provisioning/regrant_secret.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UserUtils

#update_mock_user, #update_riakcs_user

Methods included from Utils

#configure_uri_options, #riakcs_uri

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



33
34
35
# File 'lib/fog/riakcs/provisioning.rb', line 33

def initialize(options = {})
  configure_uri_options(options)
end

Class Method Details

.dataObject



25
26
27
# File 'lib/fog/riakcs/provisioning.rb', line 25

def self.data
  @data ||= Hash.new({})
end

.resetObject



29
30
31
# File 'lib/fog/riakcs/provisioning.rb', line 29

def self.reset
  @data = nil
end

Instance Method Details

#create_user(email, name, options = {}) ⇒ Object



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
# File 'lib/fog/riakcs/requests/provisioning/create_user.rb', line 47

def create_user(email, name, options = {})
  if invalid_email?(email)
    raise Fog::RiakCS::Provisioning::ServiceUnavailable, "The email address you provided is not a valid."
  end

  if user_exists?(email)
    raise Fog::RiakCS::Provisioning::UserAlreadyExists, "User with email #{email} already exists."
  end

  key_id       = rand(1000).to_s
  key_secret   = rand(1000).to_s
  data[key_id] = { :email => email, :name => name, :status => 'enabled', :key_secret => key_secret }

  Excon::Response.new.tap do |response|
    response.status = 200
    response.headers['Content-Type'] = 'application/json'
    response.body = {
      "email"        => data[:email],
      "display_name" => data[:name],
      "name"         => "user123",
      "key_id"       => key_id,
      "key_secret"   => key_secret,
      "id"           => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
      "status"       => "enabled"
    }
  end
end

#dataObject



37
38
39
# File 'lib/fog/riakcs/provisioning.rb', line 37

def data
  self.class.data[riakcs_uri]
end

#disable_user(key_id) ⇒ Object



17
18
19
# File 'lib/fog/riakcs/requests/provisioning/disable_user.rb', line 17

def disable_user(key_id)
  update_mock_user(key_id, { :status => 'disabled' })
end

#enable_user(key_id) ⇒ Object



17
18
19
# File 'lib/fog/riakcs/requests/provisioning/enable_user.rb', line 17

def enable_user(key_id)
  update_mock_user(key_id, { :status => 'enabled' })
end

#get_user(key_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/riakcs/requests/provisioning/get_user.rb', line 16

def get_user(key_id)
  if user = data[key_id]
    Excon::Response.new.tap do |response|
      response.status = 200
      response.headers['Content-Type'] = 'application/json'
      response.body = {
        "email"        => user[:email],
        "display_name" => user[:name],
        "name"         => "user123",
        "key_id"       => "XXXXXXXXXXXXXXXXXXXX",
        "key_secret"   => user[:key_secret],
        "id"           => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        "status"       => user[:status]
      }
    end
  else
    Excon::Response.new.tap do |response|
      response.status = 404
      response.headers['Content-Type'] = 'application/json'
    end
  end
end

#invalid_email?(email) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/fog/riakcs/requests/provisioning/create_user.rb', line 37

def invalid_email?(email)
  !email.include?('@')
end

#list_users(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/riakcs/requests/provisioning/list_users.rb', line 22

def list_users(options = {})
  filtered_data = options[:status] ? data.select { |key, value| value[:status] == options[:status] } : data

  Excon::Response.new.tap do |response|
    response.status = 200
    response.body   = filtered_data.map do |key, value|
      {
        "email"        => value[:email],
        "display_name" => value[:name],
        "name"         => "user123",
        "key_id"       => key,
        "key_secret"   => value[:key_secret],
        "id"           => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        "status"       => value[:status]
      }
    end.compact
  end
end

#regrant_secret(key_id) ⇒ Object



17
18
19
# File 'lib/fog/riakcs/requests/provisioning/regrant_secret.rb', line 17

def regrant_secret(key_id)
  update_mock_user(key_id, { :new_key_secret => true })
end

#reset_dataObject



41
42
43
# File 'lib/fog/riakcs/provisioning.rb', line 41

def reset_data
  self.class.data.delete(riakcs_uri)
end

#update_user(key_id, user) ⇒ Object



17
18
19
# File 'lib/fog/riakcs/requests/provisioning/update_user.rb', line 17

def update_user(key_id, user)
  update_mock_user(key_id, user)
end

#user_exists?(email) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/fog/riakcs/requests/provisioning/create_user.rb', line 41

def user_exists?(email)
  data.detect do |key, value|
    value[:email] == email
  end
end