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