Skip to content

Commit

Permalink
Merge 4ba8829 into ba93bdf
Browse files Browse the repository at this point in the history
  • Loading branch information
knu committed Jun 27, 2023
2 parents ba93bdf + 4ba8829 commit 70bf0df
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
5 changes: 3 additions & 2 deletions app/models/agents/trigger_agent.rb
Expand Up @@ -53,8 +53,9 @@ class TriggerAgent < Agent
when String
true
when Hash
rule.values_at('type', 'value', 'path').all?(&:present?) &&
VALID_COMPARISON_TYPES.include?(rule['type'])
VALID_COMPARISON_TYPES.include?(rule['type']) &&
/\S/.match?(rule['path']) &&
rule['value'].is_a?(String)
else
false
end
Expand Down
59 changes: 31 additions & 28 deletions spec/models/agents/trigger_agent_spec.rb
Expand Up @@ -7,10 +7,10 @@
'options' => {
'expected_receive_period_in_days' => 2,
'rules' => [{
'type' => "regex",
'value' => "a\\db",
'path' => "foo.bar.baz",
}],
'type' => "regex",
'value' => "a\\db",
'path' => "foo.bar.baz",
}],
'message' => "I saw '{{foo.bar.baz}}' from {{name}}"
}
}
Expand All @@ -21,7 +21,7 @@

@event = Event.new
@event.agent = agents(:bob_rain_notifier_agent)
@event.payload = { 'foo' => { "bar" => { 'baz' => "a2b" }},
@event.payload = { 'foo' => { "bar" => { 'baz' => "a2b" } },
'name' => "Joe" }
end

Expand Down Expand Up @@ -82,10 +82,14 @@
it "should validate the three fields in each rule" do
@checker.options['rules'] << { 'path' => "foo", 'type' => "fake", 'value' => "6" }
expect(@checker).not_to be_valid
@checker.options['rules'].last['type'] = "field>=value"
@checker.options['rules'].last['type'] = "field!=value"
expect(@checker).to be_valid
@checker.options['rules'].last.delete('value')
expect(@checker).not_to be_valid
@checker.options['rules'].last['value'] = ''
expect(@checker).to be_valid
@checker.options['rules'].last['path'] = ''
expect(@checker).not_to be_valid
end

it "should validate non-hash rules" do
Expand Down Expand Up @@ -115,7 +119,7 @@
@event.payload['foo']['bar']['baz'] = "a222b"
expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })

@event.payload['foo']['bar']['baz'] = "a2b"
expect {
Expand All @@ -132,7 +136,7 @@
}
expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })

@event.payload['foo']['bar']['baz'] = "a2b"
expect {
Expand All @@ -155,7 +159,7 @@

expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })

@event.payload['foo']['bar']['baz'] = "a22b"
expect {
Expand All @@ -173,7 +177,7 @@

expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })

@event.payload['foo']['bar']['baz'] = "a3b"
expect {
Expand All @@ -198,7 +202,7 @@
@checker.options['rules'].first['value'] = 3
expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })
end

it "handles array of numerical comparisons" do
Expand All @@ -213,7 +217,7 @@
@checker.options['rules'].first['value'] = [4, 3]
expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })
end

it "handles exact comparisons" do
Expand All @@ -223,7 +227,7 @@
@checker.options['rules'].first['value'] = "hello there"
expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })

@checker.options['rules'].first['value'] = "hello world"
expect {
Expand All @@ -238,7 +242,7 @@
@checker.options['rules'].first['value'] = ["hello there", "hello universe"]
expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })

@checker.options['rules'].first['value'] = ["hello world", "hello universe"]
expect {
Expand All @@ -253,7 +257,7 @@

expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })

@checker.options['rules'].first['value'] = "hello there"

Expand All @@ -269,7 +273,7 @@

expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })

@checker.options['rules'].first['value'] = ["hello there", "hello world"]

Expand All @@ -285,13 +289,13 @@

expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })

@checker.options['rules'].first['value'] = ["hello there", "hello world"]

expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })

@checker.options['rules'].first['value'] = ["hello there", "hello here"]

Expand All @@ -312,22 +316,22 @@
@checker.options['rules'].first['path'] = "foo"
expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })

@checker.options['rules'].first['value'] = "hi"
expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })
end

it "handles multiple events" do
event2 = Event.new
event2.agent = agents(:bob_weather_agent)
event2.payload = { 'foo' => { 'bar' => { 'baz' => "a2b" }}}
event2.payload = { 'foo' => { 'bar' => { 'baz' => "a2b" } } }

event3 = Event.new
event3.agent = agents(:bob_weather_agent)
event3.payload = { 'foo' => { 'bar' => { 'baz' => "a222b" }}}
event3.payload = { 'foo' => { 'bar' => { 'baz' => "a222b" } } }

expect {
@checker.receive([@event, event2, event3])
Expand Down Expand Up @@ -376,7 +380,7 @@

expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })
end

it "can accept a partial rule set match when 'must_match' is present and less than the total number of rules" do
Expand All @@ -392,8 +396,7 @@

expect {
@checker.receive([@event])
}.to change { Event.count } # but the first one matches

}.to(change { Event.count }) # but the first one matches

@checker.options['must_match'] = "2"

Expand All @@ -407,7 +410,7 @@

expect {
@checker.receive([@event])
}.not_to change { Event.count } # only 1 matches, we needed 2
}.not_to(change { Event.count }) # only 1 matches, we needed 2
end
end

Expand All @@ -425,7 +428,7 @@

expect {
@checker.receive([@event])
}.not_to change { Event.count }
}.not_to(change { Event.count })

@checker.options['rules'].first['value'] = 6
expect {
Expand All @@ -440,7 +443,7 @@

@checker.receive([@event])

expect(@checker.most_recent_event.payload).to eq(@event.payload.merge(:message => "I saw '5' from Joe"))
expect(@checker.most_recent_event.payload).to eq(@event.payload.merge(message: "I saw '5' from Joe"))
end
end
end
Expand Down

0 comments on commit 70bf0df

Please sign in to comment.