From 4bdbb005a9b214d5355d4f2036d510f898bc8a87 Mon Sep 17 00:00:00 2001 From: Simon Schuster Date: Tue, 6 Oct 2020 22:50:11 +0200 Subject: API: Add `minimum-id-length` post parameter This parameter controls the generated id for files (file/upload) and multipastes (file/create_multipaste). The post parameter has to be a positive integer value >= 2. Changes by Florian Pritz: - minor style and typo fixes - NEWS entry - check expected error reply content in tests Signed-off-by: Florian Pritz --- .../tests/api_v2/test_file_create_multipaste.php | 23 ++++++++++++ application/test/tests/api_v2/test_file_upload.php | 41 ++++++++++++++++++++++ 2 files changed, 64 insertions(+) (limited to 'application/test') diff --git a/application/test/tests/api_v2/test_file_create_multipaste.php b/application/test/tests/api_v2/test_file_create_multipaste.php index 8556616d1..2b6e9d8de 100644 --- a/application/test/tests/api_v2/test_file_create_multipaste.php +++ b/application/test/tests/api_v2/test_file_create_multipaste.php @@ -122,4 +122,27 @@ class test_file_create_multipaste extends common { $this->t->is($ret["data"]["total_count"], 1, "total_count correct"); $this->t->is($ret["data"]["deleted_count"], 1, "deleted_count correct"); } + + public function test_create_multipaste_minidlength() + { + $apikey = $this->createUserAndApikey("basic"); + $ret = $this->uploadFile($apikey, "data/tests/small-file"); + $id = $ret["data"]["ids"][0]; + + $ret = $this->uploadFile($apikey, "data/tests/small-file"); + $id2 = $ret["data"]["ids"][0]; + + $ret = $this->CallEndpoint("POST", "file/create_multipaste", array( + "apikey" => $apikey, + "ids[1]" => $id, + "ids[2]" => $id2, + "minimum-id-length" => 42, + )); + $this->expectSuccess("create multipaste", $ret); + + $this->t->isnt($ret["data"]["url_id"], "", "got a multipaste ID"); + $this->t->isnt($ret["data"]["url"], "", "got a multipaste URL"); + + $this->t->ok(strlen($ret["data"]["url_id"]) >= 42, "minimum url length upheld"); + } } diff --git a/application/test/tests/api_v2/test_file_upload.php b/application/test/tests/api_v2/test_file_upload.php index e8717b074..07769774f 100644 --- a/application/test/tests/api_v2/test_file_upload.php +++ b/application/test/tests/api_v2/test_file_upload.php @@ -68,4 +68,45 @@ class test_file_upload extends common { ), $ret, "expected reply"); } + public function test_upload_minidlength() + { + $apikey = $this->createUserAndApikey(); + $ret = $this->CallEndpoint("POST", "file/upload", array( + "apikey" => $apikey, + "file[1]" => curl_file_create("data/tests/small-file"), + "minimum-id-length" => 42, + )); + $this->expectSuccess("upload file", $ret); + + foreach ($ret["data"]["urls"] as $url) { + $matches = array(); + preg_match('/\/([^\/]+)\/$/', $url, $matches); + $this->t->ok(strlen($matches[1]) >= 42, "minimum url length upheld"); + } + } + + public function test_upload_bad_minidlength() + { + $apikey = $this->createUserAndApikey(); + + $combinations = [ + "non-numberic minimum-id-length" => "nonumber", + "negative minimum-id-length (-42)" => -42, + "minimum-id-length=0" => 0, + "minimum-id-length=1" => 1, + ]; + foreach ($combinations as $msg => $input) { + $ret = $this->CallEndpoint("POST", "file/upload", array( + "apikey" => $apikey, + "file[1]" => curl_file_create("data/tests/small-file"), + "minimum-id-length" => $input, + )); + $this->expectError("upload file with bad minimum-id-length. Test value: $msg", $ret); + $this->t->is_deeply(array( + 'status' => 'error', + 'error_id' => 'file/bad-minimum-id-length', + 'message' => "Passed parameter 'minimum-id-length' is not a valid integer or too small (min value: 2)", + ), $ret, "expected reply"); + } + } } -- cgit v1.2.3-24-g4f1b