Skip to content

Commit f3d694b

Browse files
author
Evan Sims
authored
[SDK-1514] Implement Management V2 users export job support (#461)
* Implement Management V2 users export job support * Update function docblocks in jobs class * Add test units for user export job
1 parent 80c2d8a commit f3d694b

File tree

3 files changed

+122
-7
lines changed

3 files changed

+122
-7
lines changed

src/API/Helpers/RequestBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ class RequestBuilder
3939
/**
4040
* HTTP method to use for the request.
4141
*
42-
* @var array
42+
* @var string
4343
*/
44-
protected $method = [];
44+
protected $method;
4545

4646
/**
4747
* Headers to include for the request.

src/API/Management/Jobs.php

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@
77
class Jobs extends GenericResource
88
{
99
/**
10+
* Retrieves a job. Useful to check its status.
11+
* Required scopes: "create:users" "read:users"
12+
*
13+
* @param string $id
14+
*
15+
* @throws \Exception Thrown by the HTTP client when there is a problem with the API call.
1016
*
11-
* @param string $id
1217
* @return mixed
18+
*
19+
* @see https://auth0.com/docs/api/management/v2#!/Jobs/get_jobs_by_id
1320
*/
1421
public function get($id)
1522
{
@@ -19,9 +26,16 @@ public function get($id)
1926
}
2027

2128
/**
29+
* Retrieve error details of a failed job.
30+
* Required scopes: "create:users" "read:users"
31+
*
32+
* @param string $id
33+
*
34+
* @throws \Exception Thrown by the HTTP client when there is a problem with the API call.
2235
*
23-
* @param string $id
2436
* @return mixed
37+
*
38+
* @see https://auth0.com/docs/api/management/v2#!/Jobs/get_errors
2539
*/
2640
public function getErrors($id)
2741
{
@@ -31,11 +45,18 @@ public function getErrors($id)
3145
}
3246

3347
/**
48+
* Import users from a formatted file into a connection via a long-running job.
49+
* Required scopes: "create:users" "read:users"
50+
*
51+
* @param string $file_path
52+
* @param string $connection_id
53+
* @param array $params
54+
*
55+
* @throws \Exception Thrown by the HTTP client when there is a problem with the API call.
3456
*
35-
* @param string $file_path
36-
* @param string $connection_id
37-
* @param array $params
3857
* @return mixed
58+
*
59+
* @see https://auth0.com/docs/api/management/v2#!/Jobs/post_users_imports
3960
*/
4061
public function importUsers($file_path, $connection_id, $params = [])
4162
{
@@ -59,6 +80,45 @@ public function importUsers($file_path, $connection_id, $params = [])
5980
return $request->call();
6081
}
6182

83+
84+
/**
85+
* Export all users to a file via a long-running job.
86+
* Required scopes: "read:users"
87+
*
88+
* @param array $params
89+
*
90+
* @throws \Exception Thrown by the HTTP client when there is a problem with the API call.
91+
*
92+
* @return mixed
93+
*
94+
* @see https://auth0.com/docs/api/management/v2#!/Jobs/post_users_exports
95+
*/
96+
public function exportUsers($params = [])
97+
{
98+
$request = $this->apiClient->method('post')
99+
->addPath('jobs', 'users-exports');
100+
101+
$body = [];
102+
103+
if (!empty($params['connection_id'])) {
104+
$body['connection_id'] = $params['connection_id'];
105+
}
106+
107+
if (!empty($params['format']) && in_array($params['format'], ['json', 'csv'])) {
108+
$body['format'] = $params['format'];
109+
}
110+
111+
if (!empty($params['limit']) && is_numeric($params['limit'])) {
112+
$body['limit'] = $params['limit'];
113+
}
114+
115+
if (!empty($params['fields']) && is_array($params['fields'])) {
116+
$body['fields'] = $params['fields'];
117+
}
118+
119+
return $request->withBody(json_encode($body), JSON_FORCE_OBJECT)->call();
120+
}
121+
62122
/**
63123
* Create a verification email job.
64124
* Required scope: "update:users"

tests/unit/API/Management/JobsTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,61 @@ public function testThatImportUsersRequestIsFormedProperly()
127127
$this->assertEquals( '__test_ext_id__', $form_body_arr[$ext_id_key + self::FORM_DATA_VALUE_KEY_OFFSET] );
128128
}
129129

130+
/**
131+
* @throws \Exception Should not be thrown in this test.
132+
*/
133+
public function testThatExportUsersRequestIsFormedProperly()
134+
{
135+
$api = new MockManagementApi( [ new Response( 200, self::$headers ) ] );
136+
137+
$api->call()->jobs()->exportUsers(
138+
[
139+
'connection_id' => '__test_conn_id__',
140+
'limit' => 5,
141+
'format' => 'json',
142+
'fields' => [['name' => 'user_id']],
143+
]
144+
);
145+
146+
$this->assertEquals( 'POST', $api->getHistoryMethod() );
147+
$this->assertEquals( 'https://api.test.local/api/v2/jobs/users-exports', $api->getHistoryUrl() );
148+
$this->assertEmpty( $api->getHistoryQuery() );
149+
150+
$request_body = $api->getHistoryBody();
151+
152+
$this->assertNotEmpty( $request_body['connection_id'] );
153+
$this->assertEquals( '__test_conn_id__', $request_body['connection_id'] );
154+
155+
$this->assertNotEmpty( $request_body['limit'] );
156+
$this->assertEquals( '5', $request_body['limit'] );
157+
158+
$this->assertNotEmpty( $request_body['format'] );
159+
$this->assertEquals( 'json', $request_body['format'] );
160+
161+
$this->assertNotEmpty( $request_body['fields'] );
162+
$this->assertEquals( [['name' => 'user_id']], $request_body['fields'] );
163+
}
164+
165+
/**
166+
* @throws \Exception Should not be thrown in this test.
167+
*/
168+
public function testThatExportUsersRequestIsFilteringProperly()
169+
{
170+
$api = new MockManagementApi( [ new Response( 200, self::$headers ) ] );
171+
172+
$api->call()->jobs()->exportUsers(
173+
[
174+
'connection_id' => '__test_conn_id__',
175+
'limit' => 'invalid limit',
176+
'format' => 'invalid format',
177+
]
178+
);
179+
180+
$request_body = $api->getHistoryBody();
181+
182+
$this->assertArrayNotHasKey('limit', $request_body);
183+
$this->assertArrayNotHasKey('format', $request_body);
184+
}
130185

131186
/**
132187
* @throws \Exception Should not be thrown in this test.

0 commit comments

Comments
 (0)