Transport::Mgmt tests

pull/906/head
Rick Carlino 2018-07-06 08:42:13 -05:00
parent b2aaff8355
commit d4cd3c7cfb
3 changed files with 23 additions and 7 deletions

View File

@ -25,6 +25,6 @@ class TokenIssuance < ApplicationRecord
def maybe_evict_clients
Transport::Mgmt.try(:close_connections_for_username, "device_#{device_id}")
rescue Faraday::ConnectionFailed
nil
Rollbar.error("Failed to evict clients on token revocation")
end
end

View File

@ -65,10 +65,6 @@ class Transport
module Mgmt
require "rabbitmq/http/client"
def self.endpoint
@endpoint ||= "http://#{ENV.fetch("MQTT_HOST")}:15672"
end
def self.username
@username ||= URI(Transport.amqp_url).user || "admin"
end
@ -93,11 +89,16 @@ class Transport
password: self.password)
end
def self.connections
client.list_connections
end
def self.find_connection_by_name(name)
client
.list_connections
connections
.select { |x| x.fetch("user").include?(name) }
.pluck("name")
.compact
.uniq
end
def self.close_connections_for_username(name)

View File

@ -0,0 +1,15 @@
require "spec_helper"
describe Transport::Mgmt do
it "generates a URL" do
expect(Transport::Mgmt.api_url).to eq("http://blooper.io:15672")
end
it "finds connections by name" do
fake_connections = [{ "name" => "A", "user" => "1" },
{ "name" => "B", "user" => "2" },
{ "name" => "C", "user" => "3" }]
allow(Transport::Mgmt).to receive(:connections).and_return(fake_connections)
expect(Transport::Mgmt.find_connection_by_name("1")).to eq(["A"])
end
end