Skip to content

Commit

Permalink
tests: Allow overriding 0.2 delay in integration testing API requests (
Browse files Browse the repository at this point in the history
  • Loading branch information
evansims authored Apr 12, 2021
1 parent c1dc308 commit c65f174
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 94 deletions.
32 changes: 22 additions & 10 deletions tests/API/ApiTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,37 @@ protected static function getEnv()
}

$env_path = '.env';

if (file_exists($env_path)) {
$loader = new Loader($env_path);
$loader->parse()->putenv(true);
}

$env = [
'DOMAIN' => getenv('DOMAIN'),
'APP_CLIENT_ID' => getenv('APP_CLIENT_ID'),
'APP_CLIENT_SECRET' => getenv('APP_CLIENT_SECRET'),
'API_TOKEN' => getenv('API_TOKEN'),
$env = getenv();

self::$env = [
'DOMAIN' => $env['DOMAIN'] ?? false,
'APP_CLIENT_ID' => $env['APP_CLIENT_ID'] ?? false,
'APP_CLIENT_SECRET' => $env['APP_CLIENT_SECRET'] ?? false,
'API_TOKEN' => $env['API_TOKEN'] ?? false,
'AUTH0_API_REQUEST_DELAY' => (int) ($env['AUTH0_API_REQUEST_DELAY'] ?? 0),
];

if (! $env['API_TOKEN'] && $env['APP_CLIENT_SECRET']) {
$auth_api = new Authentication( $env['DOMAIN'], $env['APP_CLIENT_ID'], $env['APP_CLIENT_SECRET'] );
$response = $auth_api->client_credentials( [ 'audience' => 'https://'.$env['DOMAIN'].'/api/v2/' ] );
$env['API_TOKEN'] = $response['access_token'];
if (self::$env['AUTH0_API_REQUEST_DELAY'] <= 0) {
self::$env['AUTH0_API_REQUEST_DELAY'] = 200000;
}

if (! isset($env['API_TOKEN']) && $env['APP_CLIENT_SECRET']) {
$auth_api = new Authentication( $env['DOMAIN'], $env['APP_CLIENT_ID'], $env['APP_CLIENT_SECRET'] );
$response = $auth_api->client_credentials( [ 'audience' => 'https://'.$env['DOMAIN'].'/api/v2/' ] );
self::$env['API_TOKEN'] = $response['access_token'];
}

self::$env = $env;
return self::$env;
}

protected static function sleep(?int $microseconds = null)
{
usleep($microseconds ?? self::$env['AUTH0_API_REQUEST_DELAY']);
}
}
2 changes: 0 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
ini_set('session.use_cookies', false);
ini_set('session.cache_limiter', false);

define( 'AUTH0_PHP_TEST_INTEGRATION_SLEEP', 200000 );

if (! defined( 'AUTH0_PHP_TEST_JSON_DIR' )) {
define( 'AUTH0_PHP_TEST_JSON_DIR', $tests_dir.'json/' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public function testBlacklistAndGet()
$test_jti = uniqid().uniqid().uniqid();

$api->blacklists()->blacklist($env['APP_CLIENT_ID'], $test_jti);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

$blacklisted = $api->blacklists()->getAll($env['APP_CLIENT_ID']);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

$this->assertGreaterThan( 0, count( $blacklisted ) );
$this->assertEquals( $env['APP_CLIENT_ID'], $blacklisted[0]['aud'] );
Expand Down
25 changes: 14 additions & 11 deletions tests/integration/API/Management/ClientGrantsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function setUpBeforeClass(): void

self::$apiIdentifier = 'TEST_PHP_SDK_CLIENT_GRANT_API_'.uniqid();
$api->resourceServers()->create(self::$apiIdentifier, $create_data);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
self::sleep();
}

public function setUp(): void
Expand All @@ -73,7 +73,7 @@ public static function tearDownAfterClass(): void
parent::tearDownAfterClass();
$api = new Management(self::$env['API_TOKEN'], self::$env['DOMAIN']);
$api->resourceServers()->delete( self::$apiIdentifier );
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
self::sleep();
}

