Skip to content

Commit

Permalink
test: improve services rules validation (#1986)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Nov 2, 2018
1 parent 769d795 commit 444babb
Show file tree
Hide file tree
Showing 53 changed files with 639 additions and 531 deletions.
12 changes: 12 additions & 0 deletions app/Exceptions/MissingParameterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@
*/
class MissingParameterException extends RuntimeException
{
/**
* The validation errors.
*
* @var array
*/
public $errors;

public function __construct(string $message, array $errors = null, int $code = 0, \Throwable $previous = null)
{
$this->errors = $errors;
parent::__construct($message, $code, $previous);
}
}
149 changes: 5 additions & 144 deletions app/Http/Controllers/Api/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@
use Illuminate\Http\Request;
use App\Models\Account\ApiUsage;
use App\Http\Controllers\Controller;
use App\Traits\JsonRespondController;

class ApiController extends Controller
{
/**
* @var int
*/
protected $httpStatusCode = 200;

/**
* @var int
*/
protected $errorCode;
use JsonRespondController;

/**
* @var int
Expand Down Expand Up @@ -50,15 +43,15 @@ public function __construct()
if (is_null($this->getSortCriteria())) {
return $this->setHTTPStatusCode(400)
->setErrorCode(39)
->respondWithError(config('api.error_codes.39'));
->respondWithError();
}
}

if ($request->has('limit')) {
if ($request->get('limit') > config('api.max_limit_per_page')) {
return $this->setHTTPStatusCode(400)
->setErrorCode(30)
->respondWithError(config('api.error_codes.30'));
->respondWithError();
}

$this->setLimitPerPage($request->get('limit'));
Expand All @@ -76,7 +69,7 @@ public function __construct()
&& is_null(json_decode($request->getContent()))) {
return $this->setHTTPStatusCode(400)
->setErrorCode(37)
->respondWithError(config('api.error_codes.37'));
->respondWithError();
}

return $next($request);
Expand All @@ -96,25 +89,6 @@ public function success()
]);
}

/**
* @return int
*/
public function getHTTPStatusCode()
{
return $this->httpStatusCode;
}

/**
* @param int $statusCode
* @return $this
*/
public function setHTTPStatusCode($statusCode)
{
$this->httpStatusCode = $statusCode;

return $this;
}

/**
* @return string
*/
Expand All @@ -134,25 +108,6 @@ public function setWithParameter($with)
return $this;
}

/**
* @return int
*/
public function getErrorCode()
{
return $this->errorCode;
}

/**
* @param int $errorCode
* @return $this
*/
public function setErrorCode($errorCode)
{
$this->errorCode = $errorCode;

return $this;
}

/**
* @return int
*/
Expand Down Expand Up @@ -236,98 +191,4 @@ public function setSQLOrderByQuery($criteria)
$this->sortDirection = 'desc';
}
}

/**
* Sends a JSON to the consumer.
* @param array $data
* @param array $headers [description]
* @return \Illuminate\Http\JsonResponse
*/
public function respond($data, $headers = [])
{
return response()->json($data, $this->getHTTPStatusCode(), $headers);
}

/**
* Sends a response not found (404) to the request.
* @param string $message
*/
public function respondNotFound()
{
return $this->setHTTPStatusCode(404)
->setErrorCode(31)
->respondWithError(config('api.error_codes.31'));
}

/**
* Sends a response invalid query to the request.
* @param string $message
*/
public function respondInvalidQuery($message = null)
{
return $this->setHTTPStatusCode(500)
->setErrorCode(40)
->respondWithError($message ?? config('api.error_codes.40'));
}

/**
* Sends an error when the query didn't have the right parameters for
* creating an object.
* @param string $message
*/
public function respondNotTheRightParameters($message = null)
{
return $this->setHTTPStatusCode(500)
->setErrorCode(33)
->respondWithError($message ?? config('api.error_codes.33'));
}

/**
* Sends an error when the query contains invalid parameters.
* @param string $message
*/
public function respondInvalidParameters($message = null)
{
return $this->setHTTPStatusCode(400)
->setErrorCode(41)
->respondWithError($message ?? config('api.error_codes.41'));
}

/**
* Sends an error when the validator failed.
* @param \Illuminate\Contracts\Validation\Validator $validator
*/
public function respondValidatorFailed($validator)
{
return $this->setHTTPStatusCode(400)
->setErrorCode(32)
->respondWithError($validator->errors()->all());
}

/**
* Sends a response with error.
* @param string message
*/
public function respondWithError($message)
{
return $this->respond([
'error' => [
'message' => $message,
'error_code' => $this->getErrorCode(),
],
]);
}

