Best Python code snippet using tempest_python
test_rest_client.py
Source:test_rest_client.py  
...531    def _test_validate_pass(self, schema, resp_body, status=200):532        resp = self.Response()533        resp.status = status534        self.rest_client.validate_response(schema, resp, resp_body)535    def _test_validate_fail(self, schema, resp_body, status=200,536                            error_msg="HTTP response body is invalid"):537        resp = self.Response()538        resp.status = status539        ex = self.assertRaises(exceptions.InvalidHTTPResponseBody,540                               self.rest_client.validate_response,541                               schema, resp, resp_body)542        self.assertIn(error_msg, ex._error_string)543class TestRestClientJSONSchemaValidation(TestJSONSchemaValidationBase):544    schema = {545        'status_code': [200],546        'response_body': {547            'type': 'object',548            'properties': {549                'foo': {550                    'type': 'integer',551                },552            },553            'required': ['foo']554        }555    }556    def test_validate_pass_with_http_success_code(self):557        body = {'foo': 12}558        self._test_validate_pass(self.schema, body, status=200)559    def test_validate_pass_with_http_redirect_code(self):560        body = {'foo': 12}561        schema = copy.deepcopy(self.schema)562        schema['status_code'] = 300563        self._test_validate_pass(schema, body, status=300)564    def test_validate_not_http_success_code(self):565        schema = {566            'status_code': [200]567        }568        body = {}569        self._test_validate_pass(schema, body, status=400)570    def test_validate_multiple_allowed_type(self):571        schema = {572            'status_code': [200],573            'response_body': {574                'type': 'object',575                'properties': {576                    'foo': {577                        'type': ['integer', 'string'],578                    },579                },580                'required': ['foo']581            }582        }583        body = {'foo': 12}584        self._test_validate_pass(schema, body)585        body = {'foo': '12'}586        self._test_validate_pass(schema, body)587    def test_validate_enable_additional_property_pass(self):588        schema = {589            'status_code': [200],590            'response_body': {591                'type': 'object',592                'properties': {593                    'foo': {'type': 'integer'}594                },595                'additionalProperties': True,596                'required': ['foo']597            }598        }599        body = {'foo': 12, 'foo2': 'foo2value'}600        self._test_validate_pass(schema, body)601    def test_validate_disable_additional_property_pass(self):602        schema = {603            'status_code': [200],604            'response_body': {605                'type': 'object',606                'properties': {607                    'foo': {'type': 'integer'}608                },609                'additionalProperties': False,610                'required': ['foo']611            }612        }613        body = {'foo': 12}614        self._test_validate_pass(schema, body)615    def test_validate_disable_additional_property_fail(self):616        schema = {617            'status_code': [200],618            'response_body': {619                'type': 'object',620                'properties': {621                    'foo': {'type': 'integer'}622                },623                'additionalProperties': False,624                'required': ['foo']625            }626        }627        body = {'foo': 12, 'foo2': 'foo2value'}628        self._test_validate_fail(schema, body)629    def test_validate_wrong_status_code(self):630        schema = {631            'status_code': [202]632        }633        body = {}634        resp = self.Response()635        resp.status = 200636        ex = self.assertRaises(exceptions.InvalidHttpSuccessCode,637                               self.rest_client.validate_response,638                               schema, resp, body)639        self.assertIn("Unexpected http success status code", ex._error_string)640    def test_validate_wrong_attribute_type(self):641        body = {'foo': 1.2}642        self._test_validate_fail(self.schema, body)643    def test_validate_unexpected_response_body(self):644        schema = {645            'status_code': [200],646        }647        body = {'foo': 12}648        self._test_validate_fail(649            schema, body,650            error_msg="HTTP response body should not exist")651    def test_validate_missing_response_body(self):652        body = {}653        self._test_validate_fail(self.schema, body)654    def test_validate_missing_required_attribute(self):655        body = {'notfoo': 12}656        self._test_validate_fail(self.schema, body)657    def test_validate_response_body_not_list(self):658        schema = {659            'status_code': [200],660            'response_body': {661                'type': 'object',662                'properties': {663                    'list_items': {664                        'type': 'array',665                        'items': {'foo': {'type': 'integer'}}666                    }667                },668                'required': ['list_items'],669            }670        }671        body = {'foo': 12}672        self._test_validate_fail(schema, body)673    def test_validate_response_body_list_pass(self):674        schema = {675            'status_code': [200],676            'response_body': {677                'type': 'object',678                'properties': {679                    'list_items': {680                        'type': 'array',681                        'items': {'foo': {'type': 'integer'}}682                    }683                },684                'required': ['list_items'],685            }686        }687        body = {'list_items': [{'foo': 12}, {'foo': 10}]}688        self._test_validate_pass(schema, body)689class TestRestClientJSONHeaderSchemaValidation(TestJSONSchemaValidationBase):690    schema = {691        'status_code': [200],692        'response_header': {693            'type': 'object',694            'properties': {695                'foo': {'type': 'integer'}696            },697            'required': ['foo']698        }699    }700    def test_validate_header_schema_pass(self):701        resp_body = {}702        resp = self.Response()703        resp.status = 200704        resp.foo = 12705        self.rest_client.validate_response(self.schema, resp, resp_body)706    def test_validate_header_schema_fail(self):707        resp_body = {}708        resp = self.Response()709        resp.status = 200710        resp.foo = 1.2711        ex = self.assertRaises(exceptions.InvalidHTTPResponseHeader,712                               self.rest_client.validate_response,713                               self.schema, resp, resp_body)714        self.assertIn("HTTP response header is invalid", ex._error_string)715class TestRestClientJSONSchemaFormatValidation(TestJSONSchemaValidationBase):716    schema = {717        'status_code': [200],718        'response_body': {719            'type': 'object',720            'properties': {721                'foo': {722                    'type': 'string',723                    'format': 'email'724                }725            },726            'required': ['foo']727        }728    }729    def test_validate_format_pass(self):730        body = {'foo': 'example@example.com'}731        self._test_validate_pass(self.schema, body)732    def test_validate_format_fail(self):733        body = {'foo': 'wrong_email'}734        self._test_validate_fail(self.schema, body)735    def test_validate_formats_in_oneOf_pass(self):736        schema = {737            'status_code': [200],738            'response_body': {739                'type': 'object',740                'properties': {741                    'foo': {742                        'type': 'string',743                        'oneOf': [744                            {'format': 'ipv4'},745                            {'format': 'ipv6'}746                        ]747                    }748                },749                'required': ['foo']750            }751        }752        body = {'foo': '10.0.0.0'}753        self._test_validate_pass(schema, body)754        body = {'foo': 'FE80:0000:0000:0000:0202:B3FF:FE1E:8329'}755        self._test_validate_pass(schema, body)756    def test_validate_formats_in_oneOf_fail_both_match(self):757        schema = {758            'status_code': [200],759            'response_body': {760                'type': 'object',761                'properties': {762                    'foo': {763                        'type': 'string',764                        'oneOf': [765                            {'format': 'ipv4'},766                            {'format': 'ipv4'}767                        ]768                    }769                },770                'required': ['foo']771            }772        }773        body = {'foo': '10.0.0.0'}774        self._test_validate_fail(schema, body)775    def test_validate_formats_in_oneOf_fail_no_match(self):776        schema = {777            'status_code': [200],778            'response_body': {779                'type': 'object',780                'properties': {781                    'foo': {782                        'type': 'string',783                        'oneOf': [784                            {'format': 'ipv4'},785                            {'format': 'ipv6'}786                        ]787                    }788                },789                'required': ['foo']790            }791        }792        body = {'foo': 'wrong_ip_format'}793        self._test_validate_fail(schema, body)794    def test_validate_formats_in_anyOf_pass(self):795        schema = {796            'status_code': [200],797            'response_body': {798                'type': 'object',799                'properties': {800                    'foo': {801                        'type': 'string',802                        'anyOf': [803                            {'format': 'ipv4'},804                            {'format': 'ipv6'}805                        ]806                    }807                },808                'required': ['foo']809            }810        }811        body = {'foo': '10.0.0.0'}812        self._test_validate_pass(schema, body)813        body = {'foo': 'FE80:0000:0000:0000:0202:B3FF:FE1E:8329'}814        self._test_validate_pass(schema, body)815    def test_validate_formats_in_anyOf_pass_both_match(self):816        schema = {817            'status_code': [200],818            'response_body': {819                'type': 'object',820                'properties': {821                    'foo': {822                        'type': 'string',823                        'anyOf': [824                            {'format': 'ipv4'},825                            {'format': 'ipv4'}826                        ]827                    }828                },829                'required': ['foo']830            }831        }832        body = {'foo': '10.0.0.0'}833        self._test_validate_pass(schema, body)834    def test_validate_formats_in_anyOf_fail_no_match(self):835        schema = {836            'status_code': [200],837            'response_body': {838                'type': 'object',839                'properties': {840                    'foo': {841                        'type': 'string',842                        'anyOf': [843                            {'format': 'ipv4'},844                            {'format': 'ipv6'}845                        ]846                    }847                },848                'required': ['foo']849            }850        }851        body = {'foo': 'wrong_ip_format'}852        self._test_validate_fail(schema, body)853    def test_validate_formats_pass_for_unknow_format(self):854        schema = {855            'status_code': [200],856            'response_body': {857                'type': 'object',858                'properties': {859                    'foo': {860                        'type': 'string',861                        'format': 'UNKNOWN'862                    }863                },864                'required': ['foo']865            }866        }...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
