webidor/src/test/java/cz/xelfi/quoridor/webidor/UsersTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 07 Nov 2009 23:26:03 +0100
changeset 145 ac9bd9be5263
parent 144 cc04ede4cb5e
child 239 a47345ebbdd7
permissions -rw-r--r--
Ability to get User by loginID and making sure tests pass in the same VM
     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 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.webidor;
    28 
    29 import com.sun.jersey.api.client.UniformInterfaceException;
    30 import com.sun.jersey.test.framework.JerseyTest;
    31 import java.io.File;
    32 import java.io.FileOutputStream;
    33 import java.io.IOException;
    34 import java.util.Properties;
    35 import javax.ws.rs.core.MediaType;
    36 import org.junit.Test;
    37 import static org.junit.Assert.*;
    38 
    39 /**
    40  *
    41  * @author Jaroslav Tulach <jtulach@netbeans.org>
    42  */
    43 public class UsersTest extends JerseyTest {
    44     static {
    45         System.setProperty("JERSEY_HTTP_PORT", "39434");
    46     }
    47 
    48     private File dir;
    49 
    50     public UsersTest() throws Exception {
    51         super("cz.xelfi.quoridor.webidor.resources");
    52     }
    53 
    54     @Override
    55     public void setUp() throws Exception {
    56         dir = File.createTempFile("quoridor", ".dir");
    57         dir.delete();
    58         System.setProperty("quoridor.dir", dir.getPath());
    59         dir.mkdirs();
    60         File passwd = new File(dir, "passwd");
    61         FileOutputStream os = new FileOutputStream(passwd);
    62         os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    63         os.close();
    64         super.setUp();
    65     }
    66 
    67     @Override
    68     public void tearDown() throws Exception {
    69         deleteRec(dir);
    70     }
    71 
    72     static void deleteRec(File dir) throws IOException {
    73         if (dir == null) {
    74             return;
    75         }
    76         File[] arr = dir.listFiles();
    77         if (arr != null) {
    78             for (File f : arr) {
    79                 deleteRec(f);
    80             }
    81         }
    82         dir.delete();
    83     }
    84 
    85     @Test public void testListUsers() throws Exception {
    86         File usersDir = new File(dir, "users");
    87         usersDir.mkdirs();
    88         File fJarda = new File(usersDir, "Jarda");
    89         {
    90             Properties p = new Properties();
    91             p.setProperty("email", "jar@da.cz");
    92             p.setProperty("permission.email", "true");
    93             p.store(new FileOutputStream(fJarda), "");
    94         }
    95         File fJirka = new File(usersDir, "Jirka");
    96         {
    97             Properties p = new Properties();
    98             p.setProperty("email", "jir@ka.cz");
    99             p.store(new FileOutputStream(fJirka), "");
   100         }
   101 
   102         String logJarda = webResource.path("login").
   103             queryParam("name", "Jarda").
   104             queryParam("password", "heslo").
   105             accept(MediaType.TEXT_PLAIN).
   106             put(String.class);
   107         String logJirka = webResource.path("login").
   108             queryParam("name", "Jirka").
   109             queryParam("password", "pesko").
   110             accept(MediaType.TEXT_PLAIN).
   111             put(String.class);
   112 
   113         User uJirka = webResource.path("users/Jirka").accept(MediaType.TEXT_XML).get(User.class);
   114         assertEquals("Jirka", uJirka.getId());
   115         assertNull("Cannot get email without login", uJirka.getProperty("email"));
   116 
   117 
   118         uJirka = webResource.path("users/Jirka").queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(User.class);
   119         assertEquals("Jirka", uJirka.getId());
   120         assertEquals("Email for ownself is OK", "jir@ka.cz", uJirka.getProperty("email"));
   121 
   122         uJirka = webResource.path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(User.class);
   123         assertEquals("Jirka", uJirka.getId());
   124         assertEquals("Jarda has permission for property email", "jir@ka.cz", uJirka.getProperty("email"));
   125 
   126         String txt = webResource.path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(String.class);
   127         if (!txt.contains("<user id=\"Jirka\"><property name=\"email\">jir@ka.cz</property></user>")) {
   128             fail(txt);
   129         }
   130 
   131         try {
   132             webResource.path("users/Jarda").queryParam("loginID", logJirka).
   133                     queryParam("name", "email").queryParam("value", "ka@jir.cz").accept(MediaType.TEXT_XML).post();
   134             fail("You cannot change email without priviledges");
   135         } catch (UniformInterfaceException e) {
   136             // OK, not allowed
   137         }
   138 
   139         webResource.path("users/Jirka").queryParam("loginID", logJirka).
   140                 queryParam("name", "email").queryParam("value", "ka@jir.cz").accept(MediaType.TEXT_XML).post();
   141 
   142         uJirka = webResource.path("users/Jirka").queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(User.class);
   143         assertEquals("Jirka", uJirka.getId());
   144         assertEquals("Email for ownself is OK", "ka@jir.cz", uJirka.getProperty("email"));
   145 
   146         try {
   147             webResource.path("users/Jirka").queryParam("loginID", logJirka).
   148                     queryParam("name", "permission.email").queryParam("value", "true").accept(MediaType.TEXT_XML).post();
   149             fail("Shall not be allowed to change own permissions");
   150         } catch (UniformInterfaceException ex) {
   151             // OK, not allowed
   152         }
   153     }
   154 }