/**
* Sends a response that the object has been deleted, and also indicates
* the id of the object that has been deleted.
* @param int $id
*/
public function respondObjectDeleted($id)
{
return $this->respond([
'deleted' => true,
'id' => $id,
]);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/ApiReminderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private function validateUpdate(Request $request)
'next_expected_date' => 'required|date',
'frequency_type' => [
'required',
Rule::in(['one_time', 'day', 'month', 'year']),
Rule::in(Reminder::$frequencyTypes),
],
'frequency_number' => 'integer',
'contact_id' => 'required|integer',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function store(Request $request)
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
} catch (MissingParameterException $e) {
return $this->respondInvalidParameters();
return $this->respondInvalidParameters($e->errors);
} catch (QueryException $e) {
return $this->respondInvalidQuery();
}
Expand Down Expand Up @@ -127,7 +127,7 @@ public function update(Request $request, $conversationId)
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
} catch (MissingParameterException $e) {
return $this->respondInvalidParameters();
return $this->respondInvalidParameters($e->errors);
} catch (QueryException $e) {
return $this->respondInvalidQuery();
}
Expand All @@ -152,7 +152,7 @@ public function destroy(Request $request, $conversationId)
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
} catch (MissingParameterException $e) {
return $this->respondInvalidParameters();
return $this->respondInvalidParameters($e->errors);
} catch (QueryException $e) {
return $this->respondInvalidQuery();
}
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Api/Contact/ApiLifeEventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function store(Request $request)
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
} catch (MissingParameterException $e) {
return $this->respondInvalidParameters();
return $this->respondInvalidParameters($e->errors);
}

return new LifeEventResource($lifeEvent);
Expand All @@ -92,7 +92,7 @@ public function update(Request $request, $lifeEventId)
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
} catch (MissingParameterException $e) {
return $this->respondInvalidParameters();
return $this->respondInvalidParameters($e->errors);
}

return new LifeEventResource($lifeEvent);
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Api/Contact/ApiMessageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function store(Request $request, int $conversationId)
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
} catch (MissingParameterException $e) {
return $this->respondInvalidParameters();
return $this->respondInvalidParameters($e->errors);
} catch (QueryException $e) {
return $this->respondInvalidQuery();
}
Expand Down Expand Up @@ -82,7 +82,7 @@ public function update(Request $request, int $conversationId, int $messageId)
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
} catch (MissingParameterException $e) {
return $this->respondInvalidParameters();
return $this->respondInvalidParameters($e->errors);
} catch (QueryException $e) {
return $this->respondInvalidQuery();
}
Expand Down Expand Up @@ -116,7 +116,7 @@ public function destroy(Request $request, int $conversationId, int $messageId)
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
} catch (MissingParameterException $e) {
return $this->respondInvalidParameters();
return $this->respondInvalidParameters($e->errors);
} catch (QueryException $e) {
return $this->respondInvalidQuery();
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Contacts/IntroductionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function update(IntroductionsRequest $request, Contact $contact)
Contact::where('account_id', auth()->user()->account_id)
->findOrFail($request->get('metThroughId'));
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
return $this->handleModelNotFound($e);
}

$contact->first_met_through_contact_id = $request->get('metThroughId');
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Contacts/RelationshipsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function store(Request $request, Contact $contact)
$validator = Validator::make($request->all(), [
'first_name' => 'required|max:50',
'last_name' => 'max:100',
'gender_id' => 'required',
'gender_id' => 'required|integer',
'birthdayDate' => 'date_format:Y-m-d',
]);

Expand Down Expand Up @@ -224,7 +224,7 @@ public function update(Request $request, Contact $contact, Contact $otherContact
$validator = Validator::make($request->all(), [
'first_name' => 'required|max:50',
'last_name' => 'max:100',
'gender_id' => 'required',
'gender_id' => 'required|integer',
'birthdayDate' => 'date_format:Y-m-d',
]);

Expand Down
5 changes: 2 additions & 3 deletions app/Http/Controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Controllers;

use Exception;
use App\Helpers\DateHelper;
use App\Jobs\ResizeAvatars;
use App\Models\Contact\Tag;
Expand Down Expand Up @@ -572,7 +571,7 @@ public function stayInTouch(Request $request, Contact $contact)
$state = $request->get('state');

if (auth()->user()->account->hasLimitations()) {
throw new Exception(trans('people.stay_in_touch_premium'));
throw new \LogicException(trans('people.stay_in_touch_premium'));
}

// if not active, set frequency to 0
Expand All @@ -582,7 +581,7 @@ public function stayInTouch(Request $request, Contact $contact)
$result = $contact->updateStayInTouchFrequency($frequency);

if (! $result) {
throw new Exception(trans('people.stay_in_touch_invalid'));
throw new \LogicException(trans('people.stay_in_touch_invalid'));
}

$contact->setStayInTouchTriggerDate($frequency, DateHelper::getTimezone());
Expand Down
30 changes: 30 additions & 0 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,39 @@
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

/**
* Handle the ModelNotFoundException.
*
* @param ModelNotFoundException $e
* @return \Illuminate\Http\Response
*/
protected function handleModelNotFound(ModelNotFoundException $e)
{
return $this->respondUnauthorized($e->getMessage());
}

/**
* Respond with an Unauthorized message.
*
* @param string $errorMessage
* @return \Illuminate\Http\Response
*/
protected function respondUnauthorized($errorMessage = null)
{
$data = [];
if ($errorMessage) {
$data = ['errors' => $errorMessage];
}

return response()->json([
'message' => trans('app.error_unauthorized'),
] + $data, 400);
}
}
Loading

0 comments on commit 444babb

Please sign in to comment.