forked from BookStackApp/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPagesApiTest.php
More file actions
337 lines (290 loc) · 11.6 KB
/
PagesApiTest.php
File metadata and controls
337 lines (290 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
namespace Tests\Api;
use BookStack\Activity\Models\Comment;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class PagesApiTest extends TestCase
{
use TestsApi;
protected string $baseEndpoint = '/api/pages';
public function test_index_endpoint_returns_expected_page()
{
$this->actingAsApiEditor();
$firstPage = Page::query()->orderBy('id', 'asc')->first();
$resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
$resp->assertJson(['data' => [
[
'id' => $firstPage->id,
'name' => $firstPage->name,
'slug' => $firstPage->slug,
'book_id' => $firstPage->book->id,
'priority' => $firstPage->priority,
'owned_by' => $firstPage->owned_by,
'created_by' => $firstPage->created_by,
'updated_by' => $firstPage->updated_by,
'revision_count' => $firstPage->revision_count,
],
]]);
}
public function test_create_endpoint()
{
$this->actingAsApiEditor();
$book = $this->entities->book();
$details = [
'name' => 'My API page',
'book_id' => $book->id,
'html' => '<p>My new page content</p>',
'tags' => [
[
'name' => 'tagname',
'value' => 'tagvalue',
],
],
'priority' => 15,
];
$resp = $this->postJson($this->baseEndpoint, $details);
unset($details['html']);
$resp->assertStatus(200);
$newItem = Page::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
$resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
$this->assertDatabaseHas('tags', [
'entity_id' => $newItem->id,
'entity_type' => $newItem->getMorphClass(),
'name' => 'tagname',
'value' => 'tagvalue',
]);
$resp->assertSeeText('My new page content');
$resp->assertJsonMissing(['book' => []]);
$this->assertActivityExists('page_create', $newItem);
}
public function test_page_name_needed_to_create()
{
$this->actingAsApiEditor();
$book = $this->entities->book();
$details = [
'book_id' => $book->id,
'html' => '<p>A page created via the API</p>',
];
$resp = $this->postJson($this->baseEndpoint, $details);
$resp->assertStatus(422);
$resp->assertJson($this->validationResponse([
'name' => ['The name field is required.'],
]));
}
public function test_book_id_or_chapter_id_needed_to_create()
{
$this->actingAsApiEditor();
$details = [
'name' => 'My api page',
'html' => '<p>A page created via the API</p>',
];
$resp = $this->postJson($this->baseEndpoint, $details);
$resp->assertStatus(422);
$resp->assertJson($this->validationResponse([
'book_id' => ['The book id field is required when chapter id is not present.'],
'chapter_id' => ['The chapter id field is required when book id is not present.'],
]));
$chapter = $this->entities->chapter();
$resp = $this->postJson($this->baseEndpoint, array_merge($details, ['chapter_id' => $chapter->id]));
$resp->assertStatus(200);
$book = $this->entities->book();
$resp = $this->postJson($this->baseEndpoint, array_merge($details, ['book_id' => $book->id]));
$resp->assertStatus(200);
}
public function test_markdown_can_be_provided_for_create()
{
$this->actingAsApiEditor();
$book = $this->entities->book();
$details = [
'book_id' => $book->id,
'name' => 'My api page',
'markdown' => "# A new API page \n[link](https://example.com)",
];
$resp = $this->postJson($this->baseEndpoint, $details);
$resp->assertJson(['markdown' => $details['markdown']]);
$respHtml = $resp->json('html');
$this->assertStringContainsString('new API page</h1>', $respHtml);
$this->assertStringContainsString('link</a>', $respHtml);
$this->assertStringContainsString('href="https://example.com"', $respHtml);
}
public function test_read_endpoint()
{
$this->actingAsApiEditor();
$page = $this->entities->page();
$resp = $this->getJson($this->baseEndpoint . "/{$page->id}");
$resp->assertStatus(200);
$resp->assertJson([
'id' => $page->id,
'slug' => $page->slug,
'created_by' => [
'name' => $page->createdBy->name,
],
'book_id' => $page->book_id,
'updated_by' => [
'name' => $page->createdBy->name,
],
'owned_by' => [
'name' => $page->ownedBy->name,
],
]);
}
public function test_read_endpoint_provides_rendered_html()
{
$this->actingAsApiEditor();
$page = $this->entities->page();
$page->html = "<p>testing</p><script>alert('danger')</script><h1>Hello</h1>";
$page->save();
$resp = $this->getJson($this->baseEndpoint . "/{$page->id}");
$html = $resp->json('html');
$this->assertStringNotContainsString('script', $html);
$this->assertStringContainsString('Hello', $html);
$this->assertStringContainsString('testing', $html);
}
public function test_read_endpoint_provides_raw_html()
{
$html = "<p>testing</p><script>alert('danger')</script><h1>Hello</h1>";
$this->actingAsApiEditor();
$page = $this->entities->page();
$page->html = $html;
$page->save();
$resp = $this->getJson($this->baseEndpoint . "/{$page->id}");
$this->assertEquals($html, $resp->json('raw_html'));
$this->assertNotEquals($html, $resp->json('html'));
}
public function test_read_endpoint_returns_not_found()
{
$this->actingAsApiEditor();
// get an id that is not used
$id = Page::orderBy('id', 'desc')->first()->id + 1;
$this->assertNull(Page::find($id));
$resp = $this->getJson($this->baseEndpoint . "/$id");
$resp->assertNotFound();
$this->assertNull($resp->json('id'));
$resp->assertJsonIsObject('error');
$resp->assertJsonStructure([
'error' => [
'code',
'message',
],
]);
$this->assertSame(404, $resp->json('error')['code']);
}
public function test_read_endpoint_includes_page_comments_tree_structure()
{
$this->actingAsApiEditor();
$page = $this->entities->page();
$relation = ['commentable_type' => 'page', 'commentable_id' => $page->id];
$active = Comment::factory()->create([...$relation, 'html' => '<p>My active<script>cat</script> comment</p>']);
Comment::factory()->count(5)->create([...$relation, 'parent_id' => $active->local_id]);
$archived = Comment::factory()->create([...$relation, 'archived' => true]);
Comment::factory()->count(2)->create([...$relation, 'parent_id' => $archived->local_id]);
$resp = $this->getJson("{$this->baseEndpoint}/{$page->id}");
$resp->assertOk();
$resp->assertJsonCount(1, 'comments.active');
$resp->assertJsonCount(1, 'comments.archived');
$resp->assertJsonCount(5, 'comments.active.0.children');
$resp->assertJsonCount(2, 'comments.archived.0.children');
$resp->assertJsonFragment([
'id' => $active->id,
'local_id' => $active->local_id,
'html' => '<p>My active comment</p>',
]);
}
public function test_update_endpoint()
{
$this->actingAsApiEditor();
$page = $this->entities->page();
$details = [
'name' => 'My updated API page',
'html' => '<p>A page created via the API</p>',
'tags' => [
[
'name' => 'freshtag',
'value' => 'freshtagval',
],
],
'priority' => 15,
];
$resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
$page->refresh();
$resp->assertStatus(200);
unset($details['html']);
$resp->assertJson(array_merge($details, [
'id' => $page->id, 'slug' => $page->slug, 'book_id' => $page->book_id,
]));
$this->assertActivityExists('page_update', $page);
}
public function test_providing_new_chapter_id_on_update_will_move_page()
{
$this->actingAsApiEditor();
$page = $this->entities->page();
$chapter = Chapter::visible()->where('book_id', '!=', $page->book_id)->first();
$details = [
'name' => 'My updated API page',
'chapter_id' => $chapter->id,
'html' => '<p>A page created via the API</p>',
];
$resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
$resp->assertStatus(200);
$resp->assertJson([
'chapter_id' => $chapter->id,
'book_id' => $chapter->book_id,
]);
}
public function test_providing_move_via_update_requires_page_create_permission_on_new_parent()
{
$this->actingAsApiEditor();
$page = $this->entities->page();
$chapter = Chapter::visible()->where('book_id', '!=', $page->book_id)->first();
$this->permissions->setEntityPermissions($chapter, ['view'], [$this->users->editor()->roles()->first()]);
$details = [
'name' => 'My updated API page',
'chapter_id' => $chapter->id,
'html' => '<p>A page created via the API</p>',
];
$resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
$resp->assertStatus(403);
}
public function test_update_endpoint_does_not_wipe_content_if_no_html_or_md_provided()
{
$this->actingAsApiEditor();
$page = $this->entities->page();
$originalContent = $page->html;
$details = [
'name' => 'My updated API page',
'tags' => [
[
'name' => 'freshtag',
'value' => 'freshtagval',
],
],
];
$this->putJson($this->baseEndpoint . "/{$page->id}", $details);
$page->refresh();
$this->assertEquals($originalContent, $page->html);
}
public function test_update_increments_updated_date_if_only_tags_are_sent()
{
$this->actingAsApiEditor();
$page = $this->entities->page();
$page->newQuery()->where('id', '=', $page->id)->update(['updated_at' => Carbon::now()->subWeek()]);
$details = [
'tags' => [['name' => 'Category', 'value' => 'Testing']],
];
$resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
$resp->assertOk();
$page->refresh();
$this->assertGreaterThan(Carbon::now()->subDay()->unix(), $page->updated_at->unix());
}
public function test_delete_endpoint()
{
$this->actingAsApiEditor();
$page = $this->entities->page();
$resp = $this->deleteJson($this->baseEndpoint . "/{$page->id}");
$resp->assertStatus(204);
$this->assertActivityExists('page_delete', $page);
}
}