From d3726c7c0e497def97efcf610fdcac9bbebb0f3e Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Thu, 5 Feb 2015 21:49:12 +0100 Subject: Add simple testsuite Signed-off-by: Florian Pritz --- application/tests/Test.php | 98 +++++++++++++++++++++++++++++++ application/tests/test_api_v1.php | 118 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 216 insertions(+) create mode 100644 application/tests/Test.php create mode 100644 application/tests/test_api_v1.php (limited to 'application/tests') diff --git a/application/tests/Test.php b/application/tests/Test.php new file mode 100644 index 000000000..81225b312 --- /dev/null +++ b/application/tests/Test.php @@ -0,0 +1,98 @@ + + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ + +namespace tests; + +abstract class Test { + protected $t; + protected $server = ""; + + public function __construct() + { + require_once APPPATH."/third_party/test-more-php/Test-More-OO.php"; + $this->t = new \TestMore(); + $this->t->plan("no_plan"); + } + + public function setServer($server) + { + $this->server = $server; + } + + // Method: POST, PUT, GET etc + // Data: array("param" => "value") ==> index.php?param=value + // Source: http://stackoverflow.com/a/9802854/953022 + protected function CallAPI($method, $url, $data = false) + { + $curl = curl_init(); + + switch ($method) { + case "POST": + curl_setopt($curl, CURLOPT_POST, 1); + + if ($data) + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); + break; + case "PUT": + curl_setopt($curl, CURLOPT_PUT, 1); + break; + default: + if ($data) + $url = sprintf("%s?%s", $url, http_build_query($data)); + } + + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_HTTPHEADER, array( + "Accept: application/json", + )); + + $result = curl_exec($curl); + + curl_close($curl); + + $json = json_decode($result, true); + if ($json === NULL) { + $this->t->fail("json decode"); + $this->diagReply($result); + } + + return $json; + } + + protected function expectSuccess($testname, $reply) + { + if (!isset($reply["status"]) || $reply["status"] != "success") { + $this->t->fail($testname); + $this->diagReply($reply); + } else { + $this->t->pass($testname); + } + return $reply; + } + + protected function diagReply($reply) + { + $this->t->diag("Request got unexpected response:"); + $this->t->diag(var_export($reply, true)); + } + + public function init() + { + } + + public function cleanup() + { + } + + public function __destruct() + { + $this->t->done_testing(); + } +} diff --git a/application/tests/test_api_v1.php b/application/tests/test_api_v1.php new file mode 100644 index 000000000..387e3fe6c --- /dev/null +++ b/application/tests/test_api_v1.php @@ -0,0 +1,118 @@ + + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ + +namespace tests; + +class test_api_v1 extends Test { + + private $apikeys = array(); + + public function __construct() + { + parent::__construct(); + + $CI =& get_instance(); + $CI->load->model("muser"); + $CI->load->model("mfile"); + + foreach (array(1,2,3,4,5) as $i) { + $CI->db->insert("users", array( + 'username' => "testuser-api_v1-$i", + 'password' => $CI->muser->hash_password("testpass$i"), + 'email' => "testuser$i@localhost.invalid", + 'referrer' => NULL + )); + $this->apikeys[$i] = \service\user::create_apikey($CI->db->insert_id(), "", "apikey"); + } + + } + + public function test_create_apikey_createNewKey() + { + $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/user/create_apikey", array( + "username" => "testuser-api_v1-1", + "password" => "testpass1", + "access_level" => "apikey", + "comment" => "main api key", + )); + $this->expectSuccess("create-apikey", $ret); + + $this->t->isnt($ret["data"]["new_key"], "", "apikey not empty"); + } + + public function test_history_empty() + { + $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/history", array( + "apikey" => $this->apikeys[1], + )); + $this->expectSuccess("get history", $ret); + + $this->t->ok(empty($ret["data"]["items"]), "items key exists and empty"); + $this->t->ok(empty($ret["data"]["multipaste_items"]), "multipaste_items key exists and empty"); + $this->t->is($ret["data"]["total_size"], 0, "total_size = 0 since no uploads"); + } + + public function test_get_config() + { + $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/get_config", array( + )); + $this->expectSuccess("get_config", $ret); + + $this->t->like($ret["data"]["upload_max_size"], '/[0-9]+/', "upload_max_size is int"); + $this->t->like($ret["data"]["max_files_per_request"], '/[0-9]+/', "max_files_per_request is int"); + } + + public function test_upload_uploadFile() + { + $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array( + "apikey" => $this->apikeys[2], + "file[1]" => curl_file_create("data/tests/small-file"), + )); + $this->expectSuccess("upload file", $ret); + + $this->t->ok(!empty($ret["data"]["ids"]), "got IDs"); + $this->t->ok(!empty($ret["data"]["urls"]), "got URLs"); + } + + public function test_history_notEmptyAfterUpload() + { + $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array( + "apikey" => $this->apikeys[3], + "file[1]" => curl_file_create("data/tests/small-file"), + )); + $this->expectSuccess("upload file", $ret); + + $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/history", array( + "apikey" => $this->apikeys[3], + )); + $this->expectSuccess("history not empty after upload", $ret); + + $this->t->ok(!empty($ret["data"]["items"]), "history not empty after upload (items)"); + $this->t->ok(empty($ret["data"]["multipaste_items"]), "didn't upload multipaste"); + $this->t->is($ret["data"]["total_size"], filesize("data/tests/small-file"), "total_size == uploaded file"); + } + + public function test_history_notSharedBetweenUsers() + { + $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array( + "apikey" => $this->apikeys[4], + "file[1]" => curl_file_create("data/tests/small-file"), + )); + $this->expectSuccess("upload file", $ret); + + $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/history", array( + "apikey" => $this->apikeys[5], + )); + $this->expectSuccess("get history", $ret); + + $this->t->ok(empty($ret["data"]["items"]), "items key exists and empty"); + $this->t->ok(empty($ret["data"]["multipaste_items"]), "multipaste_items key exists and empty"); + $this->t->is($ret["data"]["total_size"], 0, "total_size = 0 since no uploads"); + } +} -- cgit v1.2.3-24-g4f1b From 5b225c751d60d79916da4a7db761f823e12148de Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Fri, 6 Feb 2015 12:42:08 +0100 Subject: Add more tests Signed-off-by: Florian Pritz --- application/tests/Test.php | 14 +++++++++++-- application/tests/test_api_v1.php | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) (limited to 'application/tests') diff --git a/application/tests/Test.php b/application/tests/Test.php index 81225b312..192061db9 100644 --- a/application/tests/Test.php +++ b/application/tests/Test.php @@ -66,9 +66,9 @@ abstract class Test { return $json; } - protected function expectSuccess($testname, $reply) + protected function excpectStatus($testname, $reply, $status) { - if (!isset($reply["status"]) || $reply["status"] != "success") { + if (!isset($reply["status"]) || $reply["status"] != $status) { $this->t->fail($testname); $this->diagReply($reply); } else { @@ -77,6 +77,16 @@ abstract class Test { return $reply; } + protected function expectSuccess($testname, $reply) + { + return $this->excpectStatus($testname, $reply, "success"); + } + + protected function expectError($testname, $reply) + { + return $this->excpectStatus($testname, $reply, "error"); + } + protected function diagReply($reply) { $this->t->diag("Request got unexpected response:"); diff --git a/application/tests/test_api_v1.php b/application/tests/test_api_v1.php index 387e3fe6c..9f415abbd 100644 --- a/application/tests/test_api_v1.php +++ b/application/tests/test_api_v1.php @@ -115,4 +115,48 @@ class test_api_v1 extends Test { $this->t->ok(empty($ret["data"]["multipaste_items"]), "multipaste_items key exists and empty"); $this->t->is($ret["data"]["total_size"], 0, "total_size = 0 since no uploads"); } + + public function test_delete_canDeleteUploaded() + { + $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array( + "apikey" => $this->apikeys[2], + "file[1]" => curl_file_create("data/tests/small-file"), + )); + $this->expectSuccess("upload file", $ret); + + $id = $ret["data"]["ids"][0]; + + $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/delete", array( + "apikey" => $this->apikeys[2], + "ids[1]" => $id, + )); + $this->expectSuccess("delete uploaded file", $ret); + + $this->t->ok(empty($ret["data"]["errors"]), "no errors"); + $this->t->is_deeply(array($id => array("id" => $id)), $ret["data"]["deleted"], "deleted wanted ID"); + $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_delete_errorIfNotOwner() + { + $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array( + "apikey" => $this->apikeys[2], + "file[1]" => curl_file_create("data/tests/small-file"), + )); + $this->expectSuccess("upload file", $ret); + + $id = $ret["data"]["ids"][0]; + + $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/delete", array( + "apikey" => $this->apikeys[1], + "ids[1]" => $id, + )); + $this->expectSuccess("delete file of someone else", $ret); + + $this->t->ok(empty($ret["data"]["deleted"]), "not deleted"); + $this->t->is_deeply(array($id => array("id" => $id, "reason" => "wrong owner")), $ret["data"]["errors"], "error wanted ID"); + $this->t->is($ret["data"]["total_count"], 1, "total_count correct"); + $this->t->is($ret["data"]["deleted_count"], 0, "deleted_count correct"); + } } -- cgit v1.2.3-24-g4f1b From 56879097fd4246c78175867086af6d0c24a4ae89 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 8 Feb 2015 01:19:37 +0100 Subject: Improve testcases - create apikeys/users on demand (no magic numbers) - add some more testcases - extract api version into function - readability cleanup Signed-off-by: Florian Pritz --- application/tests/test_api_v1.php | 237 ++++++++++++++++++++++++++++++-------- 1 file changed, 189 insertions(+), 48 deletions(-) (limited to 'application/tests') diff --git a/application/tests/test_api_v1.php b/application/tests/test_api_v1.php index 9f415abbd..18f2a37f6 100644 --- a/application/tests/test_api_v1.php +++ b/application/tests/test_api_v1.php @@ -11,8 +11,6 @@ namespace tests; class test_api_v1 extends Test { - private $apikeys = array(); - public function __construct() { parent::__construct(); @@ -21,21 +19,75 @@ class test_api_v1 extends Test { $CI->load->model("muser"); $CI->load->model("mfile"); - foreach (array(1,2,3,4,5) as $i) { - $CI->db->insert("users", array( - 'username' => "testuser-api_v1-$i", - 'password' => $CI->muser->hash_password("testpass$i"), - 'email' => "testuser$i@localhost.invalid", - 'referrer' => NULL + } + + private function uploadFile($apikey, $file) + { + $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array( + "apikey" => $apikey, + "file[1]" => curl_file_create($file), + )); + $this->expectSuccess("upload file", $ret); + return $ret; + } + + private function createUser($counter) + { + $CI =& get_instance(); + $CI->db->insert("users", array( + 'username' => "testuser-api_v1-$counter", + 'password' => $CI->muser->hash_password("testpass$counter"), + 'email' => "testuser$counter@localhost.invalid", + 'referrer' => NULL + )); + + return $CI->db->insert_id(); + } + + private function createApikey($userid) + { + return \service\user::create_apikey($userid, "", "apikey"); + } + + private function createUserAndApikey() + { + static $counter = 100; + $counter++; + $userid = $this->createUser($counter); + return $this->createApikey($userid); + } + + private function callEndpoint($verb, $endpoint, $data) + { + return $this->CallAPI($verb, "$this->server/api/1.0.0/$endpoint", $data); + } + + public function test_callPrivateEndpointsWithoutApikey() + { + $endpoints = array( + "file/upload", + "file/history", + "file/delete", + "file/create_multipaste", + "user/apikeys", + "user/create_apikey", + ); + foreach ($endpoints as $endpoint) { + $ret = $this->CallEndpoint("POST", $endpoint, array( )); - $this->apikeys[$i] = \service\user::create_apikey($CI->db->insert_id(), "", "apikey"); + $this->expectError("call $endpoint without apikey", $ret); + $this->t->is_deeply(array( + 'status' => 'error', + 'error_id' => 'api/not-authenticated', + 'message' => 'Not authenticated. FileBin requires you to have an account, please go to the homepage for more information.', + ), $ret, "expected error"); } - } public function test_create_apikey_createNewKey() { - $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/user/create_apikey", array( + $this->createUser(1); + $ret = $this->CallEndpoint("POST", "user/create_apikey", array( "username" => "testuser-api_v1-1", "password" => "testpass1", "access_level" => "apikey", @@ -48,8 +100,9 @@ class test_api_v1 extends Test { public function test_history_empty() { - $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/history", array( - "apikey" => $this->apikeys[1], + $apikey = $this->createUserAndApikey(); + $ret = $this->CallEndpoint("POST", "file/history", array( + "apikey" => $apikey, )); $this->expectSuccess("get history", $ret); @@ -60,7 +113,7 @@ class test_api_v1 extends Test { public function test_get_config() { - $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/get_config", array( + $ret = $this->CallEndpoint("GET", "file/get_config", array( )); $this->expectSuccess("get_config", $ret); @@ -70,8 +123,9 @@ class test_api_v1 extends Test { public function test_upload_uploadFile() { - $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array( - "apikey" => $this->apikeys[2], + $apikey = $this->createUserAndApikey(); + $ret = $this->CallEndpoint("POST", "file/upload", array( + "apikey" => $apikey, "file[1]" => curl_file_create("data/tests/small-file"), )); $this->expectSuccess("upload file", $ret); @@ -80,16 +134,27 @@ class test_api_v1 extends Test { $this->t->ok(!empty($ret["data"]["urls"]), "got URLs"); } - public function test_history_notEmptyAfterUpload() + public function test_upload_uploadNothing() { - $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array( - "apikey" => $this->apikeys[3], - "file[1]" => curl_file_create("data/tests/small-file"), + $apikey = $this->createUserAndApikey(); + $ret = $this->CallEndpoint("POST", "file/upload", array( + "apikey" => $apikey, )); - $this->expectSuccess("upload file", $ret); + $this->expectError("upload no file", $ret); + $this->t->is_deeply(array( + 'status' => 'error', + 'error_id' => 'file/no-file', + 'message' => 'No file was uploaded or unknown error occured.', + ), $ret, "expected reply"); + } - $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/history", array( - "apikey" => $this->apikeys[3], + public function test_history_notEmptyAfterUpload() + { + $apikey = $this->createUserAndApikey(); + $this->uploadFile($apikey, "data/tests/small-file"); + + $ret = $this->CallEndpoint("POST", "file/history", array( + "apikey" => $apikey, )); $this->expectSuccess("history not empty after upload", $ret); @@ -100,14 +165,12 @@ class test_api_v1 extends Test { public function test_history_notSharedBetweenUsers() { - $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array( - "apikey" => $this->apikeys[4], - "file[1]" => curl_file_create("data/tests/small-file"), - )); - $this->expectSuccess("upload file", $ret); + $apikey = $this->createUserAndApikey(); + $apikey2 = $this->createUserAndApikey(); + $this->uploadFile($apikey, "data/tests/small-file"); - $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/history", array( - "apikey" => $this->apikeys[5], + $ret = $this->CallEndpoint("POST", "file/history", array( + "apikey" => $apikey2, )); $this->expectSuccess("get history", $ret); @@ -118,45 +181,123 @@ class test_api_v1 extends Test { public function test_delete_canDeleteUploaded() { - $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array( - "apikey" => $this->apikeys[2], - "file[1]" => curl_file_create("data/tests/small-file"), - )); - $this->expectSuccess("upload file", $ret); - + $apikey = $this->createUserAndApikey(); + $ret = $this->uploadFile($apikey, "data/tests/small-file"); $id = $ret["data"]["ids"][0]; - $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/delete", array( - "apikey" => $this->apikeys[2], + $ret = $this->CallEndpoint("POST", "file/delete", array( + "apikey" => $apikey, "ids[1]" => $id, )); $this->expectSuccess("delete uploaded file", $ret); $this->t->ok(empty($ret["data"]["errors"]), "no errors"); - $this->t->is_deeply(array($id => array("id" => $id)), $ret["data"]["deleted"], "deleted wanted ID"); + $this->t->is_deeply(array( + $id => array( + "id" => $id + ) + ), $ret["data"]["deleted"], "deleted wanted ID"); $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_delete_errorIfNotOwner() { - $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array( - "apikey" => $this->apikeys[2], - "file[1]" => curl_file_create("data/tests/small-file"), - )); - $this->expectSuccess("upload file", $ret); - + $apikey = $this->createUserAndApikey(); + $apikey2 = $this->createUserAndApikey(); + $ret = $this->uploadFile($apikey, "data/tests/small-file"); $id = $ret["data"]["ids"][0]; - $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/delete", array( - "apikey" => $this->apikeys[1], + $ret = $this->CallEndpoint("POST", "file/delete", array( + "apikey" => $apikey2, "ids[1]" => $id, )); $this->expectSuccess("delete file of someone else", $ret); $this->t->ok(empty($ret["data"]["deleted"]), "not deleted"); - $this->t->is_deeply(array($id => array("id" => $id, "reason" => "wrong owner")), $ret["data"]["errors"], "error wanted ID"); + $this->t->is_deeply(array( + $id => array( + "id" => $id, + "reason" => "wrong owner" + ) + ), $ret["data"]["errors"], "error wanted ID"); $this->t->is($ret["data"]["total_count"], 1, "total_count correct"); $this->t->is($ret["data"]["deleted_count"], 0, "deleted_count correct"); } + + public function test_create_multipaste_canCreate() + { + $apikey = $this->createUserAndApikey(); + $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, + )); + $this->expectSuccess("create multipaste", $ret); + + $this->t->isnt($ret["data"]["url_id"], "", "got a multipaste ID"); + } + + public function test_create_multipaste_errorOnWrongID() + { + $apikey = $this->createUserAndApikey(); + $ret = $this->uploadFile($apikey, "data/tests/small-file"); + $id = $ret["data"]["ids"][0]; + + $id2 = $id."invalid"; + $ret = $this->CallEndpoint("POST", "file/create_multipaste", array( + "apikey" => $apikey, + "ids[1]" => $id, + "ids[2]" => $id2, + )); + $this->expectError("create multipaste with wrong ID", $ret); + + $this->t->is_deeply(array( + 'status' => 'error', + 'error_id' => 'file/create_multipaste/verify-failed', + 'message' => 'Failed to verify ID(s)', + 'data' => + array ( + $id2 => + array ( + 'id' => $id2, + 'reason' => 'doesn\'t exist', + ), + ), + ), $ret, "expected error response"); + } + + public function test_create_multipaste_errorOnWrongOwner() + { + $apikey = $this->createUserAndApikey(); + $apikey2 = $this->createUserAndApikey(); + $ret = $this->uploadFile($apikey, "data/tests/small-file"); + $id = $ret["data"]["ids"][0]; + + $ret = $this->CallEndpoint("POST", "file/create_multipaste", array( + "apikey" => $apikey2, + "ids[1]" => $id, + )); + $this->expectError("create multipaste with wrong owner", $ret); + + $this->t->is_deeply(array( + 'status' => 'error', + 'error_id' => 'file/create_multipaste/verify-failed', + 'message' => 'Failed to verify ID(s)', + 'data' => + array ( + $id => + array ( + 'id' => $id, + 'reason' => 'not owned by you', + ), + ), + ), $ret, "expected error response"); + } } -- cgit v1.2.3-24-g4f1b From c302c5cc0143d96391aa544c15e4e0b64cba1182 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 8 Feb 2015 01:34:01 +0100 Subject: Add more tests Signed-off-by: Florian Pritz --- application/tests/test_api_v1.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'application/tests') diff --git a/application/tests/test_api_v1.php b/application/tests/test_api_v1.php index 18f2a37f6..faf0d9af7 100644 --- a/application/tests/test_api_v1.php +++ b/application/tests/test_api_v1.php @@ -84,6 +84,26 @@ class test_api_v1 extends Test { } } + public function test_callEndpointsWithoutEnoughPermissions() + { + $apikey = $this->createUserAndApikey(); + $endpoints = array( + "user/apikeys", + "user/create_apikey", + ); + foreach ($endpoints as $endpoint) { + $ret = $this->CallEndpoint("POST", $endpoint, array( + "apikey" => $apikey, + )); + $this->expectError("call $endpoint without enough permissions", $ret); + $this->t->is_deeply(array( + 'status' => "error", + 'error_id' => "api/insufficient-permissions", + 'message' => "Access denied: Access level too low", + ), $ret, "expected error"); + } + } + public function test_create_apikey_createNewKey() { $this->createUser(1); @@ -98,6 +118,22 @@ class test_api_v1 extends Test { $this->t->isnt($ret["data"]["new_key"], "", "apikey not empty"); } + public function test_apikeys_getApikey() + { + $userid = $this->createUser(2); + $apikey = $this->createApikey($userid); + $ret = $this->CallEndpoint("POST", "user/apikeys", array( + "username" => "testuser-api_v1-2", + "password" => "testpass2", + )); + $this->expectSuccess("get apikeys", $ret); + + $this->t->is($ret["data"][0]["key"], $apikey, "expected key 1"); + $this->t->is($ret["data"][0]["access_level"], "apikey", "expected key 1 acces_level"); + $this->t->is($ret["data"][0]["comment"], "", "expected key 1 comment"); + $this->t->ok(is_int($ret["data"][0]["created"]) , "expected key 1 creation time is int"); + } + public function test_history_empty() { $apikey = $this->createUserAndApikey(); -- cgit v1.2.3-24-g4f1b From 1e18ad9d38e0cf6e0e37d3c1ad66eace2bb82003 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 8 Feb 2015 10:57:48 +0100 Subject: tests: Fix php dev server being slow Signed-off-by: Florian Pritz --- application/tests/Test.php | 1 + 1 file changed, 1 insertion(+) (limited to 'application/tests') diff --git a/application/tests/Test.php b/application/tests/Test.php index 192061db9..e18aa9374 100644 --- a/application/tests/Test.php +++ b/application/tests/Test.php @@ -51,6 +51,7 @@ abstract class Test { curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Accept: application/json", + "Expect: ", )); $result = curl_exec($curl); -- cgit v1.2.3-24-g4f1b From f8513511ff5c77aca1cd2b7fc570d0dd34c4a417 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 8 Feb 2015 11:11:16 +0100 Subject: Add tests for invalid login Signed-off-by: Florian Pritz --- application/tests/test_api_v1.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'application/tests') diff --git a/application/tests/test_api_v1.php b/application/tests/test_api_v1.php index faf0d9af7..7fe06ca36 100644 --- a/application/tests/test_api_v1.php +++ b/application/tests/test_api_v1.php @@ -134,6 +134,38 @@ class test_api_v1 extends Test { $this->t->ok(is_int($ret["data"][0]["created"]) , "expected key 1 creation time is int"); } + public function test_authentication_invalidPassword() + { + $userid = $this->createUser(3); + $ret = $this->CallEndpoint("POST", "user/apikeys", array( + "username" => "testuser-api_v1-3", + "password" => "wrongpass", + )); + $this->expectError("invalid password", $ret); + + $this->t->is_deeply(array ( + 'status' => 'error', + 'error_id' => 'user/login-failed', + 'message' => 'Login failed', + ), $ret, "expected error"); + } + + public function test_authentication_invalidUser() + { + $userid = $this->createUser(4); + $ret = $this->CallEndpoint("POST", "user/apikeys", array( + "username" => "testuser-api_v1-invalid", + "password" => "testpass4", + )); + $this->expectError("invalid username", $ret); + + $this->t->is_deeply(array ( + 'status' => 'error', + 'error_id' => 'user/login-failed', + 'message' => 'Login failed', + ), $ret, "expected error"); + } + public function test_history_empty() { $apikey = $this->createUserAndApikey(); -- cgit v1.2.3-24-g4f1b From c8ff6f56b9b971f88fed840c2af50df11b7dc948 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 8 Feb 2015 23:29:25 +0100 Subject: Add tests for \s\f::verify_uploaded_files Signed-off-by: Florian Pritz --- application/tests/test_service_files.php | 85 ++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 application/tests/test_service_files.php (limited to 'application/tests') diff --git a/application/tests/test_service_files.php b/application/tests/test_service_files.php new file mode 100644 index 000000000..3330e8c22 --- /dev/null +++ b/application/tests/test_service_files.php @@ -0,0 +1,85 @@ + + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ + +namespace tests; + +class test_service_files extends Test { + + public function __construct() + { + parent::__construct(); + + $CI =& get_instance(); + $CI->load->model("muser"); + $CI->load->model("mfile"); + + } + + public function test_verify_uploaded_files_noFiles() + { + $a = array(); + try { + \service\files::verify_uploaded_files($a); + $this->t->fail("verify should error"); + } catch (\exceptions\UserInputException $e) { + $this->t->is($e->get_error_id(), "file/no-file", "verify should error"); + } + } + + public function test_verify_uploaded_files_normal() + { + $CI =& get_instance(); + $a = array( + array( + "name" => "foobar.txt", + "type" => "text/plain", + "tmp_name" => NULL, + "error" => UPLOAD_ERR_OK, + "size" => 1, + "formfield" => "file[1]", + ) + ); + + \service\files::verify_uploaded_files($a); + $this->t->pass("verify should work"); + } + + public function test_verify_uploaded_files_uploadError() + { + $CI =& get_instance(); + $a = array( + array( + "name" => "foobar.txt", + "type" => "text/plain", + "tmp_name" => NULL, + "error" => UPLOAD_ERR_NO_FILE, + "size" => 1, + "formfield" => "file[1]", + ) + ); + + try { + \service\files::verify_uploaded_files($a); + $this->t->fail("verify should error"); + } catch (\exceptions\UserInputException $e) { + $data = $e->get_data(); + $this->t->is($e->get_error_id(), "file/upload-verify", "verify should error"); + $this->t->is_deeply(array( + array( + 'filename' => 'foobar.txt', + 'formfield' => 'file[1]', + 'message' => 'No file was uploaded', + ), + ), $data, "expected data in exception"); + } + } + + +} + -- cgit v1.2.3-24-g4f1b From cb2df59b45d4cb35790472f76b06c59b22c6213b Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 10 Feb 2015 23:32:23 +0100 Subject: api: Require the version to start with v Makes the URL easier to understand (especially the v1 case). Signed-off-by: Florian Pritz --- application/tests/test_api_v1.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'application/tests') diff --git a/application/tests/test_api_v1.php b/application/tests/test_api_v1.php index 7fe06ca36..bba0f9180 100644 --- a/application/tests/test_api_v1.php +++ b/application/tests/test_api_v1.php @@ -23,7 +23,7 @@ class test_api_v1 extends Test { private function uploadFile($apikey, $file) { - $ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array( + $ret = $this->CallAPI("POST", "$this->server/api/v1.0.0/file/upload", array( "apikey" => $apikey, "file[1]" => curl_file_create($file), )); @@ -59,7 +59,7 @@ class test_api_v1 extends Test { private function callEndpoint($verb, $endpoint, $data) { - return $this->CallAPI($verb, "$this->server/api/1.0.0/$endpoint", $data); + return $this->CallAPI($verb, "$this->server/api/v1.0.0/$endpoint", $data); } public function test_callPrivateEndpointsWithoutApikey() -- cgit v1.2.3-24-g4f1b From b8facbbd7a9a29c6274c435932b9c810155e2460 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sat, 14 Feb 2015 19:12:13 +0100 Subject: Fix typo in error message Signed-off-by: Florian Pritz --- application/tests/test_api_v1.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'application/tests') diff --git a/application/tests/test_api_v1.php b/application/tests/test_api_v1.php index bba0f9180..e179abc69 100644 --- a/application/tests/test_api_v1.php +++ b/application/tests/test_api_v1.php @@ -212,7 +212,7 @@ class test_api_v1 extends Test { $this->t->is_deeply(array( 'status' => 'error', 'error_id' => 'file/no-file', - 'message' => 'No file was uploaded or unknown error occured.', + 'message' => 'No file was uploaded or unknown error occurred.', ), $ret, "expected reply"); } -- cgit v1.2.3-24-g4f1b From d7fc5f46a8b6faec4ec0c18089d94d21e505c36c Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sat, 14 Feb 2015 19:13:26 +0100 Subject: Use assoc array for service/user/apikeys Signed-off-by: Florian Pritz --- application/tests/test_api_v1.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'application/tests') diff --git a/application/tests/test_api_v1.php b/application/tests/test_api_v1.php index e179abc69..44b559852 100644 --- a/application/tests/test_api_v1.php +++ b/application/tests/test_api_v1.php @@ -128,10 +128,10 @@ class test_api_v1 extends Test { )); $this->expectSuccess("get apikeys", $ret); - $this->t->is($ret["data"][0]["key"], $apikey, "expected key 1"); - $this->t->is($ret["data"][0]["access_level"], "apikey", "expected key 1 acces_level"); - $this->t->is($ret["data"][0]["comment"], "", "expected key 1 comment"); - $this->t->ok(is_int($ret["data"][0]["created"]) , "expected key 1 creation time is int"); + $this->t->is($ret["data"]["apikeys"][$apikey]["key"], $apikey, "expected key 1"); + $this->t->is($ret["data"]["apikeys"][$apikey]["access_level"], "apikey", "expected key 1 acces_level"); + $this->t->is($ret["data"]["apikeys"][$apikey]["comment"], "", "expected key 1 comment"); + $this->t->ok(is_int($ret["data"]["apikeys"][$apikey]["created"]) , "expected key 1 creation time is int"); } public function test_authentication_invalidPassword() -- cgit v1.2.3-24-g4f1b From 4f5f2f496bdc182ac9edeb2f416d12fa5e56a9ca Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sat, 14 Feb 2015 19:13:59 +0100 Subject: Use assoc array for service/files/verify_uploaded_files Signed-off-by: Florian Pritz --- application/tests/test_service_files.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'application/tests') diff --git a/application/tests/test_service_files.php b/application/tests/test_service_files.php index 3330e8c22..8789a9888 100644 --- a/application/tests/test_service_files.php +++ b/application/tests/test_service_files.php @@ -71,7 +71,7 @@ class test_service_files extends Test { $data = $e->get_data(); $this->t->is($e->get_error_id(), "file/upload-verify", "verify should error"); $this->t->is_deeply(array( - array( + 'file[1]' => array( 'filename' => 'foobar.txt', 'formfield' => 'file[1]', 'message' => 'No file was uploaded', -- cgit v1.2.3-24-g4f1b