[ARVADOS] updated: c72f1de32f688690d1161a1852e471e88919e057

git at public.curoverse.com git at public.curoverse.com
Thu Apr 24 14:58:29 EDT 2014


Summary of changes:
 services/api/lib/eventbus.rb                    |   11 ++-
 services/api/test/integration/websocket_test.rb |   98 +++++++++++++++++++++--
 services/api/test/websocket_runner.rb           |    2 +-
 3 files changed, 98 insertions(+), 13 deletions(-)

       via  c72f1de32f688690d1161a1852e471e88919e057 (commit)
      from  f0ca76acaa3a4e3713f7aef6a6bb778e6e71e562 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.


commit c72f1de32f688690d1161a1852e471e88919e057
Author: Peter Amstutz <peter.amstutz at curoverse.com>
Date:   Thu Apr 24 14:58:23 2014 -0400

    Added websocket first tests for connecting and checking API token.

diff --git a/services/api/lib/eventbus.rb b/services/api/lib/eventbus.rb
index 516f5fa..c2a6c6d 100644
--- a/services/api/lib/eventbus.rb
+++ b/services/api/lib/eventbus.rb
@@ -41,7 +41,7 @@ class EventBus
 
   def on_connect ws
     if not current_user
-      ws.send '{"error":"Not logged in"}'
+      ws.send ({status: 401, message: "Valid API token required"}.to_json)
       ws.close
       return
     end
@@ -93,12 +93,13 @@ class EventBus
     end
 
     ws.on :message do |event|
-      p = oj.parse(event.data)
-      if p[:method] == 'subscribe'
-        if p[:starting_log_id]
-          ws.last_log_id = p[:starting_log_id].to_i
+      p = Oj.load event.data
+      if p["method"] == 'subscribe'
+        if p["starting_log_id"]
+          ws.last_log_id = p["starting_log_id"].to_i
         end
         ws.filters.push(Filter.new p)
+        ws.send ({status: 200, message: 'subscribe ok'}.to_json)
       end
     end
 
diff --git a/services/api/test/integration/websocket_test.rb b/services/api/test/integration/websocket_test.rb
index ed82fe0..bafab66 100644
--- a/services/api/test/integration/websocket_test.rb
+++ b/services/api/test/integration/websocket_test.rb
@@ -1,25 +1,109 @@
 require 'test_helper'
 require 'websocket_runner'
+require 'oj'
 
 class WebsocketTest < ActionDispatch::IntegrationTest
 
-  test "just connect" do
-    opened = false
+  def ws_helper (token = nil)
     EM.run {
-      ws = Faye::WebSocket::Client.new('ws://localhost:3002/websocket')
+      if token
+        ws = Faye::WebSocket::Client.new("ws://localhost:3002/websocket?api_token=#{api_client_authorizations(token).api_token}")
+      else
+        ws = Faye::WebSocket::Client.new("ws://localhost:3002/websocket")
+      end
+
+      ws.on :close do |event|
+        EM.stop_event_loop
+      end
 
+      EM::Timer.new 3 do
+        puts "\nTest took too long"
+        EM.stop_event_loop
+      end
+
+      yield ws
+    }
+  end
+
+  test "connect with no token" do
+    opened = false
+    status = nil
+
+    ws_helper do |ws|
       ws.on :open do |event|
         opened = true
+      end
+
+      ws.on :message do |event|
+        d = Oj.load event.data
+        status = d["status"]
         ws.close
       end
+    end
 
-      ws.on :close do |event|
-        p [:close, event.code, event.reason]
-        EM.stop_event_loop
+    assert opened, "Should have opened web socket"
+    assert_equal 401, status
+  end
+
+
+  test "connect, subscribe and get response" do
+    opened = false
+    status = nil
+
+    ws_helper :admin do |ws|
+      ws.on :open do |event|
+        opened = true
+        ws.send ({method: 'subscribe'}.to_json)
       end
-    }
+
+      ws.on :message do |event|
+        d = Oj.load event.data
+        status = d["status"]
+        ws.close
+      end
+    end
 
     assert opened, "Should have opened web socket"
+    assert_equal 200, status
   end
 
+
+  # test "connect, subscribe, get event" do
+  #   opened = false
+  #   state = 1
+  #   spec_uuid = nil
+  #   ev_uuid = nil
+
+  #   puts "user #{Thread.current[:user]}"
+  #   authorize_with :admin
+  #   puts "user #{Thread.current[:user]}"
+
+  #   ws_helper :admin do |ws|
+  #     ws.on :open do |event|
+  #       puts "XXX"
+  #       opened = true
+  #       ws.send ({method: 'subscribe'}.to_json)
+  #     end
+
+  #     ws.on :message do |event|
+  #       d = Oj.load event.data
+  #       puts d
+  #       case state
+  #       when 1
+  #         assert_equal 200, d["status"]
+  #         spec_uuid = Specimen.create.save.uuid
+  #         state = 2
+  #       when 2
+  #         ev_uuid = d["uuid"]
+  #         ws.close
+  #       end
+  #     end
+
+  #   end
+
+  #   assert opened, "Should have opened web socket"
+  #   assert_not spec_uuid.nil?
+  #   assert_equal spec_uuid, ev_uuid
+  # end
+
 end
diff --git a/services/api/test/websocket_runner.rb b/services/api/test/websocket_runner.rb
index 2d6f245..c35938e 100644
--- a/services/api/test/websocket_runner.rb
+++ b/services/api/test/websocket_runner.rb
@@ -6,7 +6,7 @@ SERVER_PID_PATH = 'tmp/pids/passenger.3002.pid'
 class WebsocketTestRunner < MiniTest::Unit
   def _system(*cmd)
     Bundler.with_clean_env do
-      if not system({'ARVADOS_WEBSOCKETS' => '1'}, *cmd)
+      if not system({'ARVADOS_WEBSOCKETS' => '1', 'RAILS_ENV' => 'test'}, *cmd)
         raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}"
       end
     end

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list