You are here

class RolesTestMocked in Auth0 Single Sign On 8.2

Class RolesTestMocked.

@package Auth0\Tests\API\Management

Hierarchy

Expanded class hierarchy of RolesTestMocked

File

vendor/auth0/auth0-php/tests/API/Management/RolesMockedTest.php, line 16

Namespace

Auth0\Tests\API\Management
View source
class RolesTestMocked extends \PHPUnit_Framework_TestCase {
  use ErrorHelpers;

  /**
   * Expected telemetry value.
   *
   * @var string
   */
  protected static $expectedTelemetry;

  /**
   * Default request headers.
   *
   * @var array
   */
  protected static $headers = [
    'content-type' => 'json',
  ];

  /**
   * Runs before test suite starts.
   */
  public static function setUpBeforeClass() {
    $infoHeadersData = new InformationHeaders();
    $infoHeadersData
      ->setCorePackage();
    self::$expectedTelemetry = $infoHeadersData
      ->build();
  }
  public function testThatMethodAndPropertyReturnSameClass() {
    $api = new Management(uniqid(), uniqid());
    $this
      ->assertInstanceOf(Management\Roles::class, $api->roles);
    $this
      ->assertInstanceOf(Management\Roles::class, $api
      ->roles());
    $api->roles = null;
    $this
      ->assertInstanceOf(Management\Roles::class, $api
      ->roles());
  }

