From bdba0a78c49295ed1a7ab7e2a585002034bf5fa9 Mon Sep 17 00:00:00 2001
From: Sam Saffron <sam.saffron@gmail.com>
Date: Tue, 12 Feb 2013 18:01:38 +1100
Subject: [PATCH] scaffolding for message bus diags

---
 vendor/gems/message_bus/lib/message_bus.rb    | 18 +++++++---
 .../lib/message_bus/rack/diagnostics.rb       | 35 +++++++++++++++++++
 .../lib/message_bus/rack/middleware.rb        |  5 +++
 .../message_bus/spec/lib/middleware_spec.rb   | 15 ++++++--
 4 files changed, 67 insertions(+), 6 deletions(-)
 create mode 100644 vendor/gems/message_bus/lib/message_bus/rack/diagnostics.rb

diff --git a/vendor/gems/message_bus/lib/message_bus.rb b/vendor/gems/message_bus/lib/message_bus.rb
index 7b3fefc09df..514e6f2001b 100644
--- a/vendor/gems/message_bus/lib/message_bus.rb
+++ b/vendor/gems/message_bus/lib/message_bus.rb
@@ -10,6 +10,7 @@ require "message_bus/client"
 require "message_bus/connection_manager"
 require "message_bus/message_handler"
 require "message_bus/rack/middleware"
+require "message_bus/rack/diagnostics"
 
 # we still need to take care of the logger
 if defined?(::Rails)
@@ -71,19 +72,28 @@ module MessageBus::Implementation
   end
 
   def site_id_lookup(&blk)
-    @site_id_lookup ||= blk
+    @site_id_lookup = blk if blk
+    @site_id_lookup
   end
 
   def user_id_lookup(&blk)
-    @user_id_lookup ||= blk
+    @user_id_lookup = blk if blk
+    @user_id_lookup
+  end
+
+  def is_admin_lookup(&blk)
+    @is_admin_lookup = blk if blk
+    @is_admin_lookup
   end
 
   def on_connect(&blk)
-    @on_connect ||= blk
+    @on_connect = blk if blk
+    @on_connect
   end
 
   def on_disconnect(&blk)
-    @on_disconnect ||= blk
+    @on_disconnect = blk if blk
+    @on_disconnect
   end
 
   def allow_broadcast=(val)
diff --git a/vendor/gems/message_bus/lib/message_bus/rack/diagnostics.rb b/vendor/gems/message_bus/lib/message_bus/rack/diagnostics.rb
new file mode 100644
index 00000000000..09426227884
--- /dev/null
+++ b/vendor/gems/message_bus/lib/message_bus/rack/diagnostics.rb
@@ -0,0 +1,35 @@
+module MessageBus::Rack; end
+
+class MessageBus::Rack::Diagnostics
+  def initialize(app, config = {})
+    @app = app
+  end
+
+  def index
+    html = <<HTML
+<!DOCTYPE html>
+<html>
+  <head></head>
+  <body>
+    <h2>Message Bus Diags</h2>
+  </body>
+</html>
+HTML
+    return [200, {"content-type" => "text/html;"}, html]
+  end
+
+  def call(env)
+
+    return @app.call(env) unless env['PATH_INFO'].start_with? '/message-bus/_diagnostics'
+
+    route = env['PATH_INFO'].split('/message_bus/_diagnostics')[1]
+    
+    if MessageBus.is_admin_lookup.nil? || !MessageBus.is_admin_lookup.call
+      return [403, {}, ["not allowed"]]
+    end
+
+    return index unless route
+    
+    return [404, {}, ["not found"]]
+  end
+end
diff --git a/vendor/gems/message_bus/lib/message_bus/rack/middleware.rb b/vendor/gems/message_bus/lib/message_bus/rack/middleware.rb
index 7f9e0ec402f..f240266a4f0 100644
--- a/vendor/gems/message_bus/lib/message_bus/rack/middleware.rb
+++ b/vendor/gems/message_bus/lib/message_bus/rack/middleware.rb
@@ -46,6 +46,11 @@ class MessageBus::Rack::Middleware
         return [200,{"Content-Type" => "text/html"},["sent"]]
     end
 
+    if env['PATH_INFO'].start_with? '/message-bus/_diagnostics' 
+      diags = MessageBus::Rack::Diagnostics.new(@app)
+      return diags.call(env)
+    end
+
     client_id = env['PATH_INFO'].split("/")[2]
     return [404, {}, ["not found"]] unless client_id
 
diff --git a/vendor/gems/message_bus/spec/lib/middleware_spec.rb b/vendor/gems/message_bus/spec/lib/middleware_spec.rb
index 1d7bd279797..65306cf5c65 100644
--- a/vendor/gems/message_bus/spec/lib/middleware_spec.rb
+++ b/vendor/gems/message_bus/spec/lib/middleware_spec.rb
@@ -107,11 +107,22 @@ describe MessageBus::Rack::Middleware do
       ensure
         MessageBus.long_polling_interval = 5000
       end
-
-
     end
   end
 
+  describe "diagnostics" do 
+
+    it "should return a 403 if a user attempts to get at the _diagnostics path" do 
+      get "/message-bus/_diagnostics"
+      last_response.status.should == 403
+    end
+
+    it "should get a 200 with html for an authorized user" do
+      MessageBus.stub(:is_admin_lookup).and_return(lambda{ true })
+      get "/message-bus/_diagnostics"
+      last_response.status.should == 200
+    end
+  end
   
   describe "polling" do
     before do