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