Skip to content

Commit a22b2f9

Browse files
committed
add oauth token endpoint test
1 parent 7a8d2c0 commit a22b2f9

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

auth0/src/test/java/com/auth0/authentication/AuthenticationAPIClientTest.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@
2626

2727

2828
import com.auth0.Auth0;
29-
import com.auth0.Auth0Exception;
3029
import com.auth0.authentication.result.Authentication;
3130
import com.auth0.authentication.result.Credentials;
3231
import com.auth0.authentication.result.DatabaseUser;
3332
import com.auth0.authentication.result.Delegation;
3433
import com.auth0.authentication.result.UserProfile;
35-
import com.auth0.request.ParameterizableRequest;
3634
import com.auth0.util.AuthenticationAPI;
3735
import com.auth0.util.MockBaseCallback;
3836
import com.fasterxml.jackson.core.type.TypeReference;
@@ -50,7 +48,6 @@
5048
import static com.auth0.util.AuthenticationAPI.ID_TOKEN;
5149
import static com.auth0.util.AuthenticationAPI.REFRESH_TOKEN;
5250
import static com.auth0.util.CallbackMatcher.hasNoError;
53-
import static com.auth0.util.CallbackMatcher.hasNoPayloadOfType;
5451
import static com.auth0.util.CallbackMatcher.hasPayload;
5552
import static com.auth0.util.CallbackMatcher.hasPayloadOfType;
5653
import static org.hamcrest.Matchers.equalTo;
@@ -769,7 +766,7 @@ public void shouldGetNewIdTokenWithRefreshTokenSync() throws Exception {
769766

770767
assertThat(delegation, is(notNullValue()));
771768
}
772-
769+
773770
@Test
774771
public void shouldUnlinkAccount() throws Exception {
775772
mockAPI.willReturnSuccessfulUnlinkAccount();
@@ -1173,7 +1170,29 @@ public void shouldFetchProfileAfterLoginRequest() throws Exception {
11731170
assertThat(secondRequest.getPath(), equalTo("/tokeninfo"));
11741171

11751172
assertThat(callback, hasPayloadOfType(Authentication.class));
1173+
}
1174+
1175+
@Test
1176+
public void shouldGetOAuthToken() throws Exception {
1177+
mockAPI
1178+
.willReturnAuthorizationCodeInfo()
1179+
.willReturnTokenInfo();
11761180

1181+
final MockBaseCallback<Credentials> callback = new MockBaseCallback<>();
1182+
client.token("code", AuthenticationAPI.CODE_VERIFIER, AuthenticationAPI.REDIRECT_URI)
1183+
.start(callback);
1184+
1185+
final RecordedRequest request = mockAPI.takeRequest();
1186+
assertThat(request.getPath(), equalTo("/oauth/token"));
1187+
1188+
Map<String, String> body = bodyFromRequest(request);
1189+
assertThat(body, hasEntry("redirect_uri", AuthenticationAPI.REDIRECT_URI));
1190+
assertThat(body, hasEntry("grant_type", ParameterBuilder.GRANT_TYPE_AUTHORIZATION_CODE));
1191+
assertThat(body, hasEntry("client_id", CLIENT_ID));
1192+
assertThat(body, hasEntry("code_verifier", AuthenticationAPI.CODE_VERIFIER));
1193+
assertThat(body, hasEntry("code", "code"));
1194+
1195+
assertThat(callback, hasPayloadOfType(Credentials.class));
11771196
}
11781197

11791198
private Map<String, String> bodyFromRequest(RecordedRequest request) throws java.io.IOException {

auth0/src/test/java/com/auth0/util/AuthenticationAPI.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class AuthenticationAPI {
4040
public static final String NEW_ID_TOKEN = "NEW_ID_TOKEN";
4141
public static final int EXPIRES_IN = 1234567890;
4242
public static final String TOKEN_TYPE = "TOKEN_TYPE";
43+
public static final String CODE_VERIFIER = "CODE_VERIFIER";
44+
public static final String REDIRECT_URI = "REDIRECT_URI";
4345

4446
private MockWebServer server;
4547

@@ -155,6 +157,18 @@ public AuthenticationAPI willReturnTokenInfo() {
155157
return this;
156158
}
157159

160+
public AuthenticationAPI willReturnAuthorizationCodeInfo() {
161+
String json = "{\"" +
162+
"redirect_uri\": \"" + REDIRECT_URI + "\"," +
163+
"\"code_verifier\": \"" + CODE_VERIFIER + "\"," +
164+
"\"client_id\":\"U5MhUrbyQHSVWjlEqZSTCBUFABLbJAS3\"," +
165+
"\"code\": \"" + CODE_VERIFIER + "\",\n" +
166+
"\"grant_type\":\"authorization_code\"" +
167+
"}";
168+
server.enqueue(responseWithJSON(json, 200));
169+
return this;
170+
}
171+
158172
public AuthenticationAPI willReturnApplicationResponseWithBody(String body, int statusCode) {
159173
MockResponse response = new MockResponse()
160174
.setResponseCode(statusCode)

0 commit comments

Comments
 (0)