Skip to content

Commit

Permalink
fixed a KeyError bug (#8979)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaakovi authored Sep 16, 2020
1 parent b06cce9 commit 8b03895
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 25 deletions.
5 changes: 5 additions & 0 deletions Packs/CommonScripts/ReleaseNotes/1_2_43.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

#### Scripts
##### WhereFieldEquals
- Fixed an issue where the transformer failed on KeyError.
- Updated docker image.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ def where_field_equals(args):
get_field = args.get('getField')
found_matches = []
for dict_item in values_to_search:
if dict_item.get(field, None) == equal_to:
if dict_item.get(field) == equal_to:
if get_field:
found_matches.append(dict_item[get_field])
value = dict_item.get(get_field)
if value:
found_matches.append(value)
else:
found_matches.append(dict_item)
if len(found_matches) == 1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ tags:
- entirelist
timeout: '0'
type: python
dockerimage: demisto/python3:3.8.5.10455
dockerimage: demisto/python3:3.8.5.10845
runonce: false
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@


class WhereFieldEquals(unittest.TestCase):
def test_where_field_equals_hebrew(self):
@staticmethod
def test_where_field_equals_hebrew():
"""
Given: list of dictionaries containing hebrew characters
When: Finding fields that contain the given value under the given key.
Expand All @@ -22,11 +23,12 @@ def test_where_field_equals_hebrew(self):
"name"
}
expected_result = '["מה זה","172.0.0.2"]'
recieved_result = where_field_equals(args)
received_result = where_field_equals(args)

assert expected_result == recieved_result
assert expected_result == received_result

def test_where_field_equals_latin_i(self):
@staticmethod
def test_where_field_equals_latin_i():
"""
Given: list of dictionaries containing the latin ł characters
When: Finding fields that contain the given value under the given key.
Expand All @@ -45,31 +47,55 @@ def test_where_field_equals_latin_i(self):
"name"
}
expected_result = '["łłłłł","172.0.0.2"]'
recieved_result = where_field_equals(args)
assert expected_result == recieved_result
received_result = where_field_equals(args)
assert expected_result == received_result

def test_where_field_equals_missing_get_field(self):
@staticmethod
def test_where_field_equals_missing_get_field():
"""
Given: list of dictionaries while missing the getField argument.
When: Finding fields that contain the given value under the given key.
Then: Return properly formatted tuple containing a human readable representation
and a dictionary containing the correct context.
"""
args = {
"value":
'[{ "name": "Testing", "type": "IP" }, { "name": "myFile.txt", "type": '
'"File" }, { "name": "172.0.0.2", "type": "IP" }]',
"field":
"type",
"equalTo":
"IP"
"value": '[{ "name": "Testing", "text": "Hello", "type": "IP" }, '
'{ "name": "myFile.txt", "type": "IP" }, '
'{ "name": "172.0.0.2", "text": "World", "type": "IP" }]',
"field": "type",
"equalTo": "IP",
"getField": "text",
}
expected_result = '["Hello","World"]'

received_result = where_field_equals(args)
assert expected_result == received_result

@staticmethod
def test_where_field_equals_json_value():
"""
Given: list of dictionaries while missing the getField argument.
When: Finding fields that contain the given value under the given key.
Then: Return properly formatted tuple containing a human readable representation
and a dictionary containing the correct context.
"""
args = {
"value": [
{"name": "Testing", "text": "Hello", "type": "IP"},
{"name": "myFile.txt", "type": "IP"},
{"name": "172.0.0.2", "text": "World", "type": "IP"},
],
"field": "type",
"equalTo": "IP",
"getField": "text",
}
expected_result = '[{"name":"Testing","type":"IP"},{"name":"172.0.0.2","type":"IP"}]'
expected_result = '["Hello","World"]'

recieved_result = where_field_equals(args)
assert expected_result == recieved_result
received_result = where_field_equals(args)
assert expected_result == received_result

def test_where_field_equals_malformed(self):
@staticmethod
def test_where_field_equals_malformed():
"""
Given: list of dictionaries where some keys are not present in all dictionaries.
When: Finding fields that contain the given value under the given key.
Expand All @@ -88,5 +114,5 @@ def test_where_field_equals_malformed(self):
"name"
}
expected_result = "מה זה"
recieved_result = where_field_equals(args)
assert expected_result == recieved_result
received_result = where_field_equals(args)
assert expected_result == received_result
4 changes: 2 additions & 2 deletions Packs/CommonScripts/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Common Scripts",
"description": "Frequently used scripts pack.",
"support": "xsoar",
"currentVersion": "1.2.42",
"currentVersion": "1.2.43",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down Expand Up @@ -41,4 +41,4 @@
"display_name": "Microsoft Graph Mail Single User"
}
}
}
}

0 comments on commit 8b03895

Please sign in to comment.