2 require 'websocket_runner'
4 require 'database_cleaner'
6 DatabaseCleaner.strategy = :deletion
8 class WebsocketTest < ActionDispatch::IntegrationTest
9 self.use_transactional_fixtures = false
19 def ws_helper (token = nil)
24 ws = Faye::WebSocket::Client.new("ws://localhost:3002/websocket?api_token=#{api_client_authorizations(token).api_token}")
26 ws = Faye::WebSocket::Client.new("ws://localhost:3002/websocket")
29 ws.on :close do |event|
30 close_status = [:close, event.code, event.reason]
41 assert_not_nil close_status, "Test took too long"
42 assert_equal 1000, close_status[1], "Server closed the connection unexpectedly (check server log for errors)"
45 test "connect with no token" do
50 ws.on :open do |event|
54 ws.on :message do |event|
55 d = Oj.load event.data
61 assert opened, "Should have opened web socket"
62 assert_equal 401, status
66 test "connect, subscribe and get response" do
70 ws_helper :admin do |ws|
71 ws.on :open do |event|
73 ws.send ({method: 'subscribe'}.to_json)
76 ws.on :message do |event|
77 d = Oj.load event.data
83 assert opened, "Should have opened web socket"
84 assert_equal 200, status
87 test "connect, subscribe, get event" do
95 ws_helper :admin do |ws|
96 ws.on :open do |event|
98 ws.send ({method: 'subscribe'}.to_json)
101 ws.on :message do |event|
102 d = Oj.load event.data
105 assert_equal 200, d["status"]
106 spec = Specimen.create
108 spec_uuid = spec.uuid
111 ev_uuid = d["object_uuid"]
118 assert opened, "Should have opened web socket"
119 assert_not_nil spec_uuid
120 assert_equal spec_uuid, ev_uuid