freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/ChangeEmailTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 04 Mar 2010 19:37:58 +0100
changeset 233 ecddc9f373bb
child 234 0a71b6bd786f
permissions -rw-r--r--
Changing an email needs to be confirmed by receiving an email and responding to an URL send inside it
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2010 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.freemarkerdor;
    28 
    29 import com.sun.jersey.api.client.Client;
    30 import com.sun.jersey.api.client.ClientResponse;
    31 import com.sun.jersey.api.client.WebResource;
    32 import com.sun.jersey.core.header.MediaTypes;
    33 import com.sun.jersey.core.util.MultivaluedMapImpl;
    34 import com.sun.net.httpserver.HttpServer;
    35 import cz.xelfi.quoridor.webidor.resources.Quoridor;
    36 import java.io.File;
    37 import java.io.FileOutputStream;
    38 import java.io.IOException;
    39 import java.io.InputStream;
    40 import java.net.URI;
    41 import java.util.Locale;
    42 import java.util.concurrent.Callable;
    43 import java.util.regex.Matcher;
    44 import java.util.regex.Pattern;
    45 import javax.ws.rs.core.Cookie;
    46 import javax.ws.rs.core.MediaType;
    47 import javax.ws.rs.core.MultivaluedMap;
    48 import javax.ws.rs.core.UriBuilder;
    49 import org.junit.AfterClass;
    50 import org.junit.Before;
    51 import org.junit.BeforeClass;
    52 import org.junit.Test;
    53 import org.netbeans.junit.MockServices;
    54 import static org.junit.Assert.*;
    55 
    56 /**
    57  *
    58  * @author Jaroslav Tulach <jtulach@netbeans.org>
    59  */
    60 public class ChangeEmailTest extends Object {
    61     private static File dir;
    62     private static HttpServer stopAPI;
    63     private static HttpServer stopStatistics;
    64     private static Callable<Void> stop;
    65     private WebResource webResource;
    66     private WebResource apiResource;
    67     private WebResource statResource;
    68     private Client client;
    69 
    70     public ChangeEmailTest() throws Exception {
    71     }
    72 
    73     @BeforeClass
    74     public static void localeEnglish() throws Exception {
    75         Locale.setDefault(Locale.ENGLISH);
    76         dir = File.createTempFile("quoridor", ".dir");
    77         dir.delete();
    78         dir.mkdirs();
    79         System.setProperty("quoridor.dir", dir.getPath());
    80         stopAPI = Quoridor.start(7990);
    81         stopStatistics = Quoridor.start(7992);
    82         stop = UI.startServers(7991, "http://localhost:7990", "http://localhost:7992");
    83 
    84         File passwd = new File(dir, "passwd");
    85         FileOutputStream os = new FileOutputStream(passwd);
    86         os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
    87         os.close();
    88 
    89         MockServices.setServices(MockEmailer.class);
    90     }
    91 
    92     @Before
    93     public void setUp() throws Exception {
    94         MockEmailer.clear();
    95 
    96         client = new Client();
    97         webResource = client.resource(new URI("http://localhost:7991/"));
    98         apiResource = client.resource(new URI("http://localhost:7990/"));
    99         statResource = client.resource(new URI("http://localhost:7992/"));
   100     }
   101 
   102     @AfterClass
   103     public static void cleanUpAll() throws Exception {
   104         deleteRec(dir);
   105         if (stop != null) {
   106             stop.call();
   107         }
   108         if (stopAPI != null) {
   109             stopAPI.stop(0);
   110         }
   111         MockServices.setServices();
   112     }
   113 
   114     static void deleteRec(File dir) throws IOException {
   115         if (dir == null) {
   116             return;
   117         }
   118         File[] arr = dir.listFiles();
   119         if (arr != null) {
   120             for (File f : arr) {
   121                 deleteRec(f);
   122             }
   123         }
   124         dir.delete();
   125     }
   126 
   127     @Test public void testChangeEmail() throws Exception {
   128         String logTest = apiResource.path("login").
   129             queryParam("name", "test").
   130             queryParam("password", "pes").
   131             accept(MediaType.TEXT_PLAIN).
   132             put(String.class);
   133         ClientResponse res = webResource.path("options").
   134             queryParam("email", "xxx@xxx.cz").
   135             cookie(Cookie.valueOf("login=" + logTest)).
   136             accept("text/html").
   137             get(ClientResponse.class);
   138         final String txt = res.getEntity(String.class);
   139 
   140         assertEquals("Emails sent to", "xxx@xxx.cz", MockEmailer.address);
   141 
   142         int urlIndex = MockEmailer.text.indexOf(webResource.getURI().toString());
   143         assertTrue("Found in " + MockEmailer.text, urlIndex >= 0);
   144 
   145         int endIndex = urlIndex;
   146         while (!Character.isWhitespace(MockEmailer.text.charAt(endIndex))) {
   147             endIndex++;
   148         }
   149         final String url = MockEmailer.text.substring(urlIndex, endIndex);
   150         WebResource requestURL = client.resource(url);
   151 
   152         String response = requestURL.accept("text/html").get(String.class);
   153         Pattern p = Pattern.compile("<input *type=\"text\" *name=\"email\" *value='xxx@xxx.cz'");
   154         assertTrue("New email found" + response, p.matcher(response).find());
   155     }
   156 
   157     public static final class MockEmailer extends EmailService {
   158         static String address;
   159         static String subject;
   160         static String text;
   161 
   162         @Override
   163         public void sendEmail(String address, String subject, String text) throws IOException {
   164             assertNull("No email sent yet", MockEmailer.address);
   165             MockEmailer.address = address;
   166             MockEmailer.subject = subject;
   167             MockEmailer.text = text;
   168         }
   169 
   170         public static void clear() {
   171             MockEmailer.address = null;
   172             MockEmailer.subject = null;
   173             MockEmailer.text = null;
   174         }
   175     }
   176 }