Skip to content

Commit eb5fcc5

Browse files
committed
Save point #5
1 parent 9fbbe12 commit eb5fcc5

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

plugins/cisco_umbrella_destinations/unit_test/mock.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, filename: str, status_code: int, text: str = "") -> None:
2424

2525
def json(self):
2626
with open(
27-
os.path.join(os.path.dirname(os.path.realpath(__file__)), f"responses/{self.filename}.json.resp")
27+
os.path.join(os.path.dirname(os.path.realpath(__file__)), f"responses/{self.filename}.json.resp")
2828
) as file:
2929
return json.load(file)
3030

@@ -35,7 +35,6 @@ def mocked_request(side_effect: Callable) -> None:
3535

3636

3737
def mock_conditions(method: str, url: str, status_code: int) -> MockResponse:
38-
print(method, url, status_code)
3938
base_url = f"https://management.api.umbrella.com/v1/organizations/{STUB_ORG_ID}/destinationlists"
4039
if url == base_url:
4140
if method == "GET":
@@ -74,5 +73,9 @@ def mock_request_403(*args, **kwargs) -> MockResponse:
7473
return mock_conditions(args[0], args[1], 403)
7574

7675

76+
def mock_request_404(*args, **kwargs) -> MockResponse:
77+
return mock_conditions(args[0], args[1], 403)
78+
79+
7780
def mock_request_500(*args, **kwargs) -> MockResponse:
7881
return mock_conditions(args[0], args[1], 500)

plugins/cisco_umbrella_destinations/unit_test/test_dlCreate.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
import sys
22
import os
33

4+
from parameterized import parameterized
5+
46
sys.path.append(os.path.abspath("../"))
57

68
from unittest import TestCase, mock
79
from icon_cisco_umbrella_destinations.connection.connection import Connection
810
from icon_cisco_umbrella_destinations.actions.dlCreate import DlCreate
911
from icon_cisco_umbrella_destinations.actions.dlCreate.schema import Input
10-
import json
12+
from insightconnect_plugin_runtime.exceptions import PluginException
1113
import logging
12-
from unit_test.mock import STUB_CONNECTION, mock_request_200, STUB_DESTINATION_LIST_ID
14+
from unit_test.mock import (
15+
STUB_CONNECTION,
16+
mock_request_200,
17+
mock_request_403,
18+
mock_request_401,
19+
mock_request_500,
20+
mock_request_400,
21+
mock_request_404,
22+
STUB_DESTINATION_LIST_ID,
23+
mocked_request,
24+
)
1325

1426

1527
class TestDlCreate(TestCase):
@@ -28,21 +40,18 @@ def setUp(self) -> None:
2840
"name": "DELETEME2",
2941
}
3042

43+
self.params = {Input.ACCESS: "allow", Input.ISGLOBAL: False, Input.LABEL: "DELETEME2"}
44+
3145
@mock.patch("requests.request", side_effect=mock_request_200)
3246
def test_successful(self, mock_post):
33-
response = self.action.run({
34-
Input.ACCESS: 'allow',
35-
Input.ISGLOBAL: False,
36-
Input.LABEL: 'DELETEME2'
37-
})
47+
response = self.action.run({Input.ACCESS: "allow", Input.ISGLOBAL: False, Input.LABEL: "DELETEME2"})
3848
expected_response = {
3949
"success": {
4050
"id": 15786904,
4151
"organizationId": 2372338,
4252
"access": "allow",
4353
"isGlobal": False,
4454
"name": "DELETEME2",
45-
"thirdpartyCategoryId": None,
4655
"createdAt": "2022-02-10T11:01:20+0000",
4756
"modifiedAt": "2022-02-10T11:01:20+0000",
4857
"isMspDefault": False,
@@ -52,3 +61,18 @@ def test_successful(self, mock_post):
5261
}
5362
}
5463
self.assertEqual(response, expected_response)
64+
65+
@parameterized.expand(
66+
[
67+
(mock_request_401, PluginException.Preset.USERNAME_PASSWORD),
68+
(mock_request_403, PluginException.Preset.UNAUTHORIZED),
69+
(mock_request_404, PluginException.Preset.UNAUTHORIZED),
70+
(mock_request_500, PluginException.Preset.SERVER_ERROR),
71+
],
72+
)
73+
def test_not_ok(self, mock_request, exception):
74+
mocked_request(mock_request)
75+
76+
with self.assertRaises(PluginException) as context:
77+
self.action.run(self.params)
78+
self.assertEqual(context.exception.cause, PluginException.causes[exception])

plugins/cisco_umbrella_destinations/unit_test/test_dlGet.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
import sys
22
import os
3+
from parameterized import parameterized
34

45
sys.path.append(os.path.abspath("../"))
56

67
from unittest import TestCase, mock
78
from icon_cisco_umbrella_destinations.connection.connection import Connection
89
from icon_cisco_umbrella_destinations.actions.dlGet import DlGet
910
from icon_cisco_umbrella_destinations.actions.dlGet.schema import Input
10-
import json
11+
from insightconnect_plugin_runtime.exceptions import PluginException
1112
import logging
1213

13-
from unit_test.mock import STUB_CONNECTION, mock_request_200, STUB_DESTINATION_LIST_ID
14+
from unit_test.mock import (
15+
STUB_CONNECTION,
16+
mock_request_200,
17+
mock_request_403,
18+
mock_request_401,
19+
mock_request_500,
20+
mock_request_400,
21+
mock_request_404,
22+
STUB_DESTINATION_LIST_ID,
23+
mocked_request,
24+
)
1425

1526

1627
class TestDlGet(TestCase):
@@ -48,3 +59,18 @@ def test_destination_list_get_success(self, mock_get):
4859
}
4960
}
5061
self.assertEqual(response, expected_response)
62+
63+
@parameterized.expand(
64+
[
65+
(mock_request_401, PluginException.Preset.USERNAME_PASSWORD),
66+
(mock_request_403, PluginException.Preset.UNAUTHORIZED),
67+
(mock_request_404, PluginException.Preset.UNAUTHORIZED),
68+
(mock_request_500, PluginException.Preset.SERVER_ERROR),
69+
],
70+
)
71+
def test_not_ok(self, mock_request, exception):
72+
mocked_request(mock_request)
73+
74+
with self.assertRaises(PluginException) as context:
75+
self.action.run({Input.DESTINATIONLISTID: STUB_DESTINATION_LIST_ID})
76+
self.assertEqual(context.exception.cause, PluginException.causes[exception])

0 commit comments

Comments
 (0)