Skip to content

Commit 58182fb

Browse files
authored
Export users does not require connection_id (#537)
1 parent 682204d commit 58182fb

2 files changed

Lines changed: 142 additions & 10 deletions

File tree

src/main/java/com/auth0/client/mgmt/JobsEntity.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class JobsEntity extends BaseManagementEntity {
3838

3939
/**
4040
* Request a Job. A token with scope create:users is needed.
41-
* See https://auth0.com/docs/api/management/v2#!/Jobs/get_jobs_by_id.
41+
* See <a href="https://auth0.com/docs/api/management/v2#!/Jobs/get_jobs_by_id">GET Jobs by ID</a>.
4242
*
4343
* @param jobId the id of the job to retrieve.
4444
* @return a Request to execute.
@@ -59,7 +59,7 @@ public Request<Job> get(String jobId) {
5959

6060
/**
6161
* Get error details of a failed job. A token with scope create:users is needed.
62-
* See https://auth0.com/docs/api/management/v2#!/Jobs/get_errors.
62+
* See <a href="https://auth0.com/docs/api/management/v2#!/Jobs/get_errors">GET Job Errors</a>.
6363
*
6464
* @param jobId the id of the job to retrieve.
6565
* @return a Request to execute.
@@ -90,7 +90,7 @@ protected List<JobErrorDetails> readResponseBody(Auth0HttpResponse response) thr
9090

9191
/**
9292
* Sends an Email Verification. A token with scope update:users is needed.
93-
* See https://auth0.com/docs/api/management/v2#!/Jobs/post_verification_email.
93+
* See <a href="https://auth0.com/docs/api/management/v2#!/Jobs/post_verification_email">POST Verification Email</a>.
9494
*
9595
* @param userId The user_id of the user to whom the email will be sent.
9696
* @param clientId The id of the client, if not provided the global one will be used.
@@ -105,7 +105,7 @@ public Request<Job> sendVerificationEmail(String userId, String clientId) {
105105

106106
/**
107107
* Sends an Email Verification. A token with scope update:users is needed.
108-
* See https://auth0.com/docs/api/management/v2#!/Jobs/post_verification_email.
108+
* See <a href="https://auth0.com/docs/api/management/v2#!/Jobs/post_verification_email">POST Verification Email</a>.
109109
*
110110
* @param userId The user_id of the user to whom the email will be sent.
111111
* @param clientId The id of the client, if not provided the global one will be used.
@@ -121,7 +121,7 @@ public Request<Job> sendVerificationEmail(String userId, String clientId, EmailV
121121

122122
/**
123123
* Sends an Email Verification. A token with scope update:users is needed.
124-
* See https://auth0.com/docs/api/management/v2#!/Jobs/post_verification_email.
124+
* See <a href="https://auth0.com/docs/api/management/v2#!/Jobs/post_verification_email">POST Verification Email</a>.
125125
*
126126
* @param userId The user_id of the user to whom the email will be sent.
127127
* @param clientId The id of the client, if not provided the global one will be used.
@@ -161,14 +161,17 @@ public Request<Job> sendVerificationEmail(String userId, String clientId, EmailV
161161
}
162162

163163
/**
164+
* @deprecated Use {@link #exportUsers(UsersExportFilter)} instead.
165+
*
164166
* Requests a Users Exports job. A token with scope read:users is needed.
165-
* See https://auth0.com/docs/api/management/v2#!/Jobs/post_users_exports.
166-
* See https://auth0.com/docs/users/guides/bulk-user-exports.
167+
* See <a href="https://auth0.com/docs/api/management/v2#!/Jobs/post_users_exports">POST Users Exports</a>.
168+
* See <a href="https://auth0.com/docs/users/guides/bulk-user-exports">Bulk User Exports</a>.
167169
*
168170
* @param connectionId The id of the connection to export the users from.
169171
* @param filter the filter to use. Can be null.
170172
* @return a Request to execute.
171173
*/
174+
@Deprecated
172175
public Request<Job> exportUsers(String connectionId, UsersExportFilter filter) {
173176
Asserts.assertNotNull(connectionId, "connection id");
174177

@@ -190,11 +193,36 @@ public Request<Job> exportUsers(String connectionId, UsersExportFilter filter) {
190193
return request;
191194
}
192195

196+
/**
197+
* Requests a Users Exports job. A token with scope read:users is needed.
198+
* See <a href="https://auth0.com/docs/api/management/v2#!/Jobs/post_users_exports">POST Users Exports</a>.
199+
* See <a href="https://auth0.com/docs/users/guides/bulk-user-exports">Bulk User Exports</a>.
200+
*
201+
* @param filter the filter to use. Can be null.
202+
* @return a Request to execute.
203+
*/
204+
public Request<Job> exportUsers(UsersExportFilter filter) {
205+
String url = baseUrl
206+
.newBuilder()
207+
.addPathSegments("api/v2/jobs/users-exports")
208+
.build()
209+
.toString();
210+
211+
Map<String, Object> requestBody = new HashMap<>();
212+
if (filter != null) {
213+
requestBody.putAll(filter.getAsMap());
214+
}
215+
216+
BaseRequest<Job> request = new BaseRequest<>(client, tokenProvider, url, HttpMethod.POST, new TypeReference<Job>() {
217+
});
218+
request.setBody(requestBody);
219+
return request;
220+
}
193221

194222
/**
195223
* Requests a Users Imports job. A token with scope write:users is needed.
196-
* See https://auth0.com/docs/api/management/v2#!/Jobs/post_users_imports.
197-
* See https://auth0.com/docs/users/guides/bulk-user-imports.
224+
* See <a href="https://auth0.com/docs/api/management/v2#!/Jobs/post_users_imports">POST User Imports</a>.
225+
* See <a href="https://auth0.com/docs/users/guides/bulk-user-imports">Bulk User Imports</a>.
198226
*
199227
* @param connectionId The id of the connection to import the users to.
200228
* @param users The users file. Must have an array with the users' information in JSON format.

src/test/java/com/auth0/client/mgmt/JobsEntityTest.java

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.auth0.json.mgmt.jobs.JobErrorDetails;
88
import com.auth0.json.mgmt.jobs.UsersExportField;
99
import com.auth0.net.Request;
10+
import com.auth0.net.Response;
1011
import com.auth0.net.client.HttpMethod;
1112
import com.auth0.net.client.multipart.FilePart;
1213
import com.auth0.net.client.multipart.KeyValuePart;
@@ -100,18 +101,21 @@ public void shouldGetJobErrorDetails_noErrors() throws Exception {
100101
}
101102

102103
@Test
104+
@SuppressWarnings("deprecation")
103105
public void shouldThrowOnRequestUsersExportWithNullConnectionId() {
104106
exception.expect(IllegalArgumentException.class);
105107
exception.expectMessage("'connection id' cannot be null!");
106108
api.jobs().exportUsers(null, null);
107109
}
108110

109111
@Test
112+
@SuppressWarnings("deprecation")
110113
public void shouldNotThrowOnRequestUsersExportWithNullFilter() {
111114
api.jobs().exportUsers("con_123456789", null);
112115
}
113116

114117
@Test
118+
@SuppressWarnings("deprecation")
115119
public void shouldRequestUsersExport() throws Exception {
116120
Request<Job> request = api.jobs().exportUsers("con_123456789", null);
117121
assertThat(request, is(notNullValue()));
@@ -132,6 +136,105 @@ public void shouldRequestUsersExport() throws Exception {
132136
}
133137

134138
@Test
139+
@SuppressWarnings("deprecation")
140+
public void usersExportWithFilterOverridesClientId() throws Exception {
141+
UsersExportFilter filter = new UsersExportFilter();
142+
filter.withConnectionId("filter_con");
143+
144+
Request<Job> request = api.jobs().exportUsers("con_123456789", filter);
145+
assertThat(request, is(notNullValue()));
146+
147+
server.jsonResponse(MGMT_JOB_POST_USERS_EXPORTS, 200);
148+
Job response = request.execute().getBody();
149+
RecordedRequest recordedRequest = server.takeRequest();
150+
151+
assertThat(recordedRequest, hasMethodAndPath(HttpMethod.POST, "/api/v2/jobs/users-exports"));
152+
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
153+
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
154+
155+
Map<String, Object> body = bodyFromRequest(recordedRequest);
156+
assertThat(body.size(), is(1));
157+
assertThat(body, hasEntry("connection_id", "filter_con"));
158+
159+
assertThat(response, is(notNullValue()));
160+
}
161+
162+
@Test
163+
public void shouldRequestUsersExportWithoutConnectionOrFilter() throws Exception {
164+
Request<Job> request = api.jobs().exportUsers(null);
165+
assertThat(request, is(notNullValue()));
166+
167+
server.jsonResponse(MGMT_JOB_POST_USERS_EXPORTS, 200);
168+
Job response = request.execute().getBody();
169+
RecordedRequest recordedRequest = server.takeRequest();
170+
171+
assertThat(recordedRequest, hasMethodAndPath(HttpMethod.POST, "/api/v2/jobs/users-exports"));
172+
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
173+
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
174+
175+
Map<String, Object> body = bodyFromRequest(recordedRequest);
176+
assertThat(body.size(), is(0));
177+
178+
assertThat(response, is(notNullValue()));
179+
}
180+
181+
@Test
182+
public void shouldRequestUsersExportWithFilter() throws Exception {
183+
UsersExportFilter filter = new UsersExportFilter();
184+
filter.withConnectionId("conId");
185+
filter.withFormat("json");
186+
List<UsersExportField> fields = new ArrayList<>();
187+
fields.add(new UsersExportField("full_name"));
188+
fields.add(new UsersExportField("user_metadata.company_name", "company"));
189+
filter.withFields(fields);
190+
191+
Request<Job> request = api.jobs().exportUsers(filter);
192+
assertThat(request, is(notNullValue()));
193+
194+
server.jsonResponse(MGMT_JOB_POST_USERS_EXPORTS, 200);
195+
Job response = request.execute().getBody();
196+
RecordedRequest recordedRequest = server.takeRequest();
197+
198+
assertThat(recordedRequest, hasMethodAndPath(HttpMethod.POST, "/api/v2/jobs/users-exports"));
199+
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
200+
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
201+
202+
Map<String, Object> body = bodyFromRequest(recordedRequest);
203+
assertThat(body.size(), is(3));
204+
assertThat(body, hasEntry("connection_id", "conId"));
205+
assertThat(body, hasEntry("format", "json"));
206+
assertThat(body, hasKey("fields"));
207+
@SuppressWarnings("unchecked")
208+
List<Map<String, String>> bodyFields = (List<Map<String, String>>) body.get("fields");
209+
assertThat(bodyFields.get(0).get("name"), is("full_name"));
210+
assertThat(bodyFields.get(0).get("export_as"), is(nullValue()));
211+
assertThat(bodyFields.get(1).get("name"), is("user_metadata.company_name"));
212+
assertThat(bodyFields.get(1).get("export_as"), is("company"));
213+
214+
assertThat(response, is(notNullValue()));
215+
}
216+
217+
@Test
218+
public void usersExportShouldHandleNullFilter() throws Exception {
219+
Request<Job> request = api.jobs().exportUsers(null);
220+
assertThat(request, is(notNullValue()));
221+
222+
server.jsonResponse(MGMT_JOB_POST_USERS_EXPORTS, 200);
223+
Job response = request.execute().getBody();
224+
RecordedRequest recordedRequest = server.takeRequest();
225+
226+
assertThat(recordedRequest, hasMethodAndPath(HttpMethod.POST, "/api/v2/jobs/users-exports"));
227+
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
228+
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
229+
230+
Map<String, Object> body = bodyFromRequest(recordedRequest);
231+
assertThat(body.size(), is(0));
232+
233+
assertThat(response, is(notNullValue()));
234+
}
235+
236+
@Test
237+
@SuppressWarnings("deprecation")
135238
public void shouldRequestUsersExportWithLimit() throws Exception {
136239
UsersExportFilter filter = new UsersExportFilter();
137240
filter.withLimit(82);
@@ -155,6 +258,7 @@ public void shouldRequestUsersExportWithLimit() throws Exception {
155258
}
156259

157260
@Test
261+
@SuppressWarnings("deprecation")
158262
public void shouldRequestUsersExportWithFormat() throws Exception {
159263
UsersExportFilter filter = new UsersExportFilter();
160264
filter.withFormat("csv");
@@ -178,6 +282,7 @@ public void shouldRequestUsersExportWithFormat() throws Exception {
178282
}
179283

180284
@Test
285+
@SuppressWarnings("deprecation")
181286
public void shouldRequestUsersExportWithFields() throws Exception {
182287
UsersExportFilter filter = new UsersExportFilter();
183288
ArrayList<UsersExportField> fields = new ArrayList<>();
@@ -437,5 +542,4 @@ public void shouldRequestUsersImportWithOptions() throws Exception {
437542

438543
assertThat(response, is(notNullValue()));
439544
}
440-
441545
}

0 commit comments

Comments
 (0)