This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
266 lines
8.8 KiB
PHP
266 lines
8.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Client;
|
|
|
|
use DOMDocument;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Str;
|
|
use LDC\LDCCRM\CrmApiV2;
|
|
use LDC\LDCCRM\Enums\ArcadiaOrderType;
|
|
use LDC\LDCCRM\Models\ArcadiaMailDefault;
|
|
use LDC\LDCCRM\Models\ArcadiaMailGame;
|
|
|
|
class CrmClientV2ArcadiaTest extends CrmClientTester
|
|
{
|
|
/**
|
|
* Send arcadia order default test.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testArcadiaSendOrderDefault()
|
|
{
|
|
$api = new CrmApiV2();
|
|
$customer = $api->getCustomerOrNew(static::$info->hashcode);
|
|
|
|
$arcadiaOrderMail = new ArcadiaMailDefault([
|
|
'email' => static::$customer->email,
|
|
'firstname' => static::$info->firstname,
|
|
'lastname' => static::$info->lastname,
|
|
'phone' => static::$info->phone,
|
|
'address' => static::$info->address,
|
|
'address2' => static::$info->address2,
|
|
'city' => static::$info->city,
|
|
'postcode' => static::$info->postcode,
|
|
'operation' => 16,
|
|
'label_type_support' => 'Send Departaimants',
|
|
'order_date' => \date('Y/m/d H:i:s'),
|
|
'state_order' => ArcadiaOrderType::cb()->value,
|
|
'support_code' => 1,
|
|
'label_type_support' => 'Fond carte Aimanté',
|
|
'order_num' => 23,
|
|
'package_unit_weight' => 0,
|
|
]);
|
|
$this->assertTrue($arcadiaOrderMail->send($customer));
|
|
}
|
|
|
|
/**
|
|
* Test arcadia default mail can serialize to json
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testArcadiaOrderDefaultJson()
|
|
{
|
|
$data = [
|
|
'email' => static::$customer->email,
|
|
'firstname' => static::$info->firstname,
|
|
'lastname' => static::$info->lastname,
|
|
'phone' => static::$info->phone,
|
|
'address' => static::$info->address,
|
|
'address2' => static::$info->address2,
|
|
'city' => static::$info->city,
|
|
'postcode' => static::$info->postcode,
|
|
'operation' => 16,
|
|
'label_type_support' => 'Send Departaimants',
|
|
'order_date' => \date('Y/m/d H:i:s'),
|
|
'state_order' => ArcadiaOrderType::cb()->value,
|
|
'support_code' => 1,
|
|
'label_type_support' => 'Fond carte Aimanté',
|
|
'order_num' => 23,
|
|
'package_unit_weight' => 0,
|
|
];
|
|
|
|
$arcadiaOrderMail = new ArcadiaMailDefault($data);
|
|
|
|
/** Mutator and accessor tests */
|
|
foreach ($data as $propName => $value) {
|
|
$arcadiaOrderMail->{$propName} = $value;
|
|
$arcadiaOrderMail->{$propName} = $arcadiaOrderMail->{$propName};
|
|
}
|
|
|
|
$jsonDecoded = \json_decode(\json_encode($arcadiaOrderMail), true);
|
|
$this->assertIsArray($jsonDecoded);
|
|
foreach ($data as $key => $value) {
|
|
if (isset($jsonDecoded['deliver'][$key])) {
|
|
$this->assertArrayHasKey($key, $jsonDecoded['deliver']);
|
|
$this->assertEquals($value, $jsonDecoded['deliver'][$key]);
|
|
} else {
|
|
$this->assertArrayHasKey($key, $jsonDecoded);
|
|
$this->assertEquals($value, $jsonDecoded[$key]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send arcadia order default game.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testArcadiaSendOrderGame()
|
|
{
|
|
$api = new CrmApiV2();
|
|
$customer = $api->getCustomerOrNew(static::$info->hashcode);
|
|
$arcadiaOrderMail = new ArcadiaMailGame([
|
|
'email' => static::$customer->email,
|
|
'firstname' => static::$info->firstname,
|
|
'lastname' => static::$info->lastname,
|
|
'phone' => static::$info->phone,
|
|
'address' => static::$info->address,
|
|
'address2' => static::$info->address2,
|
|
'city' => static::$info->city,
|
|
'postcode' => static::$info->postcode,
|
|
'operation' => 16,
|
|
'order_date' => date('Y/m/d H:i:s'),
|
|
'support_code' => 32,
|
|
'label_type_support' => 'Jeux Reghalal',
|
|
'code_unique' => \strtoupper(\str_replace('-', '', (string)Str::uuid())),
|
|
'code_on_pack' => '12365214587562'
|
|
]);
|
|
$this->assertTrue($arcadiaOrderMail->send($customer));
|
|
}
|
|
|
|
/**
|
|
* Test arcadia game mail can serialize to json
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testArcadiaOrderGameJson()
|
|
{
|
|
$data = [
|
|
'email' => static::$customer->email,
|
|
'firstname' => static::$info->firstname,
|
|
'lastname' => static::$info->lastname,
|
|
'phone' => static::$info->phone,
|
|
'address' => static::$info->address,
|
|
'address2' => static::$info->address2,
|
|
'city' => static::$info->city,
|
|
'postcode' => static::$info->postcode,
|
|
'operation' => 16,
|
|
'order_date' => date('Y/m/d H:i:s'),
|
|
'support_code' => 32,
|
|
'label_type_support' => 'Jeux Reghalal',
|
|
'code_unique' => \strtoupper(\str_replace('-', '', (string)Str::uuid())),
|
|
'code_on_pack' => '12365214587562'
|
|
];
|
|
|
|
$arcadiaGameMail = new ArcadiaMailGame($data);
|
|
|
|
/** Mutator and accessor tests */
|
|
foreach ($data as $propName => $value) {
|
|
$arcadiaGameMail->{$propName} = $value;
|
|
$arcadiaGameMail->{$propName} = $arcadiaGameMail->{$propName};
|
|
}
|
|
|
|
$jsonDecoded = \json_decode(\json_encode($arcadiaGameMail), true);
|
|
$this->assertIsArray($jsonDecoded);
|
|
foreach ($data as $key => $value) {
|
|
if (isset($jsonDecoded['deliver'][$key])) {
|
|
$this->assertArrayHasKey($key, $jsonDecoded['deliver']);
|
|
$this->assertEquals($value, $jsonDecoded['deliver'][$key]);
|
|
} else {
|
|
$this->assertArrayHasKey($key, $jsonDecoded);
|
|
$this->assertEquals($value, $jsonDecoded[$key]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Valdate xml generated files default.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testValidateGeneratedXmlDefault()
|
|
{
|
|
$fileFound = false;
|
|
|
|
$files = File::allFiles(\storage_path('export/arcadia'));
|
|
|
|
/** @var \Symfony\Component\Finder\SplFileInfo $file */
|
|
foreach ($files as $file) {
|
|
if (\strpos($file->getFilename(), 'v2-order-000000023-default-') === 0) {
|
|
$fileFound = true;
|
|
|
|
// Enable user error handling.
|
|
\libxml_use_internal_errors(true);
|
|
|
|
$xml = new DOMDocument();
|
|
$xml->load($file->getRealPath());
|
|
|
|
$this->assertTrue($xml->schemaValidate(\resource_path('documents/arcadia_default.xsd')));
|
|
$this->libxmlDisplayErrors();
|
|
}
|
|
}
|
|
$this->assertTrue($fileFound, 'Should have found a xml file to validate and pass validation');
|
|
}
|
|
|
|
/**
|
|
* Valdate xml generated files game.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testValidateGeneratedXmlGame()
|
|
{
|
|
$fileFound = false;
|
|
|
|
$files = File::allFiles(\storage_path('export/arcadia'));
|
|
|
|
/** @var \Symfony\Component\Finder\SplFileInfo $file */
|
|
foreach ($files as $file) {
|
|
if (\strpos($file->getFilename(), 'v2-order-000000000-game') === 0) {
|
|
$fileFound = true;
|
|
|
|
// Enable user error handling.
|
|
\libxml_use_internal_errors(true);
|
|
|
|
$xml = new DOMDocument();
|
|
$xml->load($file->getRealPath());
|
|
|
|
$this->assertTrue($xml->schemaValidate(\resource_path('documents/arcadia_game.xsd')));
|
|
$this->libxmlDisplayErrors();
|
|
}
|
|
}
|
|
$this->assertTrue($fileFound, 'Should have found a xml file to validate and pass validation');
|
|
}
|
|
|
|
/**
|
|
* Render a \LibXMLError
|
|
*
|
|
* @param \LibXMLError $error
|
|
* @return string
|
|
*/
|
|
private function libxmlDisplayError(\LibXMLError $error): string
|
|
{
|
|
$return = "<br/>\n";
|
|
switch ($error->level) {
|
|
case LIBXML_ERR_WARNING:
|
|
$return .= "<b>Warning $error->code</b>: ";
|
|
break;
|
|
case LIBXML_ERR_ERROR:
|
|
$return .= "<b>Error $error->code</b>: ";
|
|
break;
|
|
case LIBXML_ERR_FATAL:
|
|
$return .= "<b>Fatal Error $error->code</b>: ";
|
|
break;
|
|
}
|
|
$return .= trim($error->message);
|
|
if ($error->file) {
|
|
$return .= " in <b>$error->file</b>";
|
|
}
|
|
$return .= " on line <b>$error->line</b>\n";
|
|
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* Display xml errors.
|
|
*
|
|
* @return void
|
|
*/
|
|
private function libxmlDisplayErrors()
|
|
{
|
|
$errors = libxml_get_errors();
|
|
foreach ($errors as $error) {
|
|
print $this->libxmlDisplayError($error);
|
|
}
|
|
libxml_clear_errors();
|
|
}
|
|
}
|