  /**
   * Test a basic getAll roles call.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatGetAllRolesRequestIsFormattedProperly() {
    $api = new MockManagementApi([
      new Response(200, self::$headers),
    ]);
    $api
      ->call()
      ->roles()
      ->getAll([
      'name_filter' => '__test_name_filter__',
      'page' => 2,
    ]);
    $this
      ->assertEquals('GET', $api
      ->getHistoryMethod());
    $this
      ->assertStringStartsWith('https://api.test.local/api/v2/roles', $api
      ->getHistoryUrl());
    $query = $api
      ->getHistoryQuery();
    $this
      ->assertContains('page=2', $query);
    $this
      ->assertContains('name_filter=__test_name_filter__', $query);
    $headers = $api
      ->getHistoryHeaders();
    $this
      ->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
    $this
      ->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
  }

  /**
   * Test that an empty role name throws an exception when trying to create a role.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatCreateRoleRequestWithEmptyNameThrowsException() {
    $api = new MockManagementApi();
    try {
      $api
        ->call()
        ->roles()
        ->create('');
      $exception_message = '';
    } catch (EmptyOrInvalidParameterException $e) {
      $exception_message = $e
        ->getMessage();
    }
    $this
      ->assertContains('Empty or invalid name', $exception_message);
  }

  /**
   * Test create role is requested properly.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatCreateRoleRequestIsFormattedProperly() {
    $api = new MockManagementApi([
      new Response(200, self::$headers),
    ]);
    $api
      ->call()
      ->roles()
      ->create('__test_name__', [
      'description' => '__test_description__',
    ]);
    $this
      ->assertEquals('POST', $api
      ->getHistoryMethod());
    $this
      ->assertEquals('https://api.test.local/api/v2/roles', $api
      ->getHistoryUrl());
    $headers = $api
      ->getHistoryHeaders();
    $this
      ->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
    $this
      ->assertEquals('application/json', $headers['Content-Type'][0]);
    $this
      ->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
    $body = $api
      ->getHistoryBody();
    $this
      ->assertArrayHasKey('name', $body);
    $this
      ->assertEquals('__test_name__', $body['name']);
    $this
      ->assertArrayHasKey('description', $body);
    $this
      ->assertEquals('__test_description__', $body['description']);
  }

  /**
   * Test that an empty role ID throws an exception when trying to get a role.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatGetRoleRequestWithEmptyRoleIdThrowsException() {
    $api = new MockManagementApi();
    try {
      $api
        ->call()
        ->roles()
        ->get('');
      $exception_message = '';
    } catch (EmptyOrInvalidParameterException $e) {
      $exception_message = $e
        ->getMessage();
    }
    $this
      ->assertContains('Empty or invalid role_id', $exception_message);
  }

  /**
   * Test a get role call.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatGetRoleRequestIsFormattedProperly() {
    $api = new MockManagementApi([
      new Response(200, self::$headers),
    ]);
    $api
      ->call()
      ->roles()
      ->get('__test_role_id__');
    $this
      ->assertEquals('GET', $api
      ->getHistoryMethod());
    $this
      ->assertEquals('https://api.test.local/api/v2/roles/__test_role_id__', $api
      ->getHistoryUrl());
    $headers = $api
      ->getHistoryHeaders();
    $this
      ->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
    $this
      ->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
  }

  /**
   * Test that an empty role ID throws an exception when trying to delete a role.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatDeleteRoleRequestWithEmptyRoleIdThrowsException() {
    $api = new MockManagementApi();
    try {
      $api
        ->call()
        ->roles()
        ->delete('');
      $exception_message = '';
    } catch (EmptyOrInvalidParameterException $e) {
      $exception_message = $e
        ->getMessage();
    }
    $this
      ->assertContains('Empty or invalid role_id', $exception_message);
  }

  /**
   * Test a delete role call.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatDeleteRoleRequestIsFormattedProperly() {
    $api = new MockManagementApi([
      new Response(200, self::$headers),
    ]);
    $api
      ->call()
      ->roles()
      ->delete('__test_role_id__');
    $this
      ->assertEquals('DELETE', $api
      ->getHistoryMethod());
    $this
      ->assertEquals('https://api.test.local/api/v2/roles/__test_role_id__', $api
      ->getHistoryUrl());
    $headers = $api
      ->getHistoryHeaders();
    $this
      ->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
    $this
      ->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
  }

  /**
   * Test that an empty role ID throws an exception when trying to update a role.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatUpdateRoleRequestWithEmptyRoleIdThrowsException() {
    $api = new MockManagementApi();
    try {
      $api
        ->call()
        ->roles()
        ->update('', []);
      $exception_message = '';
    } catch (EmptyOrInvalidParameterException $e) {
      $exception_message = $e
        ->getMessage();
    }
    $this
      ->assertContains('Empty or invalid role_id', $exception_message);
  }

  /**
   * Test an update role call.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatUpdateRoleRequestIsFormattedProperly() {
    $api = new MockManagementApi([
      new Response(200, self::$headers),
    ]);
    $api
      ->call()
      ->roles()
      ->update('__test_role_id__', [
      'name' => '__test_new_name__',
    ]);
    $this
      ->assertEquals('PATCH', $api
      ->getHistoryMethod());
    $this
      ->assertEquals('https://api.test.local/api/v2/roles/__test_role_id__', $api
      ->getHistoryUrl());
    $headers = $api
      ->getHistoryHeaders();
    $this
      ->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
    $this
      ->assertEquals('application/json', $headers['Content-Type'][0]);
    $this
      ->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
    $body = $api
      ->getHistoryBody();
    $this
      ->assertArrayHasKey('name', $body);
    $this
      ->assertEquals('__test_new_name__', $body['name']);
  }

  /**
   * Test that an empty role ID throws an exception when trying to get permissions for a role.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatGetRolePermissionsRequestWithEmptyRoleIdThrowsException() {
    $api = new MockManagementApi();
    try {
      $api
        ->call()
        ->roles()
        ->getPermissions('');
      $exception_message = '';
    } catch (EmptyOrInvalidParameterException $e) {
      $exception_message = $e
        ->getMessage();
    }
    $this
      ->assertContains('Empty or invalid role_id', $exception_message);
  }

  /**
   * Test a get role permissions call.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatGetRolePermissionsRequestIsFormattedProperly() {
    $api = new MockManagementApi([
      new Response(200, self::$headers),
    ]);
    $api
      ->call()
      ->roles()
      ->getPermissions('__test_role_id__', [
      'page' => -3,
      'per_page' => -6,
    ]);
    $this
      ->assertEquals('GET', $api
      ->getHistoryMethod());
    $this
      ->assertStringStartsWith('https://api.test.local/api/v2/roles/__test_role_id__/permissions', $api
      ->getHistoryUrl());
    $headers = $api
      ->getHistoryHeaders();
    $this
      ->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
    $this
      ->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
    $query = $api
      ->getHistoryQuery();
    $this
      ->assertContains('page=3', $query);
    $this
      ->assertContains('per_page=6', $query);
  }

  /**
   * Test a get role permissions call including totals.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatGetRolePermissionsRequestIncludesTotals() {
    $api = new MockManagementApi([
      new Response(200, self::$headers),
      new Response(200, self::$headers),
    ]);
    $api
      ->call()
      ->roles()
      ->getPermissions('__test_role_id__', [
      'include_totals' => false,
    ]);
    $this
      ->assertContains('include_totals=false', $api
      ->getHistoryQuery());
    $api
      ->call()
      ->roles()
      ->getPermissions('__test_role_id__', [
      'include_totals' => 1,
    ]);
    $this
      ->assertContains('include_totals=true', $api
      ->getHistoryQuery());
  }

  /**
   * Test that an empty role ID throws an exception when trying to add permissions for a role.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatAddRolePermissionsRequestWithEmptyRoleIdThrowsException() {
    $api = new MockManagementApi();
    try {
      $api
        ->call()
        ->roles()
        ->addPermissions('', []);
      $exception_message = '';
    } catch (EmptyOrInvalidParameterException $e) {
      $exception_message = $e
        ->getMessage();
    }
    $this
      ->assertContains('Empty or invalid role_id', $exception_message);
  }

  /**
   * Test that an invalid permissions array throws an exception when trying to add permissions for a role.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatAddRolePermissionsRequestWithEmptyPermissionsThrowsException() {
    $api = new MockManagementApi();
    try {
      $api
        ->call()
        ->roles()
        ->addPermissions('__test_role_id__', []);
      $caught_exception = false;
    } catch (InvalidPermissionsArrayException $e) {
      $caught_exception = true;
    }
    $this
      ->assertTrue($caught_exception);
  }

  /**
   * Test an add role permissions call.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatAddRolePermissionsRequestIsFormattedProperly() {
    $api = new MockManagementApi([
      new Response(200, self::$headers),
    ]);
    $api
      ->call()
      ->roles()
      ->addPermissions('__test_role_id__', [
      [
        'permission_name' => '__test_permission_name__',
        'resource_server_identifier' => '__test_server_id__',
      ],
    ]);
    $this
      ->assertEquals('POST', $api
      ->getHistoryMethod());
    $this
      ->assertEquals('https://api.test.local/api/v2/roles/__test_role_id__/permissions', $api
      ->getHistoryUrl());
    $headers = $api
      ->getHistoryHeaders();
    $this
      ->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
    $this
      ->assertEquals('application/json', $headers['Content-Type'][0]);
    $this
      ->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
    $body = $api
      ->getHistoryBody();
    $this
      ->assertArrayHasKey('permissions', $body);
    $this
      ->assertCount(1, $body['permissions']);
    $this
      ->assertArrayHasKey('permission_name', $body['permissions'][0]);
    $this
      ->assertEquals('__test_permission_name__', $body['permissions'][0]['permission_name']);
    $this
      ->assertArrayHasKey('resource_server_identifier', $body['permissions'][0]);
    $this
      ->assertEquals('__test_server_id__', $body['permissions'][0]['resource_server_identifier']);
  }

  /**
   * Test that an empty role ID throws an exception when trying to delete permissions from a role.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatRemoveRolePermissionsRequestWithEmptyRoleIdThrowsException() {
    $api = new MockManagementApi();
    try {
      $api
        ->call()
        ->roles()
        ->removePermissions('', []);
      $exception_message = '';
    } catch (EmptyOrInvalidParameterException $e) {
      $exception_message = $e
        ->getMessage();
    }
    $this
      ->assertContains('Empty or invalid role_id', $exception_message);
  }

  /**
   * Test that an invalid permissions array throws an exception when trying to delete permissions from a role.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatRemoveRolePermissionsRequestWithInvalidPermissionsThrowsException() {
    $api = new MockManagementApi();
    try {
      $api
        ->call()
        ->roles()
        ->removePermissions('__test_role_id__', [
        [
          'permission_name' => uniqid(),
        ],
      ]);
      $caught_exception = false;
    } catch (InvalidPermissionsArrayException $e) {
      $caught_exception = true;
    }
    $this
      ->assertTrue($caught_exception);
    try {
      $api
        ->call()
        ->roles()
        ->removePermissions('__test_role_id__', [
        [
          'resource_server_identifier' => uniqid(),
        ],
      ]);
      $caught_exception = false;
    } catch (InvalidPermissionsArrayException $e) {
      $caught_exception = true;
    }
    $this
      ->assertTrue($caught_exception);
  }

  /**
   * Test a delete role permissions call.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatRemoveRolePermissionsRequestIsFormattedProperly() {
    $api = new MockManagementApi([
      new Response(200, self::$headers),
    ]);
    $api
      ->call()
      ->roles()
      ->removePermissions('__test_role_id__', [
      [
        'permission_name' => '__test_permission_name__',
        'resource_server_identifier' => '__test_server_id__',
      ],
    ]);
    $this
      ->assertEquals('DELETE', $api
      ->getHistoryMethod());
    $this
      ->assertEquals('https://api.test.local/api/v2/roles/__test_role_id__/permissions', $api
      ->getHistoryUrl());
    $headers = $api
      ->getHistoryHeaders();
    $this
      ->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
    $this
      ->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
    $body = $api
      ->getHistoryBody();
    $this
      ->assertArrayHasKey('permissions', $body);
    $this
      ->assertCount(1, $body['permissions']);
    $this
      ->assertArrayHasKey('permission_name', $body['permissions'][0]);
    $this
      ->assertEquals('__test_permission_name__', $body['permissions'][0]['permission_name']);
    $this
      ->assertArrayHasKey('resource_server_identifier', $body['permissions'][0]);
    $this
      ->assertEquals('__test_server_id__', $body['permissions'][0]['resource_server_identifier']);
  }

  /**
   * Test that an empty role ID throws an exception when trying to get users for a role.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatGetUsersRequestWithEmptyRoleIdThrowsException() {
    $api = new MockManagementApi();
    try {
      $api
        ->call()
        ->roles()
        ->getUsers('');
      $exception_message = '';
    } catch (EmptyOrInvalidParameterException $e) {
      $exception_message = $e
        ->getMessage();
    }
    $this
      ->assertContains('Empty or invalid role_id', $exception_message);
  }

  /**
   * Test a paginated get role users call.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatGetUsersRequestIsFormattedProperly() {
    $api = new MockManagementApi([
      new Response(200, self::$headers),
    ]);
    $api
      ->call()
      ->roles()
      ->getUsers('__test_role_id__', [
      'per_page' => 6,
      'include_totals' => 1,
    ]);
    $this
      ->assertEquals('GET', $api
      ->getHistoryMethod());
    $this
      ->assertStringStartsWith('https://api.test.local/api/v2/roles/__test_role_id__/users', $api
      ->getHistoryUrl());
    $headers = $api
      ->getHistoryHeaders();
    $this
      ->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
    $this
      ->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
    $query = $api
      ->getHistoryQuery();
    $this
      ->assertContains('page=6', $query);
    $this
      ->assertContains('include_totals=true', $query);
  }

  /**
   * Test that an empty role ID throws an exception when trying to add users to a role.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatAddUsersRequestWithEmptyRoleIdThrowsException() {
    $api = new MockManagementApi();
    try {
      $api
        ->call()
        ->roles()
        ->addUsers('', []);
      $exception_message = '';
    } catch (EmptyOrInvalidParameterException $e) {
      $exception_message = $e
        ->getMessage();
    }
    $this
      ->assertContains('Empty or invalid role_id', $exception_message);
  }

  /**
   * Test that an empty users parameter throws an exception when trying to add users to a role.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatAddUsersRequestWithEmptyUsersThrowsException() {
    $api = new MockManagementApi();
    try {
      $api
        ->call()
        ->roles()
        ->addUsers('__test_role_id__', []);
      $exception_message = '';
    } catch (EmptyOrInvalidParameterException $e) {
      $exception_message = $e
        ->getMessage();
    }
    $this
      ->assertContains('Empty or invalid users', $exception_message);
  }

  /**
   * Test an add role user call.
   *
   * @return void
   *
   * @throws \Exception Should not be thrown in this test.
   */
  public function testThatAddUsersRequestIsFormattedProperly() {
    $api = new MockManagementApi([
      new Response(200, self::$headers),
    ]);
    $api
      ->call()
      ->roles()
      ->addUsers('__test_role_id__', [
      'strategy|1234567890',
      'strategy|0987654321',
    ]);
    $this
      ->assertEquals('POST', $api
      ->getHistoryMethod());
    $this
      ->assertEquals('https://api.test.local/api/v2/roles/__test_role_id__/users', $api
      ->getHistoryUrl());
    $headers = $api
      ->getHistoryHeaders();
    $this
      ->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
    $this
      ->assertEquals('application/json', $headers['Content-Type'][0]);
    $this
      ->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
    $body = $api
      ->getHistoryBody();
    $this
      ->assertArrayHasKey('users', $body);
    $this
      ->assertCount(2, $body['users']);
    $this
      ->assertContains('strategy|1234567890', $body['users']);
    $this
      ->assertContains('strategy|0987654321', $body['users']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ErrorHelpers::errorHasString protected function Does an error message contain a specific string?
RolesTestMocked::$expectedTelemetry protected static property Expected telemetry value.
RolesTestMocked::$headers protected static property Default request headers.
RolesTestMocked::setUpBeforeClass public static function Runs before test suite starts.
RolesTestMocked::testThatAddRolePermissionsRequestIsFormattedProperly public function Test an add role permissions call.
RolesTestMocked::testThatAddRolePermissionsRequestWithEmptyPermissionsThrowsException public function Test that an invalid permissions array throws an exception when trying to add permissions for a role.
RolesTestMocked::testThatAddRolePermissionsRequestWithEmptyRoleIdThrowsException public function Test that an empty role ID throws an exception when trying to add permissions for a role.
RolesTestMocked::testThatAddUsersRequestIsFormattedProperly public function Test an add role user call.
RolesTestMocked::testThatAddUsersRequestWithEmptyRoleIdThrowsException public function Test that an empty role ID throws an exception when trying to add users to a role.
RolesTestMocked::testThatAddUsersRequestWithEmptyUsersThrowsException public function Test that an empty users parameter throws an exception when trying to add users to a role.
RolesTestMocked::testThatCreateRoleRequestIsFormattedProperly public function Test create role is requested properly.
RolesTestMocked::testThatCreateRoleRequestWithEmptyNameThrowsException public function Test that an empty role name throws an exception when trying to create a role.
RolesTestMocked::testThatDeleteRoleRequestIsFormattedProperly public function Test a delete role call.
RolesTestMocked::testThatDeleteRoleRequestWithEmptyRoleIdThrowsException public function Test that an empty role ID throws an exception when trying to delete a role.
RolesTestMocked::testThatGetAllRolesRequestIsFormattedProperly public function Test a basic getAll roles call.
RolesTestMocked::testThatGetRolePermissionsRequestIncludesTotals public function Test a get role permissions call including totals.
RolesTestMocked::testThatGetRolePermissionsRequestIsFormattedProperly public function Test a get role permissions call.
RolesTestMocked::testThatGetRolePermissionsRequestWithEmptyRoleIdThrowsException public function Test that an empty role ID throws an exception when trying to get permissions for a role.
RolesTestMocked::testThatGetRoleRequestIsFormattedProperly public function Test a get role call.
RolesTestMocked::testThatGetRoleRequestWithEmptyRoleIdThrowsException public function Test that an empty role ID throws an exception when trying to get a role.
RolesTestMocked::testThatGetUsersRequestIsFormattedProperly public function Test a paginated get role users call.
RolesTestMocked::testThatGetUsersRequestWithEmptyRoleIdThrowsException public function Test that an empty role ID throws an exception when trying to get users for a role.
RolesTestMocked::testThatMethodAndPropertyReturnSameClass public function
RolesTestMocked::testThatRemoveRolePermissionsRequestIsFormattedProperly public function Test a delete role permissions call.
RolesTestMocked::testThatRemoveRolePermissionsRequestWithEmptyRoleIdThrowsException public function Test that an empty role ID throws an exception when trying to delete permissions from a role.
RolesTestMocked::testThatRemoveRolePermissionsRequestWithInvalidPermissionsThrowsException public function Test that an invalid permissions array throws an exception when trying to delete permissions from a role.
RolesTestMocked::testThatUpdateRoleRequestIsFormattedProperly public function Test an update role call.
RolesTestMocked::testThatUpdateRoleRequestWithEmptyRoleIdThrowsException public function Test that an empty role ID throws an exception when trying to update a role.