diff --git a/src/API/Management/ResourceServers.php b/src/API/Management/ResourceServers.php index 0594eaad..b6291280 100644 --- a/src/API/Management/ResourceServers.php +++ b/src/API/Management/ResourceServers.php @@ -2,72 +2,150 @@ namespace Auth0\SDK\API\Management; -use Auth0\SDK\API\Header\ContentType; +use Auth0\SDK\Exception\CoreException; +/** + * Class ResourceServers. + * Handles requests to the Resource Servers endpoint of the v2 Management API. + * + * @package Auth0\SDK\API\Management + */ class ResourceServers extends GenericResource { /** + * Get all Resource Servers, by page if desired. + * Required scope: "read:resource_servers" + * + * @param null|integer $page Page number to get, zero-based. + * @param null|integer $per_page Number of results to get, null to return the default number. * * @return mixed + * + * @throws \Exception Thrown by the HTTP client when there is a problem with the API call. + * + * @link https://auth0.com/docs/api/management/v2#!/Resource_Servers/get_resource_servers */ - public function getAll() + public function getAll($page = null, $per_page = null) { - return $this->apiClient->get() - ->addPath('resource-servers') - ->call(); + $params = []; + + // Pagination parameters. + if (null !== $page) { + $params['page'] = abs(intval($page)); + } + + if (null !== $per_page) { + $params['per_page'] = abs(intval($per_page)); + } + + return $this->apiClient->method('get') + ->withDictParams($params) + ->addPath('resource-servers') + ->call(); } /** + * Get a single Resource Server by ID or API identifier. + * Required scope: "read:resource_servers" + * + * @param string $id Resource Server ID or identifier to get. * - * @param string $id * @return mixed + * + * @throws CoreException Thrown if the id parameter is empty or is not a string. + * @throws \Exception Thrown by the HTTP client when there is a problem with the API call. + * + * @link https://auth0.com/docs/api/management/v2#!/Resource_Servers/get_resource_servers_by_id */ public function get($id) { - return $this->apiClient->get() - ->addPath('resource-servers', $id) - ->call(); + if (empty($id) || ! is_string($id)) { + throw new CoreException('Invalid "id" parameter.'); + } + + return $this->apiClient->method('get') + ->addPath('resource-servers', $id) + ->call(); } /** + * Create a new Resource Server. + * Required scope: "create:resource_servers" + * + * @param string $identifier API identifier to use. + * @param array $data Additional fields to add. * - * @param string $client_id - * @param array $data * @return mixed + * + * @throws CoreException Thrown if the identifier parameter or data field is empty or is not a string. + * @throws \Exception Thrown by the HTTP client when there is a problem with the API call. + * + * @link https://auth0.com/docs/api/management/v2#!/Resource_Servers/post_resource_servers */ - public function create($client_id, $data) + public function create($identifier, array $data) { - return $this->apiClient->post() - ->addPath('resource-servers') - ->withHeader(new ContentType('application/json')) - ->withBody(json_encode($data)) - ->call(); + // Backwards-compatibility with previously-unused $identifier parameter. + if (empty($data['identifier'])) { + $data['identifier'] = $identifier; + } + + if (empty($data['identifier']) || ! is_string($data['identifier'])) { + throw new CoreException('Invalid "identifier" field.'); + } + + return $this->apiClient->method('post') + ->addPath('resource-servers') + ->withBody(json_encode($data)) + ->call(); } /** + * Delete a Resource Server by ID. + * Required scope: "delete:resource_servers" + * + * @param string $id Resource Server ID or identifier to delete. * - * @param string $id * @return mixed + * + * @throws CoreException Thrown if the id parameter is empty or is not a string. + * @throws \Exception Thrown by the HTTP client when there is a problem with the API call. + * + * @link https://auth0.com/docs/api/management/v2#!/Resource_Servers/delete_resource_servers_by_id */ public function delete($id) { - return $this->apiClient->delete() - ->addPath('resource-servers', $id) - ->call(); + if (empty($id) || ! is_string($id)) { + throw new CoreException('Invalid "id" parameter.'); + } + + return $this->apiClient->method('delete') + ->addPath('resource-servers', $id) + ->call(); } /** + * Update a Resource Server by ID. + * Required scope: "update:resource_servers" + * + * @param string $id Resource Server ID or identifier to update. + * @param array $data Data to update. * - * @param string $id - * @param array $data * @return mixed + * + * @throws CoreException Thrown if the id parameter is empty or is not a string. + * @throws \Exception Thrown by the HTTP client when there is a problem with the API call. + * + * @link https://auth0.com/docs/api/management/v2#!/Resource_Servers/patch_resource_servers_by_id */ - public function update($id, $data) + public function update($id, array $data) { - return $this->apiClient->patch() - ->addPath('resource-servers', $id) - ->withHeader(new ContentType('application/json')) - ->withBody(json_encode($data)) - ->call(); + if (empty($id) || ! is_string($id)) { + throw new CoreException('Invalid "id" parameter.'); + } + + return $this->apiClient->method('patch') + ->addPath('resource-servers', $id) + ->withBody(json_encode($data)) + ->call(); } } diff --git a/tests/API/Management/ResourceServersTest.php b/tests/API/Management/ResourceServersTest.php index 93dec140..b9b0d9f3 100644 --- a/tests/API/Management/ResourceServersTest.php +++ b/tests/API/Management/ResourceServersTest.php @@ -5,141 +5,237 @@ use Auth0\SDK\API\Management; use Auth0\Tests\API\ApiTests; use GuzzleHttp\Exception\ClientException; +use Auth0\SDK\Exception\CoreException; +/** + * Class ResourceServersTest. + * + * @package Auth0\Tests\API\Management + */ class ResourceServersTest extends ApiTests { - protected $domain; - - protected $clientId; - - protected $token; - - protected $api; - - public static $serverName; - - public static $serverIdentifier; + /** + * Resource Server API client. + * + * @var Management\ResourceServers + */ + protected static $api; - public static $createdServerId = ''; + /** + * Resource Server identifier. + * + * @var string + */ + protected static $serverIdentifier; /** - * Test fixture for class + * Test scopes to use. + * + * @var array */ - public static function setUpBeforeClass() - { - self::$serverName = 'TEST_PHP_SDK_'.uniqid(); - self::$serverIdentifier = 'TEST_PHP_SDK_'.uniqid(); - } + protected static $scopes = [ + [ + 'value' => 'read:test1', + 'description' => 'Testing scope' + ], + [ + 'value' => 'read:test2', + 'description' => 'Testing scope' + ], + ]; /** - * Test fixture for each method + * Sets up API client for the testing class. + * + * @return void */ - protected function setUp() + public static function setUpBeforeClass() { - $env = $this->getEnv(); - - $this->domain = $env['DOMAIN']; - $this->clientId = $env['APP_CLIENT_ID']; - $this->token = $token = $this->getToken( - $env, [ - 'resource_servers' => [ - 'actions' => ['create', 'read', 'delete', 'update'] - ] - ] - ); - - $this->api = new Management($this->token, $this->domain); - - $this->assertNotEmpty($this->token); + self::$api = self::getApiStatic( 'resource_servers', ['read', 'create', 'delete', 'update'] ); + self::$serverIdentifier = 'TEST_PHP_SDK_ID_'.uniqid(); } /** - * Test creating a resource server + * Test creating a Resource Server. + * + * @return void + * + * @throws CoreException Thrown if the identifier parameter or data field is empty or is not a string. + * @throws \Exception Thrown by the HTTP client when there is a problem with the API call. */ public function testCreate() { - $response = $this->api->resource_servers->create( - $this->clientId, [ - 'name' => self::$serverName, - 'identifier' => self::$serverIdentifier, - ] - ); + $create_data = [ + 'name' => 'TEST_PHP_SDK_CREATE_'.uniqid(), + 'token_lifetime' => rand( 10000, 20000 ), + 'signing_alg' => 'HS256', + // Only add a single scope so we can update it later. + 'scopes' => [self::$scopes[0]] + ]; + + $response = self::$api->create(self::$serverIdentifier, $create_data); $this->assertNotEmpty($response); $this->assertNotEmpty($response['id']); - $this->assertEquals($response['name'], self::$serverName); - $this->assertEquals($response['identifier'], self::$serverIdentifier); - - self::$createdServerId = $response['id']; + $this->assertEquals(self::$serverIdentifier, $response['identifier']); + $this->assertEquals($create_data['name'], $response['name']); + $this->assertEquals($create_data['token_lifetime'], $response['token_lifetime']); + $this->assertEquals($create_data['signing_alg'], $response['signing_alg']); + $this->assertEquals($create_data['scopes'], $response['scopes']); } /** - * Test getting the resource server created above + * Test getting a Resource Server. + * + * @return void + * + * @throws CoreException Thrown if the identifier parameter or data field is empty or is not a string. + * @throws \Exception Thrown by the HTTP client when there is a problem with the API call. */ public function testGet() { - $response = $this->api->resource_servers->get(self::$createdServerId); - - $this->assertEquals($response['id'], self::$createdServerId); - $this->assertEquals($response['name'], self::$serverName); - $this->assertEquals($response['identifier'], self::$serverIdentifier); + $response = self::$api->get(self::$serverIdentifier); + $this->assertNotEmpty($response); + $this->assertEquals(self::$serverIdentifier, $response['identifier']); } /** - * Test getting all resource servers and finding the one we added + * Test getting all Resource Servers and looking for the created one. + * + * @return void + * + * @throws \Exception Thrown by the HTTP client when there is a problem with the API call. */ public function testGetAll() { - $response = $this->api->resource_servers->getAll(); + $response = self::$api->getAll(); - // Make sure the one we added was found + // Should have at least the one we created and the management API. + $this->assertGreaterThanOrEqual(2, count($response)); + + // Make sure the one we created was found. $found_added = false; foreach ($response as $server) { - if ($server['id'] === self::$createdServerId) { + if ($server['identifier'] === self::$serverIdentifier) { $found_added = true; break; } } - $this->assertGreaterThanOrEqual(2, count($response)); $this->assertTrue($found_added); + + // Test pagination. + $response_paged = self::$api->getAll(1, 1); + $this->assertNotEmpty($response_paged); + $this->assertEquals($response[1]['id'], $response_paged[0]['id']); } + /** + * Test updating the created Resource Server. + * + * @return void + * + * @throws CoreException Thrown if the identifier parameter or data field is empty or is not a string. + * @throws \Exception Thrown by the HTTP client when there is a problem with the API call. + */ public function testUpdate() { - // Swap name and identifier - $update_data = [ 'signing_alg' => 'HS256' ]; - $response = $this->api->resource_servers->update(self::$createdServerId, $update_data); - - // Make sure everything we tried to update was updated - $matched = true; - foreach ($update_data as $key => $val) { - if ($response[$key] !== $val) { - $matched = false; - break; - } - } - - $this->assertTrue($matched); + $update_data = [ + 'name' => 'TEST_PHP_SDK_UPDATE_'.uniqid(), + 'token_lifetime' => rand( 20001, 30000 ), + 'signing_alg' => 'RS256', + 'scopes' => self::$scopes + ]; + + $response = self::$api->update(self::$serverIdentifier, $update_data); + + $this->assertEquals($update_data['name'], $response['name']); + $this->assertEquals($update_data['token_lifetime'], $response['token_lifetime']); + $this->assertEquals($update_data['signing_alg'], $response['signing_alg']); + $this->assertEquals($update_data['scopes'], $response['scopes']); } /** - * Test deleting the resource server created above + * Test deleting the Resource Server created above. + * + * @return void + * + * @throws CoreException Thrown if the identifier parameter or data field is empty or is not a string. + * @throws \Exception Thrown by the HTTP client when there is a problem with the API call. */ public function testDelete() { - $response = $this->api->resource_servers->delete(self::$createdServerId); + $response = self::$api->delete(self::$serverIdentifier); - // Look for the resource server we just deleted + // Look for the resource server we just deleted. $get_server_throws_error = false; try { - $this->api->resource_servers->get(self::$createdServerId); + self::$api->get(self::$serverIdentifier); } catch (ClientException $e) { - $get_server_throws_error = 404 === $e->getCode(); + $get_server_throws_error = (404 === $e->getCode()); } $this->assertNull($response); $this->assertTrue($get_server_throws_error); } + + /** + * Test that exceptions are thrown for specific methods in specific cases. + * + * @return void + * + * @throws \Exception Thrown by the HTTP client when there is a problem with the API call. + */ + public function testExceptions() + { + // Test that the get method throws an exception if the $id parameter is empty. + $caught_get_no_id_exception = false; + try { + self::$api->get(null); + } catch (CoreException $e) { + $caught_get_no_id_exception = $this->errorHasString($e, 'Invalid "id" parameter'); + } + + $this->assertTrue($caught_get_no_id_exception); + + // Test that the delete method throws an exception if the $id parameter is empty. + $caught_delete_no_id_exception = false; + try { + self::$api->delete(null); + } catch (CoreException $e) { + $caught_delete_no_id_exception = $this->errorHasString($e, 'Invalid "id" parameter'); + } + + $this->assertTrue($caught_delete_no_id_exception); + + // Test that the update method throws an exception if the $id parameter is empty. + $caught_update_no_id_exception = false; + try { + self::$api->update(null, []); + } catch (CoreException $e) { + $caught_update_no_id_exception = $this->errorHasString($e, 'Invalid "id" parameter'); + } + + $this->assertTrue($caught_update_no_id_exception); + + // Test that the create method throws an exception if the $identifier parameter is empty. + $caught_create_empty_identifier_param_exception = false; + try { + self::$api->create(null, []); + } catch (CoreException $e) { + $caught_create_empty_identifier_param_exception = $this->errorHasString($e, 'Invalid "identifier" field'); + } + + $this->assertTrue($caught_create_empty_identifier_param_exception); + + $caught_create_invalid_identifier_field_exception = false; + try { + self::$api->create('identifier', ['identifier' => 1234]); + } catch (CoreException $e) { + $caught_create_invalid_identifier_field_exception = $this->errorHasString($e, 'Invalid "identifier" field'); + } + + $this->assertTrue($caught_create_invalid_identifier_field_exception); + } } diff --git a/tests/API/Management/RulesTest.php b/tests/API/Management/RulesTest.php index 0fa68f28..d3926c66 100644 --- a/tests/API/Management/RulesTest.php +++ b/tests/API/Management/RulesTest.php @@ -170,6 +170,7 @@ public function testExceptions() } catch (CoreException $e) { $caught_get_no_id_exception = $this->errorHasString($e, 'Invalid "id" parameter'); } + $this->assertTrue($caught_get_no_id_exception); // Test that the delete method throws an exception if the $id parameter is empty. @@ -179,6 +180,7 @@ public function testExceptions() } catch (CoreException $e) { $caught_delete_no_id_exception = $this->errorHasString($e, 'Invalid "id" parameter'); } + $this->assertTrue($caught_delete_no_id_exception); // Test that the create method throws an exception if no "name" field is passed. @@ -188,6 +190,7 @@ public function testExceptions() } catch (CoreException $e) { $caught_create_no_name_exception = $this->errorHasString($e, 'Missing required "name" field'); } + $this->assertTrue($caught_create_no_name_exception); // Test that the create method throws an exception if no "script" field is passed. @@ -197,6 +200,7 @@ public function testExceptions() } catch (CoreException $e) { $caught_create_no_script_exception = $this->errorHasString($e, 'Missing required "script" field'); } + $this->assertTrue($caught_create_no_script_exception); // Test that the update method throws an exception if the $id parameter is empty. @@ -206,6 +210,7 @@ public function testExceptions() } catch (CoreException $e) { $caught_update_no_id_exception = $this->errorHasString($e, 'Invalid "id" parameter'); } + $this->assertTrue($caught_update_no_id_exception); } }