From 88d8a547cf0f4ea25382dff5da8c4a4f839e4100 Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Fri, 19 Feb 2021 14:57:19 +0000 Subject: [PATCH] successfully test state-balance/ajaxGetBalance --- src/Controller/AppController.php | 1 + src/Controller/StateBalancesController.php | 5 +- .../Controller/ElopageBuysControllerTest.php | 80 --------------- .../JsonRequestHandlerControllerTest.php | 16 +-- .../StateBalancesControllerTest.php | 40 +++++++- .../Controller/UsersControllerTest.php | 78 --------------- .../Model/Table/ElopageBuysTableTest.php | 97 ------------------- tests/TestCase/Model/Table/UsersTableTest.php | 95 ------------------ tests/testApi.sh | 4 + 9 files changed, 56 insertions(+), 360 deletions(-) delete mode 100644 tests/TestCase/Controller/ElopageBuysControllerTest.php delete mode 100644 tests/TestCase/Controller/UsersControllerTest.php delete mode 100644 tests/TestCase/Model/Table/ElopageBuysTableTest.php delete mode 100644 tests/TestCase/Model/Table/UsersTableTest.php create mode 100755 tests/testApi.sh diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index 9f3526979..2c59a17e0 100644 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -158,6 +158,7 @@ class AppController extends Controller if ($session_id != 0) { $userStored = $session->read('StateUser'); + $transactionPendings = $session->read('Transaction.pending'); $transactionExecutings = $session->read('Transaction.executing'); diff --git a/src/Controller/StateBalancesController.php b/src/Controller/StateBalancesController.php index 65a99496a..5164dce4b 100644 --- a/src/Controller/StateBalancesController.php +++ b/src/Controller/StateBalancesController.php @@ -21,7 +21,7 @@ class StateBalancesController extends AppController { parent::initialize(); //$this->Auth->allow(['add', 'edit']); - $this->Auth->allow(['overview', 'overviewGdt']); + $this->Auth->allow(['overview', 'overviewGdt', 'ajaxGetBalance']); $this->loadComponent('JsonRequestClient'); } /** @@ -189,7 +189,8 @@ class StateBalancesController extends AppController return $this->returnJson(['state' => 'error', 'msg' => 'session not found']); } $user = $session->read('StateUser'); - return $this->returnJson(['state' => 'success', 'balance' => $session->read('StateUser.balance')]); + //var_dump($user); + return $this->returnJson(['state' => 'success', 'balance' => $user['balance']]); } diff --git a/tests/TestCase/Controller/ElopageBuysControllerTest.php b/tests/TestCase/Controller/ElopageBuysControllerTest.php deleted file mode 100644 index 38783976e..000000000 --- a/tests/TestCase/Controller/ElopageBuysControllerTest.php +++ /dev/null @@ -1,80 +0,0 @@ -markTestIncomplete('Not implemented yet.'); - } - - /** - * Test view method - * - * @return void - */ - public function testView() - { - $this->markTestIncomplete('Not implemented yet.'); - } - - /** - * Test add method - * - * @return void - */ - public function testAdd() - { - $this->markTestIncomplete('Not implemented yet.'); - } - - /** - * Test edit method - * - * @return void - */ - public function testEdit() - { - $this->markTestIncomplete('Not implemented yet.'); - } - - /** - * Test delete method - * - * @return void - */ - public function testDelete() - { - $this->markTestIncomplete('Not implemented yet.'); - } -} diff --git a/tests/TestCase/Controller/JsonRequestHandlerControllerTest.php b/tests/TestCase/Controller/JsonRequestHandlerControllerTest.php index d51062563..d8a241d7d 100644 --- a/tests/TestCase/Controller/JsonRequestHandlerControllerTest.php +++ b/tests/TestCase/Controller/JsonRequestHandlerControllerTest.php @@ -1,7 +1,6 @@ postAndParse( + $this->postAndParse( ['method' => 'putTransaction'], ['state' => 'error', 'msg' => 'parameter error'] ); } public function testNotSetMethod() { - $this->postAndParse( + $this->postAndParse( ['transaction' => $this->transactions['validCreation']], ['state' => 'error', 'msg' => 'parameter error'] ); @@ -86,7 +85,7 @@ class JsonRequestHandlerControllerTest extends TestCase public function testUnknownMethod() { //$this->post('/TransactionJsonRequestHandler', ['method' => 'putTransaction', 'transaction' => 'CgpIYWxsbyBXZWx0EgYIyfSG7gVKLwonCiCboKikqwjZfes9xuqgthFH3/cHHaWchkUhWiGhQjB23xCg2pMBELWJ7ZYK']); - $this->postAndParse( + $this->postAndParse( ['method' => 'foobar', 'transaction' => $this->transactions['validCreation']], ['state' => 'error', 'msg' => 'unknown method', 'details' => 'foobar'] ); @@ -160,9 +159,14 @@ class JsonRequestHandlerControllerTest extends TestCase private function postAndParse($params, $expected) { - + //$this->enableCsrfToken(); + //$this->enableSecurityToken(); + + $token = 'my-csrf-token'; + $this->cookie('csrfToken', $token); + $this->configRequest([ - 'headers' => ['Accept' => 'application/json'] + 'headers' => ['Accept' => 'application/json', 'X-CSRF-Token' => $token] ]); $this->disableErrorHandlerMiddleware(); diff --git a/tests/TestCase/Controller/StateBalancesControllerTest.php b/tests/TestCase/Controller/StateBalancesControllerTest.php index 443d481f5..cfa59d80b 100644 --- a/tests/TestCase/Controller/StateBalancesControllerTest.php +++ b/tests/TestCase/Controller/StateBalancesControllerTest.php @@ -1,7 +1,6 @@ markTestIncomplete('Not implemented yet.'); + $session_id = rand(); + $balance = rand(); + $this->session([ + 'session_id' => $session_id, + 'Transaction' => ['pending' => 0, 'executing' => 0], + 'StateUser' => [ + 'id' => 2, // 1 don't work, I don't know why + 'email_checked' => 1, + 'balance' => $balance + ] + ]); + //echo "balance: $balance"; + $this->getAndParse('/state-balances/ajaxGetBalance/' . $session_id, + ['state' => 'success', 'balance' => $balance] + ); } /** @@ -139,4 +152,27 @@ class StateBalancesControllerTest extends TestCase { $this->markTestIncomplete('Not implemented yet.'); } + + + private function getAndParse($path, $expected) + { + $this->configRequest([ + 'headers' => ['Accept' => 'application/json'] + ]); + + $this->disableErrorHandlerMiddleware(); + $this->get($path); + + // Check that the response was a 200 + $this->assertResponseOk(); + + $responseBodyString = (string)$this->_response->getBody(); + $json = json_decode($responseBodyString); + $this->assertNotFalse($json); + + if(is_array($expected)) { + $expected = json_encode($expected); + } + $this->assertEquals($expected, $responseBodyString); + } } diff --git a/tests/TestCase/Controller/UsersControllerTest.php b/tests/TestCase/Controller/UsersControllerTest.php deleted file mode 100644 index db9f98307..000000000 --- a/tests/TestCase/Controller/UsersControllerTest.php +++ /dev/null @@ -1,78 +0,0 @@ -markTestIncomplete('Not implemented yet.'); - } - - /** - * Test view method - * - * @return void - */ - public function testView() - { - $this->markTestIncomplete('Not implemented yet.'); - } - - /** - * Test add method - * - * @return void - */ - public function testAdd() - { - $this->markTestIncomplete('Not implemented yet.'); - } - - /** - * Test edit method - * - * @return void - */ - public function testEdit() - { - $this->markTestIncomplete('Not implemented yet.'); - } - - /** - * Test delete method - * - * @return void - */ - public function testDelete() - { - $this->markTestIncomplete('Not implemented yet.'); - } -} diff --git a/tests/TestCase/Model/Table/ElopageBuysTableTest.php b/tests/TestCase/Model/Table/ElopageBuysTableTest.php deleted file mode 100644 index 35aecc813..000000000 --- a/tests/TestCase/Model/Table/ElopageBuysTableTest.php +++ /dev/null @@ -1,97 +0,0 @@ -exists('ElopageBuys') ? [] : ['className' => ElopageBuysTable::class]; - $this->ElopageBuys = TableRegistry::getTableLocator()->get('ElopageBuys', $config); - } - - /** - * tearDown method - * - * @return void - */ - public function tearDown() - { - unset($this->ElopageBuys); - - parent::tearDown(); - } - - /** - * Test initialize method - * - * @return void - */ - public function testInitialize() - { - $this->markTestIncomplete('Not implemented yet.'); - } - - /** - * Test validationDefault method - * - * @return void - */ - public function testValidationDefault() - { - $this->markTestIncomplete('Not implemented yet.'); - } - - /** - * Test buildRules method - * - * @return void - */ - public function testBuildRules() - { - $this->markTestIncomplete('Not implemented yet.'); - } - - /** - * Test defaultConnectionName method - * - * @return void - */ - public function testDefaultConnectionName() - { - $this->markTestIncomplete('Not implemented yet.'); - } -} diff --git a/tests/TestCase/Model/Table/UsersTableTest.php b/tests/TestCase/Model/Table/UsersTableTest.php deleted file mode 100644 index 7b730de54..000000000 --- a/tests/TestCase/Model/Table/UsersTableTest.php +++ /dev/null @@ -1,95 +0,0 @@ -exists('Users') ? [] : ['className' => UsersTable::class]; - $this->Users = TableRegistry::getTableLocator()->get('Users', $config); - } - - /** - * tearDown method - * - * @return void - */ - public function tearDown() - { - unset($this->Users); - - parent::tearDown(); - } - - /** - * Test initialize method - * - * @return void - */ - public function testInitialize() - { - $this->markTestIncomplete('Not implemented yet.'); - } - - /** - * Test validationDefault method - * - * @return void - */ - public function testValidationDefault() - { - $this->markTestIncomplete('Not implemented yet.'); - } - - /** - * Test buildRules method - * - * @return void - */ - public function testBuildRules() - { - $this->markTestIncomplete('Not implemented yet.'); - } - - /** - * Test defaultConnectionName method - * - * @return void - */ - public function testDefaultConnectionName() - { - $this->markTestIncomplete('Not implemented yet.'); - } -} diff --git a/tests/testApi.sh b/tests/testApi.sh new file mode 100755 index 000000000..9476dc7da --- /dev/null +++ b/tests/testApi.sh @@ -0,0 +1,4 @@ +#!/bin/bash +cd .. +sudo -u www-data ./vendor/bin/phpunit --testdox tests/TestCase/Controller/StateBalancesControllerTest +cd tests