From 3a15bd320cc6293942e87189cad74ee4cac4b56c Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Wed, 6 Nov 2019 08:30:18 +0000 Subject: [PATCH] getting first tests to work --- config/bootstrap.php | 2 + src/Model/Transactions/Transaction.php | 85 ++----------- src/Model/Transactions/TransactionBase.php | 11 +- ...actionJsonRequestHandlerControllerTest.php | 115 ++++++++++++++++-- 4 files changed, 120 insertions(+), 93 deletions(-) diff --git a/config/bootstrap.php b/config/bootstrap.php index 1c266a499..c57f81374 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -114,7 +114,9 @@ ini_set('intl.default_locale', Configure::read('App.defaultLocale')); /* * Register application error and exception handlers. */ + $isCli = PHP_SAPI === 'cli'; + if ($isCli) { (new ConsoleErrorHandler(Configure::read('Error')))->register(); } else { diff --git a/src/Model/Transactions/Transaction.php b/src/Model/Transactions/Transaction.php index 64088f55b..1a16516cd 100644 --- a/src/Model/Transactions/Transaction.php +++ b/src/Model/Transactions/Transaction.php @@ -1,4 +1,3 @@ -<<<<<<< HEAD addError('base64 decode failed'); - $this->addError('Transaction', ['data' => $base64Data, 'bin' => $transactionBin, 'msg' => 'base64 decode error']); + $this->addError('Transaction', 'base64 decode error'); } else { $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:
'; //echo $this->mProtoTransaction->serializeToJsonString(); @@ -122,71 +130,4 @@ class Transaction extends TransactionBase { -======= -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 } \ No newline at end of file diff --git a/src/Model/Transactions/TransactionBase.php b/src/Model/Transactions/TransactionBase.php index b0df59d14..2eb0f6dc7 100644 --- a/src/Model/Transactions/TransactionBase.php +++ b/src/Model/Transactions/TransactionBase.php @@ -1,4 +1,3 @@ -<<<<<<< HEAD errors; } public function addError($functionName, $errorName) { @@ -41,12 +40,4 @@ class TransactionBase { return NULL; } -======= ->>>>>> bcb8f1761e3eb94f89e9d2c4e70ab096e528e6c6 } \ No newline at end of file diff --git a/tests/TestCase/Controller/TransactionJsonRequestHandlerControllerTest.php b/tests/TestCase/Controller/TransactionJsonRequestHandlerControllerTest.php index 147154c4e..c3e96dadd 100644 --- a/tests/TestCase/Controller/TransactionJsonRequestHandlerControllerTest.php +++ b/tests/TestCase/Controller/TransactionJsonRequestHandlerControllerTest.php @@ -28,16 +28,85 @@ class TransactionJsonRequestHandlerControllerTest extends TestCase 'app.StateBalances', 'app.TransactionTypes' ]; - - /** - * Test initialize method - * - * @return void - */ - public function testInitialize() - { - $this->markTestIncomplete('Not implemented yet.'); + + public $transactions = [ + 'valid' => 'CgpIYWxsbyBXZWx0EgYIyfSG7gVKLwonCiCboKikqwjZfes9xuqgthFH3', + 'notBase64' => 'CgpIYWxsbyBXZW-0EgYIyfSG7gV_LwonCiCboKikqwjZfes9xuqgthFH3' + ]; + + /*public function setUp() { + parent::setUp(); } +*/ + 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 @@ -46,7 +115,31 @@ class TransactionJsonRequestHandlerControllerTest extends TestCase */ 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); } }