Fix resource_update test, but now other tests are blinking...

pull/979/head
Rick Carlino 2018-09-12 14:40:14 -05:00
parent ac6e5387b3
commit 7d7e884d66
2 changed files with 30 additions and 25 deletions

View File

@ -249,7 +249,7 @@ module CeleryScriptSettingsBag
.node(:resource_update, RESOURCE_UPDATE_ARGS) do |x|
resource_type = x.args.fetch("resource_type").value
resource_id = x.args.fetch("resource_id").value
check_resource_type(x, resource_type, resource_id, Device.current)
check_resource_type(x, resource_type, resource_id)
end
.node(:install_first_party_farmware, [])
@ -260,22 +260,17 @@ module CeleryScriptSettingsBag
node.invalidate!(BAD_RESOURCE_ID % [klass.name, resource_id])
end
def self.check_resource_type(node,
resource_type,
resource_id,
me = Device.current)
def self.check_resource_type(node, resource_type, resource_id)
case resource_type # <= Security critical code (for const_get'ing)
when "Device"
# When "resource_type" is "Device", resource_id always refers to
# the current_device.
# For convinience, we try to set it here, defaulting to 0 if
# current_user can't be found.
node
.args["resource_id"]
.instance_variable_set("@value", me.try(:id) || 0)
# For convinience, we try to set it here, defaulting to 0
node.args["resource_id"].instance_variable_set("@value", 0)
when *RESOURCE_NAME.without("Device")
resource_ok = resource_type.constantize.exists?(resource_id)
no_resource(node, resource_type, resource_id) unless resource_ok
klass = Kernel.const_get(resource_type)
resource_ok = klass.exists?(resource_id)
no_resource(node, klass, resource_id) unless resource_ok
end
end
# Given an array of allowed values and a CeleryScript AST node, will DETERMINE

View File

@ -157,18 +157,28 @@ describe CeleryScript::Corpus do
end
it "Validates resource_update nodes" do
ast = {
kind: "resource_update",
args: {
resource_type: "Device",
resource_id: 23,
label: "mounted_tool_id",
value: 1
}
}
ast = { "kind": "tool", "args": { "tool_id": 0 } };
checker = CeleryScript::Checker.new(CeleryScript::AstNode.new(ast),
corpus,
device)
ast = { "kind": "resource_update",
"args": { "resource_type" => "Device",
"resource_id" => 23, # Mutated to "0" later..
"label" => "mounted_tool_id",
"value" => 1 } }
checker = CeleryScript::Checker.new(CeleryScript::AstNode.new(ast), corpus, device)
expect(checker.valid?).to be(true)
expect(checker.tree.args["resource_id"].value).to eq(0)
end
it "rejects bogus resource_updates" do
fake_id = (4 + Sequence.count * 3)
ast = { "kind": "resource_update",
"args": { "resource_type" => "Sequence",
"resource_id" => fake_id,
"label" => "name",
"value" => "Should Fail" } }
hmm = CeleryScript::AstNode.new(ast)
expect(hmm.args.fetch("resource_id").value).to eq(fake_id)
checker = CeleryScript::Checker.new(hmm, corpus, device)
expect(checker.valid?).to be(false)
expect(checker.error.message)
.to eq("Can't find Sequence with id of #{fake_id}")
end
end