-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodules_spec.cr
More file actions
511 lines (402 loc) · 18 KB
/
modules_spec.cr
File metadata and controls
511 lines (402 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
require "../helper"
require "timecop"
module PlaceOS::Api
describe Modules do
::Spec.before_each do
Model::Module.clear
Model::ControlSystem.clear
end
Spec.test_404(Modules.base_route, model_name: Model::Module.table_name, headers: Spec::Authentication.headers)
describe "CRUD operations", tags: "crud" do
Spec.test_crd(klass: Model::Module, controller_klass: Modules)
it "update preserves logic module connection status" do
driver = Model::Generator.driver(role: Model::Driver::Role::Logic).save!
mod = Model::Generator.module(driver: driver).save!
mod.connected = false
id = mod.id.as(String)
path = File.join(Modules.base_route, id)
result = client.patch(
path: path,
body: mod.to_json,
headers: Spec::Authentication.headers,
)
result.status_code.should eq 200
updated = Model::Module.from_trusted_json(result.body)
updated.id.should eq mod.id
updated.connected.should be_true
end
it "update" do
driver = Model::Generator.driver(role: Model::Driver::Role::Service).save!
mod = Model::Generator.module(driver: driver).save!
connected = mod.connected
mod.connected = !connected
id = mod.id.as(String)
path = File.join(Modules.base_route, id)
result = client.patch(
path: path,
body: mod.to_json,
headers: Spec::Authentication.headers,
)
result.status_code.should eq 200
updated = Model::Module.from_trusted_json(result.body)
updated.id.should eq mod.id
updated.connected.should eq !connected
end
it "can update logic modules as a management user" do
control_system = Model::Generator.control_system
control_system.zones << Spec::Authentication.org_zone.id.as(String)
control_system.zones_will_change!
control_system.save!
# ensure the user can't edit modules that are not logics
ser_driver = Model::Generator.driver(role: Model::Driver::Role::Service).save!
protected_mod = Model::Generator.module(driver: ser_driver).save!
connected = protected_mod.connected
protected_mod.connected = !connected
id = protected_mod.id.as(String)
path = File.join(Modules.base_route, id)
result = client.patch(
path: path,
body: protected_mod.to_json,
headers: Spec::Authentication.headers(sys_admin: false, support: false, groups: ["management"]),
)
result.status_code.should eq 403
# ensure they can edit modules they have access to
driver = Model::Generator.driver(role: Model::Driver::Role::Logic).save!
mod = Model::Generator.module(driver: driver, control_system: control_system).save!
connected = mod.connected
mod.connected = !connected
id = mod.id.as(String)
path = File.join(Modules.base_route, id)
result = client.patch(
path: path,
body: mod.to_json,
headers: Spec::Authentication.headers(sys_admin: false, support: false, groups: ["management"]),
)
result.status_code.should eq 200
updated = Model::Module.from_trusted_json(result.body)
updated.id.should eq mod.id
end
it "POST endpoint should discard read-only field updates" do
mod = PlaceOS::Model::Generator.module
mod_json = JSON.parse(mod.to_json)
mod_json.as_h["has_runtime_error"] = JSON::Any.new(true)
mod_json.as_h["error_timestamp"] = JSON::Any.new(0)
groups = [] of String
result = client.post(Modules.base_route, body: mod_json.to_json,
headers: Spec::Authentication.headers(sys_admin: true, support: true, groups: groups))
result.status_code.should eq 201
mod1 = PlaceOS::Model::Module.from_trusted_json(result.body)
result = client.get(path: File.join(Modules.base_route, mod1.id.as(String)),
headers: Spec::Authentication.headers)
result.status_code.should eq 200
mod2 = PlaceOS::Model::Module.from_json(result.body)
mod2.has_runtime_error.should be_false
mod2.error_timestamp.should be_nil
end
end
describe "index", tags: "search" do
it "queries by parent driver" do
name = random_name
driver = Model::Generator.driver
driver.name = name
driver.save!
# Module name is dependent on the driver's name
doc = Model::Generator.module(driver: driver).save!
doc.persisted?.should be_true
refresh_elastic(Model::Module.table_name)
params = HTTP::Params.encode({"q" => name})
path = "#{Modules.base_route.rstrip('/')}?#{params}"
header = Spec::Authentication.headers
found = until_expected("GET", path, header) do |response|
Array(Hash(String, JSON::Any)).from_json(response.body).any? do |result|
result["id"].as_s == doc.id
end
end
found.should be_true
end
it "looks up by system_id" do
mod = Model::Generator.module.save!
sys = Model::Generator.control_system
sys.modules = [mod.id.as(String)]
sys.save!
# Call the index method of the controller
params = HTTP::Params{"control_system_id" => sys.id.as(String)}
response = client.get(
"#{Modules.base_route}?#{params}",
headers: Spec::Authentication.headers,
)
response.status_code.should eq 200
response.headers["X-Total-Count"].should eq("1")
Array(Hash(String, JSON::Any)).from_json(response.body.to_s).map(&.["id"].as_s).first?.should eq(mod.id)
end
it "ensures management users can only see the modules they have access to" do
Model::Generator.module.save!
Model::Generator.module.save!
Model::Generator.module.save!
mod = Model::Generator.module.save!
sys = Model::Generator.control_system
sys.zones << Spec::Authentication.org_zone.id.as(String)
sys.zones_will_change!
sys.modules = [mod.id.as(String)]
sys.save!
refresh_elastic(Model::Module.table_name)
# ensure regular users can't use the route
params = HTTP::Params{"control_system_id" => sys.id.as(String)}
response = client.get(
"#{Modules.base_route}?#{params}",
headers: Spec::Authentication.headers(sys_admin: false, support: false),
)
response.status_code.should eq 403
# management users can see the list of modules in a control system
params = HTTP::Params{"control_system_id" => sys.id.as(String)}
response = client.get(
"#{Modules.base_route}?#{params}",
headers: Spec::Authentication.headers(sys_admin: false, support: false, groups: ["management"]),
)
response.status_code.should eq 200
# Admins can see everything
header = Spec::Authentication.headers
until_expected("GET", Modules.base_route, header) do |resp|
response = resp
resp.success? ? (resp.headers["X-Total-Count"].to_i > 1) : false
end
response.status_code.should eq 200
response.headers["X-Total-Count"].to_i > 1
# Management can only see the one
header = Spec::Authentication.headers(sys_admin: false, support: false, groups: ["management"])
until_expected("GET", Modules.base_route, header) do |resp|
response = resp
resp.success? ? (resp.headers["X-Total-Count"].to_i > 0) : false
end
# check the results
response.status_code.should eq 200
response.headers["X-Total-Count"].should eq("1")
Array(Hash(String, JSON::Any)).from_json(response.body.to_s).map(&.["id"].as_s).first?.should eq(mod.id)
end
context "query parameter" do
it "as_of" do
mod1 = Model::Generator.module
mod1.connected = true
Timecop.freeze(2.days.ago) do
mod1.save!
end
mod1.persisted?.should be_true
mod2 = Model::Generator.module
mod2.connected = true
mod2.save!
mod2.persisted?.should be_true
params = HTTP::Params.encode({"as_of" => (mod1.updated_at.try &.to_unix).to_s})
path = "#{Modules.base_route}?#{params}"
found = until_expected("GET", path, Spec::Authentication.headers) do |response|
results = Array(Hash(String, JSON::Any)).from_json(response.body).map(&.["id"].as_s)
contains_correct = results.any?(mod1.id)
contains_incorrect = results.any?(mod2.id)
!results.empty? && contains_correct && !contains_incorrect
end
found.should be_true
end
it "connected" do
mod = Model::Generator.module
mod.ignore_connected = false
mod.connected = true
mod.save!
mod.persisted?.should be_true
params = HTTP::Params.encode({"connected" => "true"})
path = "#{Modules.base_route}?#{params}"
found = until_expected("GET", path, Spec::Authentication.headers) do |response|
results = Array(Hash(String, JSON::Any)).from_json(response.body)
all_connected = results.all? { |r| r["connected"].as_bool == true }
contains_created = results.any? { |r| r["id"].as_s == mod.id }
!results.empty? && all_connected && contains_created
end
found.should be_true
end
it "no_logic" do
driver = Model::Generator.driver(role: Model::Driver::Role::Service).save!
mod = Model::Generator.module(driver: driver)
mod.role = Model::Driver::Role::Service
mod.save!
params = HTTP::Params.encode({"no_logic" => "true"})
path = "#{Modules.base_route}?#{params}"
found = until_expected("GET", path, Spec::Authentication.headers) do |response|
results = Array(Hash(String, JSON::Any)).from_json(response.body)
no_logic = results.all? { |r| r["role"].as_i != Model::Driver::Role::Logic.to_i }
contains_created = results.any? { |r| r["id"].as_s == mod.id }
!results.empty? && no_logic && contains_created
end
found.should be_true
end
end
it "only returns matches" do
name_one = random_name
driver_one = Model::Generator.driver
driver_one.name = name_one
driver_one.save!
doc_one = Model::Generator.module(driver: driver_one).save!
doc_one.persisted?.should be_true
name_two = random_name
driver_two = Model::Generator.driver
driver_two.name = name_two
driver_two.save!
doc_two = Model::Generator.module(driver: driver_two).save!
doc_two.persisted?.should be_true
refresh_elastic(Model::Module.table_name)
params = HTTP::Params.encode({"q" => name_one})
path = "#{Modules.base_route.rstrip('/')}?#{params}"
header = Spec::Authentication.headers
found = until_expected("GET", path, header) do |response|
result = Array(Hash(String, JSON::Any)).from_json(response.body)
result.any? { |r| r["id"].as_s == doc_one.id } && !result.any? { |r| r["id"].as_s == doc_two.id } ? true : false
end
found.should be_true
end
end
describe "GET /modules/:id/settings" do
it "collates Module settings" do
driver = Model::Generator.driver(role: Model::Driver::Role::Logic).save!
driver_settings_string = %(value: 0\nscreen: 0\nfrangos: 0\nchop: 0)
Model::Generator.settings(driver: driver, settings_string: driver_settings_string).save!
control_system = Model::Generator.control_system.save!
control_system_settings_string = %(frangos: 1)
Model::Generator.settings(control_system: control_system, settings_string: control_system_settings_string).save!
zone = Model::Generator.zone.save!
zone_settings_string = %(screen: 1)
Model::Generator.settings(zone: zone, settings_string: zone_settings_string).save!
control_system.zones = [zone.id.as(String)]
control_system.update!
mod = Model::Generator.module(driver: driver, control_system: control_system).save!
module_settings_string = %(value: 2\n)
Model::Generator.settings(mod: mod, settings_string: module_settings_string).save!
expected_settings_ids = [
mod.settings,
control_system.settings,
zone.settings,
driver.settings,
].flat_map(&.compact_map(&.id)).reverse!
path = File.join(Modules.base_route, "#{mod.id}/settings")
result = client.get(
path: path,
headers: Spec::Authentication.headers,
)
result.success?.should be_true
settings = Array(Hash(String, JSON::Any)).from_json(result.body)
settings_hierarchy_ids = settings.map &.["id"].to_s
settings_hierarchy_ids.should eq expected_settings_ids
{mod, control_system, zone, driver}.each &.destroy
end
it "returns an empty array for a logic module without associated settings" do
driver = Model::Generator.driver(role: Model::Driver::Role::Logic).save!
control_system = Model::Generator.control_system.save!
zone = Model::Generator.zone.save!
control_system.zones = [zone.id.as(String)]
control_system.update!
mod = Model::Generator.module(driver: driver, control_system: control_system).save!
path = File.join(Modules.base_route, "#{mod.id}/settings")
result = client.get(
path: path,
headers: Spec::Authentication.headers,
)
unless result.success?
puts "\ncode: #{result.status_code} body: #{result.body}"
end
result.success?.should be_true
Array(JSON::Any).from_json(result.body).should be_empty
end
it "returns an empty array for a module without associated settings" do
driver = Model::Generator.driver(role: Model::Driver::Role::Service).save!
mod = Model::Generator.module(driver: driver).save!
path = File.join(Modules.base_route, "#{mod.id}/settings")
result = client.get(
path: path,
headers: Spec::Authentication.headers,
)
unless result.success?
puts "\ncode: #{result.status_code} body: #{result.body}"
end
result.success?.should be_true
Array(JSON::Any).from_json(result.body).should be_empty
end
describe "POST /:id/ping" do
it "fails for logic module" do
driver = Model::Generator.driver(role: Model::Driver::Role::Logic)
mod = Model::Generator.module(driver: driver).save!
path = File.join(Modules.base_route, "#{mod.id}/ping")
result = client.post(
path: path,
headers: Spec::Authentication.headers,
)
result.success?.should be_false
result.status_code.should eq 422
end
it "pings a module" do
driver = Model::Generator.driver(role: Model::Driver::Role::Device)
driver.default_port = 8080
driver.save!
mod = Model::Generator.module(driver: driver)
mod.ip = "127.0.0.1"
mod.save!
path = File.join(Modules.base_route, "#{mod.id}/ping")
result = client.post(
path: path,
headers: Spec::Authentication.headers,
)
body = JSON.parse(result.body)
result.success?.should be_true
body["pingable"].should be_true
end
describe "scopes" do
Spec.test_controller_scope(Modules)
it "checks scope on update" do
_, scoped_headers = Spec::Authentication.authentication(scope: [PlaceOS::Model::UserJWT::Scope.new("modules", PlaceOS::Model::UserJWT::Scope::Access::Write)])
driver = Model::Generator.driver(role: Model::Driver::Role::Service).save!
mod = Model::Generator.module(driver: driver).save!
connected = mod.connected
mod.connected = !connected
id = mod.id.as(String)
path = File.join(Modules.base_route, id)
result = Scopes.update(path, mod, scoped_headers)
result.status_code.should eq 200
updated = Model::Module.from_trusted_json(result.body)
updated.id.should eq mod.id
updated.connected.should eq !connected
_, scoped_headers = Spec::Authentication.authentication(scope: [PlaceOS::Model::UserJWT::Scope.new("modules", PlaceOS::Model::UserJWT::Scope::Access::Read)])
result = Scopes.update(path, mod, scoped_headers)
result.success?.should be_false
result.status_code.should eq 403
end
end
end
end
describe "POST /:id/start and POST /:id/stop" do
it "allows support users to start and stop modules" do
control_system = Model::Generator.control_system
control_system.zones << Spec::Authentication.org_zone.id.as(String)
control_system.zones_will_change!
control_system.save!
driver = Model::Generator.driver(role: Model::Driver::Role::Logic).save!
mod = Model::Generator.module(driver: driver, control_system: control_system)
mod.running = false
mod.save!
id = mod.id.as(String)
start_path = File.join(Modules.base_route, id, "start")
stop_path = File.join(Modules.base_route, id, "stop")
# Test that admin user can start a module
result = client.post(
path: start_path,
headers: Spec::Authentication.headers(sys_admin: false, support: true),
)
result.status_code.should eq 200
mod.reload!
mod.running.should be_true
# Test that admin user can stop a module
result = client.post(
path: stop_path,
headers: Spec::Authentication.headers(sys_admin: false, support: true),
)
result.status_code.should eq 200
mod.reload!
mod.running.should be_false
end
end
end
end