Skip to content

Commit

Permalink
Fix double-encoding of log event filter query param (#420)
Browse files Browse the repository at this point in the history
* Fix double-encoding of LogEventFilter query

* code organization

* add test (fails without fix)
  • Loading branch information
jimmyjames authored Apr 12, 2022
1 parent c940044 commit 3a8e640
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 34 deletions.
34 changes: 16 additions & 18 deletions src/main/java/com/auth0/client/mgmt/UsersEntity.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.auth0.client.mgmt;

import com.auth0.client.mgmt.filter.FieldsFilter;
import com.auth0.client.mgmt.filter.LogEventFilter;
import com.auth0.client.mgmt.filter.PageFilter;
import com.auth0.client.mgmt.filter.UserFilter;
import com.auth0.client.mgmt.filter.*;
import com.auth0.json.mgmt.Permission;
import com.auth0.json.mgmt.PermissionsPage;
import com.auth0.json.mgmt.RolesPage;
Expand Down Expand Up @@ -86,15 +83,7 @@ public Request<UsersPage> list(UserFilter filter) {
HttpUrl.Builder builder = baseUrl
.newBuilder()
.addPathSegments("api/v2/users");
if (filter != null) {
for (Map.Entry<String, Object> e : filter.getAsMap().entrySet()) {
if (KEY_QUERY.equals(e.getKey())) {
builder.addEncodedQueryParameter(e.getKey(), String.valueOf(e.getValue()));
} else {
builder.addQueryParameter(e.getKey(), String.valueOf(e.getValue()));
}
}
}
encodeAndAddQueryParam(builder, filter);
String url = builder.build().toString();
CustomRequest<UsersPage> request = new CustomRequest<>(client, url, "GET", new TypeReference<UsersPage>() {
});
Expand Down Expand Up @@ -245,11 +234,8 @@ public Request<LogEventsPage> getLogEvents(String userId, LogEventFilter filter)
.addPathSegments("api/v2/users")
.addPathSegment(userId)
.addPathSegment("logs");
if (filter != null) {
for (Map.Entry<String, Object> e : filter.getAsMap().entrySet()) {
builder.addQueryParameter(e.getKey(), String.valueOf(e.getValue()));
}
}

encodeAndAddQueryParam(builder, filter);
String url = builder.build().toString();
CustomRequest<LogEventsPage> request = new CustomRequest<>(client, url, "GET", new TypeReference<LogEventsPage>() {
});
Expand Down Expand Up @@ -607,4 +593,16 @@ public Request<OrganizationsPage> getOrganizations(String userId, PageFilter fil
request.addHeader("Authorization", "Bearer " + apiToken);
return request;
}

private static void encodeAndAddQueryParam(HttpUrl.Builder builder, BaseFilter filter) {
if (filter != null) {
for (Map.Entry<String, Object> e : filter.getAsMap().entrySet()) {
if (KEY_QUERY.equals(e.getKey())) {
builder.addEncodedQueryParameter(e.getKey(), String.valueOf(e.getValue()));
} else {
builder.addQueryParameter(e.getKey(), String.valueOf(e.getValue()));
}
}
}
}
}
43 changes: 27 additions & 16 deletions src/test/java/com/auth0/client/mgmt/UsersEntityTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
package com.auth0.client.mgmt;

import static com.auth0.client.MockServer.*;
import static com.auth0.client.RecordedRequestMatcher.hasHeader;
import static com.auth0.client.RecordedRequestMatcher.hasMethodAndPath;
import static com.auth0.client.RecordedRequestMatcher.hasQueryParameter;
import static org.hamcrest.Matchers.emptyCollectionOf;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import com.auth0.client.mgmt.filter.FieldsFilter;
import com.auth0.client.mgmt.filter.LogEventFilter;
import com.auth0.client.mgmt.filter.PageFilter;
Expand All @@ -30,12 +16,18 @@
import com.auth0.json.mgmt.users.User;
import com.auth0.json.mgmt.users.UsersPage;
import com.auth0.net.Request;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Test;

import static com.auth0.client.MockServer.*;
import static com.auth0.client.RecordedRequestMatcher.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class UsersEntityTest extends BaseMgmtEntityTest {

Expand Down Expand Up @@ -404,6 +396,25 @@ public void shouldGetUserLogEvents() throws Exception {
assertThat(response, is(notNullValue()));
}

@Test
public void shouldGetUserLogEventsWithQuery() throws Exception {
LogEventFilter filter = new LogEventFilter().withQuery("date:[2022-03-13 TO *]");
Request<LogEventsPage> request = api.users().getLogEvents("1", filter);
assertThat(request, is(notNullValue()));

server.jsonResponse(MGMT_LOG_EVENTS_LIST, 200);
LogEventsPage response = request.execute();
RecordedRequest recordedRequest = server.takeRequest();

assertThat(recordedRequest, hasMethodAndPath("GET", "/api/v2/users/1/logs"));
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
assertThat(recordedRequest, hasQueryParameter("q", "date:[2022-03-13 TO *]"));

assertThat(response, is(notNullValue()));
assertThat(response.getItems(), hasSize(2));
}

@Test
public void shouldGetUserLogEventsWithPage() throws Exception {
LogEventFilter filter = new LogEventFilter().withPage(23, 5);
Expand Down

0 comments on commit 3a8e640

Please sign in to comment.