View source
<?php
namespace Auth0\Tests\API;
use Auth0\SDK\API\Management;
use Auth0\SDK\Exception\CoreException;
class ClientGrantsTest extends ApiTests {
protected static $api;
protected static $apiIdentifier;
protected static $scopes = [
'test:scope1',
'test:scope2',
];
public static function setUpBeforeClass() {
self::getEnv();
$api = new Management(self::$env['API_TOKEN'], self::$env['DOMAIN']);
self::$api = $api
->clientGrants();
$create_data = [
'name' => 'TEST_PHP_SDK_CREATE_' . uniqid(),
'token_lifetime' => rand(10000, 20000),
'signing_alg' => 'RS256',
];
self::$apiIdentifier = 'TEST_PHP_SDK_CLIENT_GRANT_API_' . uniqid();
$api
->resourceServers()
->create(self::$apiIdentifier, $create_data);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
}
public function setUp() {
parent::setUp();
}
public function testThatMethodAndPropertyReturnSameClass() {
$api = new Management(uniqid(), uniqid());
$this
->assertInstanceOf(Management\ClientGrants::class, $api->client_grants);
$this
->assertInstanceOf(Management\ClientGrants::class, $api
->clientGrants());
$api->client_grants = null;
$this
->assertInstanceOf(Management\ClientGrants::class, $api
->clientGrants());
}
public static function tearDownAfterClass() {
parent::tearDownAfterClass();
$api = new Management(self::$env['API_TOKEN'], self::$env['DOMAIN']);
$api
->resourceServers()
->delete(self::$apiIdentifier);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
}
public function testGet() {
$all_results = self::$api
->getAll();
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this
->assertNotEmpty($all_results);
$expected_client_id = $all_results[0]['client_id'] ?: null;
$this
->assertNotNull($expected_client_id);
$expected_audience = $all_results[0]['audience'] ?: null;
$this
->assertNotNull($expected_audience);
$audience_results = self::$api
->getByAudience($expected_audience);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this
->assertNotEmpty($audience_results);
$this
->assertEquals($expected_audience, $audience_results[0]['audience']);
$client_id_results = self::$api
->getByClientId($expected_client_id);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this
->assertNotEmpty($client_id_results);
$this
->assertEquals($expected_client_id, $client_id_results[0]['client_id']);
}
public function testGetWithPagination() {
$expected_count = 2;
$results_1 = self::$api
->getAll([], 0, $expected_count);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this
->assertCount($expected_count, $results_1);
$expected_page = 1;
$results_2 = self::$api
->getAll([], $expected_page, 1);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this
->assertCount(1, $results_2);
$this
->assertEquals($results_1[$expected_page]['client_id'], $results_2[0]['client_id']);
$this
->assertEquals($results_1[$expected_page]['audience'], $results_2[0]['audience']);
}
public function testGetAllIncludeTotals() {
$expected_page = 0;
$expected_count = 2;
$results = self::$api
->getAll([
'include_totals' => true,
], $expected_page, $expected_count);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this
->assertArrayHasKey('total', $results);
$this
->assertEquals($expected_page * $expected_count, $results['start']);
$this
->assertEquals($expected_count, $results['limit']);
$this
->assertNotEmpty($results['client_grants']);
}
public function testCreateUpdateDeleteGrant() {
$client_id = self::$env['APP_CLIENT_ID'];
$audience = self::$apiIdentifier;
$create_result = self::$api
->create($client_id, $audience, [
self::$scopes[0],
]);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this
->assertArrayHasKey('id', $create_result);
$this
->assertEquals($client_id, $create_result['client_id']);
$this
->assertEquals($audience, $create_result['audience']);
$this
->assertEquals([
self::$scopes[0],
], $create_result['scope']);
$grant_id = $create_result['id'];
$update_result = self::$api
->update($grant_id, self::$scopes);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this
->assertEquals(self::$scopes, $update_result['scope']);
$delete_result = self::$api
->delete($grant_id);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this
->assertNull($delete_result);
}
public function testCreateGrantExceptions() {
$throws_missing_client_id_exception = false;
try {
self::$api
->create('', self::$apiIdentifier, []);
} catch (CoreException $e) {
$throws_missing_client_id_exception = $this
->errorHasString($e, 'Empty or invalid "client_id" parameter');
}
$this
->assertTrue($throws_missing_client_id_exception);
$throws_missing_audience_exception = false;
try {
self::$api
->create(self::$env['APP_CLIENT_ID'], '', []);
} catch (CoreException $e) {
$throws_missing_audience_exception = $this
->errorHasString($e, 'Empty or invalid "audience" parameter');
}
$this
->assertTrue($throws_missing_audience_exception);
}
}