diff options
author | Bjoern Schiessle <bjoern@schiessle.org> | 2016-08-03 13:17:54 +0300 |
---|---|---|
committer | Bjoern Schiessle <bjoern@schiessle.org> | 2016-08-04 16:49:09 +0300 |
commit | 72e7dcd58c1dc2a37968712d9e09df6e170187c9 (patch) | |
tree | 594c24e734afb5f61cfc127f76fd3aa3a02e2521 /tests | |
parent | e8ba81ee7c41c8073713e50f4286d359d2771214 (diff) |
add statistics of active users
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/Controller/ApiControllerTest.php | 15 | ||||
-rw-r--r-- | tests/lib/SessionStatisticsTest.php | 111 |
2 files changed, 125 insertions, 1 deletions
diff --git a/tests/lib/Controller/ApiControllerTest.php b/tests/lib/Controller/ApiControllerTest.php index 29e4457..7073a02 100644 --- a/tests/lib/Controller/ApiControllerTest.php +++ b/tests/lib/Controller/ApiControllerTest.php @@ -26,6 +26,7 @@ namespace OCA\ServerInfo\Tests\lib\Controller; use OCA\ServerInfo\Controller\ApiController; use OCA\ServerInfo\DatabaseStatistics; use OCA\ServerInfo\PhpStatistics; +use OCA\ServerInfo\SessionStatistics; use OCA\ServerInfo\ShareStatistics; use OCA\ServerInfo\StorageStatistics; use OCA\ServerInfo\SystemStatistics; @@ -53,6 +54,9 @@ class ApiControllerTest extends TestCase { /** @var ShareStatistics | \PHPUnit_Framework_MockObject_MockObject */ private $shareStatistics; + /** @var SessionStatistics | \PHPUnit_Framework_MockObject_MockObject */ + private $sessionStatistics; + /** @var ApiController */ private $instance; @@ -83,6 +87,11 @@ class ApiControllerTest extends TestCase { ->disableOriginalConstructor() ->getMock(); + $this->sessionStatistics = $this->getMockBuilder('OCA\ServerInfo\SessionStatistics') + ->disableOriginalConstructor() + ->getMock(); + + $this->instance = new ApiController( 'ServerInfoTest', $this->request, @@ -90,7 +99,8 @@ class ApiControllerTest extends TestCase { $this->storageStatistics, $this->phpStatistics, $this->databaseStatistics, - $this->shareStatistics + $this->shareStatistics, + $this->sessionStatistics ); } @@ -106,6 +116,8 @@ class ApiControllerTest extends TestCase { ->willReturn('databaseStatistics'); $this->shareStatistics->expects($this->once())->method('getShareStatistics') ->willReturn('shareStatistics'); + $this->sessionStatistics->expects($this->once())->method('getSessionStatistics') + ->willReturn('sessionStatistics'); $result = $this->instance->info(); $this->assertTrue($result instanceof DataResponse); @@ -119,6 +131,7 @@ class ApiControllerTest extends TestCase { $this->assertSame('unknown', $data['data']['server']['webserver']); $this->assertSame('databaseStatistics', $data['data']['server']['database']); $this->assertSame('phpStatistics', $data['data']['server']['php']); + $this->assertSame('sessionStatistics', $data['data']['activeUsers']); } } diff --git a/tests/lib/SessionStatisticsTest.php b/tests/lib/SessionStatisticsTest.php new file mode 100644 index 0000000..5bd2381 --- /dev/null +++ b/tests/lib/SessionStatisticsTest.php @@ -0,0 +1,111 @@ +<?php +/** + * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + + +namespace OCA\ServerInfo\Tests; + + +use OCA\ServerInfo\SessionStatistics; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IDBConnection; +use Test\TestCase; + +/** + * Class SessionStatisticsTest + * + * @group DB + * @package OCA\ServerInfo\Tests + */ +class SessionStatisticsTest extends TestCase { + + /** @var ITimeFactory | \PHPUnit_Framework_MockObject_MockObject */ + private $timeFactory; + + /** @var IDBConnection */ + private $connection; + + /** @var SessionStatistics */ + private $instance; + + private $table = 'authtoken'; + + private $offset5Minutes = 300; + + private $offset1Hour = 3600; + + private $offset1Day = 86400; + + private $currentTime = 100000; + + public function setUp() { + parent::setUp(); + + $this->timeFactory = $this->getMockBuilder('OCP\AppFramework\Utility\ITimeFactory') + ->disableOriginalConstructor()->getMock(); + + $this->connection = \OC::$server->getDatabaseConnection(); + + $this->instance = new SessionStatistics($this->connection, $this->timeFactory); + } + + private function addDummyValues() { + $this->addDummyValuesWithLastLogin($this->currentTime - $this->offset5Minutes +1, 10); + $this->addDummyValuesWithLastLogin($this->currentTime - $this->offset1Hour +1, 20); + $this->addDummyValuesWithLastLogin($this->currentTime - $this->offset1Day +1, 30); + } + + private function addDummyValuesWithLastLogin($lastActivity, $numOfEntries) { + for ($i = 0; $i < $numOfEntries; $i++) { + $query = $this->connection->getQueryBuilder(); + $query->insert($this->table) + ->values( + array( + 'uid' => $query->createNamedParameter('user-' . ($numOfEntries + $i % 2)), + 'login_name' => $query->createNamedParameter('user-' . ($numOfEntries + $i % 2)), + 'password' => $query->createNamedParameter('password'), + 'name' => $query->createNamedParameter('user agent'), + 'token' => $query->createNamedParameter('token-' . ($i + $numOfEntries*10)), + 'type' => $query->createNamedParameter(0), + 'last_activity' => $query->createNamedParameter($lastActivity), + 'last_check' => $query->createNamedParameter($lastActivity), + ) + ); + $query->execute(); + } + } + + public function testGetSessionStatistics() { + $this->addDummyValues(); + $this->timeFactory->expects($this->any())->method('getTime') + ->willReturn($this->currentTime); + + $result = $this->instance->getSessionStatistics(); + + $this->assertSame(3, count($result)); + $this->assertSame(2, $result['last5minutes']); + $this->assertSame(4, $result['last1hour']); + $this->assertSame(6, $result['last24hours']); + + } + + + +} |