Skip to content

Commit

Permalink
Merge pull request #176 from rapid7/fix-discovery-connection-update
Browse files Browse the repository at this point in the history
Fix discovery connection update
  • Loading branch information
gschneider-r7 committed May 29, 2015
2 parents 9dab94b + 1d91e2a commit 8d9fb65
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
34 changes: 27 additions & 7 deletions lib/nexpose/discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,41 @@ def initialize(name = nil, address = nil, user = nil, password = nil)
@protocol = Protocol::HTTPS
end

# Save this discovery connection to a Nexpose console.
# Save this discovery connection on a given Nexpose console.
#
# @param [Connection] nsc Connection to a console.
#
def save(nsc)
if @id == -1
xml = nsc.make_xml('DiscoveryConnectionCreateRequest')
else
xml = nsc.make_xml('DiscoveryConnectionUpdateRequest')
end
def create(nsc)
xml = nsc.make_xml('DiscoveryConnectionCreateRequest')
xml.add_element(as_xml)

response = nsc.execute(xml, '1.2')
if response.success
ret = REXML::XPath.first(response.res, 'DiscoveryConnectionCreateResponse')
@id = ret.attributes['id'].to_i unless ret.nil?
end
end

# Update this (existing) discovery connection on a given Nexpose console.
#
# @param [Connection] nsc Connection to a console.
# @return [Boolean] whether the update request was successful
#
def update(nsc)
xml = nsc.make_xml('DiscoveryConnectionUpdateRequest')
xml.add_element(as_xml)

response = nsc.execute(xml, '1.2')
response.success
end

# Save this discovery connection to a Nexpose console.
#
# @param [Connection] nsc Connection to a console.
#
def save(nsc)
@id == -1 ? create(nsc) : update(nsc)

@id
end

Expand Down Expand Up @@ -170,6 +189,7 @@ def as_xml
xml.attributes['exchange-password'] = @exchange_password if @exchange_password
xml.attributes['type'] = @type if @type
xml.attributes['engine-id'] = @engine_id if @engine_id && @engine_id != -1
xml.attributes['id'] = @id if @id && @id != -1
xml
end

Expand Down
45 changes: 45 additions & 0 deletions spec/nexpose/discovery_connection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'spec_helper'

describe Nexpose::DiscoveryConnection do
describe '#save' do
context 'for a new discovery connection' do
subject do
discovery_connection = Nexpose::DiscoveryConnection.new("test-discovery-connection-#{Time.now.to_i}", 'example-host.local', 'johndoe', 'password123')
end

it 'sends a request to create the connection' do
xml = REXML::Element.new(%(<DiscoveryConnectionCreateResponse id="1"></DiscoveryConnectionCreateResponse>))
response = double(:response, res: xml, success: true)
nexpose_connection = Nexpose::Connection.new('example-host.local', 'johndoe', 'password123')
expect(nexpose_connection).to receive(:make_xml)
.with('DiscoveryConnectionCreateRequest')
.and_call_original
allow(nexpose_connection).to receive(:execute).and_return(response)

subject.save(nexpose_connection)
end
end

context 'for a discovery connection with an id' do
subject do
discovery_connection = Nexpose::DiscoveryConnection.new("test-discovery-connection-#{Time.now.to_i}", 'example-host.local', 'johndoe', 'password123')

discovery_connection.id = 1

discovery_connection
end

it 'sends a request to update the connection' do
xml = REXML::Element.new(%(<DiscoveryConnectionUpdateResponse id="1"></DiscoveryConnectionUpdateResponse>))
response = double(:response, res: xml, success: true)
nexpose_connection = Nexpose::Connection.new('example-host.local', 'johndoe', 'password123')
expect(nexpose_connection).to receive(:make_xml)
.with('DiscoveryConnectionUpdateRequest')
.and_call_original
allow(nexpose_connection).to receive(:execute).and_return(response)

subject.save(nexpose_connection)
end
end
end
end

0 comments on commit 8d9fb65

Please sign in to comment.