server/src/tests/Libraries/Handle/HandleClientIntegrationTest.php
changeset 153 338bcc78d431
child 405 f239c8c5bb94
equal deleted inserted replaced
152:dd6b3adde73b 153:338bcc78d431
       
     1 <?php
       
     2 
       
     3 use Illuminate\Foundation\Testing\WithoutMiddleware;
       
     4 use Illuminate\Foundation\Testing\DatabaseMigrations;
       
     5 use Illuminate\Foundation\Testing\DatabaseTransactions;
       
     6 
       
     7 use GuzzleHttp\Client;
       
     8 use GuzzleHttp\Middleware;
       
     9 use GuzzleHttp\HandlerStack;
       
    10 
       
    11 
       
    12 use CorpusParole\Libraries\Handle\HandleClient;
       
    13 
       
    14 class HandleClientIntegrationTest extends TestCase
       
    15 {
       
    16 
       
    17     public function setUp() {
       
    18         parent::setUp();
       
    19 
       
    20         $this->handleHost = env('HANDLE_HOST', null);
       
    21         $this->handlePort = env('HANDLE_PORT', 8000);
       
    22 
       
    23         if(empty($this->handleHost)) {
       
    24             $this->markTestSkipped('Handle host empty');
       
    25         }
       
    26 
       
    27         $this->testPrefix = env('HANDLE_TEST_PREFIX', null);
       
    28         if(empty($this->testPrefix)) {
       
    29             $this->markTestSkipped('Handle test prefix not defined');
       
    30         }
       
    31 
       
    32         $certPem = env('HANDLE_TEST_CERT', null);
       
    33         $this->certPemPassword = env('HANDLE_TEST_CERT_PASSWORD', null);
       
    34         $this->certAdminHandle = env('HANDLE_TEST_CERT_ADMIN_HANDLE', null);
       
    35         if(empty($certPem) || empty($this->certAdminHandle)) {
       
    36             $this->markTestSkipped('Handle test certificate or user is empty');
       
    37         }
       
    38 
       
    39         $dsaPem = env('HANDLE_TEST_DSA_KEY', null);
       
    40         $this->dsaPemPassword = env('HANDLE_TEST_DSA_PASSWORD', null);
       
    41         $this->dsaAdminHandle = env('HANDLE_TEST_DSA_ADMIN_HANDLE', null);
       
    42         if(empty($dsaPem) || empty($this->dsaAdminHandle)) {
       
    43             $this->markTestSkipped('Handle dsa key or user not set');
       
    44         }
       
    45         $this->dsaAdminRawHandle = str_replace("300:", "", $this->dsaAdminHandle);
       
    46 
       
    47         $rsaPem = env('HANDLE_TEST_RSA_KEY', null);
       
    48         $this->rsaPemPassword = env('HANDLE_TEST_RSA_PASSWORD', null);
       
    49         $this->rsaAdminHandle = env('HANDLE_TEST_RSA_ADMIN_HANDLE', null);
       
    50         if(empty($rsaPem) || empty($this->rsaAdminHandle)) {
       
    51             $this->markTestSkipped('Handle rsa key or user not set');
       
    52         }
       
    53         $this->rsaAdminRawHandle = str_replace("300:", "", $this->rsaAdminHandle);
       
    54 
       
    55         $this->filesToDelete = [];
       
    56 
       
    57         if(is_file($certPem)) {
       
    58             $this->certTmpPath = $certPem;
       
    59         } else {
       
    60             $this->certTmpPath = tempnam("/tmp", "CERT_TEST");
       
    61             array_push($this->filesToDelete,$this->certTmpPath);
       
    62             file_put_contents($this->certTmpPath, $certPem);
       
    63         }
       
    64 
       
    65         if(is_file($dsaPem)) {
       
    66             $this->dsaTmpPath = $dsaPem;
       
    67         } else {
       
    68             $this->dsaTmpPath = tempnam("/tmp", "DSA_TEST");
       
    69             array_push($this->filesToDelete,$this->dsaTmpPath);
       
    70             file_put_contents($this->dsaTmpPath, $dsaPem);
       
    71         }
       
    72 
       
    73         if(is_file($rsaPem)) {
       
    74             $this->rsaTmpPath = $rsaPem;
       
    75         } else {
       
    76             $this->rsaTmpPath = tempnam("/tmp", "RSA_TEST");
       
    77             array_push($this->filesToDelete,$this->rsaTmpPath);
       
    78             file_put_contents($this->rsaTmpPath, $rsaPem);
       
    79         }
       
    80 
       
    81         // Create a middleware that echoes parts of the request.
       
    82         // $tapMiddleware = Middleware::tap(function ($request) {
       
    83         //     echo "\n+++++++++++++++++\nREQ HEADERS : ".print_r($request->getHeaders(), true)."\n+++++++++++++++++++\n";
       
    84         //     echo "\n------------------\nREQ BODY : ".$request->getBody()."\n-------------------\n";
       
    85         // });
       
    86 
       
    87         // $stack = HandlerStack::create();
       
    88         // $stack->push(Middleware::mapResponse(function ($response) {
       
    89         //     echo "\n+++++++++++++++++\nRESP HEADERS : ".print_r($response->getHeaders(), true)."\n+++++++++++++++++++\n";
       
    90         //     echo "\n------------------\nRESP BODY : ".$response->getBody()."\n-------------------\n";
       
    91         //     return $response;
       
    92         // }));
       
    93 
       
    94         // $this->httpClient = new Client(['debug' => true, 'handler' => $tapMiddleware($stack)]);
       
    95         $this->httpClient = new Client();
       
    96 
       
    97     }
       
    98 
       
    99     public function tearDown() {
       
   100         foreach($this->filesToDelete as $ftd) {
       
   101             unlink($ftd);
       
   102         }
       
   103 
       
   104         //$this->httpClient->delete("repositories/$this->sesameRepository");
       
   105         parent::tearDown();
       
   106     }
       
   107 
       
   108 
       
   109     /**
       
   110      * Test Pagination with DSA key.
       
   111      *
       
   112      * @return void
       
   113      */
       
   114     public function testPaginateDSA()
       
   115     {
       
   116         $handleClient = new HandleClient('file://'.$this->dsaTmpPath, $this->dsaPemPassword, $this->dsaAdminHandle, $this->handleHost, $this->handlePort, $this->httpClient);
       
   117 
       
   118         $pagination = $handleClient->paginateAll($this->testPrefix);
       
   119 
       
   120         $this->assertNotNull($pagination);
       
   121 
       
   122         $this->assertInstanceOf("Illuminate\Pagination\LengthAwarePaginator", $pagination, "Must be a LengthAwarePaginator");
       
   123 
       
   124         $this->assertGreaterThanOrEqual(1, $pagination->count(), "Must have at least 1 handle");
       
   125         $this->assertGreaterThanOrEqual(1, $pagination->total(), "Total is at least 1");
       
   126 
       
   127     }
       
   128 
       
   129 
       
   130     /**
       
   131      * Test Pagination with RSA key.
       
   132      *
       
   133      * @return void
       
   134      */
       
   135     public function testPaginateRSA()
       
   136     {
       
   137         $handleClient = new HandleClient('file://'.$this->rsaTmpPath, $this->rsaPemPassword, $this->rsaAdminHandle, $this->handleHost, $this->handlePort, $this->httpClient);
       
   138 
       
   139         $pagination = $handleClient->paginateAll($this->testPrefix);
       
   140 
       
   141         $this->assertNotNull($pagination);
       
   142 
       
   143         $this->assertInstanceOf("Illuminate\Pagination\LengthAwarePaginator", $pagination, "Must be a LengthAwarePaginator");
       
   144 
       
   145         $this->assertGreaterThanOrEqual(1, $pagination->count(), "Must have at least 1 handle");
       
   146         $this->assertGreaterThanOrEqual(1, $pagination->total(), "Total is at least 1");
       
   147 
       
   148     }
       
   149 
       
   150     /**
       
   151      * Test Pagination with Client Certificate.
       
   152      *
       
   153      * @return void
       
   154      */
       
   155     public function testPaginateCert()
       
   156     {
       
   157         $handleClient = new HandleClient($this->certTmpPath, $this->certPemPassword, $this->certAdminHandle, $this->handleHost, $this->handlePort, $this->httpClient);
       
   158 
       
   159         $pagination = $handleClient->paginateAll($this->testPrefix);
       
   160 
       
   161         $this->assertNotNull($pagination);
       
   162 
       
   163         $this->assertInstanceOf("Illuminate\Pagination\LengthAwarePaginator", $pagination, "Must be a LengthAwarePaginator");
       
   164 
       
   165         $this->assertGreaterThanOrEqual(1, $pagination->count(), "Must have at least 1 handle");
       
   166         $this->assertGreaterThanOrEqual(1, $pagination->total(), "Total is at least 1");
       
   167 
       
   168     }
       
   169 
       
   170 
       
   171     /**
       
   172      * Test Handle creation
       
   173      */
       
   174     public function testCreateHandleUrl() {
       
   175 
       
   176         $handleClient = new HandleClient('file://'.$this->rsaTmpPath, $this->rsaPemPassword, $this->rsaAdminHandle, $this->handleHost, $this->handlePort, $this->httpClient);
       
   177 
       
   178         $pagination = $handleClient->paginateAll($this->testPrefix);
       
   179         $totalPrev = $pagination->total();
       
   180 
       
   181         $handle = strtoupper(uniqid("$this->testPrefix/TEST_HANDLE_CLIENT_"));
       
   182 
       
   183         //$handle = "$this->testPrefix/TEST_HANDLE_CLIENT_";
       
   184 
       
   185         $handleClient->createHandleUrlRecord($handle, "http://www.example.com");
       
   186 
       
   187         $pagination = $handleClient->paginateAll($this->testPrefix);
       
   188 
       
   189         $this->assertEquals($totalPrev + 1, $pagination->total(), "Must have one more handle");
       
   190 
       
   191         $this->assertContains($handle, $pagination);
       
   192 
       
   193         $handleClient->deleteHandle($handle);
       
   194 
       
   195     }
       
   196 
       
   197     /**
       
   198      * Test Handle delete
       
   199      */
       
   200     public function testDeleteHandle() {
       
   201 
       
   202         $handleClient = new HandleClient('file://'.$this->rsaTmpPath, $this->rsaPemPassword, $this->rsaAdminHandle, $this->handleHost, $this->handlePort, $this->httpClient);
       
   203 
       
   204         $pagination = $handleClient->paginateAll($this->testPrefix);
       
   205         $totalPrev = $pagination->total();
       
   206 
       
   207         $handle = strtoupper(uniqid("$this->testPrefix/TEST_HANDLE_CLIENT_"));
       
   208 
       
   209         $handleClient->createHandleUrlRecord($handle, "http://www.example.com");
       
   210         $handleClient->deleteHandle($handle);
       
   211 
       
   212 
       
   213         $pagination = $handleClient->paginateAll($this->testPrefix);
       
   214 
       
   215         $this->assertEquals($totalPrev, $pagination->total(), "Must have one more handle");
       
   216 
       
   217         $this->assertNotContains($handle, $pagination);
       
   218 
       
   219 
       
   220     }
       
   221 
       
   222 }