-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdrivers_spec.cr
More file actions
144 lines (119 loc) · 4.59 KB
/
drivers_spec.cr
File metadata and controls
144 lines (119 loc) · 4.59 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
require "../helper"
module PlaceOS::Api
describe Drivers, tags: "drivers" do
describe "index", tags: "search" do
Spec.test_base_index(klass: Model::Driver, controller_klass: Drivers)
it "filters queries by driver role" do
service = Model::Generator.driver(role: Model::Driver::Role::Service)
service.name = random_name
service.save!
params = HTTP::Params.encode({
"role" => Model::Driver::Role::Service.to_i.to_s,
"q" => service.name,
})
refresh_elastic(Model::Driver.table_name)
path = "#{Drivers.base_route}?#{params}"
found = until_expected("GET", path, Spec::Authentication.headers) do |response|
results = Array(Hash(String, JSON::Any)).from_json(response.body)
all_service_roles = results.all? { |r| r["role"] == Model::Driver::Role::Service.to_i }
contains_search_term = results.any? { |r| r["id"] == service.id }
!results.empty? && all_service_roles && contains_search_term
end
found.should be_true
end
end
Spec.test_404(Drivers.base_route, model_name: Model::Driver.table_name, headers: Spec::Authentication.headers)
describe "CRUD operations", tags: "crud" do
before_each do
HttpMocks.reset
end
Spec.test_crd(klass: Model::Driver, controller_klass: Drivers)
describe "update" do
it "if role is preserved" do
driver = Model::Generator.driver.save!
original_name = driver.name
driver.name = random_name
id = driver.id.as(String)
path = File.join(Drivers.base_route, id)
result = client.patch(
path: path,
body: driver.changed_attributes.to_json,
headers: Spec::Authentication.headers,
)
result.success?.should be_true
updated = Model::Driver.from_trusted_json(result.body)
updated.id.should eq driver.id
updated.name.should_not eq original_name
end
it "fails if role differs" do
driver = Model::Generator.driver(role: Model::Driver::Role::SSH).save!
driver.role = Model::Driver::Role::Device
id = driver.id.as(String)
path = File.join(Drivers.base_route, id)
result = client.patch(
path: path,
body: driver.changed_attributes.to_json,
headers: Spec::Authentication.headers,
)
result.success?.should_not be_true
result.body.should contain "role must not change"
end
end
end
describe "readme" do
it "returns the readme content for a driver" do
# Create a repository pointing to the real PlaceOS/drivers repo
repository = Model::Generator.repository(type: Model::Repository::Type::Driver)
repository.uri = "https://github.com/PlaceOS/drivers"
repository.save!
# Create a driver with a real file path that has a readme
driver = Model::Driver.new(
name: "Auto Release",
role: Model::Driver::Role::Logic,
commit: "HEAD",
module_name: "AutoRelease",
file_name: "drivers/place/auto_release.cr",
)
driver.repository = repository
driver.save!
id = driver.id.as(String)
path = File.join(Drivers.base_route, id, "readme")
result = client.get(
path: path,
headers: Spec::Authentication.headers,
)
result.success?.should be_true
result.body.should contain("Auto Release")
end
it "returns 404 when readme does not exist" do
# Create a repository pointing to the real PlaceOS/drivers repo
repository = Model::Generator.repository(type: Model::Repository::Type::Driver)
repository.uri = "https://github.com/PlaceOS/drivers"
repository.save!
# Create a driver with a file path that does NOT have a readme
driver = Model::Driver.new(
name: "No Readme Driver",
role: Model::Driver::Role::Logic,
commit: "HEAD",
module_name: "NoReadme",
file_name: "drivers/place/nonexistent_driver.cr",
)
driver.repository = repository
driver.save!
id = driver.id.as(String)
path = File.join(Drivers.base_route, id, "readme")
result = client.get(
path: path,
headers: Spec::Authentication.headers,
)
result.status_code.should eq 404
end
end
describe "scopes" do
before_each do
HttpMocks.core_compiled
end
Spec.test_controller_scope(Drivers)
end
end
end