diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index aaddf7d..623b4b8 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -2,6 +2,8 @@ namespace App\Providers; +use App\Enums\ApiError; +use App\Exceptions\Api\V2\ApiErrorException; use App\Lib\Helper; use App\Models\Service; use Illuminate\Database\Eloquent\ModelNotFoundException; @@ -37,7 +39,7 @@ class AuthServiceProvider extends ServiceProvider $apiToken = $request->api_token; if (is_null($apiToken)) { - return null; + throw new ApiErrorException(ApiError::apiAuthFailed(), 401); } /** @@ -47,20 +49,22 @@ class AuthServiceProvider extends ServiceProvider ** this to route as a parameter */ if (!($route = Helper::getRoute($request))) { - return null; + throw new ApiErrorException(ApiError::apiAuthFailed(), 401); } try { $service = Service::getFromApiToken($apiToken); - $service = $service->load('brand'); - app()->bind(Service::class, function () use ($service) { - return $service; - }); - $test = $service->authenticateApi($apiToken); - return $test ? $service : null; } catch (ModelNotFoundException $e) { - return null; + throw new ApiErrorException(ApiError::apiAuthFailed(), 401); } + $service = $service->load('brand'); + app()->bind(Service::class, function () use ($service) { + return $service; + }); + if ($service->authenticateApi($apiToken)) { + return $service; + } + throw new ApiErrorException(ApiError::apiAuthFailed(), 401); }); } } diff --git a/clients/V2 b/clients/V2 index 0fa8c8d..b4982ab 160000 --- a/clients/V2 +++ b/clients/V2 @@ -1 +1 @@ -Subproject commit 0fa8c8d691f72c9d48a5db296a830920ef25534a +Subproject commit b4982ab41b2d2df4bd0ec43b2e300c701aaa0184 diff --git a/composer.json b/composer.json index a754ddd..9329edd 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "ldc/ldccrmv2client", "type": "project", - "version": "2.1.7", + "version": "2.1.8", "description": "LDC CRM V2", "authors": [ { diff --git a/tests/Feature/ApiV2/CustomerTest.php b/tests/Feature/ApiV2/CustomerTest.php index bf144b4..fc7ba50 100644 --- a/tests/Feature/ApiV2/CustomerTest.php +++ b/tests/Feature/ApiV2/CustomerTest.php @@ -64,6 +64,32 @@ class CustomerTest extends ApiBase $this->assertNotEmpty($json->json('data.hashcode')); } + /** + * route: apiV2.customers.authenticate + */ + public function testApiAuthenticateCustomerShouldFail() + { + extract($this->getCustomer()); + $response = $this->callApiWithToken( + $service, + 'post', + route('apiV2.customers.authenticate', []), + [ + 'email' => $customer->email, + 'password' => 'testPasswords' + ] + ); + + $json = $response->decodeResponseJson(); + + $this->assertApiResponseStatusIs($json, ApiStatus::KO()); + $this->assertNotEmpty($json->json('data')); + $this->assertNotEmpty($json->json('data.message')); + $this->assertNotEmpty($json->json('data.code')); + $this->assertEquals(ApiError::authFailed()->value, $json->json('data.code')); + $this->assertEquals(ApiError::authFailed()->label, $json->json('data.message')); + } + /** * route: apiV2.customers.store */