From 5fee6629c1aeda35669d1a836bce308e557c0ae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 10 Aug 2021 10:20:52 +0200 Subject: [PATCH] API V1 Test ok + All test OK --- app/Http/Middleware/ApiResponse.php | 4 + app/Http/Middleware/Authenticate.php | 6 +- app/Http/Middleware/CheckBrand.php | 32 +-- app/Models/CustomerLogin.php | 8 +- app/Models/Service.php | 8 +- app/Providers/RouteServiceProvider.php | 1 + clients/{LDCCRM_V1.php => ApiV1.php} | 53 ++-- composer.json | 3 +- tests/Unit/CrmClientTester.php | 6 +- tests/Unit/CrmClientV1CustomerTest.php | 360 +++++++++++++++++++++++++ 10 files changed, 425 insertions(+), 56 deletions(-) rename clients/{LDCCRM_V1.php => ApiV1.php} (84%) create mode 100644 tests/Unit/CrmClientV1CustomerTest.php diff --git a/app/Http/Middleware/ApiResponse.php b/app/Http/Middleware/ApiResponse.php index f74005f..3509de8 100644 --- a/app/Http/Middleware/ApiResponse.php +++ b/app/Http/Middleware/ApiResponse.php @@ -53,6 +53,10 @@ class ApiResponse Log::error(sprintf('Could not increment stats %s', $e->getMessage())); } + if ($isApiV1Request) { + return $response; + } + $data = $response->getData(); // Is reponse is already a V2ApiResponse then just return it diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php index bacf566..dafed4a 100644 --- a/app/Http/Middleware/Authenticate.php +++ b/app/Http/Middleware/Authenticate.php @@ -27,12 +27,10 @@ class Authenticate extends Middleware */ public function handle($request, Closure $next, ...$guards) { - $isApiV1Request = $request->isJson() && Helper::isRequestForApiV1($request); - // TODO: check this works - if ($isApiV1Request) { + if (Helper::isRequestForApiV1($request)) { // Use patched v1 Auth mecanism (without bearer reimplementation) - $bearerToken = $request->bearerToken(); + $bearerToken = $request->bearerToken() ?? $request->api_token; $brand = Service::getFromBearerToken($bearerToken)->brand; $request->request->add(['brand_id' => $brand->id]); $response = $next($request); diff --git a/app/Http/Middleware/CheckBrand.php b/app/Http/Middleware/CheckBrand.php index cd79b38..cbe94b1 100644 --- a/app/Http/Middleware/CheckBrand.php +++ b/app/Http/Middleware/CheckBrand.php @@ -7,14 +7,12 @@ use App\Models\Service; use App\Models\ServiceStat; use Closure; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\Request; use Illuminate\Support\Facades\Hash; class CheckBrand { - - private $apiToken; - /** * Handle an incoming request. * @@ -24,29 +22,17 @@ class CheckBrand */ public function handle(Request $request, Closure $next) { - if ($request->bearerToken() !== null) { - $this->apiToken = $request->bearerToken(); - } else { - $this->apiToken = ($request->api_token !== null ? $request->api_token : null); - } - print_r($request->all()); + $apiToken = $request->bearerToken() ?? $request->api_token; - if ($this->apiToken === null) { - abort(401, 'Le token d\'authentification est incorrect'); - } - $brand = Brand::whereHas('services', function (Builder $query) { - $query->where("bearer_token", Hash::make($this->apiToken)); - })->first(); - - if (!$brand) { - abort(401, 'Le token d\'authentification est incorrect'); + if ($apiToken === null) { + abort(401, 'Le token d\'authentification est incorrect dfd' . \json_encode($request->all())); } - $serviceStat = ServiceStat::firstOrNew(['date' => date('Y-m-d')]); - $serviceStat->brand_id = $brand->id; - $serviceStat->service_id = Service::where('bearer_token', Hash::make($this->apiToken))->first()->id; - $serviceStat->nb_calls = $serviceStat->nb_calls === 0 ? 1 : $serviceStat->nb_calls + 1; - $serviceStat->save(); + try { + $brand = Service::getFromBearerToken($apiToken)->brand; + } catch (ModelNotFoundException $e) { + abort(401, 'Le token d\'authentification est incorrect ' . $e->getMessage()); + } $request->request->add(['brand_id' => $brand->id]); return $next($request); diff --git a/app/Models/CustomerLogin.php b/app/Models/CustomerLogin.php index 8b7bfd6..6922714 100644 --- a/app/Models/CustomerLogin.php +++ b/app/Models/CustomerLogin.php @@ -17,10 +17,14 @@ class CustomerLogin extends Model protected static function booted() { static::creating(function ($customerLogin) { - $customerLogin->password = Hash::make(trim($customerLogin->password)); + if (Hash::needsRehash($customerLogin->password)) { + $customerLogin->password = Hash::make(trim($customerLogin->password)); + } }); static::updating(function ($customerLogin) { - $customerLogin->password = Hash::make(trim($customerLogin->password)); + if (Hash::needsRehash($customerLogin->password)) { + $customerLogin->password = Hash::make(trim($customerLogin->password)); + } }); } diff --git a/app/Models/Service.php b/app/Models/Service.php index 7716f4b..c11afb0 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -75,10 +75,12 @@ class Service extends Model */ public function incrementStat(): void { - $serviceStat = ServiceStat::firstOrNew(['date' => date('Y-m-d')]); + $serviceStat = ServiceStat::firstOrNew(['date' => date('Y-m-d')]); + if (!$serviceStat->exists) { + $serviceStat->service()->associate($this); + $serviceStat->brand()->associate($this->brand); + } $serviceStat->nb_calls += 1; - $serviceStat->service()->associate($this); - $serviceStat->brand()->associate($this->brand); $serviceStat->saveOrFail(); } diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index d80193e..cba00ca 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -36,6 +36,7 @@ class RouteServiceProvider extends ServiceProvider }); Route::bind('customer', function ($value) { + $test = Customer::where('email', '=', $value); return Customer::whereHas('infos', function ($q) use ($value) { $q->where('customer_infos.hashcode', $value); })->first() ?? (\ctype_digit($value) ? Customer::where('id', '=', $value)->first() : null) ?? diff --git a/clients/LDCCRM_V1.php b/clients/ApiV1.php similarity index 84% rename from clients/LDCCRM_V1.php rename to clients/ApiV1.php index 54a351d..d8e23b3 100644 --- a/clients/LDCCRM_V1.php +++ b/clients/ApiV1.php @@ -13,15 +13,18 @@ class ApiV1 $this->api_key = $api_key; } - public function getCustomer($id){ + public function getCustomer($id) + { return $this->callAPI('GET', "/customer/id/$id"); } - public function getCustomerByEmail($email){ + public function getCustomerByEmail($email) + { return $this->callAPI('GET', "/customer/email/$email"); } - public function createCustomer($email, $password, $firstname, $lastname, $optin, $birthdate = null, $phone = null, $address = null, $address2 = null, $city = null, $postcode = null, $country = null){ + public function createCustomer($email, $password, $firstname, $lastname, $optin, $birthdate = null, $phone = null, $address = null, $address2 = null, $city = null, $postcode = null, $country = null) + { $user = [ 'email' => $email, 'password' => $password, @@ -39,7 +42,8 @@ class ApiV1 return $this->callAPI('POST', '/customer/create', $user); } - public function updateCustomer($crmId, $firstname = null, $lastname = null, $optin = null, $birthdate = null, $phone = null, $address = null, $address2 = null, $city = null, $postcode = null, $country = null){ + public function updateCustomer($crmId, $firstname = null, $lastname = null, $optin = null, $birthdate = null, $phone = null, $address = null, $address2 = null, $city = null, $postcode = null, $country = null) + { $user = []; if ($firstname !== null) @@ -76,55 +80,64 @@ class ApiV1 return $this->callAPI('PUT', "/customer/$crmId", $user); } - public function deleteCustomer($id){ + public function deleteCustomer($id) + { return $this->callAPI('DELETE', "/customer/$id"); } - public function changePwdCustomer($id, $password){ + public function changePwdCustomer($id, $password) + { return $this->callAPI('POST', "/customer/$id/change-password", ['password' => $password]); } - public function activateCustomer($id){ + public function activateCustomer($id) + { return $this->callAPI('POST', "/customer/$id/activate"); } - public function toggleOptInCustomer($id){ + public function toggleOptInCustomer($id) + { return $this->callAPI('POST', "/customer/$id/toggle-optin"); } - public function removeOptInCustomer($hashcode){ + public function removeOptInCustomer($hashcode) + { return $this->callAPI('POST', "/customer/$hashcode/remove-optin"); } - public function loginCustomer($email, $password){ + public function loginCustomer($email, $password) + { return $this->callAPI('POST', "/customer/login", ['email' => $email, 'password' => $password]); } - public function createSingleOptin($email){ + public function createSingleOptin($email) + { return $this->callAPI('POST', '/single-optin', ['email' => $email]); } - public function ArcadiaOutgoingMail($crm_id, $fields, $type, $shipping = false){ + public function ArcadiaOutgoingMail($crm_id, $fields, $type, $shipping = false) + { if ($shipping !== false && $type === 'default') { return $this->callAPI('POST', '/arcadia/outgoing-mail', ['crm_id' => $crm_id, 'fields' => json_encode($fields), 'type' => $type, 'shipping' => json_encode($shipping)]); - }else{ + } else { return $this->callAPI('POST', '/arcadia/outgoing-mail', ['crm_id' => $crm_id, 'fields' => json_encode($fields), 'type' => $type]); } } - private function callAPI($method, $url, $data = false){ + private function callAPI($method, $url, $data = false) + { - if (strpos($this->endpoint, "dev")){ - if ($data !== false){ + if (strpos($this->endpoint, "dev")) { + if ($data !== false) { $data = array_merge($data, ['api_token' => $this->api_key]); - }else{ + } else { $data = ['api_token' => $this->api_key]; } } $url = $this->endpoint . '/api/v1' . $url; $curl = curl_init(); - switch ($method){ + switch ($method) { case "POST": curl_setopt($curl, CURLOPT_POST, 1); if ($data) @@ -159,9 +172,9 @@ class ApiV1 if (!curl_errno($curl)) { $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if (!in_array($http_code, [200, 201])) - return (object) ['status' => 'KO', 'code' => 500, 'message' => 'Connection to API failed - Code HTTP : '. $http_code]; + return (object) ['status' => 'KO', 'code' => 500, 'message' => 'Connection to API failed - Code HTTP : ' . $http_code]; } else { - return (object) ['status' => 'KO', 'code' => 500, 'message' => 'Connection to API failed : '. curl_error($curl)]; + return (object) ['status' => 'KO', 'code' => 500, 'message' => 'Connection to API failed : ' . curl_error($curl)]; } curl_close($curl); diff --git a/composer.json b/composer.json index 16c6cd7..3180222 100644 --- a/composer.json +++ b/composer.json @@ -63,7 +63,8 @@ "Database\\Factories\\": "database/factories/", "Database\\migrations\\": "database/migrations/", "Database\\Seeders\\": "database/seeders/", - "LDC\\LDCCRM\\" : "clients/V2/src/" + "LDC\\LDCCRM\\" : "clients/V2/src/", + "LDCCRM\\" : "clients/" } }, "autoload-dev": { diff --git a/tests/Unit/CrmClientTester.php b/tests/Unit/CrmClientTester.php index b2ec773..f76d422 100644 --- a/tests/Unit/CrmClientTester.php +++ b/tests/Unit/CrmClientTester.php @@ -36,10 +36,10 @@ abstract class CrmClientTester extends TestCase self::$service = $service; self::$login = $login; self::$info = $info; - config(['crm.token' => self::$service->bearer_token]); - config(['crm.password' => self::$service->bearer_pwd]); - config(['crm.endpoint' => 'http://127.0.0.25:8888']); } + config(['crm.token' => self::$service->bearer_token]); + config(['crm.password' => self::$service->bearer_pwd]); + config(['crm.endpoint' => 'http://127.0.0.25:8888']); } /** diff --git a/tests/Unit/CrmClientV1CustomerTest.php b/tests/Unit/CrmClientV1CustomerTest.php new file mode 100644 index 0000000..c47b006 --- /dev/null +++ b/tests/Unit/CrmClientV1CustomerTest.php @@ -0,0 +1,360 @@ +getCustomer(static::$customer->id); + + $this->assertEquals($reponse->status, 'OK'); + $this->assertEquals($reponse->user->crm_id, static::$customer->id); + $this->assertEquals($reponse->user->email, static::$customer->email); + $this->assertEquals($reponse->user->firstname, static::$info->firstname); + $this->assertEquals($reponse->user->lastname, static::$info->lastname); + $this->assertEquals($reponse->user->birth_date, static::$info->birth_date); + $this->assertEquals($reponse->user->address, static::$info->address); + $this->assertEquals($reponse->user->address2, static::$info->address2); + $this->assertEquals($reponse->user->phone, static::$info->phone); + $this->assertEquals($reponse->user->country, static::$info->country); + $this->assertEquals($reponse->user->city, static::$info->city); + $this->assertEquals($reponse->user->opt_in, static::$info->opt_in); + $this->assertEquals($reponse->user->hashcode, static::$info->hashcode); + $this->assertEquals($reponse->user->status, static::$login->status); + } + + public function testGetCustomerByIdShouldFail() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->getCustomer(9999999); + + $this->assertEquals($reponse->status, 'KO'); + $this->assertEquals($reponse->code, 101); + } + + public function testGetCustomerByEmail() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->getCustomerByEmail(static::$customer->email); + + $this->assertEquals($reponse->status, 'OK'); + $this->assertEquals($reponse->user->crm_id, static::$customer->id); + $this->assertEquals($reponse->user->email, static::$customer->email); + $this->assertEquals($reponse->user->firstname, static::$info->firstname); + $this->assertEquals($reponse->user->lastname, static::$info->lastname); + $this->assertEquals($reponse->user->birth_date, static::$info->birth_date); + $this->assertEquals($reponse->user->address, static::$info->address); + $this->assertEquals($reponse->user->address2, static::$info->address2); + $this->assertEquals($reponse->user->phone, static::$info->phone); + $this->assertEquals($reponse->user->country, static::$info->country); + $this->assertEquals($reponse->user->city, static::$info->city); + $this->assertEquals($reponse->user->opt_in, static::$info->opt_in); + $this->assertEquals($reponse->user->hashcode, static::$info->hashcode); + $this->assertEquals($reponse->user->status, static::$login->status); + } + + public function testGetCustomerByEmailShouldFail() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->getCustomer('totoo@toto.fr'); + + $this->assertEquals($reponse->status, 'KO'); + $this->assertEquals($reponse->code, 101); + } + + public function testCustomerStoreProps() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->createCustomer( + 'v1toto.delarogerie@gmail.com', + 'Complic4tEd!P4assW0rd', + 'toto', + 'delarogerie', + true, + '1992-03-08', + '0673891160', + 'rue des totos', + '', + 'Toto ville', + '72160', + Country::france()->label + ); + + $this->assertEquals($reponse->status, 'OK'); + $this->assertNotEmpty($reponse->user->crm_id); + $this->assertNotEmpty($reponse->user->hashcode); + $this->assertEquals($reponse->user->email, 'v1toto.delarogerie@gmail.com'); + $this->assertEquals($reponse->user->firstname, 'Toto'); + $this->assertEquals($reponse->user->lastname, 'Delarogerie'); + $this->assertEquals($reponse->user->birth_date, '1992-03-08'); + $this->assertEquals($reponse->user->address, 'rue des totos'); + $this->assertEquals($reponse->user->address2, ''); + $this->assertEquals($reponse->user->phone, '0673891160'); + $this->assertEquals($reponse->user->country, Country::france()->label); + $this->assertEquals($reponse->user->city, 'Toto ville'); + $this->assertEquals($reponse->user->opt_in, true); + $this->assertEquals($reponse->user->status, false); + } + + public function testCustomerStorePropsShouldFailBecauseAlreadyExist() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->createCustomer( + 'v1toto.delarogerie@gmail.com', + 'Complic4tEd!P4assW0rd', + 'toto', + 'delarogerie', + true, + '1992-03-08', + '0673891160', + 'rue des totos', + '', + 'Toto ville', + '72160', + Country::france()->label + ); + + $this->assertEquals($reponse->status, 'KO'); + $this->assertEquals($reponse->code, 103); + } + + public function testCustomerStorePropsShouldFail() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->createCustomer( + 'v1bistoto.delarogerie@gmail.com', + '123456', + 'toto bis', + 'delarogerie', + true, + '1992-08', + '0673891160', + 'rue des totos', + '', + 'Toto ville', + '72160', + Country::france()->label + ); + + $this->assertEquals($reponse->status, 'KO'); + $this->assertEquals($reponse->code, 102); + } + + public function testCustomerUpdateProps() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->updateCustomer( + ModelsCustomer::where('email', 'v1toto.delarogerie@gmail.com')->firstOrFail()->id, + 'toto new', + 'delarogerie', + true, + '1992-03-08', + '0673891160', + 'rue des totos', + '', + 'Toto ville', + '72160', + Country::france()->label + ); + + $this->assertEquals($reponse->status, 'OK'); + $this->assertNotEmpty($reponse->user->crm_id); + $this->assertNotEmpty($reponse->user->hashcode); + $this->assertEquals($reponse->user->email, 'v1toto.delarogerie@gmail.com'); + $this->assertEquals($reponse->user->firstname, 'toto new'); + $this->assertEquals($reponse->user->lastname, 'delarogerie'); + $this->assertEquals($reponse->user->birth_date, '1992-03-08'); + $this->assertEquals($reponse->user->address, 'rue des totos'); + $this->assertEquals($reponse->user->address2, ''); + $this->assertEquals($reponse->user->phone, '0673891160'); + $this->assertEquals($reponse->user->country, Country::france()->label); + $this->assertEquals($reponse->user->city, 'Toto ville'); + $this->assertEquals($reponse->user->opt_in, true); + $this->assertEquals($reponse->user->status, false); + } + + public function testCustomerLoginShouldFailDontExists() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->loginCustomer('v1totso.delarogerie@gmail.com', 'Complic4tEd!P4assW0rd'); + + $this->assertEquals('KO', $reponse->status); + $this->assertEquals(119, $reponse->code); + } + + public function testCustomerLoginShouldFailNotActivated() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->loginCustomer('v1toto.delarogerie@gmail.com', 'Complic4tEd!P4assW0rd'); + + $this->assertEquals('KO', $reponse->status); + $this->assertEquals(116, $reponse->code); + } + + public function testCustomeActivate() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->activateCustomer( + ModelsCustomer::where('email', 'v1toto.delarogerie@gmail.com')->firstOrFail()->id + ); + + $this->assertEquals('OK', $reponse->status); + } + + public function testCustomerLogin() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->loginCustomer('v1toto.delarogerie@gmail.com', 'Complic4tEd!P4assW0rd'); + + $this->assertEquals('OK', $reponse->status); + } + + public function testCustomeActivateShouldFail() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->activateCustomer( + ModelsCustomer::where('email', 'v1toto.delarogerie@gmail.com')->firstOrFail()->id + ); + + $this->assertEquals('KO', $reponse->status); + $this->assertEquals(110, $reponse->code); + } + + public function testCustomeToggleOptin() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->toggleOptInCustomer( + ModelsCustomer::where('email', 'v1toto.delarogerie@gmail.com')->firstOrFail()->id + ); + + $this->assertEquals('OK', $reponse->status); + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->toggleOptInCustomer( + ModelsCustomer::where('email', 'v1toto.delarogerie@gmail.com')->firstOrFail()->id + ); + + $this->assertEquals('OK', $reponse->status); + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->toggleOptInCustomer( + ModelsCustomer::where('email', 'v1toto.delarogerie@gmail.com')->firstOrFail()->id + ); + + $this->assertEquals('OK', $reponse->status); + } + + public function testCustomeRemoveOptin() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $hashcode = $api->getCustomerByEmail('v1toto.delarogerie@gmail.com')->user->hashcode; + $reponse = $api->removeOptInCustomer($hashcode); + + $this->assertEquals('OK', $reponse->status); + } + + public function testCustomerDelete() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->deleteCustomer(ModelsCustomer::where('email', 'v1toto.delarogerie@gmail.com') + ->firstOrFail()->id); + + $this->assertNull($reponse); + } + + public function testCreateSingleOptin() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->createSingleOptin('singlev1toto.delarogerie@gmail.com'); + + $this->assertEquals('OK', $reponse->status); + } + + public function testCreateSingleOptinShouldFail() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->createSingleOptin('singlev1toto.delarogerie@gmail.com'); + + $this->assertEquals('KO', $reponse->status); + $this->assertEquals(121, $reponse->code); + } + + public function testCreateArcadiaOutgoingMail() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->ArcadiaOutgoingMail( + ModelsCustomer::where('email', 'v1toto.delarogerie@gmail.com') + ->firstOrFail()->id, + [ + 'root' => [ + [ + 'fieldName' => 'order_num', + //format 9 numeric characters + 'fieldValue' => str_pad(12, 9, '0', \STR_PAD_LEFT), + ], + [ + 'fieldName' => 'order_date', + //format : DD/MM/YYYY + 'fieldValue' => '12/05/1992', + ], + [ + 'fieldName' => 'state_order_cb', + // format 1 ou Null (oui Null et pas NULL) + 'fieldValue' => 1, + ], + [ + 'fieldName' => 'state_order_check', + // format 1 ou Null (oui Null et pas NULL) + 'fieldValue' => 'Null' + ], + [ + 'fieldName' => 'operation', + // mettre valeur dans .env + 'fieldValue' => 16 + ] + ], + 'orderLine' => [ + [ + 'fieldName' => 'package_unit_weight', + 'fieldValue' => 200 + ], + [ + 'fieldName' => 'qty', + // forced to 1 faire un appel par produit et pour chaque quantité + 'fieldValue' => '1' + ], + [ + 'fieldName' => 'support_code', + 'fieldValue' => 2 + ], + [ + 'fieldName' => 'label_type_support', + 'fieldValue' => 'label supoport' + ] + ] + ], + 'default', + [ + "firstname" => 'toto', + "lastname" => 'delarogerie', + "address" => 'rue des toto', + "city" => 'toto ville', + "postcode" => '72000', + "phone" => '0673891160' + ] + ); + + $this->assertEquals('OK', $reponse->status); + } +}