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.UniformInterfaceException;
30 import com.sun.jersey.test.framework.JerseyTest;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.util.Properties;
35 import javax.ws.rs.core.MediaType;
36 import org.junit.Test;
37 import static org.junit.Assert.*;
41 * @author Jaroslav Tulach <jtulach@netbeans.org>
43 public class UsersTest extends JerseyTest {
45 System.setProperty("JERSEY_HTTP_PORT", "39434");
50 public UsersTest() throws Exception {
51 super("cz.xelfi.quoridor.webidor.resources");
55 public void setUp() throws Exception {
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\nJirka=pesko\n".getBytes("UTF-8"));
68 public void tearDown() throws Exception {
72 static void deleteRec(File dir) throws IOException {
76 File[] arr = dir.listFiles();
85 @Test public void testListUsers() throws Exception {
86 File usersDir = new File(dir, "users");
88 File fJarda = new File(usersDir, "Jarda");
90 Properties p = new Properties();
91 p.setProperty("email", "jar@da.cz");
92 p.setProperty("permission.email", "true");
93 p.store(new FileOutputStream(fJarda), "");
95 File fJirka = new File(usersDir, "Jirka");
97 Properties p = new Properties();
98 p.setProperty("email", "jir@ka.cz");
99 p.store(new FileOutputStream(fJirka), "");
102 String logJarda = webResource.path("login").
103 queryParam("name", "Jarda").
104 queryParam("password", "heslo").
105 accept(MediaType.TEXT_PLAIN).
107 String logJirka = webResource.path("login").
108 queryParam("name", "Jirka").
109 queryParam("password", "pesko").
110 accept(MediaType.TEXT_PLAIN).
113 User uJirka = webResource.path("users/Jirka").accept(MediaType.TEXT_XML).get(User.class);
114 assertEquals("Jirka", uJirka.getId());
115 assertNull("Cannot get email without login", uJirka.getProperty("email"));
118 uJirka = webResource.path("users/Jirka").queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(User.class);
119 assertEquals("Jirka", uJirka.getId());
120 assertEquals("Email for ownself is OK", "jir@ka.cz", uJirka.getProperty("email"));
122 uJirka = webResource.path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(User.class);
123 assertEquals("Jirka", uJirka.getId());
124 assertEquals("Jarda has permission for property email", "jir@ka.cz", uJirka.getProperty("email"));
126 String txt = webResource.path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(String.class);
127 if (!txt.contains("<user id=\"Jirka\"><property name=\"email\">jir@ka.cz</property></user>")) {
132 webResource.path("users/Jarda").queryParam("loginID", logJirka).
133 queryParam("name", "email").queryParam("value", "ka@jir.cz").accept(MediaType.TEXT_XML).post();
134 fail("You cannot change email without priviledges");
135 } catch (UniformInterfaceException e) {
139 webResource.path("users/Jirka").queryParam("loginID", logJirka).
140 queryParam("name", "email").queryParam("value", "ka@jir.cz").accept(MediaType.TEXT_XML).post();
142 uJirka = webResource.path("users/Jirka").queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(User.class);
143 assertEquals("Jirka", uJirka.getId());
144 assertEquals("Email for ownself is OK", "ka@jir.cz", uJirka.getProperty("email"));
147 webResource.path("users/Jirka").queryParam("loginID", logJirka).
148 queryParam("name", "permission.email").queryParam("value", "true").accept(MediaType.TEXT_XML).post();
149 fail("Shall not be allowed to change own permissions");
150 } catch (UniformInterfaceException ex) {