2 * Quoridor server and related libraries
3 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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/.
18 package cz.xelfi.quoridor.webidor;
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;
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.*;
35 * @author Jaroslav Tulach <jtulach@netbeans.org>
37 public class UsersTest extends JerseyTest {
39 System.setProperty("JERSEY_HTTP_PORT", "39434");
45 protected AppDescriptor configure() {
47 dir = File.createTempFile("quoridor", ".dir");
49 System.setProperty("quoridor.dir", dir.getPath());
51 File passwd = new File(dir, "passwd");
52 FileOutputStream os = new FileOutputStream(passwd);
53 os.write("Jarda=heslo\n".getBytes("UTF-8"));
55 File usersDir = new File(dir, "users");
57 File fJirka = new File(usersDir, "Jirka");
59 Properties p = new Properties();
60 p.setProperty("email", "jir@ka.cz");
61 p.setProperty("passwd", "pesko");
62 p.store(new FileOutputStream(fJirka), "");
64 } catch (Exception ex) {
65 throw new IllegalStateException(ex);
67 return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").build();
71 public void tearDown() throws Exception {
75 static void deleteRec(File dir) throws IOException {
79 File[] arr = dir.listFiles();
88 @Test public void testListUsers() throws Exception {
89 File usersDir = new File(dir, "users");
91 File fJarda = new File(usersDir, "Jarda");
93 Properties p = new Properties();
94 p.setProperty("email", "jar@da.cz");
95 p.setProperty("permission.email", "true");
96 p.store(new FileOutputStream(fJarda), "");
98 File fJirka = new File(usersDir, "Jirka");
100 Properties p = new Properties();
101 p.load(new FileInputStream(fJirka));
102 p.setProperty("email", "jir@ka.cz");
103 p.store(new FileOutputStream(fJirka), "");
106 String logJarda = resource().path("login").
107 queryParam("name", "Jarda").
108 queryParam("password", "heslo").
109 accept(MediaType.TEXT_PLAIN).
111 String logJirka = resource().path("login").
112 queryParam("name", "Jirka").
113 queryParam("password", "pesko").
114 accept(MediaType.TEXT_PLAIN).
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"));
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"));
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"));
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>")) {
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) {
143 resource().path("users/Jirka").queryParam("loginID", logJirka).
144 queryParam("name", "email").queryParam("value", "ka@jir.cz").accept(MediaType.TEXT_XML).post();
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"));
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) {