]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Merge pull request #1616 from dimtion/fix-api-redirect
authorArthurHoaro <arthur@hoa.ro>
Thu, 29 Oct 2020 15:03:07 +0000 (16:03 +0100)
committerGitHub <noreply@github.com>
Thu, 29 Oct 2020 15:03:07 +0000 (16:03 +0100)
API postLink: change relative path to absolute path

1  2 
application/api/controllers/Links.php
tests/api/controllers/links/PostLinkTest.php

index 73a1b84e1727e1a4567e53ec9db6fe1299ce2b39,16fc8688887e44af1d4c6c07e8f17f12f12182d4..6bf529e4a570a3ff5f789f2de34045946205cd67
@@@ -96,12 -96,11 +96,12 @@@ class Links extends ApiControlle
       */
      public function getLink($request, $response, $args)
      {
 -        if (!$this->bookmarkService->exists($args['id'])) {
 +        $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
 +        if ($id === null || ! $this->bookmarkService->exists($id)) {
              throw new ApiLinkNotFoundException();
          }
          $index = index_url($this->ci['environment']);
 -        $out = ApiUtils::formatLink($this->bookmarkService->get($args['id']), $index);
 +        $out = ApiUtils::formatLink($this->bookmarkService->get($id), $index);
  
          return $response->withJson($out, 200, $this->jsonStyle);
      }
       */
      public function postLink($request, $response)
      {
 -        $data = $request->getParsedBody();
 -        $bookmark = ApiUtils::buildLinkFromRequest($data, $this->conf->get('privacy.default_private_links'));
 +        $data = (array) ($request->getParsedBody() ?? []);
 +        $bookmark = ApiUtils::buildBookmarkFromRequest($data, $this->conf->get('privacy.default_private_links'));
          // duplicate by URL, return 409 Conflict
          if (! empty($bookmark->getUrl())
              && ! empty($dup = $this->bookmarkService->findByUrl($bookmark->getUrl()))
  
          $this->bookmarkService->add($bookmark);
          $out = ApiUtils::formatLink($bookmark, index_url($this->ci['environment']));
-         $redirect = $this->ci->router->relativePathFor('getLink', ['id' => $bookmark->getId()]);
+         $redirect = $this->ci->router->pathFor('getLink', ['id' => $bookmark->getId()]);
          return $response->withAddedHeader('Location', $redirect)
                          ->withJson($out, 201, $this->jsonStyle);
      }
       */
      public function putLink($request, $response, $args)
      {
 -        if (! $this->bookmarkService->exists($args['id'])) {
 +        $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
 +        if ($id === null || !$this->bookmarkService->exists($id)) {
              throw new ApiLinkNotFoundException();
          }
  
          $index = index_url($this->ci['environment']);
          $data = $request->getParsedBody();
  
 -        $requestBookmark = ApiUtils::buildLinkFromRequest($data, $this->conf->get('privacy.default_private_links'));
 +        $requestBookmark = ApiUtils::buildBookmarkFromRequest($data, $this->conf->get('privacy.default_private_links'));
          // duplicate URL on a different link, return 409 Conflict
          if (! empty($requestBookmark->getUrl())
              && ! empty($dup = $this->bookmarkService->findByUrl($requestBookmark->getUrl()))
 -            && $dup->getId() != $args['id']
 +            && $dup->getId() != $id
          ) {
              return $response->withJson(
                  ApiUtils::formatLink($dup, $index),
              );
          }
  
 -        $responseBookmark = $this->bookmarkService->get($args['id']);
 +        $responseBookmark = $this->bookmarkService->get($id);
          $responseBookmark = ApiUtils::updateLink($responseBookmark, $requestBookmark);
          $this->bookmarkService->set($responseBookmark);
  
       */
      public function deleteLink($request, $response, $args)
      {
 -        if (! $this->bookmarkService->exists($args['id'])) {
 +        $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
 +        if ($id === null || !$this->bookmarkService->exists($id)) {
              throw new ApiLinkNotFoundException();
          }
 -        $bookmark = $this->bookmarkService->get($args['id']);
 +        $bookmark = $this->bookmarkService->get($id);
          $this->bookmarkService->remove($bookmark);
  
          return $response->withStatus(204);
index 7ff92f5c96968c6e12c75098dc164526c0a3224d,969b9fd9a3cbbb1a51fa0297294eaffa4dabf80e..e12f803be3ce99004294b926e4e14dd7b98f0ba1
@@@ -2,12 -2,11 +2,12 @@@
  
  namespace Shaarli\Api\Controllers;
  
 -use PHPUnit\Framework\TestCase;
 +use malkusch\lock\mutex\NoMutex;
  use Shaarli\Bookmark\Bookmark;
  use Shaarli\Bookmark\BookmarkFileService;
  use Shaarli\Config\ConfigManager;
  use Shaarli\History;
 +use Shaarli\TestCase;
  use Slim\Container;
  use Slim\Http\Environment;
  use Slim\Http\Request;
@@@ -71,9 -70,8 +71,9 @@@ class PostLinkTest extends TestCas
      /**
       * Before every test, instantiate a new Api with its config, plugins and bookmarks.
       */
 -    public function setUp()
 +    protected function setUp(): void
      {
 +        $mutex = new NoMutex();
          $this->conf = new ConfigManager('tests/utils/config/configJson');
          $this->conf->set('resource.datastore', self::$testDatastore);
          $this->refDB = new \ReferenceLinkDB();
@@@ -81,7 -79,7 +81,7 @@@
          $refHistory = new \ReferenceHistory();
          $refHistory->write(self::$testHistory);
          $this->history = new History(self::$testHistory);
 -        $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
 +        $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, $mutex, true);
  
          $this->container = new Container();
          $this->container['conf'] = $this->conf;
@@@ -92,8 -90,8 +92,8 @@@
  
          $mock = $this->createMock(Router::class);
          $mock->expects($this->any())
-              ->method('relativePathFor')
-              ->willReturn('api/v1/bookmarks/1');
+              ->method('pathFor')
+              ->willReturn('/api/v1/bookmarks/1');
  
          // affect @property-read... seems to work
          $this->controller->getCi()->router = $mock;
      /**
       * After every test, remove the test datastore.
       */
 -    public function tearDown()
 +    protected function tearDown(): void
      {
          @unlink(self::$testDatastore);
          @unlink(self::$testHistory);
  
          $response = $this->controller->postLink($request, new Response());
          $this->assertEquals(201, $response->getStatusCode());
-         $this->assertEquals('api/v1/bookmarks/1', $response->getHeader('Location')[0]);
+         $this->assertEquals('/api/v1/bookmarks/1', $response->getHeader('Location')[0]);
          $data = json_decode((string) $response->getBody(), true);
          $this->assertEquals(self::NB_FIELDS_LINK, count($data));
          $this->assertEquals(43, $data['id']);
          $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']);
 -        $this->assertEquals('http://domain.tld/?' . $data['shorturl'], $data['url']);
 -        $this->assertEquals('?' . $data['shorturl'], $data['title']);
 +        $this->assertEquals('http://domain.tld/shaare/' . $data['shorturl'], $data['url']);
 +        $this->assertEquals('/shaare/' . $data['shorturl'], $data['title']);
          $this->assertEquals('', $data['description']);
          $this->assertEquals([], $data['tags']);
          $this->assertEquals(true, $data['private']);
              'description' => 'shaare description',
              'tags' => ['one', 'two'],
              'private' => true,
 +            'created' => '2015-05-05T12:30:00+03:00',
 +            'updated' => '2016-06-05T14:32:10+03:00',
          ];
          $env = Environment::mock([
              'REQUEST_METHOD' => 'POST',
          $response = $this->controller->postLink($request, new Response());
  
          $this->assertEquals(201, $response->getStatusCode());
-         $this->assertEquals('api/v1/bookmarks/1', $response->getHeader('Location')[0]);
+         $this->assertEquals('/api/v1/bookmarks/1', $response->getHeader('Location')[0]);
          $data = json_decode((string) $response->getBody(), true);
          $this->assertEquals(self::NB_FIELDS_LINK, count($data));
          $this->assertEquals(43, $data['id']);
          $this->assertEquals($link['description'], $data['description']);
          $this->assertEquals($link['tags'], $data['tags']);
          $this->assertEquals(true, $data['private']);
 -        $this->assertTrue(
 -            new \DateTime('2 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
 -        );
 -        $this->assertEquals('', $data['updated']);
 +        $this->assertSame($link['created'], $data['created']);
 +        $this->assertSame($link['updated'], $data['updated']);
      }
  
      /**