jaroslav@233: /* jaroslav@264: * Quoridor server and related libraries jaroslav@264: * Copyright (C) 2009-2010 Jaroslav Tulach jaroslav@233: * jaroslav@264: * This program is free software: you can redistribute it and/or modify jaroslav@264: * it under the terms of the GNU General Public License as published by jaroslav@264: * the Free Software Foundation, either version 3 of the License. jaroslav@233: * jaroslav@264: * This program is distributed in the hope that it will be useful, jaroslav@264: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@264: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@264: * GNU General Public License for more details. jaroslav@233: * jaroslav@264: * You should have received a copy of the GNU General Public License jaroslav@264: * along with this program. Look for COPYING file in the top folder. jaroslav@264: * If not, see http://www.gnu.org/licenses/. jaroslav@233: */ jaroslav@233: package cz.xelfi.quoridor.freemarkerdor; jaroslav@233: jaroslav@282: import org.glassfish.grizzly.http.server.HttpServer; jaroslav@233: import com.sun.jersey.api.client.Client; jaroslav@233: import com.sun.jersey.api.client.ClientResponse; jaroslav@233: import com.sun.jersey.api.client.WebResource; jaroslav@233: import cz.xelfi.quoridor.webidor.resources.Quoridor; jaroslav@233: import java.io.File; jaroslav@233: import java.io.FileOutputStream; jaroslav@233: import java.io.IOException; jaroslav@233: import java.net.URI; jaroslav@233: import java.util.Locale; jaroslav@233: import java.util.concurrent.Callable; jaroslav@233: import java.util.regex.Pattern; jaroslav@233: import javax.ws.rs.core.Cookie; jaroslav@233: import javax.ws.rs.core.MediaType; jaroslav@233: import org.junit.AfterClass; jaroslav@233: import org.junit.Before; jaroslav@233: import org.junit.BeforeClass; jaroslav@233: import org.junit.Test; jaroslav@233: import org.netbeans.junit.MockServices; jaroslav@233: import static org.junit.Assert.*; jaroslav@233: jaroslav@233: /** jaroslav@233: * jaroslav@233: * @author Jaroslav Tulach jaroslav@233: */ jaroslav@233: public class ChangeEmailTest extends Object { jaroslav@233: private static File dir; jaroslav@233: private static HttpServer stopAPI; jaroslav@233: private static HttpServer stopStatistics; jaroslav@233: private static Callable stop; jaroslav@233: private WebResource webResource; jaroslav@233: private WebResource apiResource; jaroslav@233: private WebResource statResource; jaroslav@233: private Client client; jaroslav@233: jaroslav@233: public ChangeEmailTest() throws Exception { jaroslav@233: } jaroslav@233: jaroslav@233: @BeforeClass jaroslav@233: public static void localeEnglish() throws Exception { jaroslav@233: Locale.setDefault(Locale.ENGLISH); jaroslav@233: dir = File.createTempFile("quoridor", ".dir"); jaroslav@233: dir.delete(); jaroslav@233: dir.mkdirs(); jaroslav@233: System.setProperty("quoridor.dir", dir.getPath()); jaroslav@233: stopAPI = Quoridor.start(7990); jaroslav@233: stopStatistics = Quoridor.start(7992); jaroslav@234: stop = UI.startServers(7991, "http://localhost:7990", "http://localhost:7992", null); jaroslav@233: jaroslav@233: File passwd = new File(dir, "passwd"); jaroslav@233: FileOutputStream os = new FileOutputStream(passwd); jaroslav@233: os.write("test=pes\nJarda=darda\n".getBytes("UTF-8")); jaroslav@233: os.close(); jaroslav@233: jaroslav@233: MockServices.setServices(MockEmailer.class); jaroslav@233: } jaroslav@233: jaroslav@233: @Before jaroslav@233: public void setUp() throws Exception { jaroslav@233: MockEmailer.clear(); jaroslav@233: jaroslav@233: client = new Client(); jaroslav@233: webResource = client.resource(new URI("http://localhost:7991/")); jaroslav@233: apiResource = client.resource(new URI("http://localhost:7990/")); jaroslav@233: statResource = client.resource(new URI("http://localhost:7992/")); jaroslav@233: } jaroslav@233: jaroslav@233: @AfterClass jaroslav@233: public static void cleanUpAll() throws Exception { jaroslav@233: deleteRec(dir); jaroslav@233: if (stop != null) { jaroslav@233: stop.call(); jaroslav@233: } jaroslav@233: if (stopAPI != null) { jaroslav@282: stopAPI.stop(); jaroslav@233: } jaroslav@233: MockServices.setServices(); jaroslav@233: } jaroslav@233: jaroslav@233: static void deleteRec(File dir) throws IOException { jaroslav@233: if (dir == null) { jaroslav@233: return; jaroslav@233: } jaroslav@233: File[] arr = dir.listFiles(); jaroslav@233: if (arr != null) { jaroslav@233: for (File f : arr) { jaroslav@233: deleteRec(f); jaroslav@233: } jaroslav@233: } jaroslav@233: dir.delete(); jaroslav@233: } jaroslav@233: jaroslav@233: @Test public void testChangeEmail() throws Exception { jaroslav@233: String logTest = apiResource.path("login"). jaroslav@233: queryParam("name", "test"). jaroslav@233: queryParam("password", "pes"). jaroslav@233: accept(MediaType.TEXT_PLAIN). jaroslav@233: put(String.class); jaroslav@233: ClientResponse res = webResource.path("options"). jaroslav@233: queryParam("email", "xxx@xxx.cz"). jaroslav@233: cookie(Cookie.valueOf("login=" + logTest)). jaroslav@233: accept("text/html"). jaroslav@233: get(ClientResponse.class); jaroslav@233: final String txt = res.getEntity(String.class); jaroslav@233: jaroslav@233: assertEquals("Emails sent to", "xxx@xxx.cz", MockEmailer.address); jaroslav@233: jaroslav@233: int urlIndex = MockEmailer.text.indexOf(webResource.getURI().toString()); jaroslav@233: assertTrue("Found in " + MockEmailer.text, urlIndex >= 0); jaroslav@233: jaroslav@233: int endIndex = urlIndex; jaroslav@233: while (!Character.isWhitespace(MockEmailer.text.charAt(endIndex))) { jaroslav@233: endIndex++; jaroslav@233: } jaroslav@233: final String url = MockEmailer.text.substring(urlIndex, endIndex); jaroslav@233: WebResource requestURL = client.resource(url); jaroslav@233: jaroslav@233: String response = requestURL.accept("text/html").get(String.class); jaroslav@233: Pattern p = Pattern.compile("