From b9256b14377251e6303d6abbe3cd7e7edc2583a4 Mon Sep 17 00:00:00 2001 From: cweiske Date: Tue, 28 Sep 2010 22:09:58 +0000 Subject: add header to httpauth.inc.php git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@765 b3834d28-1941-0410-a4f8-b48e95affb8f --- www/api/httpauth.inc.php | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) (limited to 'www/api') diff --git a/www/api/httpauth.inc.php b/www/api/httpauth.inc.php index 0e3a66d..ee5c7f2 100644 --- a/www/api/httpauth.inc.php +++ b/www/api/httpauth.inc.php @@ -1,10 +1,29 @@ + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ require_once '../www-header.php'; -// Provides HTTP Basic authentication of a user -// and logs the user in if necessary - -function authenticate() { +/** + * Sends HTTP auth headers to the browser + */ +function authenticate() +{ header('WWW-Authenticate: Basic realm="SemanticScuttle API"'); header('HTTP/1.0 401 Unauthorized'); @@ -26,7 +45,9 @@ if (!$userservice->isLoggedOn()) { if (!isset($_SERVER['PHP_AUTH_USER'])) { authenticate(); } else { - $login = $userservice->login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); + $login = $userservice->login( + $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] + ); if ($login) { $currentUser = $userservice->getCurrentObjectUser(); } else { -- cgit v1.2.3 From 22c9a01ee845d2b92fcab6b6cb10ac6ff0eec52e Mon Sep 17 00:00:00 2001 From: cweiske Date: Tue, 28 Sep 2010 22:14:31 +0000 Subject: rewrite api/posts/delete to be more secure and add unit tests for it git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@769 b3834d28-1941-0410-a4f8-b48e95affb8f --- src/SemanticScuttle/Service/Bookmark.php | 18 ++++++--- tests/Api/PostsDeleteTest.php | 9 +++-- www/api/posts_delete.php | 63 +++++++++++++++++++++++--------- 3 files changed, 62 insertions(+), 28 deletions(-) (limited to 'www/api') diff --git a/src/SemanticScuttle/Service/Bookmark.php b/src/SemanticScuttle/Service/Bookmark.php index dde1df5..4e18d3f 100644 --- a/src/SemanticScuttle/Service/Bookmark.php +++ b/src/SemanticScuttle/Service/Bookmark.php @@ -176,7 +176,10 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService * Retrieves a bookmark with the given URL. * DOES NOT RESPECT PRIVACY SETTINGS! * - * @param string $address URL to get bookmarks for + * @param string $address URL to get bookmarks for + * @param boolean $all Retrieve from all users (true) + * or only bookmarks owned by the current + * user (false) * * @return mixed Array with bookmark data or false in case * of an error (i.e. not found). @@ -184,9 +187,9 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService * @uses getBookmarkByHash() * @see getBookmarkByShortname() */ - public function getBookmarkByAddress($address) + public function getBookmarkByAddress($address, $all = true) { - return $this->getBookmarkByHash($this->getHash($address)); + return $this->getBookmarkByHash($this->getHash($address), $all); } @@ -195,16 +198,19 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService * Retrieves a bookmark with the given hash. * DOES NOT RESPECT PRIVACY SETTINGS! * - * @param string $hash URL hash + * @param string $hash URL hash + * @param boolean $all Retrieve from all users (true) + * or only bookmarks owned by the current + * user (false) * * @return mixed Array with bookmark data or false in case * of an error (i.e. not found). * * @see getHash() */ - public function getBookmarkByHash($hash) + public function getBookmarkByHash($hash, $all = true) { - return $this->_getbookmark('bHash', $hash, true); + return $this->_getbookmark('bHash', $hash, $all); } diff --git a/tests/Api/PostsDeleteTest.php b/tests/Api/PostsDeleteTest.php index 705f94e..626746f 100644 --- a/tests/Api/PostsDeleteTest.php +++ b/tests/Api/PostsDeleteTest.php @@ -202,8 +202,9 @@ class Api_PostsDeleteTest extends TestBaseApi //send request $res = $req->send(); - //401 - unauthorized - $this->assertEquals(401, $res->getStatus()); + //404 - user does not have that bookmark + $this->assertEquals(404, $res->getStatus()); + //verify MIME content type $this->assertEquals( 'text/xml; charset=utf-8', @@ -211,10 +212,10 @@ class Api_PostsDeleteTest extends TestBaseApi ); //verify xml - $this->assertNotTag( + $this->assertTag( array( 'tag' => 'result', - 'attributes' => array('code' => 'done') + 'attributes' => array('code' => 'something went wrong') ), $res->getBody(), '', false diff --git a/www/api/posts_delete.php b/www/api/posts_delete.php index a63cc62..982b686 100644 --- a/www/api/posts_delete.php +++ b/www/api/posts_delete.php @@ -1,33 +1,60 @@ + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ // Force HTTP authentication first! $httpContentType = 'text/xml'; require_once 'httpauth.inc.php'; -/* Service creation: only useful services are created */ -$bookmarkservice =SemanticScuttle_Service_Factory::get('Bookmark'); - +$bs = SemanticScuttle_Service_Factory::get('Bookmark'); +$uId = $userservice->getCurrentUserId(); -// Note that del.icio.us only errors out if no URL was passed in; there's no error on attempting -// to delete a bookmark you don't have. // Error out if there's no address -if (is_null($_REQUEST['url'])) { +if (!isset($_REQUEST['url']) + || $_REQUEST['url'] == '' +) { $deleted = false; +} else if (!$bs->bookmarkExists($_REQUEST['url'], $uId)) { + //the user does not have such a bookmark + // Note that del.icio.us only errors out if no URL was passed in; + // there's no error on attempting to delete a bookmark you don't have. + // this sucks, and I don't care about being different but correct here. + header('HTTP/1.0 404 Not Found'); + $deleted = false; + } else { - $bookmark = $bookmarkservice->getBookmarkByAddress($_REQUEST['url']); - $bid = $bookmark['bId']; - $delete = $bookmarkservice->deleteBookmark($bid); - $deleted = true; + $bookmark = $bs->getBookmarkByAddress($_REQUEST['url'], false); + $bId = $bookmark['bId']; + $deleted = $bs->deleteBookmark($bId); + if (!$deleted) { + //something really went wrong + header('HTTP/1.0 500 Internal Server Error'); + } } // Set up the XML file and output the result. -echo '\r\n"; -echo ''; +echo '\r\n"; +echo ''; ?> \ No newline at end of file -- cgit v1.2.3 From 70c39a8eea7896271c0ad3f0c435ec06c64074d1 Mon Sep 17 00:00:00 2001 From: cweiske Date: Wed, 29 Sep 2010 20:49:14 +0000 Subject: delicious returns a proper error message when deleting non-existant items, which we do now, too git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@770 b3834d28-1941-0410-a4f8-b48e95affb8f --- doc/developers/api | 10 ++++++++++ tests/Api/PostsDeleteTest.php | 2 +- www/api/posts_delete.php | 14 +++++--------- 3 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 doc/developers/api (limited to 'www/api') diff --git a/doc/developers/api b/doc/developers/api new file mode 100644 index 0000000..efa05fe --- /dev/null +++ b/doc/developers/api @@ -0,0 +1,10 @@ +SemanticScuttle API +=================== + +SemanticScuttle tries to implement the delicious API v1 as closely as sensible. + +Where it makes sense and the delicious API just does things plainly wrong +(i.e. when returning a wrong status code on an error), we do it better. + +- http://www.delicious.com/help/api +- http://support.delicious.com/forum/comments.php?DiscussionID=5286&page=1 diff --git a/tests/Api/PostsDeleteTest.php b/tests/Api/PostsDeleteTest.php index 626746f..d9fb6cd 100644 --- a/tests/Api/PostsDeleteTest.php +++ b/tests/Api/PostsDeleteTest.php @@ -215,7 +215,7 @@ class Api_PostsDeleteTest extends TestBaseApi $this->assertTag( array( 'tag' => 'result', - 'attributes' => array('code' => 'something went wrong') + 'attributes' => array('code' => 'item not found') ), $res->getBody(), '', false diff --git a/www/api/posts_delete.php b/www/api/posts_delete.php index 982b686..03cc968 100644 --- a/www/api/posts_delete.php +++ b/www/api/posts_delete.php @@ -4,8 +4,6 @@ * The delicious API is implemented here. * * The delicious API behaves like that: - * - returns "done" even if the bookmark doesn't exist - * - we do it correctly * - does NOT allow the hash for the url parameter * - doesn't set the Content-Type to text/xml * - we do it correctly, too @@ -35,26 +33,24 @@ $uId = $userservice->getCurrentUserId(); if (!isset($_REQUEST['url']) || $_REQUEST['url'] == '' ) { - $deleted = false; + $msg = 'something went wrong'; } else if (!$bs->bookmarkExists($_REQUEST['url'], $uId)) { //the user does not have such a bookmark - // Note that del.icio.us only errors out if no URL was passed in; - // there's no error on attempting to delete a bookmark you don't have. - // this sucks, and I don't care about being different but correct here. header('HTTP/1.0 404 Not Found'); - $deleted = false; - + $msg = 'item not found'; } else { $bookmark = $bs->getBookmarkByAddress($_REQUEST['url'], false); $bId = $bookmark['bId']; $deleted = $bs->deleteBookmark($bId); + $msg = 'done'; if (!$deleted) { //something really went wrong header('HTTP/1.0 500 Internal Server Error'); + $msg = 'something really went wrong'; } } // Set up the XML file and output the result. echo '\r\n"; -echo ''; +echo ''; ?> \ No newline at end of file -- cgit v1.2.3 From 20ec8b4958dc2d6a4a08f9b0dcae27c90f0155ef Mon Sep 17 00:00:00 2001 From: cweiske Date: Wed, 29 Sep 2010 20:50:38 +0000 Subject: test for api/posts/update git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@771 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/AllTests.php | 1 + tests/Api/PostsUpdateTest.php | 135 ++++++++++++++++++++++++++++++++++++++++++ www/api/posts_delete.php | 1 + www/api/posts_update.php | 46 ++++++++++---- 4 files changed, 170 insertions(+), 13 deletions(-) create mode 100644 tests/Api/PostsUpdateTest.php (limited to 'www/api') diff --git a/tests/AllTests.php b/tests/AllTests.php index 61e1a57..799f43b 100644 --- a/tests/AllTests.php +++ b/tests/AllTests.php @@ -66,6 +66,7 @@ class AllTests extends PHPUnit_Framework_TestSuite $suite->addTestFile($tdir . '/UserTest.php'); $suite->addTestFile($tdir . '/Api/ExportCsvTest.php'); $suite->addTestFile($tdir . '/Api/PostsDeleteTest.php'); + $suite->addTestFile($tdir . '/Api/PostsUpdateTest.php'); return $suite; } diff --git a/tests/Api/PostsUpdateTest.php b/tests/Api/PostsUpdateTest.php new file mode 100644 index 0000000..c497a55 --- /dev/null +++ b/tests/Api/PostsUpdateTest.php @@ -0,0 +1,135 @@ + + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +require_once dirname(__FILE__) . '/../prepare.php'; +require_once 'HTTP/Request2.php'; + +if (!defined('PHPUnit_MAIN_METHOD')) { + define('PHPUnit_MAIN_METHOD', 'Api_PostsUpdateTest::main'); +} + +/** + * Unit tests for the SemanticScuttle last-update time API. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Benjamin Huynh-Kim-Bang + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ +class Api_PostsUpdateTest extends TestBaseApi +{ + protected $urlPart = 'api/posts/update'; + + + + /** + * Used to run this test class standalone + * + * @return void + */ + public static function main() + { + require_once 'PHPUnit/TextUI/TestRunner.php'; + PHPUnit_TextUI_TestRunner::run( + new PHPUnit_Framework_TestSuite(__CLASS__) + ); + } + + + + /** + * Test if authentication is required when sending no auth data + */ + public function testAuthWithoutAuthData() + { + $req = $this->getRequest(null, false); + $res = $req->send(); + $this->assertEquals(401, $res->getStatus()); + } + + + + /** + * Test if authentication is required when sending wrong user data + + */ + public function testAuthWrongCredentials() + { + $req = $this->getRequest(null, false); + $req->setAuth('user', 'password', HTTP_Request2::AUTH_BASIC); + $res = $req->send(); + $this->assertEquals(401, $res->getStatus()); + } + + + + /** + * See if posts/update behaves correct if there is one bookmark + */ + public function testPostUpdateOneBookmark() + { + $this->bs->deleteAll(); + + list($req, $uId) = $this->getAuthRequest(); + $bId = $this->addBookmark( + $uId, 'http://example.org/tag1', 0, + array('unittest', 'tag1') + ); + + $data = $this->bs->getBookmarks(0, null, $uId); + $this->assertEquals(1, $data['total']); + $bookmark = $data['bookmarks'][0]; + + //send request + $res = $req->send(); + + $this->assertEquals(200, $res->getStatus()); + //verify MIME content type + $this->assertEquals( + 'text/xml; charset=utf-8', + $res->getHeader('content-type') + ); + + //verify xml + $this->assertTag( + array( + 'tag' => 'update', + 'attributes' => array( + 'inboxnew' => '0' + ) + ), + $res->getBody(), + '', false + ); + //check time + $xml = simplexml_load_string($res->getBody()); + $this->assertTrue(isset($xml['time'])); + $this->assertEquals( + strtotime($bookmark['bDatetime']), + strtotime( + (string)$xml['time'] + ) + ); + } + +} + +if (PHPUnit_MAIN_METHOD == 'Api_PostsUpdateTest::main') { + Api_PostsUpdateTest::main(); +} +?> \ No newline at end of file diff --git a/www/api/posts_delete.php b/www/api/posts_delete.php index 03cc968..69b2429 100644 --- a/www/api/posts_delete.php +++ b/www/api/posts_delete.php @@ -19,6 +19,7 @@ * @author Eric Dane * @license GPL http://www.gnu.org/licenses/gpl.html * @link http://sourceforge.net/projects/semanticscuttle + * @link http://www.delicious.com/help/api */ // Force HTTP authentication first! diff --git a/www/api/posts_update.php b/www/api/posts_update.php index 4aeedc3..4b080e2 100644 --- a/www/api/posts_update.php +++ b/www/api/posts_update.php @@ -1,24 +1,44 @@ + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + * @link http://www.delicious.com/help/api + */ // Force HTTP authentication first! $httpContentType = 'text/xml'; require_once 'httpauth.inc.php'; -/* Service creation: only useful services are created */ -$bookmarkservice =SemanticScuttle_Service_Factory::get('Bookmark'); - - -// Get the posts relevant to the passed-in variables. -$bookmarks =& $bookmarkservice->getBookmarks(0, 1, $userservice->getCurrentUserId()); +$bs = SemanticScuttle_Service_Factory::get('Bookmark'); +$bookmarks = $bs->getBookmarks(0, 1, $userservice->getCurrentUserId()); // Set up the XML file and output all the tags. -echo '\r\n"; -foreach($bookmarks['bookmarks'] as $row) { - echo ''; +echo '\r\n"; +//foreach is used in case there are no bookmarks +foreach ($bookmarks['bookmarks'] as $row) { + echo ''; } ?> \ No newline at end of file -- cgit v1.2.3 From 2f3b23f96a5f595c9d0ef02abf5037f41e907390 Mon Sep 17 00:00:00 2001 From: cweiske Date: Wed, 29 Sep 2010 20:52:01 +0000 Subject: first tests for api/posts/add git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@772 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/AllTests.php | 1 + tests/Api/PostsAddTest.php | 197 +++++++++++++++++++++++++++++++++++++++++++++ www/api/posts_add.php | 2 + 3 files changed, 200 insertions(+) create mode 100644 tests/Api/PostsAddTest.php (limited to 'www/api') diff --git a/tests/AllTests.php b/tests/AllTests.php index 799f43b..ded6824 100644 --- a/tests/AllTests.php +++ b/tests/AllTests.php @@ -65,6 +65,7 @@ class AllTests extends PHPUnit_Framework_TestSuite $suite->addTestFile($tdir . '/VoteTest.php'); $suite->addTestFile($tdir . '/UserTest.php'); $suite->addTestFile($tdir . '/Api/ExportCsvTest.php'); + $suite->addTestFile($tdir . '/Api/PostsAddTest.php'); $suite->addTestFile($tdir . '/Api/PostsDeleteTest.php'); $suite->addTestFile($tdir . '/Api/PostsUpdateTest.php'); return $suite; diff --git a/tests/Api/PostsAddTest.php b/tests/Api/PostsAddTest.php new file mode 100644 index 0000000..1db8af9 --- /dev/null +++ b/tests/Api/PostsAddTest.php @@ -0,0 +1,197 @@ + + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +require_once dirname(__FILE__) . '/../prepare.php'; +require_once 'HTTP/Request2.php'; + +if (!defined('PHPUnit_MAIN_METHOD')) { + define('PHPUnit_MAIN_METHOD', 'Api_PostsAddTest::main'); +} + +/** + * Unit tests for the SemanticScuttle post addition API. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Benjamin Huynh-Kim-Bang + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ +class Api_PostsAddTest extends TestBaseApi +{ + protected $urlPart = 'api/posts/add'; + + + + /** + * Used to run this test class standalone + * + * @return void + */ + public static function main() + { + require_once 'PHPUnit/TextUI/TestRunner.php'; + PHPUnit_TextUI_TestRunner::run( + new PHPUnit_Framework_TestSuite(__CLASS__) + ); + } + + + + /** + * Test if authentication is required when sending no auth data + */ + public function testAuthWithoutAuthData() + { + $req = $this->getRequest(null, false); + $res = $req->send(); + $this->assertEquals(401, $res->getStatus()); + } + + + + /** + * Test if authentication is required when sending wrong user data + */ + public function testAuthWrongCredentials() + { + $req = $this->getRequest(null, false); + $req->setAuth('user', 'password', HTTP_Request2::AUTH_BASIC); + $res = $req->send(); + $this->assertEquals(401, $res->getStatus()); + } + + + + /** + * Test if adding a bookmark via POST works. + */ + public function testAddBookmarkPost() + { + $this->bs->deleteAll(); + + $bmUrl = 'http://example.org/tag-1'; + $bmTags = array('foo', 'bar', 'baz'); + $bmDatetime = '2010-09-08T03:02:01Z'; + $bmTitle = 'This is a foo title'; + $bmDescription = <<?&\$ÄÖ'"§special"' +characters +TXT; + + list($req, $uId) = $this->getAuthRequest(); + $req->setMethod(HTTP_Request2::METHOD_POST); + $req->addPostParameter('url', $bmUrl); + $req->addPostParameter('description', $bmTitle); + $req->addPostParameter('extended', $bmDescription); + $req->addPostParameter('tags', implode(' ', $bmTags)); + $res = $req->send(); + + //all should be well + $this->assertEquals(200, $res->getStatus()); + //verify MIME content type + $this->assertEquals( + 'text/xml; charset=utf-8', + $res->getHeader('content-type') + ); + + //verify xml + $this->assertTag( + array( + 'tag' => 'result', + 'attributes' => array('code' => 'done') + ), + $res->getBody(), + null, false + ); + + //user should have one bookmark now + $data = $this->bs->getBookmarks(0, null, $uId); + $this->assertEquals(1, $data['total']); + $bm = $data['bookmarks'][0]; + + $this->assertEquals($bmUrl, $bm['bAddress']); + $this->assertEquals($bmTitle, $bm['bTitle']); + $this->assertEquals($bmDescription, $bm['bDescription']); + $this->assertEquals($bmTags, $bm['tags']); + } + + + + /** + * Test if adding a bookmark via GET works. + */ + public function testAddBookmarkGet() + { + $this->bs->deleteAll(); + + $bmUrl = 'http://example.org/tag-1'; + $bmTags = array('foo', 'bar', 'baz'); + $bmDatetime = '2010-09-08T03:02:01Z'; + $bmTitle = 'This is a foo title'; + $bmDescription = <<?&\$ÄÖ'"§special"' +characters +TXT; + + list($req, $uId) = $this->getAuthRequest( + '?url=' . urlencode($bmUrl) + . '&description=' . urlencode($bmTitle) + . '&extended=' . urlencode($bmDescription) + . '&tags=' . urlencode(implode(' ', $bmTags)) + ); + $res = $req->send(); + + //all should be well + $this->assertEquals(200, $res->getStatus()); + //verify MIME content type + $this->assertEquals( + 'text/xml; charset=utf-8', + $res->getHeader('content-type') + ); + + //verify xml + $this->assertTag( + array( + 'tag' => 'result', + 'attributes' => array('code' => 'done') + ), + $res->getBody(), + null, false + ); + + //user should have one bookmark now + $data = $this->bs->getBookmarks(0, null, $uId); + $this->assertEquals(1, $data['total']); + $bm = $data['bookmarks'][0]; + + $this->assertEquals($bmUrl, $bm['bAddress']); + $this->assertEquals($bmTitle, $bm['bTitle']); + $this->assertEquals($bmDescription, $bm['bDescription']); + $this->assertEquals($bmTags, $bm['tags']); + } + +} + +if (PHPUnit_MAIN_METHOD == 'Api_PostsAddTest::main') { + Api_PostsAddTest::main(); +} +?> \ No newline at end of file diff --git a/www/api/posts_add.php b/www/api/posts_add.php index 59f7dce..b392a80 100644 --- a/www/api/posts_add.php +++ b/www/api/posts_add.php @@ -5,6 +5,8 @@ // del.icio.us behavior: // - tags can't have spaces // - address and description are mandatory +// - description == title in semanticscuttle +// - extended == description in semanticscuttle // Scuttle behavior: // - Additional 'status' variable for privacy -- cgit v1.2.3 From 3ff661c0e0180b7808b1b95f9d25cc736d710026 Mon Sep 17 00:00:00 2001 From: cweiske Date: Wed, 29 Sep 2010 20:55:14 +0000 Subject: send 400 status code if a parameter is missing git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@775 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/Api/PostsAddTest.php | 107 +++++++++++++++++++++++++++++++++++++++++++++ www/api/posts_add.php | 26 +++++++---- 2 files changed, 124 insertions(+), 9 deletions(-) (limited to 'www/api') diff --git a/tests/Api/PostsAddTest.php b/tests/Api/PostsAddTest.php index 0769bdd..dea002e 100644 --- a/tests/Api/PostsAddTest.php +++ b/tests/Api/PostsAddTest.php @@ -199,6 +199,113 @@ TXT; ); } + /** + * Verify that the URL and description/title are enough parameters + * to add a bookmark. + */ + public function testUrlDescEnough() + { + $this->bs->deleteAll(); + + list($req, $uId) = $this->getAuthRequest(); + $req->setMethod(HTTP_Request2::METHOD_POST); + $req->addPostParameter('url', 'http://example.org/tag2'); + $req->addPostParameter('description', 'foo bar'); + $res = $req->send(); + + //all should be well + $this->assertEquals(200, $res->getStatus()); + //verify MIME content type + $this->assertEquals( + 'text/xml; charset=utf-8', + $res->getHeader('content-type') + ); + + //verify xml + $this->assertTag( + array( + 'tag' => 'result', + 'attributes' => array('code' => 'done') + ), + $res->getBody(), + null, false + ); + + //user has 1 bookmark now + $data = $this->bs->getBookmarks(0, null, $uId); + $this->assertEquals(1, $data['total']); + } + + /** + * Verify that the URL is required + */ + public function testUrlRequired() + { + $this->bs->deleteAll(); + + list($req, $uId) = $this->getAuthRequest(); + $req->setMethod(HTTP_Request2::METHOD_POST); + //$req->addPostParameter('url', 'http://example.org/tag2'); + $req->addPostParameter('description', 'foo bar'); + $res = $req->send(); + + //all should be well + $this->assertEquals(400, $res->getStatus()); + //verify MIME content type + $this->assertEquals( + 'text/xml; charset=utf-8', + $res->getHeader('content-type') + ); + + //verify xml + $this->assertTag( + array( + 'tag' => 'result', + 'attributes' => array('code' => 'URL missing') + ), + $res->getBody(), + null, false + ); + + //user still has 0 bookmarks + $data = $this->bs->getBookmarks(0, null, $uId); + $this->assertEquals(0, $data['total']); + } + + /** + * Verify that the description/title is required + */ + public function testDescriptionRequired() + { + $this->bs->deleteAll(); + + list($req, $uId) = $this->getAuthRequest(); + $req->setMethod(HTTP_Request2::METHOD_POST); + $req->addPostParameter('url', 'http://example.org/tag2'); + $res = $req->send(); + + //all should be well + $this->assertEquals(400, $res->getStatus()); + //verify MIME content type + $this->assertEquals( + 'text/xml; charset=utf-8', + $res->getHeader('content-type') + ); + + //verify xml + $this->assertTag( + array( + 'tag' => 'result', + 'attributes' => array('code' => 'Description missing') + ), + $res->getBody(), + null, false + ); + + //user still has 0 bookmarks + $data = $this->bs->getBookmarks(0, null, $uId); + $this->assertEquals(0, $data['total']); + } } if (PHPUnit_MAIN_METHOD == 'Api_PostsAddTest::main') { diff --git a/www/api/posts_add.php b/www/api/posts_add.php index b392a80..8b63a16 100644 --- a/www/api/posts_add.php +++ b/www/api/posts_add.php @@ -73,17 +73,25 @@ if (isset($_REQUEST['shared']) && (trim($_REQUEST['shared']) == 'no')) { } // Error out if there's no address or description -if (is_null($url) || is_null($description)) { - $added = false; +if (is_null($url)) { + header('HTTP/1.0 400 Bad Request'); + $msg = 'URL missing'; +} else if (is_null($description)) { + header('HTTP/1.0 400 Bad Request'); + $msg = 'Description missing'; } else { -// We're good with info; now insert it! - if ($bookmarkservice->bookmarkExists($url, $userservice->getCurrentUserId())) - $added = false; - else - $added = $bookmarkservice->addBookmark($url, $description, $extended, '', $status, $tags, null, $dt, true); + // We're good with info; now insert it! + if ($bookmarkservice->bookmarkExists($url, $userservice->getCurrentUserId())) { + $msg = 'something went wrong'; + } else { + $added = $bookmarkservice->addBookmark( + $url, $description, $extended, '', $status, $tags, null, $dt, true + ); + $msg = 'done'; + } } // Set up the XML file and output the result. -echo '\r\n"; -echo ''; +echo '\r\n"; +echo ''; ?> \ No newline at end of file -- cgit v1.2.3 From 66af94feaf9ef817da260c0d293577bf74e53bcf Mon Sep 17 00:00:00 2001 From: cweiske Date: Wed, 29 Sep 2010 20:56:10 +0000 Subject: reformat api/posts/add and document it better git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@776 b3834d28-1941-0410-a4f8-b48e95affb8f --- www/api/posts_add.php | 115 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 74 insertions(+), 41 deletions(-) (limited to 'www/api') diff --git a/www/api/posts_add.php b/www/api/posts_add.php index 8b63a16..0e06d50 100644 --- a/www/api/posts_add.php +++ b/www/api/posts_add.php @@ -1,69 +1,102 @@ + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + * @link http://www.delicious.com/help/api + */ // Force HTTP authentication $httpContentType = 'text/xml'; require_once 'httpauth.inc.php'; -/* Service creation: only useful services are created */ -$bookmarkservice =SemanticScuttle_Service_Factory::get('Bookmark'); +$bs = SemanticScuttle_Service_Factory::get('Bookmark'); // Get all the bookmark's passed-in information -if (isset($_REQUEST['url']) && (trim($_REQUEST['url']) != '')) +if (isset($_REQUEST['url']) && (trim($_REQUEST['url']) != '')) { $url = trim(urldecode($_REQUEST['url'])); -else - $url = NULL; +} else { + $url = null; +} -if (isset($_REQUEST['description']) && (trim($_REQUEST['description']) != '')) +if (isset($_REQUEST['description']) && (trim($_REQUEST['description']) != '')) { $description = trim($_REQUEST['description']); -else - $description = NULL; +} else { + $description = null; +} -if (isset($_REQUEST['extended']) && (trim($_REQUEST['extended']) != "")) +if (isset($_REQUEST['extended']) && (trim($_REQUEST['extended']) != '')) { $extended = trim($_REQUEST['extended']); -else - $extended = NULL; +} else { + $extended = null; +} -if (isset($_REQUEST['tags']) && (trim($_REQUEST['tags']) != '') && (trim($_REQUEST['tags']) != ',')) +if (isset($_REQUEST['tags']) && (trim($_REQUEST['tags']) != '') + && (trim($_REQUEST['tags']) != ',') +) { $tags = trim($_REQUEST['tags']); -else - $tags = NULL; +} else { + $tags = null; +} -if (isset($_REQUEST['dt']) && (trim($_REQUEST['dt']) != '')) +if (isset($_REQUEST['dt']) && (trim($_REQUEST['dt']) != '')) { $dt = trim($_REQUEST['dt']); -else - $dt = NULL; +} else { + $dt = null; +} $status = 0; if (isset($_REQUEST['status'])) { $status_str = trim($_REQUEST['status']); if (is_numeric($status_str)) { $status = intval($status_str); - if($status < 0 || $status > 2) { + if ($status < 0 || $status > 2) { $status = 0; } } else { switch ($status_str) { - case 'private': - $status = 2; - break; - case 'shared': - $status = 1; - break; - default: - $status = 0; - break; + case 'private': + $status = 2; + break; + case 'shared': + $status = 1; + break; + default: + $status = 0; + break; } } } @@ -81,10 +114,10 @@ if (is_null($url)) { $msg = 'Description missing'; } else { // We're good with info; now insert it! - if ($bookmarkservice->bookmarkExists($url, $userservice->getCurrentUserId())) { + if ($bs->bookmarkExists($url, $userservice->getCurrentUserId())) { $msg = 'something went wrong'; } else { - $added = $bookmarkservice->addBookmark( + $added = $bs->addBookmark( $url, $description, $extended, '', $status, $tags, null, $dt, true ); $msg = 'done'; -- cgit v1.2.3 From 8e2b25a095ab769146ac7fa672d7a4c4eda32ab4 Mon Sep 17 00:00:00 2001 From: cweiske Date: Wed, 29 Sep 2010 20:57:30 +0000 Subject: api/posts/add respects the "replace" parameter now git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@777 b3834d28-1941-0410-a4f8-b48e95affb8f --- doc/ChangeLog | 7 +-- tests/Api/PostsAddTest.php | 121 +++++++++++++++++++++++++++++++++++++++++++++ www/api/posts_add.php | 24 +++++++-- 3 files changed, 145 insertions(+), 7 deletions(-) (limited to 'www/api') diff --git a/doc/ChangeLog b/doc/ChangeLog index e2562e4..d711dd4 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -3,12 +3,13 @@ ChangeLog for SemantiScuttle 0.9X.X - 2010-XX-XX ------------------- -- Fix bug getTagsForBookmarks() that fetched all tags +- Fix bug in getTagsForBookmarks() that fetched all tags +- Fix bug #3073215: Updating bookmark time does not work +- Fix bug #3074816: French translation breaks edit javascript - Show error message on mysqli connection errors - Implement patch #3059829: update FR_CA translation - Update php-gettext library to 1.0.10 -- Fix bug #3073215: Updating bookmark time does not work -- Fix bug #3074816: French translation breaks edit javascript +- api/posts/add respects the "replace" parameter now 0.97.0 - 2010-06-09 diff --git a/tests/Api/PostsAddTest.php b/tests/Api/PostsAddTest.php index dea002e..1f21d04 100644 --- a/tests/Api/PostsAddTest.php +++ b/tests/Api/PostsAddTest.php @@ -306,6 +306,127 @@ TXT; $data = $this->bs->getBookmarks(0, null, $uId); $this->assertEquals(0, $data['total']); } + + /** + * Test that the replace=no parameter prevents the bookmark from being + * overwritten. + */ + public function testReplaceNo() + { + $this->bs->deleteAll(); + + $url = 'http://example.org/tag2'; + $title1 = 'foo bar 1'; + $title2 = 'bar 2 foo'; + + list($req, $uId) = $this->getAuthRequest(); + $req->setMethod(HTTP_Request2::METHOD_POST); + $req->addPostParameter('url', $url); + $req->addPostParameter('description', $title1); + $res = $req->send(); + + //all should be well + $this->assertEquals(200, $res->getStatus()); + + //send it a second time, with different title + list($req, $dummy) = $this->getAuthRequest(); + $req->setMethod(HTTP_Request2::METHOD_POST); + $req->addPostParameter('url', $url); + $req->addPostParameter('description', $title2); + $req->addPostParameter('replace', 'no'); + $res = $req->send(); + + //this time we should get an error + $this->assertEquals(409, $res->getStatus()); + //verify MIME content type + $this->assertEquals( + 'text/xml; charset=utf-8', + $res->getHeader('content-type') + ); + + //verify xml + $this->assertTag( + array( + 'tag' => 'result', + 'attributes' => array('code' => 'bookmark does already exist') + ), + $res->getBody(), + null, false + ); + + //user still has 1 bookmark now + $data = $this->bs->getBookmarks(0, null, $uId); + $this->assertEquals(1, $data['total']); + $this->assertEquals($title1, $data['bookmarks'][0]['bTitle']); + + //send it a third time, without the replace parameter + // it defaults to "no", so the bookmark should not get overwritten + list($req, $dummy) = $this->getAuthRequest(); + $req->setMethod(HTTP_Request2::METHOD_POST); + $req->addPostParameter('url', $url); + $req->addPostParameter('description', $title2); + $res = $req->send(); + + //this time we should get an error + $this->assertEquals(409, $res->getStatus()); + + //bookmark should not have changed + $data = $this->bs->getBookmarks(0, null, $uId); + $this->assertEquals(1, $data['total']); + $this->assertEquals($title1, $data['bookmarks'][0]['bTitle']); + } + + /** + * Test that the replace=yes parameter causes the bookmark to be updated. + */ + public function testReplaceYes() + { + $this->bs->deleteAll(); + + $url = 'http://example.org/tag2'; + $title1 = 'foo bar 1'; + $title2 = 'bar 2 foo'; + + list($req, $uId) = $this->getAuthRequest(); + $req->setMethod(HTTP_Request2::METHOD_POST); + $req->addPostParameter('url', $url); + $req->addPostParameter('description', $title1); + $res = $req->send(); + + //all should be well + $this->assertEquals(200, $res->getStatus()); + + //send it a second time, with different title + list($req, $dummy) = $this->getAuthRequest(); + $req->setMethod(HTTP_Request2::METHOD_POST); + $req->addPostParameter('url', $url); + $req->addPostParameter('description', $title2); + $req->addPostParameter('replace', 'yes'); + $res = $req->send(); + + //no error + $this->assertEquals(200, $res->getStatus()); + //verify MIME content type + $this->assertEquals( + 'text/xml; charset=utf-8', + $res->getHeader('content-type') + ); + + //verify xml + $this->assertTag( + array( + 'tag' => 'result', + 'attributes' => array('code' => 'done') + ), + $res->getBody(), + null, false + ); + + //user still has 1 bookmark now, but with the new title + $data = $this->bs->getBookmarks(0, null, $uId); + $this->assertEquals(1, $data['total']); + $this->assertEquals($title2, $data['bookmarks'][0]['bTitle']); + } } if (PHPUnit_MAIN_METHOD == 'Api_PostsAddTest::main') { diff --git a/www/api/posts_add.php b/www/api/posts_add.php index 0e06d50..7f9dc59 100644 --- a/www/api/posts_add.php +++ b/www/api/posts_add.php @@ -16,6 +16,8 @@ * - 0 or 'public': Everyone can see the bookmark * @param string $shared "no" or "yes": Switches between private and * public (optional) + * @param string $replace "yes" or "no" - replaces a bookmark with the + * same URL (optional) * * Notes: * - tags cannot have spaces @@ -23,7 +25,6 @@ * - delicious "description" is the "title" in SemanticScuttle * - delicious "extended" is the "description" in SemanticScuttle * - "status" is a SemanticScuttle addition to this API method - * - SemanticScuttle currently ignores the "replace" parameter * * SemanticScuttle - your social bookmark manager. * @@ -78,6 +79,8 @@ if (isset($_REQUEST['dt']) && (trim($_REQUEST['dt']) != '')) { $dt = null; } +$replace = isset($_REQUEST['replace']) && ($_REQUEST['replace'] == 'yes'); + $status = 0; if (isset($_REQUEST['status'])) { $status_str = trim($_REQUEST['status']); @@ -114,9 +117,22 @@ if (is_null($url)) { $msg = 'Description missing'; } else { // We're good with info; now insert it! - if ($bs->bookmarkExists($url, $userservice->getCurrentUserId())) { - $msg = 'something went wrong'; - } else { + $exists = $bs->bookmarkExists($url, $userservice->getCurrentUserId()); + if ($exists) { + if (!$replace) { + header('HTTP/1.0 409 Conflict'); + $msg = 'bookmark does already exist'; + } else { + //delete it before we re-add it + $bookmark = $bs->getBookmarkByAddress($url, false); + $bId = $bookmark['bId']; + $bs->deleteBookmark($bId); + + $exists = false; + } + } + + if (!$exists) { $added = $bs->addBookmark( $url, $description, $extended, '', $status, $tags, null, $dt, true ); -- cgit v1.2.3