8f28fc80a2bc58989fa7dee86ad8ba4b38f92c35
[arvados.git] / services / api / test / integration / websocket_test.rb
1 require 'test_helper'
2 require 'websocket_runner'
3 require 'oj'
4 require 'database_cleaner'
5
6 DatabaseCleaner.strategy = :deletion
7
8 class WebsocketTest < ActionDispatch::IntegrationTest
9   self.use_transactional_fixtures = false
10
11   setup do
12     DatabaseCleaner.start
13   end
14
15   teardown do
16     DatabaseCleaner.clean
17   end
18
19   def ws_helper (token = nil)
20     close_status = nil
21
22     EM.run {
23       if token
24         ws = Faye::WebSocket::Client.new("ws://localhost:3002/websocket?api_token=#{api_client_authorizations(token).api_token}")
25       else
26         ws = Faye::WebSocket::Client.new("ws://localhost:3002/websocket")
27       end
28
29       ws.on :close do |event|
30         close_status = [:close, event.code, event.reason]
31         EM.stop_event_loop
32       end
33
34       EM::Timer.new 3 do
35         EM.stop_event_loop
36       end
37
38       yield ws
39     }
40
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)"
43   end
44
45   test "connect with no token" do
46     opened = false
47     status = nil
48
49     ws_helper do |ws|
50       ws.on :open do |event|
51         opened = true
52       end
53
54       ws.on :message do |event|
55         d = Oj.load event.data
56         status = d["status"]
57         ws.close
58       end
59     end
60
61     assert opened, "Should have opened web socket"
62     assert_equal 401, status
63   end
64
65
66   test "connect, subscribe and get response" do
67     opened = false
68     status = nil
69
70     ws_helper :admin do |ws|
71       ws.on :open do |event|
72         opened = true
73         ws.send ({method: 'subscribe'}.to_json)
74       end
75
76       ws.on :message do |event|
77         d = Oj.load event.data
78         status = d["status"]
79         ws.close
80       end
81     end
82
83     assert opened, "Should have opened web socket"
84     assert_equal 200, status
85   end
86
87   test "connect, subscribe, get event" do
88     opened = false
89     state = 1
90     spec_uuid = nil
91     ev_uuid = nil
92
93     authorize_with :admin
94
95     ws_helper :admin do |ws|
96       ws.on :open do |event|
97         opened = true
98         ws.send ({method: 'subscribe'}.to_json)
99       end
100
101       ws.on :message do |event|
102         d = Oj.load event.data
103         case state
104         when 1
105           assert_equal 200, d["status"]
106           spec = Specimen.create
107           spec.save
108           spec_uuid = spec.uuid
109           state = 2
110         when 2
111           ev_uuid = d["object_uuid"]
112           ws.close
113         end
114       end
115
116     end
117
118     assert opened, "Should have opened web socket"
119     assert_not_nil spec_uuid
120     assert_equal spec_uuid, ev_uuid
121   end
122
123 end