Skip to content

Commit

Permalink
All mock tests working.
Browse files Browse the repository at this point in the history
Refining tests to catch edge cases and problems with Real before burning API limits
  • Loading branch information
dldinternet committed Mar 31, 2017
1 parent 0378306 commit d02a524
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 43 deletions.
58 changes: 29 additions & 29 deletions lib/fog/digitalocean/dns.rb
Expand Up @@ -169,35 +169,35 @@ def reset_data
end
end

class Real
def initialize(options={})
digitalocean_token = options[:digitalocean_token]
persistent = false
options = {
headers: {
'Authorization' => "Bearer #{digitalocean_token}",
}
}
@connection = Fog::Core::Connection.new 'https://api.digitalocean.com', persistent, options
end

def request(params)
params[:headers] ||= {}
begin
response = @connection.request(params)
rescue Excon::Errors::HTTPStatusError => error
raise case error
when Excon::Errors::NotFound
NotFound.slurp(error)
else
error
end
end
unless response.body.empty?
response.body = Fog::JSON.decode(response.body)
end
response
end
class Real < Fog::Compute::DigitalOcean::Real
# def initialize(options={})
# digitalocean_token = options[:digitalocean_token]
# persistent = false
# options = {
# headers: {
# 'Authorization' => "Bearer #{digitalocean_token}",
# }
# }
# @connection = Fog::Core::Connection.new 'https://api.digitalocean.com', persistent, options
# end
#
# def request(params)
# params[:headers] ||= {}
# begin
# response = @connection.request(params)
# rescue Excon::Errors::HTTPStatusError => error
# raise case error
# when Excon::Errors::NotFound
# NotFound.slurp(error)
# else
# error
# end
# end
# unless response.body.empty?
# response.body = Fog::JSON.decode(response.body)
# end
# response
# end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fog/digitalocean/models/dns/domains.rb
Expand Up @@ -29,7 +29,7 @@ def all(filters = {})
# @raise [Fog::DNS::DigitalOceanV2::ServiceError]
# @see https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-key
def get(id)
key = service.get_domain(id).body['name']
key = service.get_domain(id).body['domain']
new(key) if key
rescue Fog::Errors::NotFound
nil
Expand Down
1 change: 1 addition & 0 deletions lib/fog/digitalocean/requests/dns/create_record.rb
Expand Up @@ -33,6 +33,7 @@ def create_record(name, rec={})
response.status = 200

data[:domain_records][name] << rec.dup
data[:domain_records][name].last['name'] = %(#{data[:domain_records][name].last['name']}.#{name}.) unless data[:domain_records][name].last['name'].match(%r{\.$})
data[:domain_records][name].last['id'] = Fog::Mock.random_numbers(8).to_i
response.body = {
"domain_record" => data[:domain_records][name].last
Expand Down
4 changes: 2 additions & 2 deletions lib/fog/digitalocean/requests/dns/delete_domain.rb
Expand Up @@ -6,7 +6,7 @@ class Real
def delete_domain(id)
id = id.with_indifferent_access['domain'] if id.is_a?(Hash)
request(
:expects => [200],
:expects => [204],
:headers => {
'Content-Type' => "application/json; charset=UTF-8",
},
Expand All @@ -26,7 +26,7 @@ def delete_domain(id)
end

response = Excon::Response.new
response.status = 200
response.status = 204
response
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/fog/digitalocean/requests/dns/delete_record.rb
Expand Up @@ -7,7 +7,7 @@ class Real
def delete_record(name, id)
id = id.with_indifferent_access['id'] if id.is_a?(Hash)
request(
:expects => [200],
:expects => [204],
:method => 'DELETE',
:path => "/v2/domains/#{name}/records/#{id}",
)
Expand All @@ -19,7 +19,7 @@ class Mock
def delete_record(name, id)
id = id.with_indifferent_access['id'] if id.is_a?(Hash)
response = Excon::Response.new
response.status = 200
response.status = 204

data[:domain_records][name].select!{ |rec| rec['id'] != id }

Expand Down
2 changes: 1 addition & 1 deletion tests/digitalocean/models/dns/record_tests.rb
Expand Up @@ -21,7 +21,7 @@

end

tests("domains#destroy").succeeds do
tests("domain.destroy").succeeds do
@domain.destroy
end

Expand Down
2 changes: 1 addition & 1 deletion tests/digitalocean/models/dns/records_tests.rb
@@ -1,7 +1,7 @@
Shindo.tests("Fog::DNS[:digitalocean] | records", ['digitalocean', 'dns']) do

@domain = nil
tests("zones#create").succeeds do
tests("domains.create").succeeds do
@domain = Fog::DNS[:digitalocean].domains.create(name: generate_unique_domain)
end

Expand Down
8 changes: 6 additions & 2 deletions tests/digitalocean/requests/dns/dns_tests.rb
Expand Up @@ -131,8 +131,12 @@

if response.status == 200
dr = response.body['domain_record'].with_indifferent_access
@new_records << dr
@service.get_record(@zone_id, dr["id"]).body['domain_record']["name"].eql?(rr[:name])
if dr['name'].eql?(rr['name'])
@new_records << dr
@service.get_record(@zone_id, dr["id"]).body['domain_record']["name"].eql?(rr[:name])
else
false
end
else
false
end
Expand Down
21 changes: 16 additions & 5 deletions tests/helpers/collection_helper.rb
@@ -1,28 +1,39 @@
def collection_tests(collection, params = {}, mocks_implemented = true)
tests('success') do

# tests('valid name in params').returns(true) do
# !params[:name].eql?(collection.domain.name)
# end
#
# params[:fqdn] = %(#{params[:name]}.#{collection.domain.name}.) unless params[:name].match(%r{\.$})

tests("#new(#{params.inspect})").succeeds do
pending if Fog.mocking? && !mocks_implemented
collection.new(params)
coll = collection.new(params)
@collection_size = coll.collection.size
true
end

tests("#create(#{params.inspect})").succeeds do
pending if Fog.mocking? && !mocks_implemented
@instance = collection.create(params)
@instance.name.eql?(params[:name])
end

tests("#all").succeeds do
tests("#all").returns(@collection_size+1) do
pending if Fog.mocking? && !mocks_implemented
collection.all
coll = collection.all
coll.size
end

if !Fog.mocking? || mocks_implemented
@identity = @instance.identity
end

tests("#get(#{@identity})").succeeds do
tests("#get(#{@identity})").returns(params[:name]) do
pending if Fog.mocking? && !mocks_implemented
collection.get(@identity)
record = collection.get(@identity)
record.name
end

tests('Enumerable') do
Expand Down

0 comments on commit d02a524

Please sign in to comment.