def test_validate_command_choices_bad_value_type(self, monkeypatch, validator): _process_mock(monkeypatch, return_value='["value"]') request = BrewtilsRequest( system="foo", command="command1", parameters={"key1": "value"}, system_version="0.0.1", instance_name="instance_name", ) command = Mock( parameters=[ Parameter( key="key1", type="String", optional=False, choices=Choices( type="command", value=1, strict=True, ), ) ] ) with pytest.raises(ModelValidationError): validator.get_and_validate_parameters(request, command)
def test_validate_command_choices_dictionary_list_response( self, monkeypatch, validator ): process_mock = _process_mock(monkeypatch, return_value='[{"value": "value"}]') request = BrewtilsRequest( system="foo", command="command1", parameters={"key1": "value"}, system_version="0.0.1", instance_name="instance_name", ) command = Mock( parameters=[ Parameter( key="key1", type="String", choices=Choices(type="command", value="command_name", strict=True), optional=False, ) ] ) validator.get_and_validate_parameters(request, command) choices_request = process_mock.call_args[0][0] assert choices_request.command == "command_name" assert choices_request.system == "foo" assert choices_request.system_version == "0.0.1" assert choices_request.instance_name == "instance_name"
def test_validate_value_in_choices_no_choices(self, validator): req = BrewtilsRequest( system="foo", command="command1", parameters={"key1": "value"} ) command_parameter = Mock(key="key1", multi=False, type="String", choices=None) command = Mock(parameters=[command_parameter]) validator.get_and_validate_parameters(req, command)
def test_command_lookup(self, monkeypatch, validator): request = BrewtilsRequest(parameters={}) lookup_mock = Mock(return_value=Mock(parameters=[])) monkeypatch.setattr( validator, "get_and_validate_command_for_system", lookup_mock ) validator.get_and_validate_parameters(request) lookup_mock.assert_called_once_with(request)
def test_validate_parameter_based_on_type_null_not_nullable(self, validator): req = BrewtilsRequest( system="foo", command="command1", parameters={"key1": None} ) command_parameter = Mock(key="key1", multi=False, type="String", nullable=False) command = Mock(parameters=[command_parameter]) with pytest.raises(ModelValidationError): validator.get_and_validate_parameters(req, command)
def test_validate_regex_nullable(self, validator): req = BrewtilsRequest( system="foo", command="command1", parameters={"key1": None} ) command_parameter = Parameter( key="key1", multi=False, type="String", regex=r"^Hi.*", nullable=True ) command = Command("test", parameters=[command_parameter]) validator.get_and_validate_parameters(req, command)
def test_missing_nested_parameters(self, validator): req = BrewtilsRequest(system="foo", command="command1", parameters={"key1": {}}) nested_parameter = Mock(key="foo", multi=False, type="String", optional=False) command_parameter = Mock( key="key1", multi=False, type="Dictionary", parameters=[nested_parameter] ) command = Mock(parameters=[command_parameter]) with pytest.raises(ModelValidationError): validator.get_and_validate_parameters(req, command)
def test_update_and_validate_parameter_extract_parameter_nullable_no_default( self, validator ): req = BrewtilsRequest(system="foo", command="command1", parameters={}) command_parameter = Parameter( key="key1", multi=False, nullable=True, default=None, optional=True ) command = Mock(parameters=[command_parameter]) validated_parameters = validator.get_and_validate_parameters(req, command) assert validated_parameters["key1"] is None
def test_update_and_validate_parameter_extract_parameter_multi_not_list( self, validator ): req = BrewtilsRequest( system="foo", command="command1", parameters={"key1": "NOT A LIST"} ) command_parameter = Mock(key="key1", multi=True) command = Mock(parameters=[command_parameter]) with pytest.raises(ModelValidationError): validator.get_and_validate_parameters(req, command)
def test_update_and_validate_parameter_extract_parameter_optional_no_default( self, validator ): req = BrewtilsRequest(system="foo", command="command1", parameters={}) command_parameter = Parameter( key="key1", multi=False, optional=True, default=None ) command = Mock(parameters=[command_parameter]) with pytest.raises(ModelValidationError): validator.get_and_validate_parameters(req, command)
def test_validate_value_in_choices_multi_valid_choice(self, validator): req = BrewtilsRequest( system="foo", command="command1", parameters={"key1": ["v1", "v2"]} ) command_parameter = Mock( key="key1", multi=True, type="String", choices=Mock(type="static", value=["v1", "v2"]), ) command = Mock(parameters=[command_parameter]) validator.get_and_validate_parameters(req, command)
def test_extract_parameter_non_multi_calls_with_default( self, validate_mock, validator ): req = BrewtilsRequest(system="foo", command="command1", parameters={}) command_parameter = Mock(key="key1", multi=False, default="default_value") command = Mock(parameters=[command_parameter]) validate_mock.side_effect = lambda w, x, y, z: w validator.get_and_validate_parameters(req, command) validate_mock.assert_called_once_with( "default_value", command_parameter, command, req )
def _parse_form_request(self) -> BrewtilsRequest: args = {"parameters": {}} for key, value in self.request.body_arguments.items(): decoded_param = value[0].decode(self.request.charset) if key.startswith("parameters."): args["parameters"][key.replace("parameters.", "")] = decoded_param else: args[key] = decoded_param return BrewtilsRequest(**args)
def test_validate_value_in_choices_optional_none_allowed(self, validator): req = BrewtilsRequest(system="foo", command="command1", parameters={}) command_parameter = Mock( key="key1", multi=False, type="String", optional=True, default=None, choices=Mock(type="static", value=["value1", "value3"]), ) command = Mock(parameters=[command_parameter]) validator.get_and_validate_parameters(req, command)
def test_validate_maximum_nullable(self, validator): req = BrewtilsRequest( system="foo", command="command1", parameters={"key1": None} ) command_parameter = Parameter( key="key1", multi=False, type="Integer", optional=False, minimum=3, nullable=True, ) command = Command("test", parameters=[command_parameter]) validator.get_and_validate_parameters(req, command)
def test_validate_value_in_choices_multi_invalid_choice(self, validator): req = BrewtilsRequest( system="foo", command="command1", parameters={"key1": ["v1", "v2"]} ) command_parameter = Mock( key="key1", multi=True, type="String", optional=False, choices=Mock(type="static", value=["v1", "v3"]), ) command = Mock(parameters=[command_parameter]) with pytest.raises(ModelValidationError): validator.get_and_validate_parameters(req, command)
def test_validate_minimum_non_sequence(self, validator): req = BrewtilsRequest(system="foo", command="command1", parameters={"key1": 5}) command_parameter = Parameter( key="key1", multi=False, type="Integer", optional=False, minimum=3 ) command = Command("test", parameters=[command_parameter]) validator.get_and_validate_parameters(req, command) command_parameter = Parameter( key="key1", multi=False, type="Integer", optional=False, minimum=10 ) command = Command("test", parameters=[command_parameter]) with pytest.raises(ModelValidationError): validator.get_and_validate_parameters(req, command)
def _parse_multipart_form_data(self) -> BrewtilsRequest: """Generate a Request object from multipart/form-data input""" request_form = self.get_body_argument("request") if request_form is None: raise BadRequest( reason="request parameter required for multipart/form-data requests" ) try: request_form_dict = json.loads(request_form) except (json.JSONDecodeError): raise BadRequest(reason="request parameter must be valid JSON") self._add_files_to_request(request_form_dict) return BrewtilsRequest(**request_form_dict)
def test_update_and_validate_parameter_extract_parameter_multi( self, validate_mock, validator ): req = BrewtilsRequest( system="foo", command="command1", parameters={"key1": [1, 2]} ) command_parameter = Mock(key="key1", multi=True) command = Mock(parameters=[command_parameter]) validate_mock.side_effect = lambda w, x, y, z: w validator.get_and_validate_parameters(req, command) validate_mock.assert_has_calls( [ call(1, command_parameter, command, req), call(2, command_parameter, command, req), ], any_order=True, )
def test_validate_regex(self, validator): req = BrewtilsRequest( system="foo", command="command1", parameters={"key1": "Hi World!"} ) command_parameter = Parameter( key="key1", multi=False, type="String", optional=False, regex=r"^Hi.*" ) command = Command("test", parameters=[command_parameter]) validator.get_and_validate_parameters(req, command) command_parameter = Parameter( key="key1", multi=False, type="String", optional=False, regex=r"^Hello.*" ) command = Command("test", parameters=[command_parameter]) with pytest.raises(ModelValidationError): validator.get_and_validate_parameters(req, command)
def test_validate_command_choices_dict_value(self, monkeypatch, validator): process_mock = _process_mock(monkeypatch, return_value='["value"]') request = BrewtilsRequest( system="foo", command="command1", parameters={"key1": "value"}, system_version="0.0.1", namespace="default", instance_name="instance_name", ) choices_value = { "command": "command_name", "system": "foo", "namespace": "default", "version": "0.0.1", "instance_name": "default", } command = Mock( parameters=[ Parameter( key="key1", type="String", optional=False, choices=Choices( type="command", value=choices_value, strict=True, ), ) ] ) validator.get_and_validate_parameters(request, command) choices_request = process_mock.call_args[0][0] assert choices_request.command == "command_name" assert choices_request.system == "foo" assert choices_request.namespace == "default" assert choices_request.system_version == "0.0.1" assert choices_request.instance_name == "default"
def test_validate_choices_static_bad_type(self, validator): command_parameter = Mock( key="key1", multi=False, type="String", optional=False, default=None, choices=Mock(type="static", value="bad str"), minimum=None, maximum=None, regex=None, ) command = Mock(parameters=[command_parameter]) req = BrewtilsRequest( system="foo", command="command1", parameters={"key1": "1"} ) with pytest.raises(ModelValidationError): validator.get_and_validate_parameters(req, command)
def test_validate_url_choices(self, validator, response): session_mock = Mock() session_mock.get.return_value.text = response validator._session = session_mock req = BrewtilsRequest( system="foo", command="command1", parameters={"key1": "value"} ) command_parameter = Mock( key="key1", type="String", optional=False, multi=False, choices=Mock(type="url", value="http://localhost"), minimum=None, maximum=None, regex=None, ) command = Mock(parameters=[command_parameter]) validator.get_and_validate_parameters(req, command) session_mock.get.assert_called_with("http://localhost", params={})
def test_no_request_command(self, validator): with pytest.raises(ModelValidationError): validator.get_and_validate_command_for_system( BrewtilsRequest(system="foo", parameters={}), Mock() )
def test_empty(self, validator): req = BrewtilsRequest(system="foo", command="command1") command = Command(parameters=[]) assert validator.get_and_validate_parameters(req, command) == {}