Skip to content

Commit

Permalink
Merge pull request #19497 from lpichler/rewrite_MiqExpression#registr…
Browse files Browse the repository at this point in the history
…y_data

Update MiqExpression#registry_data and add coverage
  • Loading branch information
kbrock authored Nov 13, 2019
2 parents 56fff77 + f54bfae commit 72ffca2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
12 changes: 5 additions & 7 deletions app/models/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,15 @@ def self.registry_data(ref, name, ohash)
# <registry>HKLM\Software\Microsoft\Windows\CurrentVersion\explorer\Shell Folders\Common AppData</registry> == 'C:\Documents and Settings\All Users\Application Data'
# <registry>HKLM\Software\Microsoft\Windows\CurrentVersion\explorer\Shell Folders : Common AppData</registry> == 'C:\Documents and Settings\All Users\Application Data'
return nil unless ref.respond_to?("registry_items")

registry_items = ref.registry_items
if ohash[:key_exists]
return ref.registry_items.where("name LIKE ? ESCAPE ''", name + "%").exists?
registry_items.where("name LIKE ? ESCAPE ''", name + "%").exists?
elsif ohash[:value_exists]
rec = ref.registry_items.find_by(:name => name)
return !!rec
registry_items.where(:name => name).exists?
else
rec = ref.registry_items.find_by(:name => name)
registry_items.find_by(:name => name)&.data
end
return nil unless rec

rec.data
end

def export_to_array
Expand Down
30 changes: 30 additions & 0 deletions spec/models/condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,34 @@
expect(result_record).to be(record)
end
end

describe ".registry_data" do
let(:name_and_value) { "HKLM\\SOFTWARE\\WindowsFirewall : EnableFirewall" }
let(:registry_item) { FactoryBot.create(:registry_item, :name => name_and_value, :data => "100") }
let(:vm) { FactoryBot.create(:vm_vmware, :registry_items => [registry_item]) }

it "finds registry item record when key exists" do
expect(described_class.registry_data(vm, 'HKLM', :key_exists => true)).to be_truthy
end

it "doesn't find registry item record when key exists" do
expect(described_class.registry_data(vm, 'SOFTWARE_X', :key_exists => true)).not_to be_truthy
end

it "finds registry item record when value exists" do
expect(described_class.registry_data(vm, name_and_value, :value_exists => true)).to be_truthy
end

it "doesn't find registry item record when value exists" do
expect(described_class.registry_data(vm, name_and_value + "X", :value_exists => true)).not_to be_truthy
end

it "returns registry data" do
expect(described_class.registry_data(vm, name_and_value, {})).to eq("100")
end

it "doesn't returns registry data" do
expect(described_class.registry_data(vm, name_and_value + "X", {})).to be_nil
end
end
end

0 comments on commit 72ffca2

Please sign in to comment.