From 4c1a40648466cc64e82c2ccba707a649f0f8bd7a Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 18 Oct 2023 01:03:57 +0000 Subject: [PATCH] add invalid api function tests --- tests/Controller/test.md | 284 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 282 insertions(+), 2 deletions(-) diff --git a/tests/Controller/test.md b/tests/Controller/test.md index d35f959..f50803c 100755 --- a/tests/Controller/test.md +++ b/tests/Controller/test.md @@ -1,3 +1,283 @@ -# Test Title +app = new class() extends ApplicationAbstract + { + protected string $appName = 'Api'; + }; + + $this->app->dbPool = $GLOBALS['dbpool']; + $this->app->unitId = 1; + $this->app->accountManager = new AccountManager($GLOBALS['session']); + $this->app->appSettings = new CoreSettings(); + $this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../Modules/'); + $this->app->dispatcher = new Dispatcher($this->app); + $this->app->eventManager = new EventManager($this->app->dispatcher); + $this->app->l11nManager = new L11nManager(); + $this->app->eventManager->importFromFile(__DIR__ . '/../../../Web/Api/Hooks.php'); + + $account = new Account(); + TestUtils::setMember($account, 'id', 1); + + $permission = new AccountPermission(); + $permission->unit = 1; + $permission->app = 2; + $permission->setPermission( + PermissionType::READ + | PermissionType::CREATE + | PermissionType::MODIFY + | PermissionType::DELETE + | PermissionType::PERMISSION + ); + + $account->addPermission($permission); + + $this->app->accountManager->add($account); + $this->app->router = new WebRouter(); + + $this->module = $this->app->moduleManager->get('Tasks'); + + TestUtils::setMember($this->module, 'app', $this->app); + } + + /** + * @covers Modules\Tasks\Controller\ApiController + * @group module + */ + public function testCreateTask() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('title', 'Controller Test Title'); + $request->setData('plain', 'Controller Test Description'); + $request->setData('due', (new \DateTime())->format('Y-m-d H:i:s')); + $request->setData('tags', '[{"title": "TestTitle", "color": "#f0f", "language": "en"}, {"id": 1}]'); + + if (!\is_file(__DIR__ . '/test_tmp.md')) { + \copy(__DIR__ . '/test.md', __DIR__ . '/test_tmp.md'); + } + + TestUtils::setMember($request, 'files', [ + 'file1' => [ + 'name' => 'test.md', + 'type' => MimeType::M_TXT, + 'tmp_name' => __DIR__ . '/test_tmp.md', + 'error' => \UPLOAD_ERR_OK, + 'size' => \filesize(__DIR__ . '/test_tmp.md'), + ], + ]); + + $request->setData('media', \json_encode([1])); + + $this->module->apiTaskCreate($request, $response); + + self::assertEquals('Controller Test Title', $response->getDataArray('')['response']->title); + self::assertGreaterThan(0, $response->getDataArray('')['response']->id); + } + + /** + * @covers Modules\Tasks\Controller\ApiController + * @group module + */ + public function testApiTaskGet() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('id', '1'); + + $this->module->apiTaskGet($request, $response); + + self::assertEquals(1, $response->getDataArray('')['response']->id); + } + + /** + * @covers Modules\Tasks\Controller\ApiController + * @group module + */ + public function testApiTaskSet() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('id', 1); + $request->setData('title', 'New Title'); + $request->setData('description', 'New Content here'); + + $this->module->apiTaskSet($request, $response); + $this->module->apiTaskGet($request, $response); + + self::assertEquals('New Title', $response->getDataArray('')['response']->title); + } + + /** + * @covers Modules\Tasks\Controller\ApiController + * @group module + */ + public function testCreateTaskElement() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('due', (new \DateTime())->format('Y-m-d H:i:s')); + $request->setData('priority', TaskPriority::HIGH); + $request->setData('status', TaskStatus::DONE); + $request->setData('task', 1); + $request->setData('plain', 'Controller Test'); + + if (!\is_file(__DIR__ . '/test_tmp.md')) { + \copy(__DIR__ . '/test.md', __DIR__ . '/test_tmp.md'); + } + + TestUtils::setMember($request, 'files', [ + 'file1' => [ + 'name' => 'test.md', + 'type' => MimeType::M_TXT, + 'tmp_name' => __DIR__ . '/test_tmp.md', + 'error' => \UPLOAD_ERR_OK, + 'size' => \filesize(__DIR__ . '/test_tmp.md'), + ], + ]); + + $request->setData('media', \json_encode([1])); + + $this->module->apiTaskElementCreate($request, $response); + + self::assertEquals('Controller Test', $response->getDataArray('')['response']->descriptionRaw); + self::assertGreaterThan(0, $response->getDataArray('')['response']->id); + } + + /** + * @covers Modules\Tasks\Controller\ApiController + * @group module + */ + public function testApiTaskElementGet() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('id', '1'); + + $this->module->apiTaskElementGet($request, $response); + + self::assertEquals(1, $response->getDataArray('')['response']->id); + } + + /** + * @covers Modules\Tasks\Controller\ApiController + * @group module + */ + public function testApiTaskElementSet() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('id', 1); + $request->setData('plain', 'This is a changed description'); + + $this->module->apiTaskElementSet($request, $response); + $this->module->apiTaskElementGet($request, $response); + + self::assertEquals('This is a changed description', $response->getDataArray('')['response']->descriptionRaw); + } + + /** + * @covers Modules\Tasks\Controller\ApiController + * @group module + */ + public function testInvalidTaskCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('plain', 'Controller Test Description'); + $request->setData('due', (new \DateTime())->format('Y-m-d H:i:s')); + + $this->module->apiTaskCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @covers Modules\Tasks\Controller\ApiController + * @group module + */ + public function testInvalidTaskElementCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('due', (new \DateTime())->format('Y-m-d H:i:s')); + $request->setData('priority', TaskPriority::HIGH); + $request->setData('status', TaskStatus::DONE); + $request->setData('plain', 'Controller Test'); + + $this->module->apiTaskElementCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + public function testInvalidapiTaskAttributeValueL11nCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $this->module->apiTaskAttributeValueL11nCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } +}