forked from sebsauvage/Shaarli
-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1597 from ArthurHoaro/feature/share-private-bookmark
Feature: Share private bookmarks using a URL containing a private key
- Loading branch information
Showing
10 changed files
with
268 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
tests/front/controller/admin/ManageShaareControllerTest/SharePrivateTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest; | ||
|
||
use Shaarli\Bookmark\Bookmark; | ||
use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper; | ||
use Shaarli\Front\Controller\Admin\ManageShaareController; | ||
use Shaarli\Http\HttpAccess; | ||
use Shaarli\TestCase; | ||
use Slim\Http\Request; | ||
use Slim\Http\Response; | ||
|
||
/** | ||
* Test GET /admin/shaare/private/{hash} | ||
*/ | ||
class SharePrivateTest extends TestCase | ||
{ | ||
use FrontAdminControllerMockHelper; | ||
|
||
/** @var ManageShaareController */ | ||
protected $controller; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->createContainer(); | ||
|
||
$this->container->httpAccess = $this->createMock(HttpAccess::class); | ||
$this->controller = new ManageShaareController($this->container); | ||
} | ||
|
||
/** | ||
* Test shaare private with a private bookmark which does not have a key yet. | ||
*/ | ||
public function testSharePrivateWithNewPrivateBookmark(): void | ||
{ | ||
$hash = 'abcdcef'; | ||
$request = $this->createMock(Request::class); | ||
$response = new Response(); | ||
|
||
$bookmark = (new Bookmark()) | ||
->setId(123) | ||
->setUrl('http://domain.tld') | ||
->setTitle('Title 123') | ||
->setPrivate(true) | ||
; | ||
|
||
$this->container->bookmarkService | ||
->expects(static::once()) | ||
->method('findByHash') | ||
->with($hash) | ||
->willReturn($bookmark) | ||
; | ||
$this->container->bookmarkService | ||
->expects(static::once()) | ||
->method('set') | ||
->with($bookmark, true) | ||
->willReturnCallback(function (Bookmark $bookmark): Bookmark { | ||
static::assertSame(32, strlen($bookmark->getAdditionalContentEntry('private_key'))); | ||
|
||
return $bookmark; | ||
}) | ||
; | ||
|
||
$result = $this->controller->sharePrivate($request, $response, ['hash' => $hash]); | ||
|
||
static::assertSame(302, $result->getStatusCode()); | ||
static::assertRegExp('#/subfolder/shaare/' . $hash . '\?key=\w{32}#', $result->getHeaderLine('Location')); | ||
} | ||
|
||
/** | ||
* Test shaare private with a private bookmark which does already have a key. | ||
*/ | ||
public function testSharePrivateWithExistingPrivateBookmark(): void | ||
{ | ||
$hash = 'abcdcef'; | ||
$existingKey = 'this is a private key'; | ||
$request = $this->createMock(Request::class); | ||
$response = new Response(); | ||
|
||
$bookmark = (new Bookmark()) | ||
->setId(123) | ||
->setUrl('http://domain.tld') | ||
->setTitle('Title 123') | ||
->setPrivate(true) | ||
->addAdditionalContentEntry('private_key', $existingKey) | ||
; | ||
|
||
$this->container->bookmarkService | ||
->expects(static::once()) | ||
->method('findByHash') | ||
->with($hash) | ||
->willReturn($bookmark) | ||
; | ||
$this->container->bookmarkService | ||
->expects(static::never()) | ||
->method('set') | ||
; | ||
|
||
$result = $this->controller->sharePrivate($request, $response, ['hash' => $hash]); | ||
|
||
static::assertSame(302, $result->getStatusCode()); | ||
static::assertSame('/subfolder/shaare/' . $hash . '?key=' . $existingKey, $result->getHeaderLine('Location')); | ||
} | ||
|
||
/** | ||
* Test shaare private with a public bookmark. | ||
*/ | ||
public function testSharePrivateWithPublicBookmark(): void | ||
{ | ||
$hash = 'abcdcef'; | ||
$request = $this->createMock(Request::class); | ||
$response = new Response(); | ||
|
||
$bookmark = (new Bookmark()) | ||
->setId(123) | ||
->setUrl('http://domain.tld') | ||
->setTitle('Title 123') | ||
->setPrivate(false) | ||
; | ||
|
||
$this->container->bookmarkService | ||
->expects(static::once()) | ||
->method('findByHash') | ||
->with($hash) | ||
->willReturn($bookmark) | ||
; | ||
$this->container->bookmarkService | ||
->expects(static::never()) | ||
->method('set') | ||
; | ||
|
||
$result = $this->controller->sharePrivate($request, $response, ['hash' => $hash]); | ||
|
||
static::assertSame(302, $result->getStatusCode()); | ||
static::assertSame('/subfolder/shaare/' . $hash, $result->getHeaderLine('Location')); | ||
} | ||
} |
Oops, something went wrong.