use BookStack\Entities\Models\Page;
use BookStack\Uploads\Attachment;
use Illuminate\Http\UploadedFile;
+use Illuminate\Testing\AssertableJsonString;
use Tests\TestCase;
class AttachmentsApiTest extends TestCase
{
use TestsApi;
- protected $baseEndpoint = '/api/attachments';
+ protected string $baseEndpoint = '/api/attachments';
public function test_index_endpoint_returns_expected_book()
{
$this->actingAsApiEditor();
- $page = Page::query()->first();
+ $page = $this->entities->page();
$attachment = $this->createAttachmentForPage($page, [
'name' => 'My test attachment',
'external' => true,
public function test_attachments_listing_based_upon_page_visibility()
{
$this->actingAsApiEditor();
- /** @var Page $page */
- $page = Page::query()->first();
+ $page = $this->entities->page();
$attachment = $this->createAttachmentForPage($page, [
'name' => 'My test attachment',
'external' => true,
],
]]);
- $page->restricted = true;
- $page->save();
- $this->regenEntityPermissions($page);
+ $this->permissions->setEntityPermissions($page, [], []);
$resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
$resp->assertJsonMissing(['data' => [
public function test_create_endpoint_for_link_attachment()
{
$this->actingAsApiAdmin();
- /** @var Page $page */
- $page = Page::query()->first();
+ $page = $this->entities->page();
$details = [
'name' => 'My attachment',
public function test_create_endpoint_for_upload_attachment()
{
$this->actingAsApiAdmin();
- /** @var Page $page */
- $page = Page::query()->first();
+ $page = $this->entities->page();
$file = $this->getTestFile('textfile.txt');
$details = [
public function test_upload_limit_restricts_attachment_uploads()
{
$this->actingAsApiAdmin();
- /** @var Page $page */
- $page = Page::query()->first();
+ $page = $this->entities->page();
config()->set('app.upload_limit', 1);
public function test_name_needed_to_create()
{
$this->actingAsApiAdmin();
- /** @var Page $page */
- $page = Page::query()->first();
+ $page = $this->entities->page();
$details = [
'uploaded_to' => $page->id,
public function test_link_or_file_needed_to_create()
{
$this->actingAsApiAdmin();
- /** @var Page $page */
- $page = Page::query()->first();
+ $page = $this->entities->page();
$details = [
'name' => 'my attachment',
public function test_message_shown_if_file_is_not_a_valid_file()
{
$this->actingAsApiAdmin();
- /** @var Page $page */
- $page = Page::query()->first();
+ $page = $this->entities->page();
$details = [
'name' => 'my attachment',
public function test_read_endpoint_for_link_attachment()
{
$this->actingAsApiAdmin();
- /** @var Page $page */
- $page = Page::query()->first();
+ $page = $this->entities->page();
$attachment = $this->createAttachmentForPage($page, [
'name' => 'my attachment',
public function test_read_endpoint_for_file_attachment()
{
$this->actingAsApiAdmin();
- /** @var Page $page */
- $page = Page::query()->first();
+ $page = $this->entities->page();
$file = $this->getTestFile('textfile.txt');
$details = [
$attachment = Attachment::query()->orderByDesc('id')->where('name', '=', $details['name'])->firstOrFail();
$resp = $this->getJson("{$this->baseEndpoint}/{$attachment->id}");
-
$resp->assertStatus(200);
- $resp->assertJson([
+ $resp->assertHeader('Content-Type', 'application/json');
+
+ $json = new AssertableJsonString($resp->streamedContent());
+ $json->assertSubset([
'id' => $attachment->id,
'content' => base64_encode(file_get_contents(storage_path($attachment->path))),
'external' => false,
public function test_attachment_not_visible_on_other_users_draft()
{
$this->actingAsApiAdmin();
- $editor = $this->getEditor();
+ $editor = $this->users->editor();
- /** @var Page $page */
- $page = Page::query()->first();
+ $page = $this->entities->page();
$page->draft = true;
- $page->owned_by = $editor;
+ $page->owned_by = $editor->id;
$page->save();
- $this->regenEntityPermissions($page);
+ $this->permissions->regenerateForEntity($page);
$attachment = $this->createAttachmentForPage($page, [
'name' => 'my attachment',
public function test_update_endpoint()
{
$this->actingAsApiAdmin();
- /** @var Page $page */
- $page = Page::query()->first();
+ $page = $this->entities->page();
$attachment = $this->createAttachmentForPage($page);
$details = [
public function test_update_link_attachment_to_file()
{
$this->actingAsApiAdmin();
- /** @var Page $page */
- $page = Page::query()->first();
+ $page = $this->entities->page();
$attachment = $this->createAttachmentForPage($page);
$file = $this->getTestFile('textfile.txt');
public function test_update_file_attachment_to_link()
{
$this->actingAsApiAdmin();
- /** @var Page $page */
- $page = Page::query()->first();
+ $page = $this->entities->page();
+ $attachment = $this->createAttachmentForPage($page);
+
+ $resp = $this->putJson("{$this->baseEndpoint}/{$attachment->id}", [
+ 'link' => 'https://example.com/donkey',
+ ]);
+
+ $resp->assertStatus(200);
+ $this->assertDatabaseHas('attachments', [
+ 'id' => $attachment->id,
+ 'path' => 'https://example.com/donkey',
+ ]);
+ }
+
+ public function test_update_does_not_require_name()
+ {
+ $this->actingAsApiAdmin();
+ $page = $this->entities->page();
$file = $this->getTestFile('textfile.txt');
$this->call('POST', $this->baseEndpoint, ['name' => 'My file attachment', 'uploaded_to' => $page->id], [], ['file' => $file]);
/** @var Attachment $attachment */
public function test_delete_endpoint()
{
$this->actingAsApiAdmin();
- /** @var Page $page */
- $page = Page::query()->first();
+ $page = $this->entities->page();
$attachment = $this->createAttachmentForPage($page);
$resp = $this->deleteJson("{$this->baseEndpoint}/{$attachment->id}");
protected function createAttachmentForPage(Page $page, $attributes = []): Attachment
{
- $admin = $this->getAdmin();
+ $admin = $this->users->admin();
/** @var Attachment $attachment */
$attachment = $page->attachments()->forceCreate(array_merge([
'uploaded_to' => $page->id,