diff -r dd6b3adde73b -r 338bcc78d431 server/src/tests/Libraries/Handle/HandleClientTest.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/server/src/tests/Libraries/Handle/HandleClientTest.php Fri Apr 22 11:20:17 2016 +0200 @@ -0,0 +1,414 @@ +markTestSkipped('Not ready yet'); + + $this->certTmpPath = tempnam("/tmp", "CERT_TEST"); + file_put_contents($this->certTmpPath, HandleClientTest::CERT_PEM); + + } + + public function tearDown() { + unlink($this->certTmpPath); + + parent::tearDown(); + } + + private function initClient($respArray) { + + $this->history = []; + + $mock = new MockHandler($respArray); + $handler = HandlerStack::create($mock); + + $history = Middleware::history($this->history); + + $handler->push($history); + + $this->httpClient = new Client(['handler' => $handler]); + + } + + + /** + * Test Pagination with DSA key. + * + * @return void + */ + public function testPaginateDSA() + { + $responses = [ + new Response(201, + [ 'Location' => '/api/sessions/this', + 'Content-Type' => 'application/json;charset=UTF-8', + 'Vary' => "Accept-Encoding" + ], + "{\"sessionId\":\"fo79koatgsdyfjkx4c6p3l0c\",\"nonce\":\"10hYzYGE5vf0a1F9eWfk6g==\"}" + ), + new Response(200, + [ 'Location' => '/api/sessions/this', + 'Content-Type' => 'application/json;charset=UTF-8' + ], + "{\"sessionId\":\"fo79koatgsdyfjkx4c6p3l0c\",\"nonce\":\"10hYzYGE5vf0a1F9eWfk6g==\",\"authenticated\":true,\"id\":\"300:11280.101/CORPUS_ADMIN_DSA\"}" + ), + new Response(200, + [ 'Location' => '/api/sessions/this', + 'Content-Type' => 'application/json;charset=UTF-8' + ], + "{\"responseCode\":1,\"prefix\":\"11280.101\",\"totalCount\":\"2\",\"page\":0,\"pageSize\":15,\"handles\":[\"11280.101/CORPUS_ADMIN\",\"11280.101/CORPUS_ADMIN_DSA\"]}" + ) + + ]; + $this->initClient($responses); + + $handleClient = new HandleClient(HandleClientTest::DSA_PEM, null, "300:11281.100/CORPUS_ADMIN_DSA", "172.16.1.6", 8000, $this->httpClient); + + $pagination = $handleClient->paginateAll('11280.101'); + + $this->assertNotNull($pagination); + + $this->assertInstanceOf("Illuminate\Pagination\LengthAwarePaginator", $pagination, "Must be a LengthAwarePaginator"); + + $this->assertEquals(2, $pagination->count(), "Must have only one handle"); + $this->assertEquals(2, $pagination->total(), "Total is one"); + + $handles = iterator_to_array($pagination); + $this->assertEquals(['11280.101/CORPUS_ADMIN','11280.101/CORPUS_ADMIN_DSA'], $handles, "Same handles"); + + $this->assertCount(3, $this->history, "Must have 3 transactions"); + + } + + + /** + * Test Pagination with RSA key. + * + * @return void + */ + public function testPaginateRSA() + { + $responses = [ + new Response(201, + [ 'Location' => '/api/sessions/this', + 'Content-Type' => 'application/json;charset=UTF-8', + 'Vary' => "Accept-Encoding" + ], + "{\"sessionId\":\"fo79koatgsdyfjkx4c6p3l0c\",\"nonce\":\"10hYzYGE5vf0a1F9eWfk6g==\"}" + ), + new Response(200, + [ 'Location' => '/api/sessions/this', + 'Content-Type' => 'application/json;charset=UTF-8' + ], + "{\"sessionId\":\"fo79koatgsdyfjkx4c6p3l0c\",\"nonce\":\"10hYzYGE5vf0a1F9eWfk6g==\",\"authenticated\":true,\"id\":\"300:11280.101/CORPUS_ADMIN\"}" + ), + new Response(200, + [ 'Location' => '/api/sessions/this', + 'Content-Type' => 'application/json;charset=UTF-8' + ], + "{\"responseCode\":1,\"prefix\":\"11280.101\",\"totalCount\":\"2\",\"page\":0,\"pageSize\":15,\"handles\":[\"11280.101/CORPUS_ADMIN\",\"11280.101/CORPUS_ADMIN_DSA\"]}" + ) + + ]; + $this->initClient($responses); + + $handleClient = new HandleClient(HandleClientTest::RSA_PEM, null, "300:11280.101/CORPUS_ADMIN", "172.16.1.6", 8000, $this->httpClient); + + $pagination = $handleClient->paginateAll('11280.101'); + + $this->assertNotNull($pagination); + + $this->assertInstanceOf("Illuminate\Pagination\LengthAwarePaginator", $pagination, "Must be a LengthAwarePaginator"); + + $this->assertEquals(2, $pagination->count(), "Must have only 2 handle"); + $this->assertEquals(2, $pagination->total(), "Total is 2"); + + $handles = iterator_to_array($pagination); + $this->assertEquals(['11280.101/CORPUS_ADMIN', '11280.101/CORPUS_ADMIN_DSA'], $handles, "Same handles"); + + $this->assertCount(3, $this->history, "Must have 3 transactions"); + + } + + /** + * Test Pagination with Client Certificate. + * + * @return void + */ + public function testPaginateCert() + { + + $responses = [ + new Response(200, + [ 'Location' => '/api/sessions/this', + 'Content-Type' => 'application/json;charset=UTF-8' + ], + "{\"responseCode\":1,\"prefix\":\"11280.101\",\"totalCount\":\"2\",\"page\":0,\"pageSize\":15,\"handles\":[\"11280.101/CORPUS_ADMIN\",\"11280.101/CORPUS_ADMIN_DSA\"]}" + ) + ]; + + + $this->initClient($responses); + + $handleClient = new HandleClient($this->certTmpPath, null, "300:11280.101/CORPUS_ADMIN", "172.16.1.6", 8000, $this->httpClient); + + $pagination = $handleClient->paginateAll('11280.101'); + + $this->assertNotNull($pagination); + + $this->assertInstanceOf("Illuminate\Pagination\LengthAwarePaginator", $pagination, "Must be a LengthAwarePaginator"); + + $this->assertEquals(2, $pagination->count(), "Must have only 2 handle"); + $this->assertEquals(2, $pagination->total(), "Total is 2"); + $this->assertFalse($pagination->hasPages(), "Do not have a page"); + + $handles = iterator_to_array($pagination); + $this->assertEquals(['11280.101/CORPUS_ADMIN', '11280.101/CORPUS_ADMIN_DSA'], $handles, "Same handles"); + + $this->assertCount(1, $this->history, "Must have 1 transactions"); + + } + + + /** + * Test Pagination with Client Certificate. + * + * @return void + */ + public function testPaginateFirstPageCert() + { + + $responses = [ + new Response(200, + [ 'Location' => '/api/sessions/this', + 'Content-Type' => 'application/json;charset=UTF-8' + ], + "{\"responseCode\":1,\"prefix\":\"11280.101\",\"totalCount\":\"12\",\"page\":0,\"pageSize\":5,\"handles\":[\"11280.101/TEST1\",\"11280.101/TEST2\",\"11280.101/TEST3\",\"11280.101/TEST4\",\"11280.101/TEST5\"]}" + ) + ]; + + + $this->initClient($responses); + + $handleClient = new HandleClient($this->certTmpPath, null, "300:11280.101/CORPUS_ADMIN", "172.16.1.6", 8000, $this->httpClient); + + $pagination = $handleClient->paginateAll('11280.101', 5); + + $this->assertNotNull($pagination); + + $this->assertInstanceOf("Illuminate\Pagination\LengthAwarePaginator", $pagination, "Must be a LengthAwarePaginator"); + + $this->assertEquals(5, $pagination->count(), "Must have only 5 handle"); + $this->assertEquals(5, $pagination->perPage(), "Per page is 5"); + $this->assertEquals(12, $pagination->total(), "Total is 12"); + $this->assertEquals(1, $pagination->currentPage(), "Current page is 1"); + $this->assertEquals(3, $pagination->lastPage(), "Last page is 3"); + $this->assertTrue($pagination->hasMorePages(), "Must have more page"); + $this->assertTrue($pagination->hasPages(), "Have at least page"); + $this->assertEquals(1, $pagination->firstItem(), "First item is 1"); + $this->assertEquals(5, $pagination->lastItem(), "First item is 4"); + + $handles = iterator_to_array($pagination); + $this->assertEquals(['11280.101/TEST1', '11280.101/TEST2', '11280.101/TEST3', '11280.101/TEST4', '11280.101/TEST5'], $handles, "Same handles"); + + + } + + /** + * Test Pagination with Client Certificate. + * + * @return void + */ + public function testPaginateNextPageCert() + { + + $responses = [ + new Response(200, + [ 'Location' => '/api/sessions/this', + 'Content-Type' => 'application/json;charset=UTF-8' + ], + "{\"responseCode\":1,\"prefix\":\"11280.101\",\"totalCount\":\"12\",\"page\":1,\"pageSize\":5,\"handles\":[\"11280.101/TEST6\",\"11280.101/TEST7\",\"11280.101/TEST8\",\"11280.101/TEST9\",\"11280.101/TEST10\"]}" + ) + ]; + + + $this->initClient($responses); + + $handleClient = new HandleClient($this->certTmpPath, null, "300:11280.101/CORPUS_ADMIN", "172.16.1.6", 8000, $this->httpClient); + + $pagination = $handleClient->paginateAll('11280.101', 5, 'page', 2); + + $this->assertNotNull($pagination); + + $this->assertInstanceOf("Illuminate\Pagination\LengthAwarePaginator", $pagination, "Must be a LengthAwarePaginator"); + + $this->assertEquals(5, $pagination->count(), "Must have only 5 handle"); + $this->assertEquals(5, $pagination->perPage(), "Per page is 5"); + $this->assertEquals(12, $pagination->total(), "Total is 12"); + $this->assertEquals(2, $pagination->currentPage(), "Current page is 2"); + $this->assertEquals(3, $pagination->lastPage(), "Last page is 3"); + $this->assertTrue($pagination->hasPages(), "Have at least page"); + $this->assertTrue($pagination->hasMorePages(), "Must have more page"); + $this->assertEquals(6, $pagination->firstItem(), "First item is 1"); + $this->assertEquals(10, $pagination->lastItem(), "First item is 4"); + + $handles = iterator_to_array($pagination); + $this->assertEquals(['11280.101/TEST6', '11280.101/TEST7', '11280.101/TEST8', '11280.101/TEST9', '11280.101/TEST10'], $handles, "Same handles"); + + + } + + /** + * Test Pagination with Client Certificate. + * + * @return void + */ + public function testPaginateLastPageCert() + { + + $responses = [ + new Response(200, + [ 'Location' => '/api/sessions/this', + 'Content-Type' => 'application/json;charset=UTF-8' + ], + "{\"responseCode\":1,\"prefix\":\"11280.101\",\"totalCount\":\"12\",\"page\":2,\"pageSize\":5,\"handles\":[\"11280.101/TEST11\",\"11280.101/TEST12\"]}" + ) + ]; + + + $this->initClient($responses); + + $handleClient = new HandleClient($this->certTmpPath, null, "300:11280.101/CORPUS_ADMIN", "172.16.1.6", 8000, $this->httpClient); + + $pagination = $handleClient->paginateAll('11280.101', 5, 'page', 3); + + $this->assertNotNull($pagination); + + $this->assertInstanceOf("Illuminate\Pagination\LengthAwarePaginator", $pagination, "Must be a LengthAwarePaginator"); + + $this->assertEquals(2, $pagination->count(), "Must have only 2 handle"); + $this->assertEquals(5, $pagination->perPage(), "Per page is 5"); + $this->assertEquals(12, $pagination->total(), "Total is 12"); + $this->assertEquals(3, $pagination->currentPage(), "Current page is 3"); + $this->assertEquals(3, $pagination->lastPage(), "Last page is 3"); + $this->assertFalse($pagination->hasMorePages(), "Must have more page"); + $this->assertTrue($pagination->hasPages(), "Have at least page"); + $this->assertEquals(11, $pagination->firstItem(), "First item is 1"); + $this->assertEquals(12, $pagination->lastItem(), "First item is 4"); + + $handles = iterator_to_array($pagination); + $this->assertEquals(['11280.101/TEST11', '11280.101/TEST12'], $handles, "Same handles"); + + + } + + +}