| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Tests\Api; | 
					
						
							| 
									
										
										
										
											2019-12-31 03:42:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 03:29:08 +08:00
										 |  |  | use BookStack\Auth\User; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-31 03:42:46 +08:00
										 |  |  | trait TestsApi | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-03-15 03:29:08 +08:00
										 |  |  |     protected string $apiTokenId = 'apitoken'; | 
					
						
							|  |  |  |     protected string $apiTokenSecret = 'password'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Set the given user as the current logged-in user via the API driver. | 
					
						
							|  |  |  |      * This does not ensure API access. The user may still lack required role permissions. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function actingAsForApi(User $user): static | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::actingAs($user, 'api'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-12-31 03:42:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-31 04:48:23 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Set the API editor role as the current user via the API driver. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-03-15 03:29:08 +08:00
										 |  |  |     protected function actingAsApiEditor(): static | 
					
						
							| 
									
										
										
										
											2019-12-31 03:42:46 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $this->actingAs($this->users->editor(), 'api'); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-31 04:48:23 +08:00
										 |  |  |         return $this; | 
					
						
							| 
									
										
										
										
											2019-12-31 03:42:46 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-20 07:58:56 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Set the API admin role as the current user via the API driver. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-03-15 03:29:08 +08:00
										 |  |  |     protected function actingAsApiAdmin(): static | 
					
						
							| 
									
										
										
										
											2021-10-20 07:58:56 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $this->actingAs($this->users->admin(), 'api'); | 
					
						
							| 
									
										
										
										
											2021-10-20 07:58:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-31 04:48:23 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Format the given items into a standardised error format. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function errorResponse(string $message, int $code): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         return ['error' => ['code' => $code, 'message' => $message]]; | 
					
						
							| 
									
										
										
										
											2019-12-31 04:48:23 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 08:26:19 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Format the given (field_name => ["messages"]) array | 
					
						
							|  |  |  |      * into a standard validation response format. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function validationResponse(array $messages): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         $err = $this->errorResponse('The given data was invalid.', 422); | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         $err['error']['validation'] = $messages; | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         return $err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-31 04:48:23 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get an approved API auth header. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function apiAuthHeader(): array | 
					
						
							| 
									
										
										
										
											2019-12-31 03:51:41 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'Authorization' => "Token {$this->apiTokenId}:{$this->apiTokenSecret}", | 
					
						
							| 
									
										
										
										
											2019-12-31 03:51:41 +08:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | } |