]> BookStack Code Mirror - bookstack/blob - tests/Api/TestsApi.php
1ad4d14b64e4c7137134552848b1f7c800dbed6f
[bookstack] / tests / Api / TestsApi.php
1 <?php namespace Tests\Api;
2
3 trait TestsApi
4 {
5
6     protected $apiTokenId = 'apitoken';
7     protected $apiTokenSecret = 'password';
8
9     /**
10      * Set the API editor role as the current user via the API driver.
11      */
12     protected function actingAsApiEditor()
13     {
14         $this->actingAs($this->getEditor(), 'api');
15         return $this;
16     }
17
18     /**
19      * Format the given items into a standardised error format.
20      */
21     protected function errorResponse(string $message, int $code): array
22     {
23         return ["error" => ["code" => $code, "message" => $message]];
24     }
25
26     /**
27      * Format the given (field_name => ["messages"]) array
28      * into a standard validation response format.
29      */
30     protected function validationResponse(array $messages): array
31     {
32         $err = $this->errorResponse("The given data was invalid.", 422);
33         $err['error']['validation'] = $messages;
34         return $err;
35     }
36     /**
37      * Get an approved API auth header.
38      */
39     protected function apiAuthHeader(): array
40     {
41         return [
42             "Authorization" => "Token {$this->apiTokenId}:{$this->apiTokenSecret}"
43         ];
44     }
45
46 }