View source
<?php
namespace Auth0\Tests\API\Management;
use Auth0\SDK\API\Management;
use Auth0\SDK\Exception\CoreException;
use Auth0\SDK\Exception\EmptyOrInvalidParameterException;
use Auth0\SDK\Exception\InvalidPermissionsArrayException;
use Auth0\Tests\Traits\ErrorHelpers;
use Auth0\SDK\API\Helpers\InformationHeaders;
use GuzzleHttp\Psr7\Response;
class UsersMockedTest extends \PHPUnit_Framework_TestCase {
use ErrorHelpers;
protected static $expectedTelemetry;
protected static $headers = [
'content-type' => 'json',
];
public static function setUpBeforeClass() {
$infoHeadersData = new InformationHeaders();
$infoHeadersData
->setCorePackage();
self::$expectedTelemetry = $infoHeadersData
->build();
}
public function testThatMethodAndPropertyReturnSameClass() {
$api = new Management(uniqid(), uniqid());
$this
->assertInstanceOf(Management\Users::class, $api->users);
$this
->assertInstanceOf(Management\Users::class, $api
->users());
$api->users = null;
$this
->assertInstanceOf(Management\Users::class, $api
->users());
}
public function testThatGetUserRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->get('__test_user_id__');
$this
->assertEquals('GET', $api
->getHistoryMethod());
$this
->assertEquals('https://api.test.local/api/v2/users/__test_user_id__', $api
->getHistoryUrl());
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
}
public function testThatUpdateUserRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->update('__test_user_id__', [
'given_name' => '__test_given_name__',
'user_metadata' => [
'__test_meta_key__' => '__test_meta_value__',
],
]);
$this
->assertEquals('PATCH', $api
->getHistoryMethod());
$this
->assertEquals('https://api.test.local/api/v2/users/__test_user_id__', $api
->getHistoryUrl());
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
$this
->assertEquals('application/json', $headers['Content-Type'][0]);
$body = $api
->getHistoryBody();
$this
->assertArrayHasKey('given_name', $body);
$this
->assertEquals('__test_given_name__', $body['given_name']);
$this
->assertArrayHasKey('user_metadata', $body);
$this
->assertArrayHasKey('__test_meta_key__', $body['user_metadata']);
$this
->assertEquals('__test_meta_value__', $body['user_metadata']['__test_meta_key__']);
}
public function testThatExceptionIsThrownWhenConnectionIsMissing() {
$api = new Management(uniqid(), uniqid());
try {
$api
->users()
->create([]);
$exception_message = '';
} catch (\Exception $e) {
$exception_message = $e
->getMessage();
}
$this
->assertContains('Missing required "connection" field', $exception_message);
}
public function testThatExceptionIsThrownWhenSmsConnectionHasNoPhoneNumber() {
$api = new Management(uniqid(), uniqid());
try {
$api
->users()
->create([
'connection' => 'sms',
]);
$exception_message = '';
} catch (\Exception $e) {
$exception_message = $e
->getMessage();
}
$this
->assertContains('Missing required "phone_number" field for sms connection', $exception_message);
}
public function testThatExceptionIsThrownWhenConnectionHasNoEmailAddress() {
$api = new Management(uniqid(), uniqid());
try {
$api
->users()
->create([
'connection' => 'email',
]);
$exception_message = '';
} catch (\Exception $e) {
$exception_message = $e
->getMessage();
}
$this
->assertContains('Missing required "email" field', $exception_message);
}
public function testThatExceptionIsThrownWhenDbConnectionHasNoPassword() {
$api = new Management(uniqid(), uniqid());
try {
$api
->users()
->create([
'connection' => 'auth0',
'email' => uniqid(),
]);
$exception_message = '';
} catch (\Exception $e) {
$exception_message = $e
->getMessage();
}
$this
->assertContains('Missing required "password" field for "auth0" connection', $exception_message);
}
public function testThatCreateUserRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->create([
'connection' => '__test_connection__',
'email' => '__test_email__',
'password' => '__test_password__',
]);
$this
->assertEquals('POST', $api
->getHistoryMethod());
$this
->assertEquals('https://api.test.local/api/v2/users', $api
->getHistoryUrl());
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
$this
->assertEquals('application/json', $headers['Content-Type'][0]);
$body = $api
->getHistoryBody();
$this
->assertArrayHasKey('connection', $body);
$this
->assertEquals('__test_connection__', $body['connection']);
$this
->assertArrayHasKey('email', $body);
$this
->assertEquals('__test_email__', $body['email']);
$this
->assertArrayHasKey('password', $body);
$this
->assertEquals('__test_password__', $body['password']);
}
public function testThatGetAllUsersRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->getAll();
$this
->assertEquals('GET', $api
->getHistoryMethod());
$this
->assertEquals('https://api.test.local/api/v2/users', $api
->getHistoryUrl());
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
}
public function testThatGetAllUsersAdditionalParamsAreSent() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->getAll([
'__test_parameter__' => '__test_value__',
]);
$query = $api
->getHistoryQuery();
$this
->assertEquals('__test_parameter__=__test_value__', $query);
}
public function testThatGetAllUsersFieldsParamDoesNotOverwrite() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->getAll([
'fields' => 'field1,field2',
], 'field3');
$query = $api
->getHistoryQuery();
$this
->assertEquals('fields=field1,field2', $query);
}
public function testThatGetAllUsersFieldsAreFormattedCorrectly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
new Response(200, self::$headers),
]);
$api
->call()
->users()
->getAll([
'fields' => 'field1,field2',
]);
$query = $api
->getHistoryQuery();
$this
->assertEquals('fields=field1,field2', $query);
$api
->call()
->users()
->getAll([
'fields' => [
'field1',
'field2',
],
]);
$query = $api
->getHistoryQuery();
$this
->assertEquals('fields=field1,field2', $query);
}
public function testThatGetAllUsersIncludeFieldsIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->getAll([], [
'field3',
'field4',
], true);
$query = $api
->getHistoryQuery();
$this
->assertContains('fields=field3,field4', $query);
$this
->assertContains('include_fields=true', $query);
}
public function testThatGetAllUsersIncludeFieldsIsNotOverwritten() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->getAll([
'include_fields' => false,
], [
'field3',
], true);
$query = $api
->getHistoryQuery();
$this
->assertContains('fields=field3', $query);
$this
->assertContains('include_fields=false', $query);
}
public function testThatGetAllUsersIncludeFieldsIsConvertedToBool() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->getAll([], [
'field3',
], uniqid());
$query = $api
->getHistoryQuery();
$this
->assertContains('fields=field3', $query);
$this
->assertContains('include_fields=true', $query);
}
public function testThatGetAllUsersWithPageIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
new Response(200, self::$headers),
]);
$api
->call()
->users()
->getAll([], [], null, 10);
$query = $api
->getHistoryQuery();
$this
->assertContains('page=10', $query);
$api
->call()
->users()
->getAll([], [], null, -10);
$query = $api
->getHistoryQuery();
$this
->assertContains('page=10', $query);
}
public function testThatGetAllUsersDoesNotOverwritePageValue() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->getAll([
'page' => 11,
], [], null, 22);
$query = $api
->getHistoryQuery();
$this
->assertContains('page=11', $query);
}
public function testThatGetAllUsersWithPerPageIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
new Response(200, self::$headers),
]);
$api
->call()
->users()
->getAll([], [], null, null, 10);
$query = $api
->getHistoryQuery();
$this
->assertContains('per_page=10', $query);
$api
->call()
->users()
->getAll([], [], null, null, -10);
$query = $api
->getHistoryQuery();
$this
->assertContains('per_page=10', $query);
}
public function testThatGetAllUsersDoesNotOverwritePerPageValue() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->getAll([
'per_page' => 8,
], [], null, null, 9);
$query = $api
->getHistoryQuery();
$this
->assertContains('per_page=8', $query);
}
public function testThatDeleteUserRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->delete('__test_user_id__');
$this
->assertEquals('DELETE', $api
->getHistoryMethod());
$this
->assertEquals('https://api.test.local/api/v2/users/__test_user_id__', $api
->getHistoryUrl());
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
}
public function testThatLinkAccountRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->linkAccount('__test_user_id__', [
'provider' => '__test_provider__',
'connection_id' => '__test_connection_id__',
'user_id' => '__test_secondary_user_id__',
]);
$this
->assertEquals('POST', $api
->getHistoryMethod());
$this
->assertEquals('https://api.test.local/api/v2/users/__test_user_id__/identities', $api
->getHistoryUrl());
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
$this
->assertEquals('application/json', $headers['Content-Type'][0]);
$body = $api
->getHistoryBody();
$this
->assertArrayHasKey('provider', $body);
$this
->assertEquals('__test_provider__', $body['provider']);
$this
->assertArrayHasKey('connection_id', $body);
$this
->assertEquals('__test_connection_id__', $body['connection_id']);
$this
->assertArrayHasKey('user_id', $body);
$this
->assertEquals('__test_secondary_user_id__', $body['user_id']);
}
public function testThatUnlinkAccountRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->unlinkAccount('__test_user_id__', '__test_provider__', '__test_identity_id__');
$this
->assertEquals('DELETE', $api
->getHistoryMethod());
$this
->assertEquals('https://api.test.local/api/v2/users/__test_user_id__/identities/__test_provider__/__test_identity_id__', $api
->getHistoryUrl());
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
}
public function testThatDeleteMfProviderIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->deleteMultifactorProvider('__test_user_id__', 'duo');
$this
->assertEquals('DELETE', $api
->getHistoryMethod());
$this
->assertEquals('https://api.test.local/api/v2/users/__test_user_id__/multifactor/duo', $api
->getHistoryUrl());
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
}
public function testThatGetRolesThrowsExceptionIfUserIdIsMissing() {
$api = new MockManagementApi();
try {
$api
->call()
->users()
->getRoles('');
$caught_message = '';
} catch (EmptyOrInvalidParameterException $e) {
$caught_message = $e
->getMessage();
}
$this
->assertContains('Empty or invalid user_id', $caught_message);
}
public function testThatGetRolesRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->getRoles('__test_user_id__', [
'per_page' => 5,
'page' => 1,
'include_totals' => 1,
]);
$this
->assertEquals('GET', $api
->getHistoryMethod());
$this
->assertStringStartsWith('https://api.test.local/api/v2/users/__test_user_id__/roles?', $api
->getHistoryUrl());
$query = $api
->getHistoryQuery();
$this
->assertContains('per_page=5', $query);
$this
->assertContains('page=1', $query);
$this
->assertContains('include_totals=true', $query);
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
}
public function testThatRemoveRolesThrowsExceptionIfUserIdIsMissing() {
$api = new MockManagementApi();
try {
$api
->call()
->users()
->removeRoles('', []);
$caught_message = '';
} catch (EmptyOrInvalidParameterException $e) {
$caught_message = $e
->getMessage();
}
$this
->assertContains('Empty or invalid user_id', $caught_message);
}
public function testThatRemoveRolesThrowsExceptionIfRolesAreMissing() {
$api = new MockManagementApi();
try {
$api
->call()
->users()
->removeRoles('__test_user_id__', []);
$caught_message = '';
} catch (EmptyOrInvalidParameterException $e) {
$caught_message = $e
->getMessage();
}
$this
->assertContains('Empty or invalid roles', $caught_message);
}
public function testThatRemoveRolesRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->removeRoles('__test_user_id__', [
'__test_role_id__',
]);
$this
->assertEquals('DELETE', $api
->getHistoryMethod());
$this
->assertEquals('https://api.test.local/api/v2/users/__test_user_id__/roles', $api
->getHistoryUrl());
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
$this
->assertEquals('application/json', $headers['Content-Type'][0]);
$body = $api
->getHistoryBody();
$this
->assertArrayHasKey('roles', $body);
$this
->assertCount(1, $body['roles']);
$this
->assertEquals('__test_role_id__', $body['roles'][0]);
}
public function testThatAddRolesThrowsExceptionIfUserIdIsMissing() {
$api = new MockManagementApi();
try {
$api
->call()
->users()
->addRoles('', []);
$caught_message = '';
} catch (EmptyOrInvalidParameterException $e) {
$caught_message = $e
->getMessage();
}
$this
->assertContains('Empty or invalid user_id', $caught_message);
}
public function testThatAddRolesThrowsExceptionIfRolesAreMissing() {
$api = new MockManagementApi();
try {
$api
->call()
->users()
->addRoles('__test_user_id__', []);
$caught_message = '';
} catch (EmptyOrInvalidParameterException $e) {
$caught_message = $e
->getMessage();
}
$this
->assertContains('Empty or invalid roles', $caught_message);
}
public function testThatAddRolesRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->addRoles('__test_user_id__', [
'__test_role_id__',
]);
$this
->assertEquals('POST', $api
->getHistoryMethod());
$this
->assertEquals('https://api.test.local/api/v2/users/__test_user_id__/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('roles', $body);
$this
->assertCount(1, $body['roles']);
$this
->assertEquals('__test_role_id__', $body['roles'][0]);
}
public function testThatGetEnrollmentsThrowsExceptionIfUserIdIsMissing() {
$api = new MockManagementApi();
try {
$api
->call()
->users()
->getEnrollments('');
$caught_message = '';
} catch (EmptyOrInvalidParameterException $e) {
$caught_message = $e
->getMessage();
}
$this
->assertContains('Empty or invalid user_id', $caught_message);
}
public function testThatGetEnrollmentsRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->getEnrollments('__test_user_id__');
$this
->assertEquals('GET', $api
->getHistoryMethod());
$this
->assertEquals('https://api.test.local/api/v2/users/__test_user_id__/enrollments', $api
->getHistoryUrl());
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
}
public function testThatGetPermissionsThrowsExceptionIfUserIdIsMissing() {
$api = new MockManagementApi();
try {
$api
->call()
->users()
->getPermissions('');
$caught_message = '';
} catch (EmptyOrInvalidParameterException $e) {
$caught_message = $e
->getMessage();
}
$this
->assertContains('Empty or invalid user_id', $caught_message);
}
public function testThatGetPermissionsRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->getPermissions('__test_user_id__', [
'per_page' => 3,
'page' => 2,
'include_totals' => 0,
]);
$this
->assertEquals('GET', $api
->getHistoryMethod());
$this
->assertStringStartsWith('https://api.test.local/api/v2/users/__test_user_id__/permissions?', $api
->getHistoryUrl());
$query = $api
->getHistoryQuery();
$this
->assertContains('per_page=3', $query);
$this
->assertContains('page=2', $query);
$this
->assertContains('include_totals=false', $query);
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
}
public function testThatRemovePermissionsThrowsExceptionIfUserIdIsMissing() {
$api = new MockManagementApi();
try {
$api
->call()
->users()
->removePermissions('', []);
$caught_message = '';
} catch (EmptyOrInvalidParameterException $e) {
$caught_message = $e
->getMessage();
}
$this
->assertContains('Empty or invalid user_id', $caught_message);
}
public function testThatRemovePermissionsThrowsExceptionIfPermissionsAreMissing() {
$api = new MockManagementApi();
try {
$api
->call()
->users()
->removePermissions('__test_user_id__', []);
$caught_exception = false;
} catch (InvalidPermissionsArrayException $e) {
$caught_exception = true;
}
$this
->assertTrue($caught_exception);
}
public function testThatRemovePermissionsRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->removePermissions('__test_user_id__', [
[
'permission_name' => 'test:permission',
'resource_server_identifier' => '__test_api_id__',
],
]);
$this
->assertEquals('DELETE', $api
->getHistoryMethod());
$this
->assertEquals('https://api.test.local/api/v2/users/__test_user_id__/permissions', $api
->getHistoryUrl());
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
$this
->assertEquals('application/json', $headers['Content-Type'][0]);
$body = $api
->getHistoryBody();
$this
->assertArrayHasKey('permissions', $body);
$this
->assertCount(1, $body['permissions']);
$this
->assertArrayHasKey('permission_name', $body['permissions'][0]);
$this
->assertEquals('test:permission', $body['permissions'][0]['permission_name']);
$this
->assertArrayHasKey('resource_server_identifier', $body['permissions'][0]);
$this
->assertEquals('__test_api_id__', $body['permissions'][0]['resource_server_identifier']);
}
public function testThatAddPermissionsThrowsExceptionIfUserIdIsMissing() {
$api = new MockManagementApi();
try {
$api
->call()
->users()
->addPermissions('', []);
$caught_message = '';
} catch (EmptyOrInvalidParameterException $e) {
$caught_message = $e
->getMessage();
}
$this
->assertContains('Empty or invalid user_id', $caught_message);
}
public function testThatAddPermissionsThrowsExceptionIfPermissionsAreMissing() {
$api = new MockManagementApi();
try {
$api
->call()
->users()
->addPermissions('__test_user_id__', []);
$caught_exception = false;
} catch (InvalidPermissionsArrayException $e) {
$caught_exception = true;
}
$this
->assertTrue($caught_exception);
}
public function testThatAddPermissionsRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->addPermissions('__test_user_id__', [
[
'permission_name' => 'test:permission',
'resource_server_identifier' => '__test_api_id__',
],
]);
$this
->assertEquals('POST', $api
->getHistoryMethod());
$this
->assertEquals('https://api.test.local/api/v2/users/__test_user_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', $body['permissions'][0]['permission_name']);
$this
->assertArrayHasKey('resource_server_identifier', $body['permissions'][0]);
$this
->assertEquals('__test_api_id__', $body['permissions'][0]['resource_server_identifier']);
}
public function testThatGetLogsThrowsExceptionIfUserIdIsMissing() {
$api = new MockManagementApi();
try {
$api
->call()
->users()
->getLogs('');
$caught_message = '';
} catch (EmptyOrInvalidParameterException $e) {
$caught_message = $e
->getMessage();
}
$this
->assertContains('Empty or invalid user_id', $caught_message);
}
public function testThatGetLogsRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->getLogs('__test_user_id__', [
'per_page' => 3,
'page' => 2,
'include_totals' => 0,
'fields' => 'date,type,ip',
]);
$this
->assertEquals('GET', $api
->getHistoryMethod());
$this
->assertStringStartsWith('https://api.test.local/api/v2/users/__test_user_id__/logs?', $api
->getHistoryUrl());
$query = $api
->getHistoryQuery();
$this
->assertContains('per_page=3', $query);
$this
->assertContains('page=2', $query);
$this
->assertContains('include_totals=false', $query);
$this
->assertContains('fields=date,type,ip', $query);
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
}
public function testThatGenerateRecoveryCodeThrowsExceptionIfUserIdIsMissing() {
$api = new MockManagementApi();
try {
$api
->call()
->users()
->generateRecoveryCode('');
$caught_message = '';
} catch (EmptyOrInvalidParameterException $e) {
$caught_message = $e
->getMessage();
}
$this
->assertContains('Empty or invalid user_id', $caught_message);
}
public function testThatGenerateRecoveryCodeRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->generateRecoveryCode('__test_user_id__');
$this
->assertEquals('POST', $api
->getHistoryMethod());
$this
->assertEquals('https://api.test.local/api/v2/users/__test_user_id__/recovery-code-regeneration', $api
->getHistoryUrl());
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
$this
->assertEquals('application/json', $headers['Content-Type'][0]);
}
public function testThatInvalidateBrowsersThrowsExceptionIfUserIdIsMissing() {
$api = new MockManagementApi();
try {
$api
->call()
->users()
->invalidateBrowsers('');
$caught_message = '';
} catch (EmptyOrInvalidParameterException $e) {
$caught_message = $e
->getMessage();
}
$this
->assertContains('Empty or invalid user_id', $caught_message);
}
public function testThatInvalidateBrowsersRequestIsFormattedProperly() {
$api = new MockManagementApi([
new Response(200, self::$headers),
]);
$api
->call()
->users()
->invalidateBrowsers('__test_user_id__');
$this
->assertEquals('POST', $api
->getHistoryMethod());
$this
->assertEquals('https://api.test.local/api/v2/users/__test_user_id__/multifactor/actions/invalidate-remember-browser', $api
->getHistoryUrl());
$headers = $api
->getHistoryHeaders();
$this
->assertEquals('Bearer __api_token__', $headers['Authorization'][0]);
$this
->assertEquals(self::$expectedTelemetry, $headers['Auth0-Client'][0]);
$this
->assertEquals('application/json', $headers['Content-Type'][0]);
}
}