From 7c06ed1e0bcfb19f0097c3dfb8f6ed3a71f884cf Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Tue, 21 Jan 2020 20:29:48 -0300 Subject: [PATCH] Add test for `userLogin()` --- tests/ZabbixApiTest.php | 109 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/tests/ZabbixApiTest.php b/tests/ZabbixApiTest.php index 31e614a..be77a45 100644 --- a/tests/ZabbixApiTest.php +++ b/tests/ZabbixApiTest.php @@ -55,6 +55,83 @@ public function testZabbixApiClass() $this->assertGreaterThanOrEqual(360, count($ro->getMethods(\ReflectionMethod::IS_PUBLIC))); } + public function testUserLoginOnConsecutiveCalls() + { + $user = 'zabbix'; + $pass = 'very_secret'; + $authToken = '4u7ht0k3n'; + $cacheDir = __DIR__.'/.token_cache'; + + $this->createTokenCacheDir($cacheDir); + + $zabbix = $this->getMockBuilder('ZabbixApi\ZabbixApi') + ->disableOriginalConstructor() + ->disableOriginalClone() + ->disableArgumentCloning() + ->setMethods(array('request', 'userGet')) + ->getMock(); + + // `userGet()` must not be called if the argument 3 (`$tokenCacheDir`) is passed with value `null`. + $zabbix + ->expects($this->never()) + ->method('userGet') + ->with(array('countOutput' => true)); + + // `request()` must be called in order to retrieve the token. + $zabbix + ->expects($this->once()) + ->method('request') + ->with('user.login') + ->willReturn($authToken); + + $this->assertSame($authToken, $zabbix->userLogin(array('user' => $user, 'password' => $pass), '', null)); + + $zabbix = $this->getMockBuilder('ZabbixApi\ZabbixApi') + ->disableOriginalConstructor() + ->disableOriginalClone() + ->disableArgumentCloning() + ->setMethods(array('request', 'userGet')) + ->getMock(); + + // `userGet()` must not be called since the token cache file is not created yet. + $zabbix + ->expects($this->never()) + ->method('userGet') + ->with(array('countOutput' => true)); + + // `request()` must be called in order to retrieve the token. + $zabbix + ->expects($this->once()) + ->method('request') + ->with('user.login') + ->willReturn($authToken); + + $this->assertSame($authToken, $zabbix->userLogin(array('user' => $user, 'password' => $pass), '', $cacheDir)); + + $zabbix = $this->getMockBuilder('ZabbixApi\ZabbixApi') + ->disableOriginalConstructor() + ->disableOriginalClone() + ->disableArgumentCloning() + ->setMethods(array('request', 'userGet')) + ->getMock(); + + // `userGet()` must be called since the token at token cache file must be validated. + $zabbix + ->expects($this->once()) + ->method('userGet') + ->with(array('countOutput' => true)); + + // `request()` must not be called since the token was already retrieved from the token cache file. + $zabbix + ->expects($this->never()) + ->method('request') + ->with('user.login'); + + $this->assertSame($authToken, $zabbix->userLogin(array('user' => $user, 'password' => $pass), '', $cacheDir)); + + $this->removeTokenCacheDir($cacheDir); + } + public function testZabbixApiConnectionNotTriggered() { $zabbix = new ZabbixApi('http://localhost/json_rpc.php'); @@ -72,4 +149,36 @@ public function testZabbixApiConnectionError() { new ZabbixApi('http://not.found.tld/json_rpc.php', 'zabbix', 'very_secret_pass'); } + + /** + * @param string $cacheDir + */ + private function createTokenCacheDir($cacheDir) + { + if (is_dir($cacheDir)) { + return; + } + + mkdir($cacheDir); + } + + /** + * @param string $cacheDir + */ + private function removeTokenCacheDir($cacheDir) + { + if (!is_dir($cacheDir)) { + return; + } + + // Remove the token cache directory. + foreach (glob($cacheDir.'/{,.}*', GLOB_BRACE) as $file) { + if (is_dir($file)) { + continue; + } + unlink($file); + } + + rmdir($cacheDir); + } }