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.api.client.GenericType;
30 import com.sun.jersey.api.client.UniformInterfaceException;
31 import com.sun.jersey.test.framework.JerseyTest;
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.*;
43 * @author Jaroslav Tulach <jtulach@netbeans.org>
45 public class UsersTest extends JerseyTest {
48 public UsersTest() throws Exception {
49 super("cz.xelfi.quoridor.webidor.resources");
53 public void setUp() throws Exception {
54 dir = File.createTempFile("quoridor", ".dir");
56 System.setProperty("quoridor.dir", dir.getPath());
58 File passwd = new File(dir, "passwd");
59 FileOutputStream os = new FileOutputStream(passwd);
60 os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
66 public void tearDown() throws Exception {
70 static void deleteRec(File dir) throws IOException {
74 File[] arr = dir.listFiles();
83 @Test public void testListUsers() throws Exception {
84 File usersDir = new File(dir, "users");
86 File fJarda = new File(usersDir, "Jarda");
88 Properties p = new Properties();
89 p.setProperty("email", "jar@da.cz");
90 p.setProperty("permission.email", "true");
91 p.store(new FileOutputStream(fJarda), "");
93 File fJirka = new File(usersDir, "Jirka");
95 Properties p = new Properties();
96 p.setProperty("email", "jir@ka.cz");
97 p.store(new FileOutputStream(fJirka), "");
100 String logJarda = webResource.path("login").
101 queryParam("name", "Jarda").
102 queryParam("password", "heslo").
103 accept(MediaType.TEXT_PLAIN).
105 String logJirka = webResource.path("login").
106 queryParam("name", "Jirka").
107 queryParam("password", "pesko").
108 accept(MediaType.TEXT_PLAIN).
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"));
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"));
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"));
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>")) {
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) {
137 webResource.path("users/Jirka").queryParam("loginID", logJirka).
138 queryParam("name", "email").queryParam("value", "ka@jir.cz").accept(MediaType.TEXT_XML).post();
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"));
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) {