forked from fundakol/pytest-jira-xray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevidence.py
36 lines (25 loc) · 994 Bytes
/
evidence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from typing import AnyStr, Dict
import base64
from pytest_xray.exceptions import XrayError
# Content Types
IMAGE_JPEG: str = 'image/jpeg'
IMAGE_PNG: str = 'image/png'
PLAIN_TEXT: str = 'plain/text'
def evidence(data: AnyStr, filename: str, content_type: str) -> Dict[str, str]:
if isinstance(data, bytes):
data_base64: str = base64.b64encode(data).decode('utf-8')
elif isinstance(data, str):
data_base64: str = base64.b64encode(data.encode('utf-8')).decode('utf-8')
else:
raise XrayError('data must be string or bytes')
return {
'data': data_base64,
'filename': filename,
'ContentType': content_type
}
def jpeg(data: AnyStr, filename: str) -> Dict[str, str]:
return evidence(data, filename, IMAGE_JPEG)
def png(data: AnyStr, filename: str) -> Dict[str, str]:
return evidence(data, filename, IMAGE_PNG)
def text(data: AnyStr, filename: str) -> Dict[str, str]:
return evidence(data, filename, PLAIN_TEXT)