use of com.ibm.cloud.cloudant.internal.TestCloudantService in project cloudant-java-sdk by IBM.
the class CouchDbSessionAuthenticatorTest method requestToken.
/**
* Assert that the requestToken method calls out to the _session endpoint successfully.
*
* @throws Exception
*/
@Test
void requestToken() throws Exception {
MockWebServer server = new MockWebServer();
String mockSession = "AuthSession=ABC123456DE";
String mockSetCookieValue = String.format("%s; Version=1; Expires=Thu, 09-Apr-2020 " + "10:30:47 GMT; Max-Age=600; Path=/; HttpOnly", mockSession);
String mockSessionPostResponseBody = "{\"ok\":true,\"name\":\"testuser\"," + "\"roles\":[\"admin\"]}";
String mockSessionGetResponseBody = "{\"ok\":true,\"userCtx\":{\"name\":\"testuser\"," + "\"roles\":[\"_admin\"]}}";
MockResponse sessionPostResponse = new MockResponse().addHeader("Set-Cookie", mockSetCookieValue).setBody(mockSessionPostResponseBody);
MockResponse sessionGetResponse = new MockResponse().setBody(mockSessionGetResponseBody);
server.start();
try {
server.getPort();
server.enqueue(sessionPostResponse);
server.enqueue(sessionGetResponse);
TestCloudantService testCloudantService = new TestCloudantService("test", testAuthenticator);
testCloudantService.setServiceUrl("http://" + server.getHostName() + ":" + server.getPort());
testCloudantService.setDefaultHeaders(customHeaders);
Response<Map<String, Object>> call = testCloudantService.getSessionInformation().execute();
// Assert on the mock server
// First request
RecordedRequest sessionPostRequest = server.takeRequest();
assertEquals("POST", sessionPostRequest.getMethod(), "Should have been a POST request" + ".");
assertEquals("/_session", sessionPostRequest.getPath(), "Should have been a _session " + "request.");
String expectedBody = String.format("{\"username\":\"%s\",\"password\":\"%s\"}", CouchDbSessionAuthenticatorTest.USERNAME, String.valueOf(CouchDbSessionAuthenticatorTest.PASSWORD));
assertEquals(expectedBody, sessionPostRequest.getBody().readUtf8(), "The body should " + "be as expected.");
assertEquals("TESTVALUE", sessionPostRequest.getHeader("X-CDT-SDK-TEST"), "The custom" + " header should have been on the request.");
// Second request
RecordedRequest sessionGetRequest = server.takeRequest();
assertEquals("GET", sessionGetRequest.getMethod(), "Should have been a GET request.");
assertEquals("/_session", sessionGetRequest.getPath(), "Should have been a _session " + "request.");
assertEquals(mockSession, sessionGetRequest.getHeader("Cookie"), "The cookie on the " + "request should be correct.");
// Additional assertions on the response
assertEquals(200, call.getStatusCode(), "The request should succeed.");
// Perform some additional assertions on the response
Map<String, Object> information = call.getResult();
assertTrue((Boolean) information.get("ok"), "The response should be OK.");
Map<String, Object> userCtx = (Map<String, Object>) information.get("userCtx");
assertNotNull(information.get("userCtx"), "There should be a user context.");
assertEquals("testuser", userCtx.get("name"), "The user should be " + "testuser.");
List<String> roles = (List<String>) userCtx.get("roles");
assertEquals("_admin", roles.get(0), "The role should be " + "_admin");
} finally {
server.shutdown();
}
}
Aggregations