|
| 1 | +from tunsberg.responses import response_bad_request, response_conflict, response_created, response_error, response_forbidden, response_gone, \ |
| 2 | + response_method_not_allowed, response_no_content, response_not_found, response_payload_too_large, response_service_unavailable, response_success, \ |
| 3 | + response_too_many_requests, response_unauthorized, response_unsupported_media_type |
| 4 | + |
| 5 | + |
| 6 | +class TestResponseSuccess: |
| 7 | + |
| 8 | + def test_success_default(self): |
| 9 | + expected_response = ({'status': 'Success', 'message': 'Resource(s) were successfully retrieved', 'data': None}, 200) |
| 10 | + response = response_success() |
| 11 | + assert response == expected_response |
| 12 | + |
| 13 | + def test_success_custom_message(self): |
| 14 | + expected_response = ({'status': 'Success', 'message': 'Custom success message', 'data': None}, 200) |
| 15 | + response = response_success(message='Custom success message') |
| 16 | + assert response == expected_response |
| 17 | + |
| 18 | + def test_success_custom_data(self): |
| 19 | + expected_response = ({'status': 'Success', 'message': 'Resource(s) were successfully retrieved', 'data': {'key': 'value'}}, 200) |
| 20 | + response = response_success(data={'key': 'value'}) |
| 21 | + assert response == expected_response |
| 22 | + |
| 23 | + |
| 24 | +class TestResponseCreated: |
| 25 | + |
| 26 | + def test_created_default(self): |
| 27 | + expected_response = ({'status': 'Created', 'message': 'Resource created successfully', 'data': None}, 201) |
| 28 | + response = response_created() |
| 29 | + assert response == expected_response |
| 30 | + |
| 31 | + def test_created_custom_message(self): |
| 32 | + expected_response = ({'status': 'Created', 'message': 'Custom created message', 'data': None}, 201) |
| 33 | + response = response_created(message='Custom created message') |
| 34 | + assert response == expected_response |
| 35 | + |
| 36 | + def test_created_custom_data(self): |
| 37 | + expected_response = ({'status': 'Created', 'message': 'Resource created successfully', 'data': {'key': 'value'}}, 201) |
| 38 | + response = response_created(data={'key': 'value'}) |
| 39 | + assert response == expected_response |
| 40 | + |
| 41 | + |
| 42 | +class TestResponseNoContent: |
| 43 | + |
| 44 | + def test_no_content_default(self): |
| 45 | + expected_response = ({'status': 'No Content', 'message': 'Resource was successfully deleted', 'data': None}, 204) |
| 46 | + response = response_no_content() |
| 47 | + assert response == expected_response |
| 48 | + |
| 49 | + def test_no_content_custom_message(self): |
| 50 | + expected_response = ({'status': 'No Content', 'message': 'Custom no content message', 'data': None}, 204) |
| 51 | + response = response_no_content(message='Custom no content message') |
| 52 | + assert response == expected_response |
| 53 | + |
| 54 | + |
| 55 | +class TestResponseBadRequest: |
| 56 | + |
| 57 | + def test_bad_request_default(self): |
| 58 | + expected_response = ({'status': 'Bad Request', 'message': 'Input validation failed', 'data': None}, 400) |
| 59 | + response = response_bad_request() |
| 60 | + assert response == expected_response |
| 61 | + |
| 62 | + def test_bad_request_custom_message(self): |
| 63 | + expected_response = ({'status': 'Bad Request', 'message': 'Custom bad request message', 'data': None}, 400) |
| 64 | + response = response_bad_request(message='Custom bad request message') |
| 65 | + assert response == expected_response |
| 66 | + |
| 67 | + def test_bad_request_custom_data(self): |
| 68 | + expected_response = ({'status': 'Bad Request', 'message': 'Input validation failed', 'data': {'key': 'value'}}, 400) |
| 69 | + response = response_bad_request(data={'key': 'value'}) |
| 70 | + assert response == expected_response |
| 71 | + |
| 72 | + |
| 73 | +class TestResponseUnauthorized: |
| 74 | + |
| 75 | + def test_unauthorized_default(self): |
| 76 | + expected_response = ({'status': 'Unauthorized', 'message': 'Missing or invalid credentials', 'data': None}, 401) |
| 77 | + response = response_unauthorized() |
| 78 | + assert response == expected_response |
| 79 | + |
| 80 | + def test_unauthorized_custom_message(self): |
| 81 | + expected_response = ({'status': 'Unauthorized', 'message': 'Custom unauthorized message', 'data': None}, 401) |
| 82 | + response = response_unauthorized(message='Custom unauthorized message') |
| 83 | + assert response == expected_response |
| 84 | + |
| 85 | + def test_unauthorized_custom_data(self): |
| 86 | + expected_response = ({'status': 'Unauthorized', 'message': 'Missing or invalid credentials', 'data': {'key': 'value'}}, 401) |
| 87 | + response = response_unauthorized(data={'key': 'value'}) |
| 88 | + assert response == expected_response |
| 89 | + |
| 90 | + |
| 91 | +class TestResponseForbidden: |
| 92 | + |
| 93 | + def test_forbidden_default(self): |
| 94 | + expected_response = ({'status': 'Forbidden', 'message': 'User is not authorized to perform this action', 'data': None}, 403) |
| 95 | + response = response_forbidden() |
| 96 | + assert response == expected_response |
| 97 | + |
| 98 | + def test_forbidden_custom_message(self): |
| 99 | + expected_response = ({'status': 'Forbidden', 'message': 'Custom forbidden message', 'data': None}, 403) |
| 100 | + response = response_forbidden(message='Custom forbidden message') |
| 101 | + assert response == expected_response |
| 102 | + |
| 103 | + def test_forbidden_custom_data(self): |
| 104 | + expected_response = ({'status': 'Forbidden', 'message': 'User is not authorized to perform this action', 'data': {'key': 'value'}}, 403) |
| 105 | + response = response_forbidden(data={'key': 'value'}) |
| 106 | + assert response == expected_response |
| 107 | + |
| 108 | + |
| 109 | +class TestResponseNotFound: |
| 110 | + |
| 111 | + def test_not_found_default(self): |
| 112 | + expected_response = ({'status': 'Not Found', 'message': 'Requested resource not found', 'data': None}, 404) |
| 113 | + response = response_not_found() |
| 114 | + assert response == expected_response |
| 115 | + |
| 116 | + def test_not_found_custom_message(self): |
| 117 | + expected_response = ({'status': 'Not Found', 'message': 'Custom not found message', 'data': None}, 404) |
| 118 | + response = response_not_found(message='Custom not found message') |
| 119 | + assert response == expected_response |
| 120 | + |
| 121 | + def test_not_found_custom_data(self): |
| 122 | + expected_response = ({'status': 'Not Found', 'message': 'Requested resource not found', 'data': {'key': 'value'}}, 404) |
| 123 | + response = response_not_found(data={'key': 'value'}) |
| 124 | + assert response == expected_response |
| 125 | + |
| 126 | + |
| 127 | +class TestResponseMethodNotAllowed: |
| 128 | + |
| 129 | + def test_method_not_allowed_default(self): |
| 130 | + expected_response = ({'status': 'Method Not Allowed', 'message': 'This method is not allowed', 'data': None}, 405) |
| 131 | + response = response_method_not_allowed() |
| 132 | + assert response == expected_response |
| 133 | + |
| 134 | + def test_method_not_allowed_custom_message(self): |
| 135 | + expected_response = ({'status': 'Method Not Allowed', 'message': 'Custom method not allowed message', 'data': None}, 405) |
| 136 | + response = response_method_not_allowed(message='Custom method not allowed message') |
| 137 | + assert response == expected_response |
| 138 | + |
| 139 | + def test_method_not_allowed_custom_data(self): |
| 140 | + expected_response = ({'status': 'Method Not Allowed', 'message': 'This method is not allowed', 'data': {'key': 'value'}}, 405) |
| 141 | + response = response_method_not_allowed(data={'key': 'value'}) |
| 142 | + assert response == expected_response |
| 143 | + |
| 144 | + |
| 145 | +class TestResponseConflict: |
| 146 | + |
| 147 | + def test_conflict_default(self): |
| 148 | + expected_response = ({'status': 'Conflict', 'message': 'Conflict with the current state of the target resource', 'data': None}, 409) |
| 149 | + response = response_conflict() |
| 150 | + assert response == expected_response |
| 151 | + |
| 152 | + def test_conflict_custom_message(self): |
| 153 | + expected_response = ({'status': 'Conflict', 'message': 'Custom conflict message', 'data': None}, 409) |
| 154 | + response = response_conflict(message='Custom conflict message') |
| 155 | + assert response == expected_response |
| 156 | + |
| 157 | + def test_conflict_custom_data(self): |
| 158 | + expected_response = ({'status': 'Conflict', 'message': 'Conflict with the current state of the target resource', 'data': {'key': 'value'}}, 409) |
| 159 | + response = response_conflict(data={'key': 'value'}) |
| 160 | + assert response == expected_response |
| 161 | + |
| 162 | + |
| 163 | +class TestResponseGone: |
| 164 | + |
| 165 | + def test_gone_default(self): |
| 166 | + expected_response = ({'status': 'Gone', 'message': 'Resource no longer available', 'data': None}, 410) |
| 167 | + response = response_gone() |
| 168 | + assert response == expected_response |
| 169 | + |
| 170 | + def test_gone_custom_message(self): |
| 171 | + expected_response = ({'status': 'Gone', 'message': 'Custom gone message', 'data': None}, 410) |
| 172 | + response = response_gone(message='Custom gone message') |
| 173 | + assert response == expected_response |
| 174 | + |
| 175 | + def test_gone_custom_data(self): |
| 176 | + expected_response = ({'status': 'Gone', 'message': 'Resource no longer available', 'data': {'key': 'value'}}, 410) |
| 177 | + response = response_gone(data={'key': 'value'}) |
| 178 | + assert response == expected_response |
| 179 | + |
| 180 | + |
| 181 | +class TestResponsePayloadTooLarge: |
| 182 | + |
| 183 | + def test_payload_too_large_default(self): |
| 184 | + expected_response = ({'status': 'Payload Too Large', 'message': 'Payload exceeds the allowed limit', 'data': None}, 413) |
| 185 | + response = response_payload_too_large() |
| 186 | + assert response == expected_response |
| 187 | + |
| 188 | + def test_payload_too_large_custom_message(self): |
| 189 | + expected_response = ({'status': 'Payload Too Large', 'message': 'Custom payload too large message', 'data': None}, 413) |
| 190 | + response = response_payload_too_large(message='Custom payload too large message') |
| 191 | + assert response == expected_response |
| 192 | + |
| 193 | + def test_payload_too_large_custom_data(self): |
| 194 | + expected_response = ({'status': 'Payload Too Large', 'message': 'Payload exceeds the allowed limit', 'data': {'key': 'value'}}, 413) |
| 195 | + response = response_payload_too_large(data={'key': 'value'}) |
| 196 | + assert response == expected_response |
| 197 | + |
| 198 | + |
| 199 | +class TestResponseUnsupportedMediaType: |
| 200 | + |
| 201 | + def test_unsupported_media_type_default(self): |
| 202 | + expected_response = ({'status': 'Unsupported Media Type', 'message': 'Unsupported media type', 'data': None}, 415) |
| 203 | + response = response_unsupported_media_type() |
| 204 | + assert response == expected_response |
| 205 | + |
| 206 | + def test_unsupported_media_type_custom_message(self): |
| 207 | + expected_response = ({'status': 'Unsupported Media Type', 'message': 'Custom message', 'data': None}, 415) |
| 208 | + response = response_unsupported_media_type(message='Custom message') |
| 209 | + assert response == expected_response |
| 210 | + |
| 211 | + def test_unsupported_media_type_custom_data(self): |
| 212 | + expected_response = ({'status': 'Unsupported Media Type', 'message': 'Unsupported media type', 'data': {'key': 'value'}}, 415) |
| 213 | + response = response_unsupported_media_type(data={'key': 'value'}) |
| 214 | + assert response == expected_response |
| 215 | + |
| 216 | + |
| 217 | +class TestResponseTooManyRequests: |
| 218 | + |
| 219 | + def test_too_many_requests_default(self): |
| 220 | + expected_response = ({'status': 'Too Many Requests', 'message': 'Client has sent too many requests in a given amount of time', 'data': None}, 429) |
| 221 | + response = response_too_many_requests() |
| 222 | + assert response == expected_response |
| 223 | + |
| 224 | + def test_too_many_requests_custom_message(self): |
| 225 | + expected_response = ({'status': 'Too Many Requests', 'message': 'Custom rate limit message', 'data': None}, 429) |
| 226 | + response = response_too_many_requests(message='Custom rate limit message') |
| 227 | + assert response == expected_response |
| 228 | + |
| 229 | + def test_too_many_requests_custom_data(self): |
| 230 | + expected_response = ( |
| 231 | + {'status': 'Too Many Requests', 'message': 'Client has sent too many requests in a given amount of time', 'data': {'key': 'value'}}, 429) |
| 232 | + response = response_too_many_requests(data={'key': 'value'}) |
| 233 | + assert response == expected_response |
| 234 | + |
| 235 | + |
| 236 | +class TestResponseError: |
| 237 | + |
| 238 | + def test_error_default(self): |
| 239 | + expected_response = ({'status': 'Internal Server Error', 'message': 'An error occurred', 'data': None}, 500) |
| 240 | + response = response_error() |
| 241 | + assert response == expected_response |
| 242 | + |
| 243 | + def test_error_custom_message(self): |
| 244 | + expected_response = ({'status': 'Internal Server Error', 'message': 'Custom error message', 'data': None}, 500) |
| 245 | + response = response_error(message='Custom error message') |
| 246 | + assert response == expected_response |
| 247 | + |
| 248 | + def test_error_custom_data(self): |
| 249 | + expected_response = ({'status': 'Internal Server Error', 'message': 'An error occurred', 'data': {'key': 'value'}}, 500) |
| 250 | + response = response_error(data={'key': 'value'}) |
| 251 | + assert response == expected_response |
| 252 | + |
| 253 | + |
| 254 | +class TestResponseServiceUnavailable: |
| 255 | + |
| 256 | + def test_service_unavailable_default(self): |
| 257 | + expected_response = ({'status': 'Service Unavailable', 'message': 'Service temporarily unavailable', 'data': None}, 503) |
| 258 | + response = response_service_unavailable() |
| 259 | + assert response == expected_response |
| 260 | + |
| 261 | + def test_service_unavailable_custom_message(self): |
| 262 | + expected_response = ({'status': 'Service Unavailable', 'message': 'Custom unavailable message', 'data': None}, 503) |
| 263 | + response = response_service_unavailable(message='Custom unavailable message') |
| 264 | + assert response == expected_response |
| 265 | + |
| 266 | + def test_service_unavailable_custom_data(self): |
| 267 | + expected_response = ({'status': 'Service Unavailable', 'message': 'Service temporarily unavailable', 'data': {'key': 'value'}}, 503) |
| 268 | + response = response_service_unavailable(data={'key': 'value'}) |
| 269 | + assert response == expected_response |
0 commit comments