mirror of
https://git.victorphan.net/basketballcantho/Teedy_custom.git
synced 2026-08-05 14:03:10 +07:00
Initial commit
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
package com.sismics.docs.rest;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.List;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import javax.mail.internet.MimeUtility;
|
||||
|
||||
import org.glassfish.grizzly.http.server.HttpServer;
|
||||
import org.glassfish.grizzly.http.server.StaticHttpHandler;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.subethamail.wiser.Wiser;
|
||||
import org.subethamail.wiser.WiserMessage;
|
||||
|
||||
import com.sismics.docs.rest.descriptor.JerseyTestWebAppDescriptorFactory;
|
||||
import com.sismics.docs.rest.util.ClientUtil;
|
||||
import com.sun.jersey.test.framework.JerseyTest;
|
||||
|
||||
/**
|
||||
* Base class of integration tests with Jersey.
|
||||
*
|
||||
* @author jtremeaux
|
||||
*/
|
||||
public abstract class BaseJerseyTest extends JerseyTest {
|
||||
/**
|
||||
* Test email server.
|
||||
*/
|
||||
protected Wiser wiser;
|
||||
|
||||
/**
|
||||
* Test HTTP server.
|
||||
*/
|
||||
HttpServer httpServer;
|
||||
|
||||
/**
|
||||
* Utility class for the REST client.
|
||||
*/
|
||||
protected ClientUtil clientUtil;
|
||||
|
||||
/**
|
||||
* Constructor of BaseJerseyTest.
|
||||
*/
|
||||
public BaseJerseyTest() {
|
||||
super(JerseyTestWebAppDescriptorFactory.build());
|
||||
this.clientUtil = new ClientUtil(resource());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
wiser = new Wiser();
|
||||
wiser.setPort(2500);
|
||||
wiser.start();
|
||||
|
||||
String httpRoot = URLDecoder.decode(new File(getClass().getResource("/").getFile()).getAbsolutePath(), "utf-8");
|
||||
httpServer = HttpServer.createSimpleServer(httpRoot, "localhost", 9997);
|
||||
// Disable file cache to fix https://java.net/jira/browse/GRIZZLY-1350
|
||||
((StaticHttpHandler) httpServer.getServerConfiguration().getHttpHandlers().keySet().iterator().next()).setFileCacheEnabled(false);
|
||||
httpServer.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
wiser.stop();
|
||||
httpServer.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts an email from the queue and consumes the email.
|
||||
*
|
||||
* @return Text of the email
|
||||
* @throws MessagingException
|
||||
* @throws IOException
|
||||
*/
|
||||
protected String popEmail() throws MessagingException, IOException {
|
||||
List<WiserMessage> wiserMessageList = wiser.getMessages();
|
||||
if (wiserMessageList.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
WiserMessage wiserMessage = wiserMessageList.get(wiserMessageList.size() - 1);
|
||||
wiserMessageList.remove(wiserMessageList.size() - 1);
|
||||
MimeMessage message = wiserMessage.getMimeMessage();
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
message.writeTo(os);
|
||||
String body = os.toString();
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes a string to "quoted-printable" characters to compare with the contents of an email.
|
||||
*
|
||||
* @param input String to encode
|
||||
* @return Encoded string
|
||||
* @throws MessagingException
|
||||
* @throws IOException
|
||||
*/
|
||||
protected String encodeQuotedPrintable(String input) throws MessagingException, IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
OutputStream os = MimeUtility.encode(baos, "quoted-printable");
|
||||
os.write(input.getBytes());
|
||||
os.close();
|
||||
return baos.toString();
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.sismics.docs.rest.descriptor;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import com.sismics.util.filter.RequestContextFilter;
|
||||
import com.sismics.util.filter.TokenBasedSecurityFilter;
|
||||
import com.sun.jersey.test.framework.WebAppDescriptor;
|
||||
|
||||
/**
|
||||
* Jersey tests Webapp descriptor.
|
||||
*
|
||||
* @author jtremeaux
|
||||
*/
|
||||
public class JerseyTestWebAppDescriptorFactory {
|
||||
private static String basePath = new File("src/main/webapp").getAbsolutePath();
|
||||
|
||||
/**
|
||||
* Constructs a new descriptor.
|
||||
*
|
||||
* @return Descriptor
|
||||
*/
|
||||
public static WebAppDescriptor build() {
|
||||
// Target the base path to the Webapp resources
|
||||
System.setProperty("user.dir", basePath);
|
||||
System.setProperty("test", "true");
|
||||
|
||||
return new WebAppDescriptor.Builder("com.sismics.docs.rest.resource")
|
||||
.contextPath("docs")
|
||||
.addFilter(RequestContextFilter.class, "requestContextFilter")
|
||||
.addFilter(TokenBasedSecurityFilter.class, "tokenBasedSecurityFilter")
|
||||
.initParam("com.sun.jersey.spi.container.ContainerRequestFilters", "com.sun.jersey.api.container.filter.LoggingFilter")
|
||||
.initParam("com.sun.jersey.spi.container.ContainerResponseFilters", "com.sun.jersey.api.container.filter.LoggingFilter")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.sismics.docs.rest.filter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ws.rs.core.Cookie;
|
||||
|
||||
import com.sismics.util.filter.TokenBasedSecurityFilter;
|
||||
import com.sun.jersey.api.client.ClientHandlerException;
|
||||
import com.sun.jersey.api.client.ClientRequest;
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
import com.sun.jersey.api.client.filter.ClientFilter;
|
||||
|
||||
/**
|
||||
* Filter to add the authentication token into a cookie.
|
||||
*
|
||||
* @author jtremeaux
|
||||
*/
|
||||
public class CookieAuthenticationFilter extends ClientFilter {
|
||||
private String authToken;
|
||||
|
||||
public CookieAuthenticationFilter(String authToken) {
|
||||
this.authToken = authToken;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientResponse handle(ClientRequest request) throws ClientHandlerException {
|
||||
Cookie cookie = new Cookie(TokenBasedSecurityFilter.COOKIE_NAME, authToken);
|
||||
List<Object> cookieList = new ArrayList<Object>();
|
||||
cookieList.add(cookie);
|
||||
if (authToken != null) {
|
||||
request.getHeaders().put("Cookie", cookieList);
|
||||
}
|
||||
ClientResponse response = getNext().handle(request);
|
||||
if (response.getCookies() != null) {
|
||||
cookieList.addAll(response.getCookies());
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package com.sismics.docs.rest.util;
|
||||
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
import javax.ws.rs.core.NewCookie;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import com.sismics.docs.rest.filter.CookieAuthenticationFilter;
|
||||
import com.sismics.util.filter.TokenBasedSecurityFilter;
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
import com.sun.jersey.api.client.ClientResponse.Status;
|
||||
import com.sun.jersey.api.client.WebResource;
|
||||
import com.sun.jersey.core.util.MultivaluedMapImpl;
|
||||
|
||||
/**
|
||||
* REST client utilities.
|
||||
*
|
||||
* @author jtremeaux
|
||||
*/
|
||||
public class ClientUtil {
|
||||
private WebResource resource;
|
||||
|
||||
/**
|
||||
* Constructor of ClientUtil.
|
||||
*
|
||||
* @param webResource Resource corresponding to the base URI of REST resources.
|
||||
*/
|
||||
public ClientUtil(WebResource resource) {
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a user.
|
||||
*
|
||||
* @param username Username
|
||||
*/
|
||||
public void createUser(String username) {
|
||||
// Login admin to create the user
|
||||
String adminAuthenticationToken = login("admin", "admin", false);
|
||||
|
||||
// Create the user
|
||||
WebResource userResource = resource.path("/user");
|
||||
userResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken));
|
||||
MultivaluedMap<String, String> postParams = new MultivaluedMapImpl();
|
||||
postParams.putSingle("username", username);
|
||||
postParams.putSingle("email", username + "@docs.com");
|
||||
postParams.putSingle("password", "12345678");
|
||||
postParams.putSingle("time_zone", "Asia/Tokyo");
|
||||
ClientResponse response = userResource.put(ClientResponse.class, postParams);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
|
||||
// Logout admin
|
||||
logout(adminAuthenticationToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects a user to the application.
|
||||
*
|
||||
* @param username Username
|
||||
* @param password Password
|
||||
* @param remember Remember user
|
||||
* @return Authentication token
|
||||
*/
|
||||
public String login(String username, String password, Boolean remember) {
|
||||
WebResource userResource = resource.path("/user/login");
|
||||
MultivaluedMap<String, String> postParams = new MultivaluedMapImpl();
|
||||
postParams.putSingle("username", username);
|
||||
postParams.putSingle("password", password);
|
||||
postParams.putSingle("remember", remember.toString());
|
||||
ClientResponse response = userResource.post(ClientResponse.class, postParams);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
|
||||
return getAuthenticationCookie(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects a user to the application.
|
||||
*
|
||||
* @param username Username
|
||||
* @return Authentication token
|
||||
*/
|
||||
public String login(String username) {
|
||||
return login(username, "12345678", false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects a user from the application.
|
||||
*
|
||||
* @param authenticationToken Authentication token
|
||||
*/
|
||||
public void logout(String authenticationToken) {
|
||||
WebResource userResource = resource.path("/user/logout");
|
||||
userResource.addFilter(new CookieAuthenticationFilter(authenticationToken));
|
||||
ClientResponse response = userResource.post(ClientResponse.class);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the authentication token of the response.
|
||||
*
|
||||
* @param response Response
|
||||
* @return Authentication token
|
||||
*/
|
||||
public String getAuthenticationCookie(ClientResponse response) {
|
||||
String authToken = null;
|
||||
for (NewCookie cookie : response.getCookies()) {
|
||||
if (TokenBasedSecurityFilter.COOKIE_NAME.equals(cookie.getName())) {
|
||||
authToken = cookie.getValue();
|
||||
}
|
||||
}
|
||||
return authToken;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.sismics.docs.rest.util;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.sismics.rest.exception.ClientException;
|
||||
import com.sismics.rest.util.ValidationUtil;
|
||||
|
||||
/**
|
||||
* Test the validations.
|
||||
*
|
||||
* @author jtremeaux
|
||||
*/
|
||||
public class TestValidationUtil {
|
||||
@Test
|
||||
public void testValidateHttpUrlFail() throws Exception {
|
||||
ValidationUtil.validateHttpUrl("http://www.google.com", "url");
|
||||
ValidationUtil.validateHttpUrl("https://www.google.com", "url");
|
||||
ValidationUtil.validateHttpUrl(" https://www.google.com ", "url");
|
||||
try {
|
||||
ValidationUtil.validateHttpUrl("ftp://www.google.com", "url");
|
||||
Assert.fail();
|
||||
} catch (ClientException e) {
|
||||
// NOP
|
||||
}
|
||||
try {
|
||||
ValidationUtil.validateHttpUrl("http://", "url");
|
||||
Assert.fail();
|
||||
} catch (ClientException e) {
|
||||
// NOP
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user