forked from BookStackApp/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestsApi.php
More file actions
67 lines (56 loc) · 1.6 KB
/
TestsApi.php
File metadata and controls
67 lines (56 loc) · 1.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
<?php
namespace Tests\Api;
trait TestsApi
{
protected $apiTokenId = 'apitoken';
protected $apiTokenSecret = 'password';
/**
* Set the API editor role as the current user via the API driver.
*/
protected function actingAsApiEditor()
{
$this->actingAs($this->users->editor(), 'api');
return $this;
}
/**
* Set the API admin role as the current user via the API driver.
*/
protected function actingAsApiAdmin()
{
$this->actingAs($this->users->admin(), 'api');
return $this;
}
/**
* Format the given items into a standardised error format.
*/
protected function errorResponse(string $message, int $code): array
{
return ['error' => ['code' => $code, 'message' => $message]];
}
/**
* Get the structure that matches a permission error response.
*/
protected function permissionErrorResponse(): array
{
return $this->errorResponse('You do not have permission to perform the requested action.', 403);
}
/**
* Format the given (field_name => ["messages"]) array
* into a standard validation response format.
*/
protected function validationResponse(array $messages): array
{
$err = $this->errorResponse('The given data was invalid.', 422);
$err['error']['validation'] = $messages;
return $err;
}
/**
* Get an approved API auth header.
*/
protected function apiAuthHeader(): array
{
return [
'Authorization' => "Token {$this->apiTokenId}:{$this->apiTokenSecret}",
];
}
}