]> BookStack Code Mirror - bookstack/blob - tests/Api/BooksApiTest.php
Fixed 'interaction_required' response for azure
[bookstack] / tests / Api / BooksApiTest.php
1 <?php namespace Tests;
2
3 use BookStack\Entities\Book;
4
5 class BooksApiTest extends TestCase
6 {
7     use TestsApi;
8
9     protected $baseEndpoint = '/api/books';
10
11     public function test_index_endpoint_returns_expected_book()
12     {
13         $this->actingAsApiEditor();
14         $firstBook = Book::query()->orderBy('id', 'asc')->first();
15
16         $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
17         $resp->assertJson(['data' => [
18             [
19                 'id' => $firstBook->id,
20                 'name' => $firstBook->name,
21                 'slug' => $firstBook->slug,
22             ]
23         ]]);
24     }
25
26     public function test_create_endpoint()
27     {
28         $this->actingAsApiEditor();
29         $details = [
30             'name' => 'My API book',
31             'description' => 'A book created via the API',
32         ];
33
34         $resp = $this->postJson($this->baseEndpoint, $details);
35         $resp->assertStatus(200);
36         $newItem = Book::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
37         $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
38         $this->assertActivityExists('book_create', $newItem);
39     }
40
41     public function test_book_name_needed_to_create()
42     {
43         $this->actingAsApiEditor();
44         $details = [
45             'description' => 'A book created via the API',
46         ];
47
48         $resp = $this->postJson($this->baseEndpoint, $details);
49         $resp->assertStatus(422);
50         $resp->assertJson([
51             "error" => [
52                 "message" => "The given data was invalid.",
53                 "validation" => [
54                     "name" => ["The name field is required."]
55                 ],
56                 "code" => 422,
57             ],
58         ]);
59     }
60
61     public function test_read_endpoint()
62     {
63         $this->actingAsApiEditor();
64         $book = Book::visible()->first();
65
66         $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
67
68         $resp->assertStatus(200);
69         $resp->assertJson([
70             'id' => $book->id,
71             'slug' => $book->slug,
72             'created_by' => [
73                 'name' => $book->createdBy->name,
74             ],
75             'updated_by' => [
76                 'name' => $book->createdBy->name,
77             ]
78         ]);
79     }
80
81     public function test_update_endpoint()
82     {
83         $this->actingAsApiEditor();
84         $book = Book::visible()->first();
85         $details = [
86             'name' => 'My updated API book',
87             'description' => 'A book created via the API',
88         ];
89
90         $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
91         $book->refresh();
92
93         $resp->assertStatus(200);
94         $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
95         $this->assertActivityExists('book_update', $book);
96     }
97
98     public function test_delete_endpoint()
99     {
100         $this->actingAsApiEditor();
101         $book = Book::visible()->first();
102         $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
103
104         $resp->assertStatus(204);
105         $this->assertActivityExists('book_delete');
106     }
107 }