private String fromGoodAccessTokenRequest(String continuationToken) { return with() .contentType(ContentType.JSON) .accept(ContentType.JSON) .body("{\"token\": \"" + continuationToken + "\", \"method\": \"password\", \"password\": \"" + userCredentials.getPassword() + "\"}") .post("/authentication") .path("accessToken") .toString(); }
@Test public void mustSendJsonContainingAccessTokenAndEndpointsWhenGoodPassword() throws Exception { String continuationToken = fromGoodContinuationTokenRequest(); zonedDateTimeProvider.setFixedDateTime(newDate); given() .contentType(ContentType.JSON) .accept(ContentType.JSON) .body("{\"token\": \"" + continuationToken + "\", \"method\": \"password\", \"password\": \"" + userCredentials.getPassword() + "\"}") .when() .post("/authentication") .then() .body("accessToken", isA(String.class)) .body("api", equalTo("/jmap")) .body("eventSource", both(isA(String.class)).and(notNullValue())) .body("upload", equalTo("/upload")) .body("download", equalTo("/download")); }
@Test public void methodShouldContainPasswordWhenValidResquest() throws Exception { given() .contentType(ContentType.JSON) .accept(ContentType.JSON) .body("{\"username\": \"" + userCredentials.getUsername() + "\", \"clientName\": \"Mozilla Thunderbird\", \"clientVersion\": \"42.0\", \"deviceName\": \"Joe Blogg’s iPhone\"}") .when() .post("/authentication") .then() .statusCode(200) .body("methods", hasItem(userCredentials.getPassword())); }
@Test public void mustReturnCreatedWhenGoodPassword() throws Exception { String continuationToken = fromGoodContinuationTokenRequest(); zonedDateTimeProvider.setFixedDateTime(newDate); given() .contentType(ContentType.JSON) .accept(ContentType.JSON) .body("{\"token\": \"" + continuationToken + "\", \"method\": \"password\", \"password\": \"" + userCredentials.getPassword() + "\"}") .when() .post("/authentication") .then() .statusCode(201); }
@Test public void mustReturnRestartAuthenticationWhenContinuationTokenIsExpired() throws Exception { String continuationToken = fromGoodContinuationTokenRequest(); zonedDateTimeProvider.setFixedDateTime(afterExpirationDate); given() .contentType(ContentType.JSON) .accept(ContentType.JSON) .body("{\"token\": \"" + continuationToken + "\", \"method\": \"password\", \"password\": \"" + userCredentials.getPassword() + "\"}") .when() .post("/authentication") .then() .statusCode(403); }
@Test public void mustReturnAuthenticationFailedWhenContinuationTokenIsRejectedByTheContinuationTokenManager() throws Exception { ContinuationToken badContinuationToken = new ContinuationToken(userCredentials.getUsername(), newDate, "badSignature"); given() .contentType(ContentType.JSON) .accept(ContentType.JSON) .body("{\"token\": \"" + badContinuationToken.serialize() + "\", \"method\": \"password\", \"password\": \"" + userCredentials.getPassword() + "\"}") .when() .post("/authentication") .then() .statusCode(401); }
@Before public void setup() throws Throwable { zonedDateTimeProvider = new FixedDateZonedDateTimeProvider(); zonedDateTimeProvider.setFixedDateTime(oldDate); jmapServer = createJmapServer(zonedDateTimeProvider); jmapServer.start(); RestAssured.requestSpecification = jmapRequestSpecBuilder .setPort(jmapServer.getProbe(JmapGuiceProbe.class).getJmapPort()) .build(); userCredentials = UserCredentials.builder() .username("user@domain.tld") .password("password") .build(); String domain = "domain.tld"; jmapServer.getProbe(DataProbeImpl.class) .fluent() .addDomain(domain) .addUser(userCredentials.getUsername(), userCredentials.getPassword()); }