5 use BookStack\Entities\Models\Page;
6 use BookStack\Uploads\Attachment;
7 use Illuminate\Http\UploadedFile;
10 class AttachmentsApiTest extends TestCase
14 protected $baseEndpoint = '/api/attachments';
16 public function test_index_endpoint_returns_expected_book()
18 $this->actingAsApiEditor();
19 $page = Page::query()->first();
20 $attachment = $this->createAttachmentForPage($page, [
21 'name' => 'My test attachment',
25 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
26 $resp->assertJson(['data' => [
28 'id' => $attachment->id,
29 'name' => 'My test attachment',
30 'uploaded_to' => $page->id,
36 public function test_attachments_listing_based_upon_page_visibility()
38 $this->actingAsApiEditor();
39 /** @var Page $page */
40 $page = Page::query()->first();
41 $attachment = $this->createAttachmentForPage($page, [
42 'name' => 'My test attachment',
46 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
47 $resp->assertJson(['data' => [
49 'id' => $attachment->id,
53 $page->restricted = true;
55 $this->regenEntityPermissions($page);
57 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
58 $resp->assertJsonMissing(['data' => [
60 'id' => $attachment->id,
65 public function test_create_endpoint_for_link_attachment()
67 $this->actingAsApiAdmin();
68 /** @var Page $page */
69 $page = Page::query()->first();
72 'name' => 'My attachment',
73 'uploaded_to' => $page->id,
74 'link' => 'https://cats.example.com',
77 $resp = $this->postJson($this->baseEndpoint, $details);
78 $resp->assertStatus(200);
79 /** @var Attachment $newItem */
80 $newItem = Attachment::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
81 $resp->assertJson(['id' => $newItem->id, 'external' => true, 'name' => $details['name'], 'uploaded_to' => $page->id]);
84 public function test_create_endpoint_for_upload_attachment()
86 $this->actingAsApiAdmin();
87 /** @var Page $page */
88 $page = Page::query()->first();
89 $file = $this->getTestFile('textfile.txt');
92 'name' => 'My attachment',
93 'uploaded_to' => $page->id,
96 $resp = $this->call('POST', $this->baseEndpoint, $details, [], ['file' => $file]);
97 $resp->assertStatus(200);
98 /** @var Attachment $newItem */
99 $newItem = Attachment::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
100 $resp->assertJson(['id' => $newItem->id, 'external' => false, 'extension' => 'txt', 'name' => $details['name'], 'uploaded_to' => $page->id]);
101 $this->assertTrue(file_exists(storage_path($newItem->path)));
102 unlink(storage_path($newItem->path));
105 public function test_name_needed_to_create()
107 $this->actingAsApiAdmin();
108 /** @var Page $page */
109 $page = Page::query()->first();
112 'uploaded_to' => $page->id,
113 'link' => 'https://example.com',
116 $resp = $this->postJson($this->baseEndpoint, $details);
117 $resp->assertStatus(422);
120 'message' => 'The given data was invalid.',
122 'name' => ['The name field is required.'],
129 public function test_link_or_file_needed_to_create()
131 $this->actingAsApiAdmin();
132 /** @var Page $page */
133 $page = Page::query()->first();
136 'name' => 'my attachment',
137 'uploaded_to' => $page->id,
140 $resp = $this->postJson($this->baseEndpoint, $details);
141 $resp->assertStatus(422);
144 'message' => 'The given data was invalid.',
146 'file' => ['The file field is required when link is not present.'],
147 'link' => ['The link field is required when file is not present.'],
154 public function test_read_endpoint_for_link_attachment()
156 $this->actingAsApiAdmin();
157 /** @var Page $page */
158 $page = Page::query()->first();
160 $attachment = $this->createAttachmentForPage($page, [
161 'name' => 'my attachment',
162 'path' => 'https://example.com',
166 $resp = $this->getJson("{$this->baseEndpoint}/{$attachment->id}");
168 $resp->assertStatus(200);
170 'id' => $attachment->id,
171 'content' => 'https://example.com',
173 'uploaded_to' => $page->id,
176 'name' => $attachment->createdBy->name,
179 'name' => $attachment->createdBy->name,
182 'html' => "<a target=\"_blank\" href=\"http://localhost/attachments/{$attachment->id}\">my attachment</a>",
183 'markdown' => "[my attachment](http://localhost/attachments/{$attachment->id})",
188 public function test_read_endpoint_for_file_attachment()
190 $this->actingAsApiAdmin();
191 /** @var Page $page */
192 $page = Page::query()->first();
193 $file = $this->getTestFile('textfile.txt');
196 'name' => 'My file attachment',
197 'uploaded_to' => $page->id,
199 $this->call('POST', $this->baseEndpoint, $details, [], ['file' => $file]);
200 /** @var Attachment $attachment */
201 $attachment = Attachment::query()->orderByDesc('id')->where('name', '=', $details['name'])->firstOrFail();
203 $resp = $this->getJson("{$this->baseEndpoint}/{$attachment->id}");
205 $resp->assertStatus(200);
207 'id' => $attachment->id,
208 'content' => base64_encode(file_get_contents(storage_path($attachment->path))),
210 'uploaded_to' => $page->id,
213 'name' => $attachment->createdBy->name,
216 'name' => $attachment->updatedBy->name,
219 'html' => "<a target=\"_blank\" href=\"http://localhost/attachments/{$attachment->id}\">My file attachment</a>",
220 'markdown' => "[My file attachment](http://localhost/attachments/{$attachment->id})",
224 unlink(storage_path($attachment->path));
227 public function test_attachment_not_visible_on_other_users_draft()
229 $this->actingAsApiAdmin();
230 $editor = $this->getEditor();
232 /** @var Page $page */
233 $page = Page::query()->first();
235 $page->owned_by = $editor;
237 $this->regenEntityPermissions($page);
239 $attachment = $this->createAttachmentForPage($page, [
240 'name' => 'my attachment',
241 'path' => 'https://example.com',
245 $resp = $this->getJson("{$this->baseEndpoint}/{$attachment->id}");
247 $resp->assertStatus(404);
250 public function test_update_endpoint()
252 $this->actingAsApiAdmin();
253 /** @var Page $page */
254 $page = Page::query()->first();
255 $attachment = $this->createAttachmentForPage($page);
258 'name' => 'My updated API attachment',
261 $resp = $this->putJson("{$this->baseEndpoint}/{$attachment->id}", $details);
262 $attachment->refresh();
264 $resp->assertStatus(200);
265 $resp->assertJson(['id' => $attachment->id, 'name' => 'My updated API attachment']);
268 public function test_update_link_attachment_to_file()
270 $this->actingAsApiAdmin();
271 /** @var Page $page */
272 $page = Page::query()->first();
273 $attachment = $this->createAttachmentForPage($page);
274 $file = $this->getTestFile('textfile.txt');
276 $resp = $this->call('PUT', "{$this->baseEndpoint}/{$attachment->id}", ['name' => 'My updated file'], [], ['file' => $file]);
277 $resp->assertStatus(200);
279 $attachment->refresh();
280 $this->assertFalse($attachment->external);
281 $this->assertEquals('txt', $attachment->extension);
282 $this->assertStringStartsWith('uploads/files/', $attachment->path);
283 $this->assertFileExists(storage_path($attachment->path));
285 unlink(storage_path($attachment->path));
288 public function test_update_file_attachment_to_link()
290 $this->actingAsApiAdmin();
291 /** @var Page $page */
292 $page = Page::query()->first();
293 $file = $this->getTestFile('textfile.txt');
294 $this->call('POST', $this->baseEndpoint, ['name' => 'My file attachment', 'uploaded_to' => $page->id], [], ['file' => $file]);
295 /** @var Attachment $attachment */
296 $attachment = Attachment::query()->where('name', '=', 'My file attachment')->firstOrFail();
298 $filePath = storage_path($attachment->path);
299 $this->assertFileExists($filePath);
302 'name' => 'My updated API attachment',
303 'link' => 'https://cats.example.com',
306 $resp = $this->putJson("{$this->baseEndpoint}/{$attachment->id}", $details);
307 $resp->assertStatus(200);
308 $attachment->refresh();
310 $this->assertFileDoesNotExist($filePath);
311 $this->assertTrue($attachment->external);
312 $this->assertEquals('https://cats.example.com', $attachment->path);
313 $this->assertEquals('', $attachment->extension);
316 public function test_delete_endpoint()
318 $this->actingAsApiAdmin();
319 /** @var Page $page */
320 $page = Page::query()->first();
321 $attachment = $this->createAttachmentForPage($page);
323 $resp = $this->deleteJson("{$this->baseEndpoint}/{$attachment->id}");
325 $resp->assertStatus(204);
326 $this->assertDatabaseMissing('attachments', ['id' => $attachment->id]);
329 protected function createAttachmentForPage(Page $page, $attributes = []): Attachment
331 $admin = $this->getAdmin();
332 /** @var Attachment $attachment */
333 $attachment = $page->attachments()->forceCreate(array_merge([
334 'uploaded_to' => $page->id,
335 'name' => 'test attachment',
338 'created_by' => $admin->id,
339 'updated_by' => $admin->id,
340 'path' => 'https://attachment.example.com',
347 * Get a test file that can be uploaded.
349 protected function getTestFile(string $fileName): UploadedFile
351 return new UploadedFile(base_path('tests/test-data/test-file.txt'), $fileName, 'text/plain', null, true);