Skip to content

Commit b09388b

Browse files
kaiseroOliver Kaiser
and
Oliver Kaiser
authored
v0.1.4 (#27)
* changed version pinning (v0.1.1) * fixed incorrect positional args (#23) * fixed incorrect api path (#24) * updated version * updated changelog * fixed conversion bug in _request * cleanup and additional tests * fixed an error in payload sanitization for put operations * added tests for object crud operations * added additional tests for helper functions * added create_object test * updated version to 0.1.4 Co-authored-by: Oliver Kaiser <ok@ong.at>
1 parent 357bf41 commit b09388b

File tree

8 files changed

+151
-139
lines changed

8 files changed

+151
-139
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# 0.1.4
2+
3+
## Bugfixes
4+
5+
Added a fix to correctly sanitize payloads for put operations
6+
Corrected cache_result condition that did not match correctly
7+
8+
## Enhancements
9+
10+
Added various tests for better qa
11+
112
# 0.1.3
213

314
## Bugfixes

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ mypy = "==0.770"
2323
pre-commit = "==2.2.0"
2424
pytest = "==5.4.1"
2525
pytest-cov = "==2.8.1"
26+
pytest-moc = "==3.2.0"
2627
pytest-runner = "==5.2"
2728
tox = "==3.14.6"
2829
twine = "==3.1.1"

fireREST/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# -*- coding: utf-8 -*-
1+
# -*- coding: utf-8 -*-
22

33
import json
44
import logging
@@ -181,7 +181,7 @@ def _create(self, url: str, data: Dict, params=None):
181181
: param params: dict of parameters for http request
182182
: return: requests.Response object
183183
'''
184-
data = self._sanitize(data)
184+
data = self._sanitize('post', data)
185185
return self._request('post', url, params=params, data=data)
186186

187187
def _update(self, url: str, data: Dict, params=None):
@@ -192,10 +192,10 @@ def _update(self, url: str, data: Dict, params=None):
192192
: param params: dict of parameters for http request
193193
: return: requests.Response object
194194
'''
195-
data = self._sanitize(data)
196-
return self._request(url, data=data, params=params)
195+
data = self._sanitize('put', data)
196+
return self._request('put', url, data=data, params=params)
197197

198-
def _sanitize(self, payload: Dict):
198+
def _sanitize(self, method: str, payload: Dict):
199199
'''
200200
Prepare json object for api operation
201201
This is neccesarry since fmc api cannot handle json objects with some
@@ -206,10 +206,12 @@ def _sanitize(self, payload: Dict):
206206
sanitized_payload = deepcopy(payload)
207207
sanitized_payload.pop('metadata', None)
208208
sanitized_payload.pop('links', None)
209-
sanitized_payload.pop('id', None)
209+
if method == 'post':
210+
sanitized_payload.pop('id', None)
210211
return sanitized_payload
211212

212-
@utils.cache_result
213+
# @utils.cache_result
214+
@utils.validate_object_type
213215
@utils.minimum_version_required('6.1.0')
214216
def get_object_id_by_name(self, object_type: str, object_name: str):
215217
'''

fireREST/decorators.py

Lines changed: 0 additions & 107 deletions
This file was deleted.

fireREST/utils.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,21 @@ def cache_result(f):
4646
decorator that applies functools lru_cache if cache is enabled in Client object
4747
'''
4848

49-
def enabled(*args, **kwargs):
50-
return args[0].cache
51-
52-
if enabled:
49+
def inner_function(f):
50+
@lru_cache(maxsize=256)
51+
def cache(*args, **kwargs):
52+
return f(*args, **kwargs)
5353

5454
@wraps(f)
5555
@lru_cache(maxsize=256)
5656
def wrapper(*args, **kwargs):
57+
if args[0].cache is True:
58+
return cache
5759
return f(*args, **kwargs)
5860

5961
return wrapper
60-
return f
62+
63+
return inner_function
6164

6265

6366
def log_request(action):

fireREST/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.1.3'
1+
__version__ = '0.1.4'

test/test_fireREST.py

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,82 @@ def test_initialization(api, constants):
2121
assert api.protocol == expected_protocol
2222
assert api.verify_cert == expected_verify_cert
2323
assert api.timeout == expected_timeout
24-
assert api.domain_name == expected_domain_name
24+
assert api.domain_name == expected_domain_name
25+
26+
27+
def test_get_domain_id_by_name_with_correct_name(api):
28+
expected_result = api.domain
29+
actual_result = api.get_domain_id_by_name(api.domain_name)
30+
31+
assert expected_result == actual_result
32+
33+
34+
def test_get_domain_id_by_name_with_incorrect_name(api):
35+
expected_result = None
36+
actual_result = api.get_domain_id_by_name('NON-EXISTING-DOMAIN')
37+
38+
assert expected_result == actual_result
39+
40+
41+
def test_get_domain_name_by_id_with_correct_id(api):
42+
expected_result = 'Global'
43+
actual_result = api.get_domain_name_by_id(api.domain)
44+
45+
assert expected_result == actual_result
46+
47+
48+
def test_get_domain_name_by_id_with_incorrect_id(api):
49+
expected_result = None
50+
actual_result = api.get_domain_name_by_id('NON-EXISTING-DOMAIN-ID')
51+
52+
assert expected_result == actual_result
53+
54+
55+
def test_create_object(api):
56+
payload = {
57+
'name': 'firerest_test_netobj',
58+
'value': '198.18.0.0/24',
59+
}
60+
61+
actual_result = api.create_object('network', payload).status_code
62+
expected_result = 201
63+
64+
assert expected_result == actual_result
65+
66+
67+
def test_get_object(api):
68+
object_id = api.get_object_id_by_name('network', 'firerest_test_netobj')
69+
expected_object = {
70+
'id': object_id,
71+
'name': 'firerest_test_netobj',
72+
'value': '198.18.0.0/24',
73+
}
74+
actual_object = api.get_object('network', object_id)
75+
76+
assert expected_object['id'] == actual_object['id']
77+
assert expected_object['name'] == actual_object['name']
78+
assert expected_object['value'] == actual_object['value']
79+
80+
81+
def test_update_object(api):
82+
expected_response = 200
83+
expected_description = 'test_update_object'
84+
85+
object_id = api.get_object_id_by_name('network', 'firerest_test_netobj')
86+
payload = api.get_object('network', object_id)
87+
payload['description'] = expected_description
88+
89+
actual_response = api.update_object('network', object_id, payload).status_code
90+
actual_description = api.get_object('network', object_id)['description']
91+
92+
assert expected_response == actual_response
93+
assert expected_description == actual_description
94+
95+
96+
def test_delete_object(api):
97+
object_id = api.get_object_id_by_name('network', 'firerest_test_netobj')
98+
99+
actual_result = api.delete_object('network', object_id).status_code
100+
expected_result = 200
101+
102+
assert expected_result == actual_result

test/utils/test_utils.py

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from uuid import uuid4
66

7-
from fireREST import utils
7+
from fireREST import exceptions, utils
88

99

1010
def test_is_uuid_with_valid_uuid():
@@ -17,7 +17,7 @@ def test_is_uuid_with_valid_uuid():
1717

1818

1919
def test_is_uuid_with_invalid_uuid():
20-
invalid_uuid = str('override')
20+
invalid_uuid = str('invalid-uuid')
2121
expected_result = False
2222

2323
actual_result = utils.is_uuid(invalid_uuid)
@@ -26,9 +26,11 @@ def test_is_uuid_with_invalid_uuid():
2626

2727

2828
def test_is_getbyid_operation_with_valid_getbyid_operation():
29-
valid_getbyid_operation = 'https://localhost' \
30-
'/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f' \
31-
'/object/networkgroups/A09351F4-691E-0ed3-0000-034359739130'
29+
valid_getbyid_operation = (
30+
'https://localhost'
31+
'/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f'
32+
'/object/networkgroups/A09351F4-691E-0ed3-0000-034359739130'
33+
)
3234
expected_result = True
3335

3436
actual_result = utils.is_getbyid_operation(valid_getbyid_operation)
@@ -37,9 +39,11 @@ def test_is_getbyid_operation_with_valid_getbyid_operation():
3739

3840

3941
def test_is_getbyid_operation_with_valid_getbyid_operation_with_params():
40-
valid_getbyid_operation = 'https://localhost' \
41-
'/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f' \
42-
'/object/networkgroups/A09351F4-691E-0ed3-0000-034359739130?expanded=True'
42+
valid_getbyid_operation = (
43+
'https://localhost'
44+
'/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f'
45+
'/object/networkgroups/A09351F4-691E-0ed3-0000-034359739130?expanded=True'
46+
)
4347
expected_result = True
4448

4549
actual_result = utils.is_getbyid_operation(valid_getbyid_operation)
@@ -48,20 +52,40 @@ def test_is_getbyid_operation_with_valid_getbyid_operation_with_params():
4852

4953

5054
def test_is_getbyid_operation_with_invalid_getbyid_operation():
51-
valid_getbyid_operation = 'https://localhost' \
52-
'/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f' \
53-
'/object/networkgroups/A09351F4-691E-0ed3-0000-034359739130/overrides'
55+
valid_getbyid_operation = (
56+
'https://localhost'
57+
'/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f'
58+
'/object/networkgroups/A09351F4-691E-0ed3-0000-034359739130/overrides'
59+
)
5460
expected_result = False
5561

5662
actual_result = utils.is_getbyid_operation(valid_getbyid_operation)
5763

5864
assert expected_result == actual_result
5965

6066

61-
# def test_rate_limit_retry(api):
62-
# for i in range(1, 121, 1):
63-
# response = api.get_system_version()
64-
#
65-
# expected_result = 200
66-
#
67-
# assert expected_result == response.status_code
67+
def test_validate_data_with_supported_payload_size():
68+
class ValidObject(object):
69+
def __init__(self):
70+
pass
71+
72+
def __sizeof__(self):
73+
return 1024
74+
75+
expected_result = None
76+
actual_result = utils.validate_data('post', ValidObject())
77+
78+
assert expected_result == actual_result
79+
80+
81+
def test_validate_data_with_unsupported_payload_size():
82+
with pytest.raises(exceptions.PayloadLimitExceededError):
83+
84+
class ValidObject(object):
85+
def __init__(self):
86+
pass
87+
88+
def __sizeof__(self):
89+
return 204800100
90+
91+
utils.validate_data('post', ValidObject())

0 commit comments

Comments
 (0)