From b4e577f55a13497e7e7d7d0b4924a0209296d4d0 Mon Sep 17 00:00:00 2001 From: jsteemann Date: Thu, 26 Mar 2020 17:16:49 +0100 Subject: [PATCH 1/2] fix schema validation HTTP error codes --- arangod/VocBase/Validators.cpp | 1 - lib/Rest/GeneralResponse.cpp | 2 + .../api-collection-rocksdb-spec.rb | 94 +++++++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) diff --git a/arangod/VocBase/Validators.cpp b/arangod/VocBase/Validators.cpp index 956cc3fd882b..330bb2f13945 100644 --- a/arangod/VocBase/Validators.cpp +++ b/arangod/VocBase/Validators.cpp @@ -178,7 +178,6 @@ ValidatorJsonSchema::ValidatorJsonSchema(VPackSlice params) : ValidatorBase(para if (!rule.isObject()) { std::string msg = "No valid schema in rule attribute given (no object): "; msg += params.toJson(); - LOG_TOPIC("ababf", ERR, Logger::VALIDATION) << msg; THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_VALIDATION_BAD_PARAMETER, msg); } auto taoRuleValue = validation::slice_to_value(rule); diff --git a/lib/Rest/GeneralResponse.cpp b/lib/Rest/GeneralResponse.cpp index ed9fc278bf34..f595660c74ae 100644 --- a/lib/Rest/GeneralResponse.cpp +++ b/lib/Rest/GeneralResponse.cpp @@ -373,6 +373,8 @@ rest::ResponseCode GeneralResponse::responseCode(int code) { case TRI_ERROR_KEY_MUST_BE_PREFIXED_WITH_SMART_JOIN_ATTRIBUTE: case TRI_ERROR_NO_SMART_JOIN_ATTRIBUTE: case TRI_ERROR_CLUSTER_MUST_NOT_CHANGE_SMART_JOIN_ATTRIBUTE: + case TRI_ERROR_VALIDATION_FAILED: + case TRI_ERROR_VALIDATION_BAD_PARAMETER: return ResponseCode::BAD; case TRI_ERROR_ARANGO_USE_SYSTEM_DATABASE: diff --git a/tests/rb/HttpInterface/api-collection-rocksdb-spec.rb b/tests/rb/HttpInterface/api-collection-rocksdb-spec.rb index 44310ccb6dc3..3445175c14b6 100644 --- a/tests/rb/HttpInterface/api-collection-rocksdb-spec.rb +++ b/tests/rb/HttpInterface/api-collection-rocksdb-spec.rb @@ -157,6 +157,100 @@ end end +################################################################################ +## schema validation +################################################################################ + + context "schema validation:" do + before do + @cn = "UnitTestsCollectionBasics" + ArangoDB.drop_collection(@cn) + @cid = ArangoDB.create_collection(@cn, false) + end + + after do + ArangoDB.drop_collection(@cn) + end + + it "sets an invalid schema" do + cmd = api + "/" + @cn + "/properties" + body = "{ \"validation\": { \"rule\": \"peng!\", \"level\": \"strict\", \"message\": \"document has an invalid schema!\" } }" + doc = ArangoDB.log_put("#{prefix}-schema", cmd, :body => body) + + doc.code.should eq(400) + doc.parsed_response['error'].should eq(true) + doc.parsed_response['code'].should eq(400) + doc.parsed_response['errorNum'].should eq(1621) + end + + it "sets a valid schema" do + cmd = api + "/" + @cn + "/properties" + body = "{ \"validation\": { \"rule\": { \"properties\": { \"_key\": { \"type\": \"string\" }, \"_rev\": { \"type\": \"string\" }, \"_id\": { \"type\": \"string\" }, \"name\": { \"type\": \"object\", \"properties\": { \"first\": { \"type\": \"string\", \"minLength\": 1, \"maxLength\": 50 }, \"last\": { \"type\": \"string\", \"minLength\": 1, \"maxLength\": 50 } }, \"required\": [\"first\", \"last\"] }, \"status\": { \"enum\": [\"active\", \"inactive\", \"deleted\"] } }, \"additionalProperties\": false, \"required\": [\"name\", \"status\"] }, \"level\": \"strict\", \"message\": \"document has an invalid schema!\" } }" + doc = ArangoDB.log_put("#{prefix}-schema", cmd, :body => body) + doc = ArangoDB.log_get("#{prefix}-get-schema", cmd) + + doc.code.should eq(200) + doc.headers['content-type'].should eq("application/json; charset=utf-8") + doc.parsed_response['error'].should eq(false) + doc.parsed_response['code'].should eq(200) + doc.parsed_response['id'].should eq(@cid) + doc.parsed_response['name'].should eq(@cn) + doc.parsed_response['validation']['level'].should eq("strict") + doc.parsed_response['validation']['message'].should eq("document has an invalid schema!") + end + + it "stores valid documents" do + cmd = api + "/" + @cn + "/properties" + body = "{ \"validation\": { \"rule\": { \"properties\": { \"_key\": { \"type\": \"string\" }, \"_rev\": { \"type\": \"string\" }, \"_id\": { \"type\": \"string\" }, \"name\": { \"type\": \"object\", \"properties\": { \"first\": { \"type\": \"string\", \"minLength\": 1, \"maxLength\": 50 }, \"last\": { \"type\": \"string\", \"minLength\": 1, \"maxLength\": 50 } }, \"required\": [\"first\", \"last\"] }, \"status\": { \"enum\": [\"active\", \"inactive\", \"deleted\"] } }, \"additionalProperties\": false, \"required\": [\"name\", \"status\"] }, \"level\": \"strict\", \"message\": \"document has an invalid schema!\" } }" + doc = ArangoDB.log_put("#{prefix}-schema", cmd, :body => body) + doc.code.should eq(200) + + body = "{ \"name\": { \"first\": \"test\", \"last\": \"test\" }, \"status\": \"active\" }" + doc = ArangoDB.log_post("#{prefix}-schema-doc", "/_api/document/?collection=" + @cn, :body => body) + doc.code.should eq(202) + + body = "{ \"name\": { \"first\": \"a\", \"last\": \"b\" }, \"status\": \"inactive\" }" + doc = ArangoDB.log_post("#{prefix}-schema-doc", "/_api/document/?collection=" + @cn, :body => body) + doc.code.should eq(202) + end + + it "stores invalid documents" do + cmd = api + "/" + @cn + "/properties" + body = "{ \"validation\": { \"rule\": { \"properties\": { \"_key\": { \"type\": \"string\" }, \"_rev\": { \"type\": \"string\" }, \"_id\": { \"type\": \"string\" }, \"name\": { \"type\": \"object\", \"properties\": { \"first\": { \"type\": \"string\", \"minLength\": 1, \"maxLength\": 50 }, \"last\": { \"type\": \"string\", \"minLength\": 1, \"maxLength\": 50 } }, \"required\": [\"first\", \"last\"] }, \"status\": { \"enum\": [\"active\", \"inactive\", \"deleted\"] } }, \"additionalProperties\": false, \"required\": [\"name\", \"status\"] }, \"level\": \"strict\", \"message\": \"document has an invalid schema!\" } }" + doc = ArangoDB.log_put("#{prefix}-schema", cmd, :body => body) + + body = "{ \"name\": { \"first\" : \"\", \"last\": \"test\" }, \"status\": \"active\" }" + doc = ArangoDB.log_post("#{prefix}-schema-doc-invalid", "/_api/document/?collection=" + @cn, :body => body) + doc.code.should eq(400) + doc.parsed_response['errorNum'].should eq(1620) + + body = "{ \"name\": { \"first\" : \"\", \"last\": \"\" }, \"status\": \"active\" }" + doc = ArangoDB.log_post("#{prefix}-schema-doc-invalid", "/_api/document/?collection=" + @cn, :body => body) + doc.code.should eq(400) + doc.parsed_response['errorNum'].should eq(1620) + + body = "{ \"name\": { \"first\" : \"test\", \"last\": \"test\" } }" + doc = ArangoDB.log_post("#{prefix}-schema-doc-invalid", "/_api/document/?collection=" + @cn, :body => body) + doc.code.should eq(400) + doc.parsed_response['errorNum'].should eq(1620) + + body = "{ \"name\": { \"first\" : \"test\", \"last\": \"test\" }, \"status\": \"foo\" }" + doc = ArangoDB.log_post("#{prefix}-schema-doc-invalid", "/_api/document/?collection=" + @cn, :body => body) + doc.code.should eq(400) + doc.parsed_response['errorNum'].should eq(1620) + + body = "{ \"name\": { }, \"status\": \"active\" }" + doc = ArangoDB.log_post("#{prefix}-schema-doc-invalid", "/_api/document/?collection=" + @cn, :body => body) + doc.code.should eq(400) + doc.parsed_response['errorNum'].should eq(1620) + + body = "{ \"first\": \"abc\", \"last\": \"test\", \"status\": \"active\" }" + doc = ArangoDB.log_post("#{prefix}-schema-doc-invalid", "/_api/document/?collection=" + @cn, :body => body) + doc.code.should eq(400) + doc.parsed_response['errorNum'].should eq(1620) + end + end + ################################################################################ ## reading a collection ################################################################################ From ec5067ea306a551dc3d192f13731c6f1802078e0 Mon Sep 17 00:00:00 2001 From: jsteemann Date: Thu, 26 Mar 2020 17:16:59 +0100 Subject: [PATCH 2/2] add unit for memory output --- lib/ApplicationFeatures/EnvironmentFeature.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ApplicationFeatures/EnvironmentFeature.cpp b/lib/ApplicationFeatures/EnvironmentFeature.cpp index 446354bf30df..6151cca58702 100644 --- a/lib/ApplicationFeatures/EnvironmentFeature.cpp +++ b/lib/ApplicationFeatures/EnvironmentFeature.cpp @@ -253,7 +253,7 @@ void EnvironmentFeature::prepare() { overriddenmsg = " (overridden by environment variable)"; } LOG_TOPIC("25362", INFO, Logger::MEMORY) - << "Available physical memory: " << ram << overriddenmsg; + << "Available physical memory: " << ram << overriddenmsg << " bytes"; // test local ipv6 support try {