-
Notifications
You must be signed in to change notification settings - Fork 216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Jobs endpoint tests #365
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
<?php | ||
namespace Auth0\Tests\API\Management; | ||
|
||
use Auth0\SDK\API\Helpers\InformationHeaders; | ||
use Auth0\SDK\API\Management; | ||
use Auth0\Tests\API\ApiTests; | ||
use GuzzleHttp\Psr7\Response; | ||
|
||
class JobsTest extends ApiTests | ||
{ | ||
|
||
const FORM_DATA_VALUE_KEY_OFFSET = 3; | ||
|
||
const TEST_IMPORT_USERS_JSON_PATH = AUTH0_PHP_TEST_JSON_DIR.'test-import-users-file.json'; | ||
|
||
/** | ||
* 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(); | ||
} | ||
|
||
/** | ||
* @throws \Exception Should not be thrown in this test. | ||
*/ | ||
public function testThatGetRequestIsFormedProperly() | ||
{ | ||
$api = new MockManagementApi( [ new Response( 200, self::$headers ) ] ); | ||
|
||
$api->call()->jobs->get( '__test_id__' ); | ||
|
||
$this->assertEquals( 'GET', $api->getHistoryMethod() ); | ||
$this->assertEquals( 'https://api.test.local/api/v2/jobs/__test_id__', $api->getHistoryUrl() ); | ||
|
||
$headers = $api->getHistoryHeaders(); | ||
$this->assertEquals( 'Bearer __api_token__', $headers['Authorization'][0] ); | ||
$this->assertEquals( self::$expectedTelemetry, $headers['Auth0-Client'][0] ); | ||
} | ||
|
||
/** | ||
* @throws \Exception Should not be thrown in this test. | ||
*/ | ||
public function testThatGetErrorsIsFormedProperly() | ||
{ | ||
$api = new MockManagementApi( [ new Response( 200, self::$headers ) ] ); | ||
|
||
$api->call()->jobs->getErrors( '__test_id__' ); | ||
|
||
$this->assertEquals( 'GET', $api->getHistoryMethod() ); | ||
$this->assertEquals( 'https://api.test.local/api/v2/jobs/__test_id__/errors', $api->getHistoryUrl() ); | ||
|
||
$headers = $api->getHistoryHeaders(); | ||
$this->assertEquals( 'Bearer __api_token__', $headers['Authorization'][0] ); | ||
$this->assertEquals( self::$expectedTelemetry, $headers['Auth0-Client'][0] ); | ||
} | ||
|
||
/** | ||
* @throws \Exception Should not be thrown in this test. | ||
*/ | ||
public function testThatImportUsersRequestIsFormedProperly() | ||
{ | ||
$api = new MockManagementApi( [ new Response( 200, self::$headers ) ] ); | ||
|
||
$api->call()->jobs->importUsers( | ||
self::TEST_IMPORT_USERS_JSON_PATH, | ||
'__test_conn_id__', | ||
[ | ||
'upsert' => true, | ||
'send_completion_email' => true, | ||
'external_id' => '__test_ext_id__', | ||
] | ||
); | ||
|
||
$this->assertEquals( 'POST', $api->getHistoryMethod() ); | ||
$this->assertEquals( 'https://api.test.local/api/v2/jobs/users-imports', $api->getHistoryUrl() ); | ||
|
||
$headers = $api->getHistoryHeaders(); | ||
$this->assertEquals( 'Bearer __api_token__', $headers['Authorization'][0] ); | ||
$this->assertEquals( self::$expectedTelemetry, $headers['Auth0-Client'][0] ); | ||
$this->assertStringStartsWith( 'multipart/form-data', $headers['Content-Type'][0] ); | ||
|
||
$form_body = $api->getHistoryBodyAsString(); | ||
$form_body_arr = explode( "\r\n", $form_body ); | ||
|
||
// Test that the form data contains our import file content. | ||
$import_content = file_get_contents( self::TEST_IMPORT_USERS_JSON_PATH ); | ||
$this->assertContains( 'name="users"; filename="test-import-users-file.json"', $form_body ); | ||
$this->assertContains( $import_content, $form_body ); | ||
|
||
$conn_id_key = array_search( 'Content-Disposition: form-data; name="connection_id"', $form_body_arr ); | ||
$this->assertNotEmpty( $conn_id_key ); | ||
$this->assertEquals( '__test_conn_id__', $form_body_arr[$conn_id_key + self::FORM_DATA_VALUE_KEY_OFFSET] ); | ||
|
||
$upsert_key = array_search( 'Content-Disposition: form-data; name="upsert"', $form_body_arr ); | ||
$this->assertNotEmpty( $upsert_key ); | ||
$this->assertEquals( 'true', $form_body_arr[$upsert_key + self::FORM_DATA_VALUE_KEY_OFFSET] ); | ||
|
||
$email_key = array_search( 'Content-Disposition: form-data; name="send_completion_email"', $form_body_arr ); | ||
$this->assertNotEmpty( $email_key ); | ||
$this->assertEquals( 'true', $form_body_arr[$email_key + self::FORM_DATA_VALUE_KEY_OFFSET] ); | ||
|
||
$ext_id_key = array_search( 'Content-Disposition: form-data; name="external_id"', $form_body_arr ); | ||
$this->assertNotEmpty( $ext_id_key ); | ||
$this->assertEquals( '__test_ext_id__', $form_body_arr[$ext_id_key + self::FORM_DATA_VALUE_KEY_OFFSET] ); | ||
} | ||
|
||
|
||
/** | ||
* @throws \Exception Should not be thrown in this test. | ||
*/ | ||
public function testThatSendVerificationEmailIsFormedProperly() | ||
{ | ||
$api = new MockManagementApi( [ new Response( 200, self::$headers ) ] ); | ||
|
||
$api->call()->jobs->sendVerificationEmail( '__test_user_id__' ); | ||
|
||
$this->assertEquals( 'POST', $api->getHistoryMethod() ); | ||
$this->assertEquals( 'https://api.test.local/api/v2/jobs/verification-email', $api->getHistoryUrl() ); | ||
$this->assertEmpty( $api->getHistoryQuery() ); | ||
|
||
$body = $api->getHistoryBody(); | ||
$this->assertArrayHasKey( 'user_id', $body ); | ||
$this->assertEquals( '__test_user_id__', $body['user_id'] ); | ||
|
||
$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] ); | ||
} | ||
|
||
/** | ||
* @throws \Auth0\SDK\Exception\ApiException | ||
* @throws \Exception | ||
*/ | ||
public function testIntegrationImportUsersJob() | ||
{ | ||
$env = self::getEnv(); | ||
|
||
if (! $env['API_TOKEN']) { | ||
$this->markTestSkipped( 'No client secret; integration test skipped' ); | ||
joshcanhelp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
$api = new Management($env['API_TOKEN'], $env['DOMAIN']); | ||
|
||
// 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] ); | ||
sleep(0.2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this required? We should be lowering the time each run takes, because of the CI OSS plan limits. 🧀 🐀 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Management API limits |
||
|
||
$conn_id = $get_connection_result[0]['id']; | ||
$import_user_params = [ | ||
'upsert' => true, | ||
'send_completion_email' => false, | ||
'external_id' => '__test_ext_id__', | ||
]; | ||
|
||
$import_job_result = $api->jobs->importUsers(self::TEST_IMPORT_USERS_JSON_PATH, $conn_id, $import_user_params); | ||
sleep(0.2); | ||
|
||
$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']); | ||
sleep(0.2); | ||
|
||
$this->assertEquals( $conn_id, $get_job_result['connection_id'] ); | ||
$this->assertEquals( $default_db_name, $get_job_result['connection'] ); | ||
$this->assertEquals( '__test_ext_id__', $get_job_result['external_id'] ); | ||
$this->assertEquals( 'users_import', $get_job_result['type'] ); | ||
} | ||
|
||
/** | ||
* @throws \Auth0\SDK\Exception\ApiException | ||
* @throws \Exception | ||
*/ | ||
public function testIntegrationSendEmailVerificationJob() | ||
{ | ||
$env = self::getEnv(); | ||
|
||
if (! $env['API_TOKEN']) { | ||
$this->markTestSkipped( 'No client secret; integration test skipped' ); | ||
} | ||
|
||
$api = new Management($env['API_TOKEN'], $env['DOMAIN']); | ||
|
||
$create_user_data = [ | ||
'connection' => 'Username-Password-Authentication', | ||
'email' => 'php-sdk-test-email-verification-job-' . uniqid() . '@auth0.com', | ||
'password' => uniqid().uniqid().uniqid(), | ||
]; | ||
$create_user_result = $api->users->create( $create_user_data ); | ||
sleep(0.2); | ||
|
||
$user_id = $create_user_result['user_id']; | ||
|
||
$email_job_result = $api->jobs->sendVerificationEmail($user_id); | ||
sleep(0.2); | ||
|
||
$this->assertEquals( 'verification_email', $email_job_result['type'] ); | ||
|
||
$get_job_result = $api->jobs->get($email_job_result['id']); | ||
sleep(0.2); | ||
|
||
$this->assertEquals( 'verification_email', $get_job_result['type'] ); | ||
|
||
$api->users->delete( $user_id ); | ||
sleep(0.2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[ | ||
{ | ||
"email": "php-sdk-test-import-user-job@auth0.com", | ||
"email_verified": true, | ||
"app_metadata": { | ||
"roles": ["admin", "super"], | ||
"plan": "premium" | ||
}, | ||
"user_metadata": { | ||
"theme": "dark" | ||
joshcanhelp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why starts with vs equals? also, curious on why this request is using form vs body?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The URL includes the query parameters and I just want the URL here.
Import users endpoint requires a file to be posted.