X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2Fapi%2FApiUtilsTest.php;h=ea0ae50087d04ee5ac89c9a681f97f6676c88979;hb=dea72c711ff740b3b829d238fcf85648465143a0;hp=10da1459a1fbfee77f469ba251ea6036884d3c2d;hpb=fc11ab2f290a3712b766d78fdbcd354625a35d0a;p=github%2Fshaarli%2FShaarli.git diff --git a/tests/api/ApiUtilsTest.php b/tests/api/ApiUtilsTest.php index 10da1459..ea0ae500 100644 --- a/tests/api/ApiUtilsTest.php +++ b/tests/api/ApiUtilsTest.php @@ -2,10 +2,12 @@ namespace Shaarli\Api; +use Shaarli\Http\Base64Url; + /** * Class ApiUtilsTest */ -class ApiUtilsTest extends \PHPUnit_Framework_TestCase +class ApiUtilsTest extends \PHPUnit\Framework\TestCase { /** * Force the timezone for ISO datetimes. @@ -24,14 +26,14 @@ class ApiUtilsTest extends \PHPUnit_Framework_TestCase */ public static function generateValidJwtToken($secret) { - $header = base64_encode('{ + $header = Base64Url::encode('{ "typ": "JWT", "alg": "HS512" }'); - $payload = base64_encode('{ + $payload = Base64Url::encode('{ "iat": '. time() .' }'); - $signature = hash_hmac('sha512', $header .'.'. $payload , $secret); + $signature = Base64Url::encode(hash_hmac('sha512', $header .'.'. $payload, $secret, true)); return $header .'.'. $payload .'.'. $signature; } @@ -46,9 +48,9 @@ class ApiUtilsTest extends \PHPUnit_Framework_TestCase */ public static function generateCustomJwtToken($header, $payload, $secret) { - $header = base64_encode($header); - $payload = base64_encode($payload); - $signature = hash_hmac('sha512', $header . '.' . $payload, $secret); + $header = Base64Url::encode($header); + $payload = Base64Url::encode($payload); + $signature = Base64Url::encode(hash_hmac('sha512', $header . '.' . $payload, $secret, true)); return $header . '.' . $payload . '.' . $signature; } @@ -203,4 +205,147 @@ class ApiUtilsTest extends \PHPUnit_Framework_TestCase $token = $this->generateCustomJwtToken('{"JSON":1}', '{"iat":' . (time() + 60) . '}', 'secret'); ApiUtils::validateJwtToken($token, 'secret'); } + + /** + * Test formatLink() with a link using all useful fields. + */ + public function testFormatLinkComplete() + { + $indexUrl = 'https://domain.tld/sub/'; + $link = [ + 'id' => 12, + 'url' => 'http://lol.lol', + 'shorturl' => 'abc', + 'title' => 'Important Title', + 'description' => 'It is very lol' . PHP_EOL . 'new line', + 'tags' => 'blip .blop ', + 'private' => '1', + 'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'), + 'updated' => \DateTime::createFromFormat('Ymd_His', '20170107_160612'), + ]; + + $expected = [ + 'id' => 12, + 'url' => 'http://lol.lol', + 'shorturl' => 'abc', + 'title' => 'Important Title', + 'description' => 'It is very lol' . PHP_EOL . 'new line', + 'tags' => ['blip', '.blop'], + 'private' => true, + 'created' => '2017-01-07T16:01:02+00:00', + 'updated' => '2017-01-07T16:06:12+00:00', + ]; + + $this->assertEquals($expected, ApiUtils::formatLink($link, $indexUrl)); + } + + /** + * Test formatLink() with only minimal fields filled, and internal link. + */ + public function testFormatLinkMinimalNote() + { + $indexUrl = 'https://domain.tld/sub/'; + $link = [ + 'id' => 12, + 'url' => '?abc', + 'shorturl' => 'abc', + 'title' => 'Note', + 'description' => '', + 'tags' => '', + 'private' => '', + 'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'), + ]; + + $expected = [ + 'id' => 12, + 'url' => 'https://domain.tld/sub/?abc', + 'shorturl' => 'abc', + 'title' => 'Note', + 'description' => '', + 'tags' => [], + 'private' => false, + 'created' => '2017-01-07T16:01:02+00:00', + 'updated' => '', + ]; + + $this->assertEquals($expected, ApiUtils::formatLink($link, $indexUrl)); + } + + /** + * Test updateLink with valid data, and also unnecessary fields. + */ + public function testUpdateLink() + { + $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102'); + $old = [ + 'id' => 12, + 'url' => '?abc', + 'shorturl' => 'abc', + 'title' => 'Note', + 'description' => '', + 'tags' => '', + 'private' => '', + 'created' => $created, + ]; + + $new = [ + 'id' => 13, + 'shorturl' => 'nope', + 'url' => 'http://somewhere.else', + 'title' => 'Le Cid', + 'description' => 'Percé jusques au fond du cœur [...]', + 'tags' => 'corneille rodrigue', + 'private' => true, + 'created' => 'creation', + 'updated' => 'updation', + ]; + + $result = ApiUtils::updateLink($old, $new); + $this->assertEquals(12, $result['id']); + $this->assertEquals('http://somewhere.else', $result['url']); + $this->assertEquals('abc', $result['shorturl']); + $this->assertEquals('Le Cid', $result['title']); + $this->assertEquals('Percé jusques au fond du cœur [...]', $result['description']); + $this->assertEquals('corneille rodrigue', $result['tags']); + $this->assertEquals(true, $result['private']); + $this->assertEquals($created, $result['created']); + $this->assertTrue(new \DateTime('5 seconds ago') < $result['updated']); + } + + /** + * Test updateLink with minimal data. + */ + public function testUpdateLinkMinimal() + { + $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102'); + $old = [ + 'id' => 12, + 'url' => '?abc', + 'shorturl' => 'abc', + 'title' => 'Note', + 'description' => 'Interesting description!', + 'tags' => 'doggo', + 'private' => true, + 'created' => $created, + ]; + + $new = [ + 'url' => '', + 'title' => '', + 'description' => '', + 'tags' => '', + 'private' => false, + ]; + + $result = ApiUtils::updateLink($old, $new); + $this->assertEquals(12, $result['id']); + $this->assertEquals('?abc', $result['url']); + $this->assertEquals('abc', $result['shorturl']); + $this->assertEquals('?abc', $result['title']); + $this->assertEquals('', $result['description']); + $this->assertEquals('', $result['tags']); + $this->assertEquals(false, $result['private']); + $this->assertEquals($created, $result['created']); + $this->assertTrue(new \DateTime('5 seconds ago') < $result['updated']); + } }