webidor/src/test/java/cz/xelfi/quoridor/webidor/UsersTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 20 Aug 2011 17:14:23 +0200
branchglassfish
changeset 285 bc4ddef89763
parent 264 d60370059c3c
permissions -rw-r--r--
Adjusting tests to use different path
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 package cz.xelfi.quoridor.webidor;
    19 
    20 import com.sun.jersey.test.framework.WebAppDescriptor;
    21 import com.sun.jersey.test.framework.AppDescriptor;
    22 import java.io.FileInputStream;
    23 import com.sun.jersey.api.client.UniformInterfaceException;
    24 import com.sun.jersey.test.framework.JerseyTest;
    25 import java.io.File;
    26 import java.io.FileOutputStream;
    27 import java.io.IOException;
    28 import java.util.Properties;
    29 import javax.ws.rs.core.MediaType;
    30 import org.junit.Test;
    31 import static org.junit.Assert.*;
    32 
    33 /**
    34  *
    35  * @author Jaroslav Tulach <jtulach@netbeans.org>
    36  */
    37 public class UsersTest extends JerseyTest {
    38     static {
    39         System.setProperty("JERSEY_HTTP_PORT", "39434");
    40     }
    41 
    42     private File dir;
    43 
    44     @Override
    45     protected AppDescriptor configure() {
    46         try {
    47             dir = File.createTempFile("quoridor", ".dir");
    48             dir.delete();
    49             System.setProperty("quoridor.dir", dir.getPath());
    50             dir.mkdirs();
    51             File passwd = new File(dir, "passwd");
    52             FileOutputStream os = new FileOutputStream(passwd);
    53             os.write("Jarda=heslo\n".getBytes("UTF-8"));
    54             os.close();
    55             File usersDir = new File(dir, "users");
    56             usersDir.mkdirs();
    57             File fJirka = new File(usersDir, "Jirka");
    58             {
    59                 Properties p = new Properties();
    60                 p.setProperty("email", "jir@ka.cz");
    61                 p.setProperty("passwd", "pesko");
    62                 p.store(new FileOutputStream(fJirka), "");
    63             }
    64         } catch (Exception ex) {
    65             throw new IllegalStateException(ex);
    66         }
    67         return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").contextPath("userstest").build();
    68     }
    69 
    70     @Override
    71     public void tearDown() throws Exception {
    72         deleteRec(dir);
    73     }
    74 
    75     static void deleteRec(File dir) throws IOException {
    76         if (dir == null) {
    77             return;
    78         }
    79         File[] arr = dir.listFiles();
    80         if (arr != null) {
    81             for (File f : arr) {
    82                 deleteRec(f);
    83             }
    84         }
    85         dir.delete();
    86     }
    87 
    88     @Test public void testListUsers() throws Exception {
    89         File usersDir = new File(dir, "users");
    90         usersDir.mkdirs();
    91         File fJarda = new File(usersDir, "Jarda");
    92         {
    93             Properties p = new Properties();
    94             p.setProperty("email", "jar@da.cz");
    95             p.setProperty("permission.email", "true");
    96             p.store(new FileOutputStream(fJarda), "");
    97         }
    98         File fJirka = new File(usersDir, "Jirka");
    99         {
   100             Properties p = new Properties();
   101             p.load(new FileInputStream(fJirka));
   102             p.setProperty("email", "jir@ka.cz");
   103             p.store(new FileOutputStream(fJirka), "");
   104         }
   105 
   106         String logJarda = resource().path("login").
   107             queryParam("name", "Jarda").
   108             queryParam("password", "heslo").
   109             accept(MediaType.TEXT_PLAIN).
   110             put(String.class);
   111         String logJirka = resource().path("login").
   112             queryParam("name", "Jirka").
   113             queryParam("password", "pesko").
   114             accept(MediaType.TEXT_PLAIN).
   115             put(String.class);
   116 
   117         User uJirka = resource().path("users/Jirka").accept(MediaType.TEXT_XML).get(User.class);
   118         assertEquals("Jirka", uJirka.getId());
   119         assertNull("Cannot get email without login", uJirka.getProperty("email"));
   120 
   121 
   122         uJirka = resource().path("users/Jirka").queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(User.class);
   123         assertEquals("Jirka", uJirka.getId());
   124         assertEquals("Email for ownself is OK", "jir@ka.cz", uJirka.getProperty("email"));
   125 
   126         uJirka = resource().path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(User.class);
   127         assertEquals("Jirka", uJirka.getId());
   128         assertEquals("Jarda has permission for property email", "jir@ka.cz", uJirka.getProperty("email"));
   129 
   130         String txt = resource().path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(String.class);
   131         if (!txt.contains("<user id=\"Jirka\"><property name=\"email\">jir@ka.cz</property></user>")) {
   132             fail(txt);
   133         }
   134 
   135         try {
   136             resource().path("users/Jarda").queryParam("loginID", logJirka).
   137                     queryParam("name", "email").queryParam("value", "ka@jir.cz").accept(MediaType.TEXT_XML).post();
   138             fail("You cannot change email without priviledges");
   139         } catch (UniformInterfaceException e) {
   140             // OK, not allowed
   141         }
   142 
   143         resource().path("users/Jirka").queryParam("loginID", logJirka).
   144                 queryParam("name", "email").queryParam("value", "ka@jir.cz").accept(MediaType.TEXT_XML).post();
   145 
   146         uJirka = resource().path("users/Jirka").queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(User.class);
   147         assertEquals("Jirka", uJirka.getId());
   148         assertEquals("Email for ownself is OK", "ka@jir.cz", uJirka.getProperty("email"));
   149 
   150         try {
   151             resource().path("users/Jirka").queryParam("loginID", logJirka).
   152                     queryParam("name", "permission.email").queryParam("value", "true").accept(MediaType.TEXT_XML).post();
   153             fail("Shall not be allowed to change own permissions");
   154         } catch (UniformInterfaceException ex) {
   155             // OK, not allowed
   156         }
   157     }
   158 }