3 use BookStack\Entities\Book;
5 class ApiAuthTest extends TestCase
9 protected $baseEndpoint = '/api/books';
11 public function test_index_endpoint_returns_expected_book()
13 $this->actingAsApiEditor();
14 $firstBook = Book::query()->orderBy('id', 'asc')->first();
16 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
17 $resp->assertJson(['data' => [
19 'id' => $firstBook->id,
20 'name' => $firstBook->name,
21 'slug' => $firstBook->slug,
26 public function test_create_endpoint()
28 $this->actingAsApiEditor();
30 'name' => 'My API book',
31 'description' => 'A book created via the API',
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);
41 public function test_read_endpoint()
43 $this->actingAsApiEditor();
44 $book = Book::visible()->first();
46 $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
48 $resp->assertStatus(200);
51 'slug' => $book->slug,
53 'name' => $book->createdBy->name,
56 'name' => $book->createdBy->name,
61 public function test_update_endpoint()
63 $this->actingAsApiEditor();
64 $book = Book::visible()->first();
66 'name' => 'My updated API book',
67 'description' => 'A book created via the API',
70 $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
73 $resp->assertStatus(200);
74 $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
75 $this->assertActivityExists('book_update', $book);
78 public function test_delete_endpoint()
80 $this->actingAsApiEditor();
81 $book = Book::visible()->first();
82 $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
84 $resp->assertStatus(204);
85 $this->assertActivityExists('book_delete');