Skip to content

Commit ce6a43b

Browse files
committed
chore: Add Auth Client tests
1 parent 3226c91 commit ce6a43b

33 files changed

Lines changed: 3971 additions & 7 deletions

.fernignore

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,12 @@ src/main/java/com/auth0/client/LoggingOptions.java
3232
# TokenProvider interface (shared between Fern-generated Management API and auth0-provided Authentication API)
3333
src/main/java/com/auth0/client/mgmt/TokenProvider.java
3434

35-
# Test infrastructure from auth0-real
36-
src/test/java/com/auth0/net/
37-
src/test/java/com/auth0/exception/
38-
src/test/java/com/auth0/utils/
39-
src/test/java/com/auth0/json/
35+
# Shared test infrastructure for the Authentication API tests (manually maintained from auth0-real)
36+
src/test/java/com/auth0/client/MockServer.java
37+
src/test/java/com/auth0/client/RecordedRequestMatcher.java
38+
src/test/java/com/auth0/client/UrlMatcher.java
39+
src/test/java/com/auth0/AssertsUtil.java
4040
src/test/resources/auth/
41-
src/test/resources/keys/
42-
src/test/resources/mgmt/
4341
src/test/resources/mockito-extensions/
4442
src/test/java/com/auth0/client/legacy/
4543

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.auth0;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.is;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
import org.junit.jupiter.api.function.Executable;
8+
9+
public class AssertsUtil {
10+
11+
public static <T extends Throwable> T verifyThrows(Class<T> expectedType, Executable executable) {
12+
return assertThrows(expectedType, executable);
13+
}
14+
15+
public static <T extends Throwable> T verifyThrows(Class<T> expectedType, Executable executable, String message) {
16+
T result = assertThrows(expectedType, executable);
17+
assertThat(result.getMessage(), is(message));
18+
return result;
19+
}
20+
}

src/test/java/com/auth0/client/MockServer.java

