Skip to content

Commit

Permalink
JAMES-1896 Tests should wait for authentication response
Browse files Browse the repository at this point in the history
  • Loading branch information
aduprat committed Dec 23, 2016
1 parent db703cd commit 9bc0442
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
4 changes: 2 additions & 2 deletions server/pom.xml
Expand Up @@ -949,7 +949,7 @@
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>2.0.0.0</version>
Expand Down Expand Up @@ -1508,7 +1508,7 @@
<dependency>
<groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>1.6.3</version>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
Expand Down
Expand Up @@ -21,30 +21,48 @@

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.james.jmap.api.access.AccessToken;
import org.hamcrest.core.IsAnything;

import com.jayway.awaitility.Awaitility;
import com.jayway.awaitility.Duration;
import com.jayway.awaitility.core.ConditionFactory;
import com.jayway.jsonpath.JsonPath;

public class HttpJmapAuthentication {

private static final ConditionFactory CALMLY_AWAIT = Awaitility.with()
.pollInterval(Duration.FIVE_HUNDRED_MILLISECONDS)
.and().with()
.pollDelay(Duration.ONE_HUNDRED_MILLISECONDS)
.await();

public static AccessToken authenticateJamesUser(URIBuilder uriBuilder, String username, String password) throws ClientProtocolException, IOException, URISyntaxException {
String continuationToken = getContinuationToken(uriBuilder, username);

Response response = Request.Post(uriBuilder.setPath("/authentication").build())
Response response = CALMLY_AWAIT
.atMost(30, TimeUnit.SECONDS)
.ignoreExceptions()
.until(() -> postAuthenticate(uriBuilder, password, continuationToken), IsAnything.anything());

return AccessToken.fromString(
JsonPath.parse(response.returnContent().asString())
.read("accessToken"));
}

private static Response postAuthenticate(URIBuilder uriBuilder, String password, String continuationToken) throws ClientProtocolException, IOException, URISyntaxException {
return Request.Post(uriBuilder.setPath("/authentication").build())
.bodyString("{\"token\": \"" + continuationToken + "\", \"method\": \"password\", \"password\": \"" + password + "\"}",
ContentType.APPLICATION_JSON)
.setHeader("Accept", ContentType.APPLICATION_JSON.getMimeType())
.execute();

return AccessToken.fromString(
JsonPath.parse(response.returnContent().asString())
.read("accessToken"));
}

private static String getContinuationToken(URIBuilder uriBuilder, String username) throws ClientProtocolException, IOException, URISyntaxException {
Expand All @@ -56,4 +74,5 @@ private static String getContinuationToken(URIBuilder uriBuilder, String usernam
return JsonPath.parse(response.returnContent().asString())
.read("continuationToken");
}

}

0 comments on commit 9bc0442

Please sign in to comment.