Skip to content

Commit a124d82

Browse files
authored
Verify attribute value length before sending to RP
1 parent 7a01835 commit a124d82

File tree

6 files changed

+53
-15
lines changed

6 files changed

+53
-15
lines changed

reportportal_client/helpers.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
"""
1414
import logging
1515
import uuid
16+
from pkg_resources import DistributionNotFound, get_distribution
1617
from platform import machine, processor, system
1718

1819
import six
19-
from pkg_resources import DistributionNotFound, get_distribution
20+
21+
from .static.defines import ATTRIBUTE_LENGTH_LIMIT
2022

2123
logger = logging.getLogger(__name__)
2224

@@ -106,3 +108,27 @@ def get_package_version(package_name):
106108
except DistributionNotFound:
107109
package_version = 'not found'
108110
return package_version
111+
112+
113+
def verify_value_length(attributes):
114+
"""Verify length of the attribute value.
115+
116+
The length of the attribute value should have size from '1' to '128'.
117+
Otherwise HTTP response will return an error.
118+
Example of the input list:
119+
[{'key': 'tag_name', 'value': 'tag_value1'}, {'value': 'tag_value2'}]
120+
:param attributes: List of attributes(tags)
121+
:return: List of attributes with corrected value length
122+
"""
123+
if attributes is not None:
124+
for pair in attributes:
125+
if not isinstance(pair, dict):
126+
continue
127+
attr_value = pair.get('value')
128+
if attr_value is None:
129+
continue
130+
try:
131+
pair['value'] = attr_value[:ATTRIBUTE_LENGTH_LIMIT]
132+
except TypeError:
133+
continue
134+
return attributes

reportportal_client/helpers.pyi

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
from logging import Logger
2-
from typing import Dict, List, Text
2+
from typing import Dict, List, Text, Union
33

44
logger: Logger
55

6+
def generate_uuid() -> Text: ...
67

7-
def generate_uuid() -> str: ...
8-
9-
10-
def convert_string(value: str) -> str: ...
11-
8+
def convert_string(value: Text) -> Text: ...
129

1310
def dict_to_payload(value: Dict) -> List[Dict]: ...
1411

15-
16-
def gen_attributes(rp_attributes: List) -> List[Dict]: ...
12+
def gen_attributes(rp_attributes: List[Dict]) -> List[Dict]: ...
1713

1814
def get_launch_sys_attrs() -> Dict[Text]: ...
1915

2016
def get_package_version(package_name: Text) -> Text: ...
17+
18+
def verify_value_length(
19+
attributes: Union[List[Dict], None]) -> Union[List[Dict], None]: ...

reportportal_client/service.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from requests.adapters import HTTPAdapter
2727

2828
from .errors import ResponseError, EntryCreatedError, OperationCompletionError
29+
from .helpers import verify_value_length
2930

3031
POST_LOGBATCH_RETRY_COUNT = 10
3132
logger = logging.getLogger(__name__)
@@ -225,7 +226,7 @@ def start_launch(self,
225226
data = {
226227
"name": name,
227228
"description": description,
228-
"attributes": attributes,
229+
"attributes": verify_value_length(attributes),
229230
"startTime": start_time,
230231
"mode": mode,
231232
"rerun": rerun,
@@ -251,7 +252,7 @@ def finish_launch(self, end_time, status=None, attributes=None, **kwargs):
251252
data = {
252253
"endTime": end_time,
253254
"status": status,
254-
"attributes": attributes
255+
"attributes": verify_value_length(attributes)
255256
}
256257
url = uri_join(self.base_url_v2, "launch", self.launch_id, "finish")
257258
r = self.session.put(url=url, json=data, verify=self.verify_ssl)
@@ -346,7 +347,7 @@ def start_test_item(self,
346347
data = {
347348
"name": name,
348349
"description": description,
349-
"attributes": attributes,
350+
"attributes": verify_value_length(attributes),
350351
"startTime": start_time,
351352
"launchUuid": self.launch_id,
352353
"type": item_type,
@@ -374,7 +375,7 @@ def update_test_item(self, item_uuid, attributes=None, description=None):
374375
"""
375376
data = {
376377
"description": description,
377-
"attributes": attributes,
378+
"attributes": verify_value_length(attributes),
378379
}
379380
item_id = self.get_item_id_by_uuid(item_uuid)
380381
url = uri_join(self.base_url_v1, "item", item_id, "update")
@@ -413,7 +414,7 @@ def finish_test_item(self,
413414
"status": status,
414415
"issue": issue,
415416
"launchUuid": self.launch_id,
416-
"attributes": attributes
417+
"attributes": verify_value_length(attributes)
417418
}
418419
url = uri_join(self.base_url_v2, "item", item_id)
419420
r = self.session.put(url=url, json=data, verify=self.verify_ssl)

reportportal_client/static/defines.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class Priority(enum.IntEnum):
7878
PRIORITY_LOW = 0x3
7979

8080

81+
ATTRIBUTE_LENGTH_LIMIT = 128
8182
DEFAULT_PRIORITY = Priority.PRIORITY_MEDIUM
8283
NOT_FOUND = _PresenceSentinel()
8384
NOT_SET = _PresenceSentinel()

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ requests>=2.23.0
22
six>=1.15.0
33
pytest
44
delayed-assert
5+
enum34

tests/test_helpers.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from reportportal_client.helpers import (
66
gen_attributes,
77
get_launch_sys_attrs,
8-
get_package_version
8+
get_package_version,
9+
verify_value_length
910
)
1011

1112

@@ -49,3 +50,12 @@ def test_get_launch_sys_attrs_docker():
4950
def test_get_package_version():
5051
"""Test for the get_package_version() function-helper."""
5152
assert get_package_version('noname') == 'not found'
53+
54+
55+
def test_verify_value_length():
56+
"""Test for validate verify_value_length() function."""
57+
inputl = [{'key': 'tn', 'value': 'v' * 130}, [1, 2],
58+
{'value': 'tv2'}, {'value': 300}]
59+
expected = [{'key': 'tn', 'value': 'v' * 128}, [1, 2],
60+
{'value': 'tv2'}, {'value': 300}]
61+
assert verify_value_length(inputl) == expected

0 commit comments

Comments
 (0)