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