2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
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]"
24 * Portions Copyrighted 2009 Jaroslav Tulach
27 package cz.xelfi.quoridor.webidor;
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;
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.*;
44 * @author Jaroslav Tulach <jtulach@netbeans.org>
46 public class UsersTest extends JerseyTest {
48 System.setProperty("JERSEY_HTTP_PORT", "39434");
54 protected AppDescriptor configure() {
56 dir = File.createTempFile("quoridor", ".dir");
58 System.setProperty("quoridor.dir", dir.getPath());
60 File passwd = new File(dir, "passwd");
61 FileOutputStream os = new FileOutputStream(passwd);
62 os.write("Jarda=heslo\n".getBytes("UTF-8"));
64 File usersDir = new File(dir, "users");
66 File fJirka = new File(usersDir, "Jirka");
68 Properties p = new Properties();
69 p.setProperty("email", "jir@ka.cz");
70 p.setProperty("passwd", "pesko");
71 p.store(new FileOutputStream(fJirka), "");
73 } catch (Exception ex) {
74 throw new IllegalStateException(ex);
76 return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").build();
80 public void tearDown() throws Exception {
84 static void deleteRec(File dir) throws IOException {
88 File[] arr = dir.listFiles();
97 @Test public void testListUsers() throws Exception {
98 File usersDir = new File(dir, "users");
100 File fJarda = new File(usersDir, "Jarda");
102 Properties p = new Properties();
103 p.setProperty("email", "jar@da.cz");
104 p.setProperty("permission.email", "true");
105 p.store(new FileOutputStream(fJarda), "");
107 File fJirka = new File(usersDir, "Jirka");
109 Properties p = new Properties();
110 p.load(new FileInputStream(fJirka));
111 p.setProperty("email", "jir@ka.cz");
112 p.store(new FileOutputStream(fJirka), "");
115 String logJarda = resource().path("login").
116 queryParam("name", "Jarda").
117 queryParam("password", "heslo").
118 accept(MediaType.TEXT_PLAIN).
120 String logJirka = resource().path("login").
121 queryParam("name", "Jirka").
122 queryParam("password", "pesko").
123 accept(MediaType.TEXT_PLAIN).
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"));
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"));
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"));
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>")) {
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) {
152 resource().path("users/Jirka").queryParam("loginID", logJirka).
153 queryParam("name", "email").queryParam("value", "ka@jir.cz").accept(MediaType.TEXT_XML).post();
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"));
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) {