getting first tests to work

This commit is contained in:
Dario Rekowski on RockPI 2019-11-06 08:30:18 +00:00
parent 16bd126133
commit 3a15bd320c
4 changed files with 120 additions and 93 deletions

View File

@ -114,7 +114,9 @@ ini_set('intl.default_locale', Configure::read('App.defaultLocale'));
/* /*
* Register application error and exception handlers. * Register application error and exception handlers.
*/ */
$isCli = PHP_SAPI === 'cli'; $isCli = PHP_SAPI === 'cli';
if ($isCli) { if ($isCli) {
(new ConsoleErrorHandler(Configure::read('Error')))->register(); (new ConsoleErrorHandler(Configure::read('Error')))->register();
} else { } else {

View File

@ -1,4 +1,3 @@
<<<<<<< HEAD
<?php <?php
/* /*
@ -20,13 +19,22 @@ class Transaction extends TransactionBase {
private $mTransactionBody = null; private $mTransactionBody = null;
public function __construct($base64Data) { public function __construct($base64Data) {
$transactionBin = base64_decode($base64Data); $transactionBin = base64_decode($base64Data, true);
if($transactionBin == FALSE) {
if($transactionBin == false) {
//$this->addError('base64 decode failed'); //$this->addError('base64 decode failed');
$this->addError('Transaction', ['data' => $base64Data, 'bin' => $transactionBin, 'msg' => 'base64 decode error']); $this->addError('Transaction', 'base64 decode error');
} else { } else {
$this->mProtoTransaction = new \Model\Messages\Gradido\Transaction(); $this->mProtoTransaction = new \Model\Messages\Gradido\Transaction();
$this->mProtoTransaction->mergeFromString($transactionBin); try {
$this->mProtoTransaction->mergeFromString($transactionBin);
// cannot catch Exception with cakePHP, I don't know why
} catch(\Google\Protobuf\Internal\GPBDecodeException $e) {
//var_dump($e);
$this->addError('Transaction', $e->getMessage());
return;
}//*/
//echo 'serialize to json: <br>'; //echo 'serialize to json: <br>';
//echo $this->mProtoTransaction->serializeToJsonString(); //echo $this->mProtoTransaction->serializeToJsonString();
@ -122,71 +130,4 @@ class Transaction extends TransactionBase {
=======
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Model\Transactions;
//use Model\Messages\Gradido\Transaction;
//use Model\Messages\Gradido\TransactionBody;
class Transaction extends TransactionBase {
private $mProtoTransaction = null;
private $mProtoTransactionBody = null;
private $errors = [];
public function __construct($base64Data) {
$transactionBin = base64_decode($base64Data);
if($transactionBin == FALSE) {
//$this->addError('base64 decode failed');
$this->addError(['data' => $base64Data, 'bin' => $transactionBin, 'msg' => 'base64 decode error']);
} else {
$this->mProtoTransaction = new \Model\Messages\Gradido\Transaction();
$this->mProtoTransaction->mergeFromString($transactionBin);
$this->mProtoTransactionBody = new \Model\Messages\Gradido\TransactionBody();
$this->mProtoTransactionBody->mergeFromString($this->mProtoTransaction->getBodyBytes());
$data = $this->mProtoTransactionBody->getData();
var_dump($data);
}
}
public function validate() {
$sigPairs = $this->mProtoTransaction->getSigMap()->getSigPair();
$bodyBytes = $this->mProtoTransaction->getBodyBytes();
// check signature(s)
foreach($sigPairs as $sigPair) {
$pubkey = $sigPair->getPubKey();
$signature = $sigPair->getEd25519();
if (!\Sodium\crypto_sign_verify_detached($signature, $bodyBytes, $pubkey)) {
$this->addError('signature for key ' . bin2hex($pubkey) . ' isn\'t valid ' );
return false;
}
}
return true;
}
public function getErrors() {
return $this->errors;
}
public function hasErrors() {
return count($this->errors) > 0;
}
private function addError($message) {
array_push($this->errors, $message);
}
>>>>>>> bcb8f1761e3eb94f89e9d2c4e70ab096e528e6c6
} }

View File

@ -1,4 +1,3 @@
<<<<<<< HEAD
<?php <?php
namespace Model\Transactions; namespace Model\Transactions;
@ -9,7 +8,7 @@ class TransactionBase {
private $errors = []; private $errors = [];
public function getErrors() { public function getErrors() {
return errors; return $this->errors;
} }
public function addError($functionName, $errorName) { public function addError($functionName, $errorName) {
@ -41,12 +40,4 @@ class TransactionBase {
return NULL; return NULL;
} }
=======
<?php
namespace Model\Transactions;
class TransactionBase {
>>>>>>> bcb8f1761e3eb94f89e9d2c4e70ab096e528e6c6
} }

View File

@ -29,15 +29,84 @@ class TransactionJsonRequestHandlerControllerTest extends TestCase
'app.TransactionTypes' 'app.TransactionTypes'
]; ];
/** public $transactions = [
* Test initialize method 'valid' => 'CgpIYWxsbyBXZWx0EgYIyfSG7gVKLwonCiCboKikqwjZfes9xuqgthFH3',
* 'notBase64' => 'CgpIYWxsbyBXZW-0EgYIyfSG7gV_LwonCiCboKikqwjZfes9xuqgthFH3'
* @return void ];
*/
public function testInitialize() /*public function setUp() {
{ parent::setUp();
$this->markTestIncomplete('Not implemented yet.');
} }
*/
public function testWrongMethod()
{
$this->configRequest([
'headers' => ['Accept' => 'application/json']
]);
$this->get('/TransactionJsonRequestHandler');
$this->assertResponseOk();
$expected = json_encode(['state' => 'error', 'msg' => 'no post']);
$this->assertEquals($expected, (string)$this->_response->getBody());
}
public function testInvalidJson()
{
$this->configRequest([
'headers' => ['Accept' => 'application/json']
]);
$this->post('/TransactionJsonRequestHandler', '{This isn\'t valid json}');
$this->assertResponseOk();
$expected = json_encode(['state' => 'error', 'msg' => 'parameter error']);
$this->assertEquals($expected, (string)$this->_response->getBody());
}
public function testNotSetTransaction()
{
$this->postAndParse(
['method' => 'putTransaction'],
['state' => 'error', 'msg' => 'parameter error']
);
}
public function testNotSetMethod()
{
$this->postAndParse(
['transaction' => $this->transactions['valid']],
['state' => 'error', 'msg' => 'parameter error']
);
}
public function testUnknownMethod()
{
//$this->post('/TransactionJsonRequestHandler', ['method' => 'putTransaction', 'transaction' => 'CgpIYWxsbyBXZWx0EgYIyfSG7gVKLwonCiCboKikqwjZfes9xuqgthFH3/cHHaWchkUhWiGhQjB23xCg2pMBELWJ7ZYK']);
$this->postAndParse(
['method' => 'foobar', 'transaction' => $this->transactions['valid']],
['state' => 'error', 'msg' => 'unknown method', 'details' => 'foobar']
);
}
public function testInvalidEncodedTransaction() {
//"msg":"error parsing transaction","details":[{"Transaction":"base64 decode error"}]
$this->postAndParse(
['method' => 'putTransaction', 'transaction' => $this->transactions['notBase64']],
['state' => 'error', 'msg' => 'error parsing transaction', 'details' => [
['Transaction' => 'base64 decode error']
]]
);
}
public function testInvalidTransaction() {
$this->postAndParse(
['method' => 'putTransaction', 'transaction' => base64_encode('Hallo Miau Welt')],
['state' => 'error', 'msg' => 'error parsing transaction', 'details' => [
['Transaction' => 'Error occurred during parsing: Unexpected wire type.']
]]
);
}
/** /**
* Test index method * Test index method
@ -46,7 +115,31 @@ class TransactionJsonRequestHandlerControllerTest extends TestCase
*/ */
public function testIndex() public function testIndex()
{ {
//$this->markTestIncomplete('Not implemented yet.');
$this->post('/TransactionJsonRequestHandler', ['method' => 'putTransaction', 'transaction' => 'CgpIYWxsbyBXZWx0EgYIyfSG7gVKLwonCiCboKikqwjZfes9xuqgthFH3/cHHaWchkUhWiGhQjB23xCg2pMBELWJ7ZYK']); $this->postAndParse(
['method' => 'putTransaction', 'transaction' => $this->transactions['valid']],
['state' => 'success']
);
}
private function postAndParse($params, $expected)
{
$this->configRequest([
'headers' => ['Accept' => 'application/json']
]);
$this->disableErrorHandlerMiddleware();
$this->post('/TransactionJsonRequestHandler', json_encode($params));
// Check that the response was a 200
$this->assertResponseOk();
$responseBodyString = (string)$this->_response->getBody();
$json = json_decode($responseBodyString);
$this->assertNotFalse($json);
$expected = json_encode($expected);
$this->assertEquals($expected, $responseBodyString);
} }
} }