Lines changed: 361 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package com.auth0.client;
2+
3+
import com.auth0.net.client.HttpMethod;
4+
import okhttp3.HttpUrl;
5+
import okhttp3.mockwebserver.RecordedRequest;
6+
import org.hamcrest.Description;
7+
import org.hamcrest.TypeSafeDiagnosingMatcher;
8+
9+
public class RecordedRequestMatcher extends TypeSafeDiagnosingMatcher<RecordedRequest> {
10+
11+
private static final int METHOD_PATH = 0;
12+
private static final int HEADER = 1;
13+
private static final int QUERY_PARAMETER = 2;
14+
private static final int QUERY_PARAMETER_PRESENCE = 3;
15+
16+
private final int checkingOption;
17+
private final String first;
18+
private final String second;
19+
20+
private RecordedRequestMatcher(String first, String second, int checkingOption) {
21+
this.checkingOption = checkingOption;
22+
this.first = first;
23+
this.second = second;
24+
}
25+
26+
@Override
27+
protected boolean matchesSafely(RecordedRequest item, Description mismatchDescription) {
28+
if (item == null) {
29+
mismatchDescription.appendText("was null");
30+
return false;
31+
}
32+
33+
switch (checkingOption) {
34+
default:
35+
case METHOD_PATH:
36+
return matchesMethodAndPath(item, mismatchDescription);
37+
case HEADER:
38+
return matchesHeader(item, mismatchDescription);
39+
case QUERY_PARAMETER:
40+
return matchesQueryParameter(item, mismatchDescription);
41+
case QUERY_PARAMETER_PRESENCE:
42+
return hasQueryParameter(item, mismatchDescription);
43+
}
44+
}
45+
46+
private boolean matchesMethodAndPath(RecordedRequest item, Description mismatchDescription) {
47+
if (!item.getMethod().equalsIgnoreCase(first)) {
48+
mismatchDescription.appendText("method was ").appendValue(item.getMethod());
49+
return false;
50+
}
51+
String path = item.getPath();
52+
boolean hasQuery = path.indexOf("?") > 0;
53+
if (hasQuery) {
54+
path = path.substring(0, path.indexOf("?"));
55+
}
56+
if (!path.equals(second)) {
57+
mismatchDescription.appendText("path was ").appendValue(path);
58+
return false;
59+
}
60+
61+
return true;
62+
}
63+
64+
private boolean matchesHeader(RecordedRequest item, Description mismatchDescription) {
65+
String value = item.getHeader(first);
66+
if (value != null && !value.equals(second) || value == null && second != null) {
67+
mismatchDescription.appendText(first).appendText(" header was ").appendValue(value);
68+
return false;
69+
}
70+
71+
return true;
72+
}
73+
74+
private boolean matchesQueryParameter(RecordedRequest item, Description mismatchDescription) {
75+
HttpUrl requestUrl = item.getRequestUrl();
76+
if (requestUrl.querySize() == 0) {
77+
mismatchDescription.appendText(" query was empty");
78+
return false;
79+
}
80+
81+
String queryParamValue = requestUrl.queryParameter(first);
82+
if (second.equals(queryParamValue)) {
83+
return true;
84+
}
85+
86+
mismatchDescription.appendValueList(
87+
"Query parameters were {", ", ", "}.", requestUrl.query().split("&"));
88+
return false;
89+
}
90+
91+
private boolean hasQueryParameter(RecordedRequest item, Description mismatchDescription) {
92+
String path = item.getPath();
93+
boolean hasQuery = path.indexOf("?") > 0;
94+
if (!hasQuery) {
95+
mismatchDescription.appendText(" query was empty");
96+
return false;
97+
}
98+
99+
String query = path.substring(path.indexOf("?") + 1);
100+
String[] parameters = query.split("&");
101+
for (String p : parameters) {
102+
if (p.startsWith(String.format("%s=", first))) {
103+
return true;
104+
}
105+
}
106+
mismatchDescription.appendValueList("Query parameters were {", ", ", "}.", parameters);
107+
return false;
108+
}
109+
110+
@Override
111+
public void describeTo(Description description) {
112+
switch (checkingOption) {
113+
default:
114+
case METHOD_PATH:
115+
description
116+
.appendText("A request with method ")
117+
.appendValue(first)
118+
.appendText(" and path ")
119+
.appendValue(second);
120+
break;
121+
case HEADER:
122+
description
123+
.appendText("A request containing header ")
124+
.appendValue(first)
125+
.appendText(" with value ")
126+
.appendValue(second);
127+
break;
128+
case QUERY_PARAMETER:
129+
description
130+
.appendText("A request containing query parameter ")
131+
.appendValue(first)
132+
.appendText(" with value ")
133+
.appendValue(second);
134+
break;
135+
case QUERY_PARAMETER_PRESENCE:
136+
description.appendText("A request containing query parameter ").appendValue(first);
137+
break;
138+
}
139+
}
140+
141+
public static RecordedRequestMatcher hasMethodAndPath(HttpMethod method, String path) {
142+
return new RecordedRequestMatcher(method.toString(), path, METHOD_PATH);
143+
}
144+
145+
public static RecordedRequestMatcher hasHeader(String name, String value) {
146+
return new RecordedRequestMatcher(name, value, HEADER);
147+
}
148+
149+
public static RecordedRequestMatcher hasQueryParameter(String name, String value) {
150+
return new RecordedRequestMatcher(name, value, QUERY_PARAMETER);
151+
}
152+
153+
public static RecordedRequestMatcher hasQueryParameter(String name) {
154+
return new RecordedRequestMatcher(name, null, QUERY_PARAMETER_PRESENCE);
155+
}
156+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package com.auth0.client;
2+
3+
import java.util.Arrays;
4+
import okhttp3.HttpUrl;
5+
import org.hamcrest.Description;
6+
import org.hamcrest.TypeSafeDiagnosingMatcher;
7+
8+
public class UrlMatcher extends TypeSafeDiagnosingMatcher<String> {
9+
10+
private static final int BASE_URL = 0;
11+
private static final int QUERY_PARAMETER = 1;
12+
private static final int ENCODED_QUERY = 2;
13+
private final int checkingOption;
14+
15+
private String encodedQueryContains;
16+
private String scheme;
17+
private String host;
18+
private String path;
19+
private String paramKey;
20+
private String paramValue;
21+
22+
private UrlMatcher(String scheme, String host, String path) {
23+
this.checkingOption = BASE_URL;
24+
this.scheme = scheme;
25+
this.host = host;
26+
this.path = path;
27+
}
28+
29+
private UrlMatcher(String paramKey, String paramValue) {
30+
this.checkingOption = QUERY_PARAMETER;
31+
this.paramKey = paramKey;
32+
this.paramValue = paramValue;
33+
}
34+
35+
private UrlMatcher(String encodedQueryContains) {
36+
this.checkingOption = ENCODED_QUERY;
37+
this.encodedQueryContains = encodedQueryContains;
38+
}
39+
40+
@Override
41+
protected boolean matchesSafely(String item, Description mismatchDescription) {
42+
if (item == null) {
43+
mismatchDescription.appendText("was null");
44+
return false;
45+
}
46+
HttpUrl url = HttpUrl.parse(item);
47+
if (url == null) {
48+
mismatchDescription.appendText("was not a valid url");
49+
return false;
50+
}
51+
52+
switch (checkingOption) {
53+
default:
54+
case BASE_URL:
55+
return matchesBaseUrl(url, mismatchDescription);
56+
case QUERY_PARAMETER:
57+
return matchesParameter(url, mismatchDescription);
58+
case ENCODED_QUERY:
59+
return matchesEncodedQuery(url, mismatchDescription);
60+
}
61+
}
62+
63+
private boolean matchesEncodedQuery(HttpUrl url, Description mismatchDescription) {
64+
if (!url.encodedQuery().contains(encodedQueryContains)) {
65+
mismatchDescription.appendText("encoded query was ").appendValue(url.encodedQuery());
66+
return false;
67+
}
68+
69+
return true;
70+
}
71+
72+
private boolean matchesBaseUrl(HttpUrl url, Description mismatchDescription) {
73+
if (!url.scheme().equals(scheme)) {
74+
mismatchDescription.appendText("scheme was ").appendValue(url.scheme());
75+
return false;
76+
}
77+
if (!url.host().equals(host)) {
78+
mismatchDescription.appendText("host was ").appendValue(url.host());
79+
return false;
80+
}
81+
if (path == null) {
82+
return true;
83+
}
84+
if (path.startsWith("/")) {
85+
path = path.substring(1);
86+
}
87+
if (path.endsWith("/")) {
88+
path = path.substring(0, path.length() - 1);
89+
}
90+
if (!url.pathSegments().equals(Arrays.asList(path.split("/")))) {
91+
StringBuilder sb = new StringBuilder();
92+
for (String p : url.pathSegments()) {
93+
sb.append(p).append("/");
94+
}
95+
sb.deleteCharAt(sb.length() - 1);
96+
mismatchDescription.appendText("path was ").appendValue(sb.toString());
97+
return false;
98+
}
99+
100+
return true;
101+
}
102+
103+
private boolean matchesParameter(HttpUrl url, Description mismatchDescription) {
104+
String value = url.queryParameter(paramKey);
105+
if (value != null && !value.equals(paramValue) || value == null && paramValue != null) {
106+
mismatchDescription.appendText("value was ").appendValue(value);
107+
return false;
108+
}
109+
110+
return true;
111+
}
112+
113+
@Override
114+
public void describeTo(Description description) {
115+
switch (checkingOption) {
116+
default:
117+
case BASE_URL:
118+
description
119+
.appendText("An url with scheme ")
120+
.appendValue(scheme)
121+
.appendText(", host ")
122+
.appendValue(host)
123+
.appendText("and path ")
124+
.appendValue(path);
125+
break;
126+
case QUERY_PARAMETER:
127+
description
128+
.appendText("An url with the query parameter ")
129+
.appendValue(paramKey)
130+
.appendText(" with value ")
131+
.appendValue(paramValue);
132+
break;
133+
case ENCODED_QUERY:
134+
description.appendText("An url with encoded query containing ").appendValue(encodedQueryContains);
135+
break;
136+
}
137+
}
138+
139+
public static UrlMatcher isUrl(String scheme, String host, String path) {
140+
return new UrlMatcher(scheme, host, path);
141+
}
142+
143+
public static UrlMatcher isUrl(String scheme, String host) {
144+
return new UrlMatcher(scheme, host, null);
145+
}
146+
147+
public static UrlMatcher hasQueryParameter(String key, String value) {
148+
return new UrlMatcher(key, value);
149+
}
150+
151+
public static UrlMatcher encodedQueryContains(String text) {
152+
return new UrlMatcher(text);
153+
}
154+
}

0 commit comments

Comments
 (0)