Commit 345441fb authored by Karamel's avatar Karamel
Browse files

Add invalid date response in customer route. Add some tests.

parent ff39cde1
<?php
use \Pasteque\Server\Exception\InvalidFieldException;
use \Pasteque\Server\Exception\RecordNotFoundException;
use \Pasteque\Server\Model\Customer;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\API\APICaller;
......@@ -55,10 +57,20 @@ $app->POST('/api/customer',
// Only here for backward compatibility, see oldApiCustomer below.
return oldApiCustomer($ptApp, $response, $structCust['customers']);
}
$structCust['expireDate'] = DateUtils::readDate($structCust['expireDate']);
if ($structCust['expireDate'] === false) {
return $response->withStatus(400, 'Invalid expireDate');
$expireDate = DateUtils::readDate($structCust['expireDate']);
if ($expireDate === false) {
$loadKey = null;
if (!empty($structCust['id'])) {
$loadKey = ['id' => $structCust['id']];
} else {
$loadKey = Customer::getLoadKey($structCust);
}
$e = new InvalidFieldException(InvalidFieldException::CSTR_INVALID_DATE,
Customer::class, 'expireDate',
$loadKey, $structCust['expireDate']);
return $response->reject($e, 'Invalid expireDate');
}
$structCust['expireDate'] = $expireDate;
$customer = null;
if (!empty($structCust['id'])) {
$customer = Customer::loadFromId($structCust['id'], $ptApp->getDao());
......@@ -66,7 +78,11 @@ $app->POST('/api/customer',
if ($customer === null) {
$customer = new Customer();
}
$customer->merge($structCust, $ptApp->getDao());
try {
$customer->merge($structCust, $ptApp->getDao());
} catch (InvalidFieldException $e) {
return $response->reject($e);
}
return $response->withApiResult(APICaller::run($ptApp, 'customer', 'write',
$customer));
});
......@@ -108,16 +124,10 @@ $app->PATCH('/api/customer/{id}/balance/{balance}',
$balance = floatval($args['balance']);
$cust = Customer::loadFromId($custId, $ptApp->getDao());
if ($cust === null) {
return $response->withStatus(404, 'Customer not found');
}
$balanceApiResp = APICaller::run($ptApp, 'customer', 'setBalance',
[$custId, $balance]);
if ($balanceApiResp->getStatus() != APIResult::STATUS_CALL_OK) {
return $response->withApiResult($balanceApiResp);
}
if ($balanceApiResp->getContent() === true) {
return $response->withStatus(200);
} else {
return $response->withStatus(404, 'Customer not found');
$e = new RecordNotFoundException(Customer::class,
['id' => $args['id']]);
return $response->notFound($e, 'Customer not found');
}
return $response->withAPIResult(APICaller::run($ptApp, 'customer',
'setBalance', [$custId, $balance]));
});
......@@ -47,6 +47,7 @@ class InvalidFieldException extends \Exception implements PastequeException
const CSTR_DEFAULT_REQUIRED = 'DefaultRequired';
/** The field is an enum and the value is not within it. */
const CSTR_ENUM = 'Enum';
const CSTR_INVALID_DATE = 'InvalidDate';
private $constraint;
private $class;
......
......@@ -83,6 +83,28 @@ class HttpCustomerTest extends TestCase
$this->dao->close();
}
public function testPostInvalidDate() {
$cust = new Customer();
$cust->setDispName('Customer');
$struct = $cust->toStruct();
$struct['expireDate'] = 'notadate';
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/customer'));
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(400, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('InvalidField', $jsResp['error']);
$this->assertEquals('InvalidDate', $jsResp['constraint']);
$this->assertEquals(Customer::class, $jsResp['class']);
$this->assertEquals('expireDate', $jsResp['field']);
$this->assertEquals('notadate', $jsResp['value']);
}
public function testSetBalance() {
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment