]> BookStack Code Mirror - bookstack/blob - tests/Api/TestsApi.php
Fix Crowdin name in the language_request issue template
[bookstack] / tests / Api / TestsApi.php
1 <?php
2
3 namespace Tests\Api;
4
5 trait TestsApi
6 {
7     protected $apiTokenId = 'apitoken';
8     protected $apiTokenSecret = 'password';
9
10     /**
11      * Set the API editor role as the current user via the API driver.
12      */
13     protected function actingAsApiEditor()
14     {
15         $this->actingAs($this->getEditor(), 'api');
16
17         return $this;
18     }
19
20     /**
21      * Set the API admin role as the current user via the API driver.
22      */
23     protected function actingAsApiAdmin()
24     {
25         $this->actingAs($this->getAdmin(), 'api');
26
27         return $this;
28     }
29
30     /**
31      * Format the given items into a standardised error format.
32      */
33     protected function errorResponse(string $message, int $code): array
34     {
35         return ['error' => ['code' => $code, 'message' => $message]];
36     }
37
38     /**
39      * Get the structure that matches a permission error response.
40      */
41     protected function permissionErrorResponse(): array
42     {
43         return $this->errorResponse('You do not have permission to perform the requested action.', 403);
44     }
45
46     /**
47      * Format the given (field_name => ["messages"]) array
48      * into a standard validation response format.
49      */
50     protected function validationResponse(array $messages): array
51     {
52         $err = $this->errorResponse('The given data was invalid.', 422);
53         $err['error']['validation'] = $messages;
54
55         return $err;
56     }
57
58     /**
59      * Get an approved API auth header.
60      */
61     protected function apiAuthHeader(): array
62     {
63         return [
64             'Authorization' => "Token {$this->apiTokenId}:{$this->apiTokenSecret}",
65         ];
66     }
67 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.