-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from typing import List | ||
|
||
import pytest | ||
from thehive4py.client import TheHiveApi | ||
from thehive4py.errors import TheHiveError | ||
from thehive4py.types.case_template import InputCaseTemplate, OutputCaseTemplate | ||
|
||
|
||
class TestCaseTemplateEndpoint: | ||
def test_create_and_get(self, thehive: TheHiveApi): | ||
created_case_template = thehive.case_template.create( | ||
case_template={ | ||
"name": "my first template", | ||
"description": "Template description", | ||
} | ||
) | ||
fetched_case_template = thehive.case_template.get(created_case_template["_id"]) | ||
assert created_case_template == fetched_case_template | ||
|
||
def test_update(self, thehive: TheHiveApi, test_case_template: OutputCaseTemplate): | ||
case_template_id = test_case_template["_id"] | ||
update_fields: InputCaseTemplate = { | ||
"name": "updated template name", | ||
"description": "updated template description", | ||
} | ||
thehive.case_template.update( | ||
case_template_id=case_template_id, fields=update_fields | ||
) | ||
updated_case_template = thehive.case_template.get( | ||
case_template_id=case_template_id | ||
) | ||
|
||
for key, value in update_fields.items(): | ||
assert updated_case_template.get(key) == value | ||
|
||
def test_update_with_wrong_argument_error( | ||
self, thehive: TheHiveApi, test_case_template: OutputCaseTemplate | ||
): | ||
case_template_id = test_case_template["_id"] | ||
update_fields: InputCaseTemplate = { | ||
"name": "updated template name", | ||
"description": "updated template description", | ||
} | ||
wrong_kwargs = {"template_fields": update_fields, "wrong_arg": "value"} | ||
with pytest.raises(TheHiveError, match=rf".*{list(wrong_kwargs.keys())}.*"): | ||
thehive.case_template.update(case_template_id=case_template_id, **wrong_kwargs) # type: ignore | ||
|
||
def test_delete(self, thehive: TheHiveApi, test_case_template: OutputCaseTemplate): | ||
case_template_id = test_case_template["_id"] | ||
thehive.case_template.delete(case_template_id=case_template_id) | ||
with pytest.raises(TheHiveError): | ||
thehive.case_template.get(case_template_id=case_template_id) | ||
|
||
def test_find( | ||
self, | ||
thehive: TheHiveApi, | ||
test_case_templates: List[OutputCaseTemplate], | ||
): | ||
filters = {"name": "my first template"} | ||
found_templates = thehive.case_template.find(filters=filters) | ||
names = [template["name"] for template in found_templates] | ||
for test_template in test_case_templates: | ||
assert test_template["name"] in names |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from thehive4py.endpoints._base import EndpointBase | ||
from thehive4py.query import QueryExpr | ||
from thehive4py.query.filters import FilterExpr | ||
from thehive4py.query.page import Paginate | ||
from thehive4py.query.sort import SortExpr | ||
from thehive4py.types.case_template import OutputCaseTemplate, InputCaseTemplate | ||
from typing import List, Optional | ||
|
||
|
||
class CaseTemplateEndpoint(EndpointBase): | ||
def find( | ||
self, | ||
filters: Optional[FilterExpr] = None, | ||
sortby: Optional[SortExpr] = None, | ||
paginate: Optional[Paginate] = None, | ||
) -> List[OutputCaseTemplate]: | ||
query: QueryExpr = [ | ||
{"_name": "listCaseTemplate"}, | ||
*self._build_subquery(filters=filters, sortby=sortby, paginate=paginate), | ||
] | ||
|
||
return self._session.make_request( | ||
"POST", | ||
path="/api/v1/query", | ||
json={"query": query}, | ||
params={"name": "caseTemplate"}, | ||
) | ||
|
||
def get(self, case_template_id: str) -> OutputCaseTemplate: | ||
return self._session.make_request( | ||
"GET", path=f"/api/v1/caseTemplate/{case_template_id}" | ||
) | ||
|
||
def create(self, case_template: InputCaseTemplate) -> OutputCaseTemplate: | ||
return self._session.make_request( | ||
"POST", path="/api/v1/caseTemplate", json=case_template | ||
) | ||
|
||
def delete(self, case_template_id: str) -> None: | ||
self._session.make_request( | ||
"DELETE", path=f"/api/v1/caseTemplate/{case_template_id}" | ||
) | ||
|
||
def update(self, case_template_id: str, fields: InputCaseTemplate) -> None: | ||
return self._session.make_request( | ||
"PATCH", path=f"/api/v1/caseTemplate/{case_template_id}", json=fields | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from typing import List, Literal, TypedDict, Union | ||
|
||
from .custom_field import InputCustomFieldValue | ||
from .task import InputCreateTask | ||
|
||
SeverityValue = Literal[1, 2, 3, 4] | ||
TlpValue = Literal[0, 1, 2, 3, 4] | ||
PapValue = Literal[0, 1, 2, 3] | ||
|
||
|
||
class InputCaseTemplateRequired(TypedDict): | ||
name: str | ||
|
||
|
||
class InputCaseTemplate(InputCaseTemplateRequired, total=False): | ||
displayName: str | ||
titlePrefix: str | ||
description: str | ||
severity: SeverityValue | ||
tags: List[str] | ||
flag: bool | ||
tlp: TlpValue | ||
pap: PapValue | ||
summary: str | ||
tasks: List[InputCreateTask] | ||
pageTemplateIds: List[str] | ||
customFields: Union[dict, List[InputCustomFieldValue]] | ||
|
||
|
||
class OutputCaseTemplateRequired(TypedDict): | ||
_id: str | ||
_type: str | ||
_createdBy: str | ||
_createdAt: int | ||
name: str | ||
|
||
|
||
class OutputCaseTemplate(OutputCaseTemplateRequired, total=False): | ||
_updatedBy: str | ||
_updatedAt: int | ||
displayName: str | ||
titlePrefix: str | ||
description: str | ||
severity: SeverityValue | ||
tags: List[str] | ||
flag: bool | ||
tlp: TlpValue | ||
pap: PapValue | ||
summary: str | ||
tasks: List[InputCreateTask] | ||
pageTemplateIds: List[str] | ||
customFields: Union[dict, List[InputCustomFieldValue]] |