Skip to content

Commit

Permalink
fix: resolves #175 - deleting a ZIA URL category will empty all other…
Browse files Browse the repository at this point in the history
… custom category fields

feat: implements a new ZIA URL category method that allows selective deletion of URLs and keywords
test: updates test suite and adds new test
  • Loading branch information
mitchos committed Feb 16, 2023
1 parent d4ba985 commit 20671b9
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 8 deletions.
60 changes: 55 additions & 5 deletions pyzscaler/zia/url_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,6 @@ def add_url_category(self, name: str, super_category: str, urls: list, **kwargs)
for key, value in kwargs.items():
payload[snake_to_camel(key)] = value

print(payload)

return self._post("urlCategories", json=payload)

def add_tld_category(self, name: str, tlds: list, **kwargs) -> Box:
Expand Down Expand Up @@ -282,7 +280,7 @@ def add_urls_to_category(self, category_id: str, urls: list) -> Box:

def delete_urls_from_category(self, category_id: str, urls: list) -> Box:
"""
Adds URLS to a URL category.
Deletes URLS from a URL category.
Args:
category_id (str):
Expand All @@ -298,9 +296,61 @@ def delete_urls_from_category(self, category_id: str, urls: list) -> Box:
... urls=['example.com'])
"""
current_config = self.get_category(category_id)
payload = {"configuredName": current_config["configured_name"], "urls": urls} # Required for successful call

payload = convert_keys(self.get_category(category_id))
payload["urls"] = urls
return self._put(f"urlCategories/{category_id}?action=REMOVE_FROM_LIST", json=payload)

def delete_from_category(self, category_id: str, **kwargs):
"""
Deletes the specified items from a URL category.
Args:
category_id (str):
The unique id for the URL category.
**kwargs:
Optional parameters.
Keyword Args:
keywords (list):
A list of keywords that will be deleted.
keywords_retaining_parent_category (list):
A list of keywords retaining their parent category that will be deleted.
urls (list):
A list of URLs that will be deleted.
db_categorized_urls (list):
A list of URLs retaining their parent category that will be deleted
Returns:
:obj:`Box`: The updated URL category resource record.
Examples:
Delete URLs retaining parent category from a custom category:
>>> zia.url_categories.delete_from_category(
... category_id="CUSTOM_01",
... db_categorized_urls=['twitter.com'])
Delete URLs and URLs retaining parent category from a custom category:
>>> zia.url_categories.delete_from_category(
... category_id="CUSTOM_01",
... urls=['news.com', 'cnn.com'],
... db_categorized_urls=['google.com, bing.com'])
"""
current_config = self.get_category(category_id)

payload = {
"configured_name": current_config["configured_name"], # Required for successful call
}

# Add optional parameters to payload
for key, value in kwargs.items():
payload[key] = value

# Convert snake to camelcase
payload = convert_keys(payload)

return self._put(f"urlCategories/{category_id}?action=REMOVE_FROM_LIST", json=payload)

Expand Down
46 changes: 43 additions & 3 deletions tests/zia/test_url_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def fixture_custom_url_categories():
"configuredName": "Test URL",
"customCategory": True,
"customUrlsCount": 2,
"dbCategorizedUrls": [],
"dbCategorizedUrls": ["news.com", "cnn.com"],
"description": "Test",
"editable": True,
"id": "CUSTOM_02",
Expand Down Expand Up @@ -271,8 +271,7 @@ def test_delete_urls_from_category(zia, custom_categories):
json=custom_categories[0],
status=200,
)
received_response = custom_categories[0]
received_response["urls"] = ["example.com"]
received_response = {"configuredName": custom_categories[0]["configuredName"], "urls": ["example.com"]}
responses.add(
method="PUT",
url="https://zsapi.zscaler.net/api/v1/urlCategories/CUSTOM_02?action=REMOVE_FROM_LIST",
Expand Down Expand Up @@ -300,6 +299,47 @@ def test_delete_urls_from_category(zia, custom_categories):
assert resp.urls[0] == "test.example.com"


@responses.activate
def test_delete_from_category(zia, custom_categories):
responses.add(
method="GET",
url="https://zsapi.zscaler.net/api/v1/urlCategories/CUSTOM_02",
json=custom_categories[0],
status=200,
)
received_response = {
"configuredName": custom_categories[0]["configuredName"],
"urls": ["example.com"],
"dbCategorizedUrls": ["news.com"],
}
responses.add(
method="PUT",
url="https://zsapi.zscaler.net/api/v1/urlCategories/CUSTOM_02?action=REMOVE_FROM_LIST",
json={
"configuredName": "Test URL",
"customCategory": True,
"customUrlsCount": 2,
"dbCategorizedUrls": ["cnn.com"],
"description": "Test",
"editable": True,
"id": "CUSTOM_02",
"keywords": [],
"keywordsRetainingParentCategory": [],
"superCategory": "TEST",
"type": "URL_CATEGORY",
"urls": ["test.example.com"],
"urlsRetainingParentCategoryCount": 0,
"val": 129,
},
status=200,
match=[matchers.json_params_matcher(received_response)],
)
resp = zia.url_categories.delete_from_category("CUSTOM_02", urls=["example.com"], db_categorized_urls=["news.com"])
assert isinstance(resp, Box)
assert resp.urls[0] == "test.example.com"
assert resp.db_categorized_urls[0] == "cnn.com"


@responses.activate
def test_delete_url_category(zia):
responses.add(
Expand Down

0 comments on commit 20671b9

Please sign in to comment.