Exemplo n.º 1
0
    def test_constructor(self):
        x = Parameter("test", ParamType.STRING)
        assert not x.required
        assert x.name == "test"
        assert x.type is ParamType.STRING

        y = Parameter("foo", ParamType.JSON, required=True)
        assert y.required
        assert y.name == "foo"
        assert y.type is ParamType.JSON
Exemplo n.º 2
0
 def __init__(self, config: PbenchServerConfig, logger: Logger):
     super().__init__(
         config,
         logger,
         Schema(
             Parameter("user", ParamType.USER, required=True),
             Parameter("start", ParamType.DATE, required=True),
             Parameter("end", ParamType.DATE, required=True),
         ),
     )
Exemplo n.º 3
0
 def __init__(self, config: PbenchServerConfig, logger: Logger):
     super().__init__(
         config,
         logger,
         Schema(
             Parameter("controller", ParamType.STRING, required=True),
             Parameter("name", ParamType.STRING, required=True),
             Parameter("access", ParamType.ACCESS, required=True),
         ),
         role=API_OPERATION.UPDATE,
     )
Exemplo n.º 4
0
 def __init__(self, config: PbenchServerConfig, logger: Logger):
     super().__init__(
         config,
         logger,
         Schema(
             Parameter("user", ParamType.USER, required=False),
             Parameter("access", ParamType.ACCESS, required=False),
             Parameter("name", ParamType.STRING, required=True),
             Parameter("start", ParamType.DATE, required=True),
             Parameter("end", ParamType.DATE, required=True),
         ),
     )
Exemplo n.º 5
0
class TestSchema:
    """
    Tests on the Schema class
    """

    schema = Schema(
        Parameter("key1", ParamType.STRING, required=True),
        Parameter("key2", ParamType.JSON),
        Parameter("key3", ParamType.DATE),
    )

    def test_bad_payload(self):
        with pytest.raises(InvalidRequestPayload):
            self.schema.validate(None)

    def test_missing_required(self):
        with pytest.raises(MissingParameters):
            self.schema.validate({"key2": "abc"})

    def test_missing_optional(self):
        test = {"key1": "OK"}
        assert test == self.schema.validate(test)

    def test_bad_dates(self):
        with pytest.raises(ConversionError):
            self.schema.validate({"key1": "yes", "key3": "2000-02-56"})

    def test_null_required(self):
        with pytest.raises(MissingParameters):
            self.schema.validate({"key1": None})

    def test_bad_json(self):
        with pytest.raises(ConversionError):
            self.schema.validate({"key1": 1, "key2": "not JSON"})

    def test_all_clear(self):
        payload = {
            "key1": "name",
            "key3": "2021-06-29",
            "key2": {
                "json": True,
                "key": "abc"
            },
        }
        expected = payload.copy()
        expected["key3"] = dateutil.parser.parse(expected["key3"])
        assert expected == self.schema.validate(payload)
Exemplo n.º 6
0
    def __init__(self, config: PbenchServerConfig, logger: Logger):
        """
        __init__ Configure the Elasticsearch passthrough class

        NOTE: there are three "expected" JSON input parameters, but only two of
        these are required and therefore appear in the schema.

        TODO: the schema could be extended to include both the type and a
        "required" flag; is that worthwhile? This would allow automatic type
        check/conversion of optional parameters, which would be cleaner.

        Args:
            config: Pbench configuration object
            logger: logger object
        """
        super().__init__(
            config,
            logger,
            Schema(
                Parameter("indices", ParamType.STRING, required=True),
                Parameter("payload", ParamType.JSON),
                Parameter("params", ParamType.JSON),
            ),
        )
Exemplo n.º 7
0
 def test_invalid_optional(self, test):
     x = Parameter("data", ParamType.STRING)
     json, expected = test
     assert x.invalid(json) is expected
Exemplo n.º 8
0
 def test_invalid_required(self, test):
     x = Parameter("data", ParamType.STRING, required=True)
     json, expected = test
     assert x.invalid(json) is expected