Newer
Older
from http import HTTPStatus
class Checks:
def __init__(self):
self.checks = {
'Check Response Status Code': Checks.check_response_status_code,
'Check Response Body Containing Array Of URIs set to': Checks.check_response_body_containing_array_of_uris_set_to,
'Check Created Resources Set To': Checks.check_created_resources_set_to,
'Check Response Headers Containing Content-Type set to': Checks.check_response_headers_containing_content_type_set_to,
'Check Response Headers Link Not Empty':Checks.check_response_headers_link_not_empty,
'Check Response Headers Containing URI set to': Checks.check_response_headers_containing_uri_set_to,
'Check Response Headers ID Not Empty': Checks.check_response_headers_id_not_empty,
'Check Response Body Containing an Attribute set to': Checks.check_response_body_containing_an_attribute_set_to
}
@staticmethod
def check_response_status_code(kwargs: list) -> str:
if "status_code" in kwargs:
status_code = kwargs['status_code']
return f'Response Status Code set to {status_code} ({HTTPStatus(status_code).phrase})'
else:
raise Exception(f'ERROR, Expected status_code parameter but received: {kwargs}')
@staticmethod
def check_response_body_containing_array_of_uris_set_to(kwargs: list) -> str:
return 'Response Body set to an array of created entities ids'
@staticmethod
def check_created_resources_set_to(kwargs: list) -> str:
return 'Created resources set to ${entities}'
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
@staticmethod
def check_response_headers_containing_content_type_set_to(kwargs: list) -> str:
if "content_type" in kwargs:
content_type = kwargs['content_type']
return f'Response Header: Content-Type set to {content_type}'
else:
raise Exception(f'ERROR, Expected status_code parameter but received: {kwargs}')
@staticmethod
def check_response_headers_link_not_empty(kwargs: list) -> str:
return f'Response Header: Link is not Empty'
@staticmethod
def check_response_headers_containing_uri_set_to(kwargs: list) -> str:
return 'Response Header: Location containing ${registration_id}'
@staticmethod
def check_response_headers_id_not_empty(kwargs: list) -> str:
return 'Response Header: Location is not Empty'
@staticmethod
def check_response_body_containing_an_attribute_set_to(kwargs: list) -> str:
if "attribute_name" in kwargs:
attribute_name = kwargs['attribute_name']
if "attribute_value" in kwargs:
attribute_value = kwargs['attribute_value']
result = f"Response Body contains the attribute: '{attribute_name}', with the value: '{attribute_value}'"
else:
result = f"Response Body contains the attribute: '{attribute_name}'"
return result
else:
raise Exception(f'ERROR, Expected status_code parameter but received: {kwargs}')
def get_checks(self, **kwargs) -> str:
checking = None
if "checks" in kwargs:
checking = kwargs["checks"]
else:
raise Exception(f'ERROR, the attribute checks is mandatory, but received: {kwargs}')
if isinstance(checking, str):
result = self.checks[checking](kwargs)
elif isinstance(checking, list):
result = [self.checks[x](kwargs) for x in checking]
result = " and\n".join(result)
else:
raise Exception(f"ERROR, checks type not supported: {checking}")
return result
"""
then {
the SUT sends a valid Response containing
Response Status Code set to 201 (Created) and
Response Body set to an array of created entities ids
and created resources set to ${entities}
}
then {
the SUT sends a valid Response containing
Response Status Code set to ${status_code}
Response Body containing
${appended_attrs_list}
and contains ${entity} with ${appended_attrs_list}
}
"""
"""
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
Check Response Body Containing Batch Operation Result
Check Response Body Containing Entity element
Check Response Body Containing List Containing Entity Elements
Check Response Body Containing List Containing Entity Elements With Different Types
Check Response Body Containing EntityTemporal element
Check Response Body Containing List Containing EntityTemporal elements
Check Response Body Containing Subscription element
Check Response Body Containing List Containing Subscription elements
Check Response Body Containing Number Of Entities
Check Response Body Containing Context Source Registration element
Check Response Body Containing EntityTypeList element
Check Response Body Containing EntityType element
Check Response Body Containing EntityTypeInfo element
Check Response Body Containing AttributeList element
Check Response Body Containing Attribute element
Check Response Body Containing List Containing Context Source Registrations elements
Check Response Body Type When Using Session Request
Check Response Body Containing ProblemDetails Element Containing Type Element set to
Check Response Body Title When Using Session Request
Check Response Body Containing ProblemDetails Element Containing Title Element
Check RL Response Body Containing ProblemDetails Element Containing Type Element set to
Check RL Response Body Containing ProblemDetails Element Containing Title Element
Check JSON Value In Response Body
Check NotificationParams
Check Pagination Prev And Next Headers
Check Resource Set To
Check Created Resource Set To
Check Updated Resource Set To
Check Updated Resources Set To
Check SUT Not Containing Resource
Check SUT Not Containing Resources
"""
if __name__ == "__main__":
data = Checks()
print(data.get_checks(checks='Check Response Status Code', status_code=201))
print(data.get_checks(checks='Check Response Body Containing Array Of URIs set to'))
print(data.get_checks(checks='Check Created Resources Set To'))
print(data.get_checks(checks='Check Response Headers Containing Content-Type set to', content_type='application/json'))
print(data.get_checks(checks='Check Response Headers Link Not Empty'))
print(data.get_checks(checks='Check Response Headers Containing URI set to'))
print(data.get_checks(checks='Check Response Headers ID Not Empty'))
print(data.get_checks(checks='Check Response Body Containing an Attribute set to', attribute_name='status'))
print(data.get_checks(checks='Check Response Body Containing an Attribute set to', attribute_name='status', attribute_value='active'))
print()
print(data.get_checks(checks=
['Check Response Status Code',
'Check Response Body Containing Array Of URIs set to',
'Check Created Resources Set To']
, status_code=201))