openstackid/tests/ApiTest.php
Sebastian Marcet 6b0d6c36af IDP Upgrade from Laravel 4.X to 5.X
In order to migrate IDP from LV 4.x to
latest LV version, following task were performed:

* Updated namespace to be complain with PSR-4
* General Refactoring: moved all DB access code
  from services to repositories.
* Migration to LV 5.X: these migration guides
  were applied
  - https://laravel.com/docs/5.3/upgrade#upgrade-5.0
  - https://laravel.com/docs/5.3/upgrade#upgrade-5.1.0
  - https://laravel.com/docs/5.3/upgrade#upgrade-5.2.0
* Improved caching: added repositories decorators
  in order to add REDIS cache to queries, entities

Change-Id: I8edf9f5fce6585129701c88bb88332f242307534
2016-11-17 18:37:40 -03:00

241 lines
7.0 KiB
PHP

<?php
use Models\OAuth2\Api;
use Models\OAuth2\ResourceServer;
/**
* Class ApiTest
*/
class ApiTest extends TestCase {
private $current_realm;
private $current_host;
protected function prepareForTests()
{
parent::prepareForTests();
$this->withoutMiddleware();
$this->current_realm = Config::get('app.url');
$parts = parse_url($this->current_realm);
$this->current_host = $parts['host'];
}
public function testGetById(){
$api = Api::where('name','=','api')->first();
$response = $this->action("GET", "Api\ApiController@get",
$parameters = array('id' => $api->id),
array(),
array(),
array());
$content = $response->getContent();
$response_api = json_decode($content);
$this->assertResponseStatus(200);
$this->assertTrue($response_api->id === $api->id);
}
public function testGetByPage(){
$response = $this->action("GET", "Api\ApiController@getByPage",
$parameters = array('offset' => 1,'limit'=>10),
array(),
array(),
array());
$content = $response->getContent();
$list = json_decode($content);
$this->assertTrue(isset($list->total_items) && intval($list->total_items)>0);
$this->assertResponseStatus(200);
}
public function testCreate(){
$resource_server = ResourceServer::where('host','=', $this->current_host)->first();
$data = array(
'name' => 'test-api',
'description' => 'test api',
'active' => true,
'resource_server_id' => $resource_server->id,
);
$response = $this->action("POST", "Api\ApiController@create",
$data,
array(),
array(),
array());
$content = $response->getContent();
$json_response = json_decode($content);
$this->assertResponseStatus(201);
$this->assertTrue(isset($json_response->api_id) && !empty($json_response->api_id));
}
public function testDelete(){
$resource_server = ResourceServer::where('host','=', $this->current_host)->first();
$data = array(
'name' => 'test-api',
'description' => 'test api',
'active' => true,
'resource_server_id' => $resource_server->id,
);
$response = $this->action("POST", "Api\ApiController@create",
$data,
array(),
array(),
array());
$content = $response->getContent();
$json_response = json_decode($content);
$this->assertResponseStatus(201);
$this->assertTrue(isset($json_response->api_id) && !empty($json_response->api_id));
$new_id = $json_response->api_id;
$response = $this->action("DELETE", "Api\ApiController@delete",$parameters = array('id' => $new_id),
array(),
array(),
array());
$this->assertResponseStatus(204);
$response = $this->action("GET", "Api\ApiController@get",
$parameters = array('id' => $new_id),
array(),
array(),
array());
$content = $response->getContent();
$response_api_endpoint = json_decode($content);
$this->assertResponseStatus(404);
}
public function testUpdate(){
$resource_server = ResourceServer::where('host','=',$this->current_host)->first();
$data = array(
'name' => 'test-api',
'description' => "test api",
'active' => true,
'resource_server_id' => $resource_server->id,
);
$response = $this->action("POST", "Api\ApiController@create",
$data,
array(),
array(),
array());
$content = $response->getContent();
$json_response = json_decode($content);
$this->assertResponseStatus(201);
$this->assertTrue(isset($json_response->api_id) && !empty($json_response->api_id));
$new_id = $json_response->api_id;
//update it
$data_update = array(
'id' => $new_id,
'name' => 'test-api-updated',
'description' => 'test api updated',
);
$response = $this->action("PUT", "Api\ApiController@update",$parameters = $data_update, array(),
array(),
array());
$content = $response->getContent();
$json_response = json_decode($content);
$this->assertResponseStatus(200);
$response = $this->action("GET", "Api\ApiController@get",
$parameters = array('id' =>$new_id),
array(),
array(),
array());
$content = $response->getContent();
$updated_values = json_decode($content);
$this->assertTrue($updated_values->name === 'test-api-updated');
$this->assertResponseStatus(200);
}
public function testUpdateStatus(){
$resource_server = ResourceServer::where('host','=',$this->current_host)->first();
$data = array(
'name' => 'test-api',
'description' => 'test api',
'active' => true,
'resource_server_id' => $resource_server->id,
);
$response = $this->action("POST", "Api\ApiController@create",$data);
$this->assertResponseStatus(201);
$content = $response->getContent();
$json_response = json_decode($content);
$this->assertTrue(isset($json_response->api_id) && !empty($json_response->api_id));
$new_id = $json_response->api_id;
//update status
$response = $this->action("PUT", "Api\ApiController@activate",array('id' => $new_id));
$this->assertResponseStatus(200);
$content = $response->getContent();
$json_response = json_decode($content);
$this->assertTrue($json_response==='ok');
$response = $this->action("GET", "Api\ApiController@get",$parameters = array('id' => $new_id));
$this->assertResponseStatus(200);
$content = $response->getContent();
$updated_values = json_decode($content);
$this->assertTrue($updated_values->active == true);
}
public function testDeleteExisting(){
$resource_server_api = Api::where('name','=','resource-server')->first();
$id = $resource_server_api->id;
$response = $this->action("DELETE", "Api\ApiController@delete",$parameters = array('id' => $id),
array(),
array(),
array());
$this->assertResponseStatus(204);
$response = $this->action("GET", "Api\ApiController@get",
$parameters = array('id' => $id),
array(),
array(),
array());
$this->assertResponseStatus(404);
}
}