/**
Expand All @@ -87,7 +87,7 @@ public static function tearDownAfterClass(): void
public function testGet()
{
$all_results = self::$api->getAll();
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();
$this->assertNotEmpty($all_results);

$expected_client_id = $all_results[0]['client_id'] ?: null;
Expand All @@ -97,12 +97,12 @@ public function testGet()
$this->assertNotNull($expected_audience);

$audience_results = self::$api->getByAudience($expected_audience);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->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->sleep();
$this->assertNotEmpty($client_id_results);
$this->assertEquals($expected_client_id, $client_id_results[0]['client_id']);
}
Expand All @@ -119,12 +119,12 @@ public function testGetWithPagination()
$expected_count = 2;

$results_1 = self::$api->getAll([], 0, $expected_count);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->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->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']);
Expand All @@ -143,7 +143,8 @@ public function testGetAllIncludeTotals()
$expected_count = 2;

$results = self::$api->getAll(['include_totals' => true], $expected_page, $expected_count);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

$this->assertArrayHasKey('total', $results);
$this->assertEquals($expected_page * $expected_count, $results['start']);
$this->assertEquals($expected_count, $results['limit']);
Expand All @@ -165,7 +166,8 @@ public function testCreateUpdateDeleteGrant()

// Create a Client Grant with just one of the testing scopes.
$create_result = self::$api->create($client_id, $audience, [self::$scopes[0]]);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

$this->assertArrayHasKey('id', $create_result);
$this->assertEquals($client_id, $create_result['client_id']);
$this->assertEquals($audience, $create_result['audience']);
Expand All @@ -175,12 +177,13 @@ public function testCreateUpdateDeleteGrant()

// Test patching the created Client Grant.
$update_result = self::$api->update($grant_id, self::$scopes);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

$this->assertEquals(self::$scopes, $update_result['scope']);

// Test deleting the created Client Grant.
$delete_result = self::$api->delete($grant_id);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();
$this->assertNull($delete_result);
}

Expand Down
12 changes: 6 additions & 6 deletions tests/integration/API/Management/ClientsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public function testIntegrationCreateGetUpdateDelete()
];

$created_client = $api->clients()->create($create_body);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

$this->assertNotEmpty($created_client['client_id']);
$this->assertEquals($create_body['name'], $created_client['name']);
$this->assertEquals($create_body['app_type'], $created_client['app_type']);

$created_client_id = $created_client['client_id'];
$got_entity = $api->clients()->get($created_client_id);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

// Make sure what we got matches what we created.
$this->assertEquals($created_client_id, $got_entity['client_id']);
Expand All @@ -53,14 +53,14 @@ public function testIntegrationCreateGetUpdateDelete()
];

$updated_client = $api->clients()->update($created_client_id, $update_body );
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

$this->assertEquals($created_client_id, $updated_client['client_id']);
$this->assertEquals($update_body['name'], $updated_client['name']);
$this->assertEquals($update_body['app_type'], $updated_client['app_type']);

$api->clients()->delete($created_client_id);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();
}

/**
Expand All @@ -80,7 +80,7 @@ public function testIntegrationGetAllMethod()

// Get the second page of Clients with 1 per page (second result).
$paged_results = $api->clients()->getAll($fields, true, $page_num, 1);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

// Make sure we only have one result, as requested.
$this->assertEquals(1, count($paged_results));
Expand All @@ -91,7 +91,7 @@ public function testIntegrationGetAllMethod()
// Get many results (needs to include the created result if self::findCreatedItem === true).
$many_results_per_page = 50;
$many_results = $api->clients()->getAll($fields, true, 0, $many_results_per_page);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

// Make sure we have at least as many results as we requested.
$this->assertLessThanOrEqual($many_results_per_page, count($many_results));
Expand Down
14 changes: 7 additions & 7 deletions tests/integration/API/Management/JobsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testIntegrationImportUsersJob()
// Get a single, active database connection.
$default_db_name = 'Username-Password-Authentication';
$get_connection_result = $api->connections()->getAll( 'auth0', ['id'], true, 0, 1, ['name' => $default_db_name] );
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

$conn_id = $get_connection_result[0]['id'];
$import_user_params = [
Expand All @@ -50,15 +50,15 @@ public function testIntegrationImportUsersJob()
];

$import_job_result = $api->jobs()->importUsers(self::$testImportUsersJsonPath, $conn_id, $import_user_params);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

$this->assertEquals( $conn_id, $import_job_result['connection_id'] );
$this->assertEquals( $default_db_name, $import_job_result['connection'] );
$this->assertEquals( '__test_ext_id__', $import_job_result['external_id'] );
$this->assertEquals( 'users_import', $import_job_result['type'] );

$get_job_result = $api->jobs()->get($import_job_result['id']);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

$this->assertEquals( $conn_id, $get_job_result['connection_id'] );
$this->assertEquals( $default_db_name, $get_job_result['connection'] );
Expand Down Expand Up @@ -86,21 +86,21 @@ public function testIntegrationSendEmailVerificationJob()
'password' => uniqid().'&*@'.uniqid().uniqid(),
];
$create_user_result = $api->users()->create( $create_user_data );
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

$user_id = $create_user_result['user_id'];

$email_job_result = $api->jobs()->sendVerificationEmail($user_id);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

$this->assertEquals( 'verification_email', $email_job_result['type'] );

$get_job_result = $api->jobs()->get($email_job_result['id']);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

$this->assertEquals( 'verification_email', $get_job_result['type'] );

$api->users()->delete( $user_id );
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();
}
}
7 changes: 4 additions & 3 deletions tests/integration/API/Management/LogsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function testLogSearchAndGetById()
'fields' => '_id,log_id,date',
'include_fields' => true,
]);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

$this->assertNotEmpty($search_results);
$this->assertNotEmpty($search_results[0]['_id']);
$this->assertNotEmpty($search_results[0]['log_id']);
Expand All @@ -56,7 +57,7 @@ public function testLogSearchAndGetById()

// Test getting a single log result with a valid ID from above.
$one_log = self::$api->get($search_results[0]['log_id']);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();
$this->assertNotEmpty($one_log);
$this->assertEquals($search_results[0]['log_id'], $one_log['log_id']);
}
Expand All @@ -81,7 +82,7 @@ public function testLogSearchPagination()
// Include totals to check pagination.
'include_totals' => true,
]);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this->sleep();

$this->assertCount($expected_count, $search_results['logs']);
$this->assertEquals($expected_count, $search_results['length']);
Expand Down
Loading

0 comments on commit c65f174

Please sign in to comment.