From 18e6796726d73d7dc90ecdd16c181493941f5487 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 15 Dec 2016 10:13:00 +0100 Subject: REST API structure using Slim framework * REST API routes are handle by Slim. * Every API controller go through ApiMiddleware which handles security. * First service implemented `/info`, for tests purpose. --- tests/api/controllers/InfoTest.php | 113 +++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 tests/api/controllers/InfoTest.php (limited to 'tests/api/controllers/InfoTest.php') diff --git a/tests/api/controllers/InfoTest.php b/tests/api/controllers/InfoTest.php new file mode 100644 index 00000000..2916eed8 --- /dev/null +++ b/tests/api/controllers/InfoTest.php @@ -0,0 +1,113 @@ +conf = new \ConfigManager('tests/utils/config/configJson.json.php'); + $this->refDB = new \ReferenceLinkDB(); + $this->refDB->write(self::$testDatastore); + + $this->container = new Container(); + $this->container['conf'] = $this->conf; + $this->container['db'] = new \LinkDB(self::$testDatastore, true, false); + + $this->controller = new Info($this->container); + } + + /** + * After every test, remove the test datastore. + */ + public function tearDown() + { + @unlink(self::$testDatastore); + } + + /** + * Test /info service. + */ + public function testGetInfo() + { + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + ]); + $request = Request::createFromEnvironment($env); + + $response = $this->controller->getInfo($request, new Response()); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode((string) $response->getBody(), true); + + $this->assertEquals(8, $data['global_counter']); + $this->assertEquals(2, $data['private_counter']); + $this->assertEquals('Shaarli', $data['settings']['title']); + $this->assertEquals('?', $data['settings']['header_link']); + $this->assertEquals('UTC', $data['settings']['timezone']); + $this->assertEquals(\ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']); + $this->assertEquals(false, $data['settings']['default_private_links']); + + $title = 'My links'; + $headerLink = 'http://shaarli.tld'; + $timezone = 'Europe/Paris'; + $enabledPlugins = array('foo', 'bar'); + $defaultPrivateLinks = true; + $this->conf->set('general.title', $title); + $this->conf->set('general.header_link', $headerLink); + $this->conf->set('general.timezone', $timezone); + $this->conf->set('general.enabled_plugins', $enabledPlugins); + $this->conf->set('privacy.default_private_links', $defaultPrivateLinks); + + $response = $this->controller->getInfo($request, new Response()); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode((string) $response->getBody(), true); + + $this->assertEquals(8, $data['global_counter']); + $this->assertEquals(2, $data['private_counter']); + $this->assertEquals($title, $data['settings']['title']); + $this->assertEquals($headerLink, $data['settings']['header_link']); + $this->assertEquals($timezone, $data['settings']['timezone']); + $this->assertEquals($enabledPlugins, $data['settings']['enabled_plugins']); + $this->assertEquals($defaultPrivateLinks, $data['settings']['default_private_links']); + } +} -- cgit v1.2.3