This is an automated email from the git hooks/post-receive script. New commit to branch feature/refonte-ui in repository pollen. See https://gitlab.nuiton.org/chorem/pollen.git commit 99ba56268224be03a1e964d6c245c3f299507d21 Author: jcouteau <couteau@codelutin.com> Date: Wed May 27 08:26:04 2020 +0200 Add some rest api tests --- .../pollen/rest/api/AbstractPollenRestApiTest.java | 49 +++++++++++++++++++- .../org/chorem/pollen/rest/api/AuthApiTest.java | 2 +- .../org/chorem/pollen/rest/api/PollApiTest.java | 5 ++- .../chorem/pollen/rest/api/PollenUserApiTest.java | 52 +++------------------- .../chorem/pollen/rest/api/RestApiFixtures.java | 5 +++ 5 files changed, 64 insertions(+), 49 deletions(-) diff --git a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AbstractPollenRestApiTest.java b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AbstractPollenRestApiTest.java index 0b98651e..b95dd883 100644 --- a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AbstractPollenRestApiTest.java +++ b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AbstractPollenRestApiTest.java @@ -27,9 +27,13 @@ import org.apache.catalina.Globals; import org.apache.catalina.startup.Tomcat; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.http.HeaderElement; +import org.apache.http.HeaderElementIterator; import org.apache.http.HttpResponse; +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.message.BasicHeaderElementIterator; import org.apache.http.util.EntityUtils; import org.chorem.pollen.persistence.PollenPersistenceContext; import org.chorem.pollen.persistence.PollenTopiaPersistenceContext; @@ -51,8 +55,11 @@ import org.junit.Rule; import org.nuiton.topia.persistence.TopiaIdFactory; import org.nuiton.util.DateUtil; +import javax.ws.rs.core.HttpHeaders; import java.io.File; import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; import java.util.Locale; import java.util.concurrent.atomic.AtomicBoolean; @@ -198,6 +205,13 @@ public class AbstractPollenRestApiTest { return content; } + protected void assertEmptyResponse(Response response) throws IOException { + HttpResponse httpResponse = response.returnResponse(); + int statusCode = httpResponse.getStatusLine().getStatusCode(); + Assert.assertEquals(204, statusCode); + Assert.assertNull(httpResponse.getEntity()); + } + protected void showTestResult(String content) { String testName = application.getMethodName(); @@ -230,8 +244,41 @@ public class AbstractPollenRestApiTest { .setScheme("http") .setHost("localhost") .setPort(application.getPort()) - .setPath(path); + .setPath(path) + .setParameter("order", "topiaCreateDate") + .setParameter("desc", "true") + .setParameter("pageSize","-1"); return builder; } + protected String login() throws URISyntaxException, IOException { + + URI uri = createRequest(RestApiFixtures.login()) + .addParameter("login", "admin@chorem.org") + .addParameter("password", "admin") + .build(); + Request request = Request.Post(uri); + + Response response = request.execute(); + HttpResponse httpResponse = response.returnResponse(); + int statusCode = httpResponse.getStatusLine().getStatusCode(); + Assert.assertEquals(200, statusCode); + String content = EntityUtils.toString(httpResponse.getEntity()); + showTestResult(content); + Assert.assertNotNull(content); + + String token = ""; + + HeaderElementIterator it = new BasicHeaderElementIterator(httpResponse.headerIterator(HttpHeaders.SET_COOKIE)); + while (it.hasNext()) { + HeaderElement elem = it.nextElement(); + if (elem.getName().equals(PollenRestApiRequestFilter.COOKIE_POLLEN_AUTH)) { + token = elem.getValue(); + } + } + + return token; + + } + } diff --git a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AuthApiTest.java b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AuthApiTest.java index 95d64e54..bba42d24 100644 --- a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AuthApiTest.java +++ b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/AuthApiTest.java @@ -43,7 +43,7 @@ import java.net.URISyntaxException; public class AuthApiTest extends AbstractPollenRestApiTest { @Test - public void login() throws URISyntaxException, IOException { + public void loginTest() throws URISyntaxException, IOException { URI uri = createRequest(RestApiFixtures.login()) diff --git a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollApiTest.java b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollApiTest.java index bbbecdc2..91f41f69 100644 --- a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollApiTest.java +++ b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollApiTest.java @@ -70,13 +70,14 @@ public class PollApiTest extends AbstractPollenRestApiTest { assertResponse(response); } - @Ignore @Test public void getPolls() throws URISyntaxException, IOException { - //TODO Login as admin before + String sessionToken = login(); + URI uri = createRequest(RestApiFixtures.polls(null, null)).build(); Request request = Request.Get(uri); + request.addHeader(PollenRestApiRequestFilter.REQUEST_HEADER_SESSION_TOKEN, sessionToken); Response response = request.execute(); assertResponse(response); } diff --git a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollenUserApiTest.java b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollenUserApiTest.java index b655f8a7..1fa52368 100644 --- a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollenUserApiTest.java +++ b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/PollenUserApiTest.java @@ -21,23 +21,14 @@ package org.chorem.pollen.rest.api; * #L% */ -import org.apache.http.HeaderElement; -import org.apache.http.HeaderElementIterator; -import org.apache.http.HttpResponse; import org.apache.http.client.fluent.Request; import org.apache.http.client.fluent.Response; -import org.apache.http.message.BasicHeaderElementIterator; -import org.apache.http.util.EntityUtils; import org.chorem.pollen.persistence.entity.PollenUser; -import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; -import javax.ws.rs.core.HttpHeaders; -import java.io.IOException; import java.net.URI; -import java.net.URISyntaxException; import static org.junit.Assert.assertTrue; @@ -50,8 +41,9 @@ import static org.junit.Assert.assertTrue; public class PollenUserApiTest extends AbstractPollenRestApiTest { @Before - public void setUp() throws Exception { + public void setUp() { + loadFixtures("user-fixtures"); loadFixtures("fixtures"); } @@ -115,18 +107,18 @@ public class PollenUserApiTest extends AbstractPollenRestApiTest { } - @Ignore @Test - public void deleteUser() throws Exception { + public void deleteUserAsAdmin() throws Exception { + + String sessionToken = login(); PollenUser pollenUser = fixture("user_jean"); String userId = encodeId(pollenUser.getTopiaId()); URI uri = createRequest(RestApiFixtures.users(userId, null)).build(); Request request = Request.Delete(uri); + request.addHeader(PollenRestApiRequestFilter.REQUEST_HEADER_SESSION_TOKEN, sessionToken); Response response = request.execute(); - String content = assertResponse(response); - assertTrue(content.contains("OK!")); - + assertEmptyResponse(response); } @Ignore @@ -142,34 +134,4 @@ public class PollenUserApiTest extends AbstractPollenRestApiTest { String content = assertResponse(response); assertTrue(content.contains("OK!")); } - - private String login() throws URISyntaxException, IOException { - - URI uri = createRequest(RestApiFixtures.login()) - .addParameter("login", "admin@chorem.org") - .addParameter("password", "admin") - .build(); - Request request = Request.Post(uri); - - Response response = request.execute(); - HttpResponse httpResponse = response.returnResponse(); - int statusCode = httpResponse.getStatusLine().getStatusCode(); - Assert.assertEquals(200, statusCode); - String content = EntityUtils.toString(httpResponse.getEntity()); - showTestResult(content); - Assert.assertNotNull(content); - - String token = ""; - - HeaderElementIterator it = new BasicHeaderElementIterator(httpResponse.headerIterator(HttpHeaders.SET_COOKIE)); - while (it.hasNext()) { - HeaderElement elem = it.nextElement(); - if (elem.getName().equals(PollenRestApiRequestFilter.COOKIE_POLLEN_AUTH)) { - token = elem.getValue(); - } - } - - return token; - - } } diff --git a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/RestApiFixtures.java b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/RestApiFixtures.java index e21cd9a8..346b9185 100644 --- a/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/RestApiFixtures.java +++ b/pollen-rest-api/src/test/java/org/chorem/pollen/rest/api/RestApiFixtures.java @@ -33,6 +33,7 @@ public class RestApiFixtures { protected static final String LOGOUT_API = "/logout"; protected static final String USERS_API = "/users"; protected static final String POLLS_API = "/polls"; + protected static final String VOTES_API = "/polls"; public static String login() { return LOGIN_API; @@ -46,6 +47,10 @@ public class RestApiFixtures { return api(POLLS_API, pollId, "/", token); } + public static String votes(String pollId, String questionId, String voteId, String token) { + return api("/polls/" + pollId + "/questions/" + questionId + "/votes", voteId, "/", token); + } + public static String logout() { return LOGOUT_API; -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.