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_update_endpoint()
229 $this->actingAsApiAdmin();
230 /** @var Page $page */
231 $page = Page::query()->first();
232 $attachment = $this->createAttachmentForPage($page);
235 'name' => 'My updated API attachment',
238 $resp = $this->putJson("{$this->baseEndpoint}/{$attachment->id}", $details);
239 $attachment->refresh();
241 $resp->assertStatus(200);
242 $resp->assertJson(['id' => $attachment->id, 'name' => 'My updated API attachment']);
245 public function test_update_link_attachment_to_file()
247 $this->actingAsApiAdmin();
248 /** @var Page $page */
249 $page = Page::query()->first();
250 $attachment = $this->createAttachmentForPage($page);
251 $file = $this->getTestFile('textfile.txt');
253 $resp = $this->call('PUT', "{$this->baseEndpoint}/{$attachment->id}", ['name' => 'My updated file'], [], ['file' => $file]);
254 $resp->assertStatus(200);
256 $attachment->refresh();
257 $this->assertFalse($attachment->external);
258 $this->assertEquals('txt', $attachment->extension);
259 $this->assertStringStartsWith('uploads/files/', $attachment->path);
260 $this->assertFileExists(storage_path($attachment->path));
262 unlink(storage_path($attachment->path));
265 public function test_update_file_attachment_to_link()
267 $this->actingAsApiAdmin();
268 /** @var Page $page */
269 $page = Page::query()->first();
270 $file = $this->getTestFile('textfile.txt');
271 $this->call('POST', $this->baseEndpoint, ['name' => 'My file attachment', 'uploaded_to' => $page->id], [], ['file' => $file]);
272 /** @var Attachment $attachment */
273 $attachment = Attachment::query()->where('name', '=', 'My file attachment')->firstOrFail();
275 $filePath = storage_path($attachment->path);
276 $this->assertFileExists($filePath);
279 'name' => 'My updated API attachment',
280 'link' => 'https://cats.example.com',
283 $resp = $this->putJson("{$this->baseEndpoint}/{$attachment->id}", $details);
284 $resp->assertStatus(200);
285 $attachment->refresh();
287 $this->assertFileDoesNotExist($filePath);
288 $this->assertTrue($attachment->external);
289 $this->assertEquals('https://cats.example.com', $attachment->path);
290 $this->assertEquals('', $attachment->extension);
293 public function test_delete_endpoint()
295 $this->actingAsApiAdmin();
296 /** @var Page $page */
297 $page = Page::query()->first();
298 $attachment = $this->createAttachmentForPage($page);
300 $resp = $this->deleteJson("{$this->baseEndpoint}/{$attachment->id}");
302 $resp->assertStatus(204);
303 $this->assertDatabaseMissing('attachments', ['id' => $attachment->id]);
306 protected function createAttachmentForPage(Page $page, $attributes = []): Attachment
308 $admin = $this->getAdmin();
309 /** @var Attachment $attachment */
310 $attachment = $page->attachments()->forceCreate(array_merge([
311 'uploaded_to' => $page->id,
312 'name' => 'test attachment',
315 'created_by' => $admin->id,
316 'updated_by' => $admin->id,
317 'path' => 'https://attachment.example.com',
324 * Get a test file that can be uploaded.
326 protected function getTestFile(string $fileName): UploadedFile
328 return new UploadedFile(base_path('tests/test-data/test-file.txt'), $fileName, 'text/plain', 55, null, true);