]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/account/TestMyDetailsEdit.java
Merge "fix: ensure no blanks are entered between name parts and hyphens"
[gigi.git] / tests / club / wpia / gigi / pages / account / TestMyDetailsEdit.java
1 package club.wpia.gigi.pages.account;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.net.URLEncoder;
7 import java.sql.Date;
8 import java.util.Arrays;
9 import java.util.Calendar;
10 import java.util.GregorianCalendar;
11 import java.util.TimeZone;
12
13 import org.hamcrest.CoreMatchers;
14 import org.junit.Test;
15
16 import club.wpia.gigi.GigiApiException;
17 import club.wpia.gigi.dbObjects.Group;
18 import club.wpia.gigi.dbObjects.Name;
19 import club.wpia.gigi.dbObjects.NamePart;
20 import club.wpia.gigi.dbObjects.NamePart.NamePartType;
21 import club.wpia.gigi.dbObjects.User;
22 import club.wpia.gigi.testUtils.ManagedTest;
23
24 public class TestMyDetailsEdit extends ManagedTest {
25
26     String email = createUniqueName() + "@e.de";
27
28     int id = createVerifiedUser("Kurti", "Hansel", email, TEST_PASSWORD);
29
30     String cookie = login(email, TEST_PASSWORD);
31
32     public TestMyDetailsEdit() throws IOException {}
33
34     @Test
35     public void testAddName() throws IOException {
36         int startn = User.getById(id).getNames().length;
37         String newName = createUniqueName();
38         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "name-type=western&fname=" + newName + "&lname=Hansel&action=addName", 0));
39         User u = User.getById(id);
40
41         NamePart[] parts = u.getNames()[startn].getParts();
42         assertThat(Arrays.asList(parts), CoreMatchers.hasItem(new NamePart(NamePartType.FIRST_NAME, newName)));
43         assertThat(Arrays.asList(parts), CoreMatchers.hasItem(new NamePart(NamePartType.LAST_NAME, "Hansel")));
44         assertEquals(2, parts.length);
45         assertEquals(startn + 1, User.getById(id).getNames().length);
46     }
47
48     @Test
49     public void testDelName() throws IOException, GigiApiException {
50         User user = User.getById(id);
51         int startn = user.getNames().length;
52         String newName = createUniqueName();
53         Name n1 = new Name(user, new NamePart(NamePartType.SINGLE_NAME, newName));
54
55         assertEquals(startn + 1, user.getNames().length);
56         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "removeName=" + n1.getId(), 0));
57         assertEquals(startn, user.getNames().length);
58     }
59
60     @Test
61     public void testDelDefaultName() throws IOException {
62         User user = User.getById(id);
63         assertEquals(1, user.getNames().length);
64         assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "removeName=" + user.getNames()[0].getId(), 0));
65         assertEquals(1, user.getNames().length);
66     }
67
68     @Test
69     public void testChangeDOBValid() throws IOException {
70         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "day=1&month=2&year=2000&action=updateDoB", 0));
71         User u = User.getById(id);
72         Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
73         cal.set(Calendar.YEAR, 2000);
74         cal.set(Calendar.DAY_OF_MONTH, Calendar.FEBRUARY);
75         cal.set(Calendar.MONTH, 1);
76         Date d = new Date(cal.getTimeInMillis());
77         assertEquals(d.toString(), u.getDoB().toSQLDate().toString());
78     }
79
80     @Test
81     public void testChangeDOBInvalid() throws IOException {
82         assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "day=1&month=1&year=test&action=updateDoB", 0));
83     }
84
85     /**
86      * Tests that changing the date of birth to a too recent one results in an
87      * error.
88      * 
89      * @throws IOException
90      *             when web interactions fail.
91      * @see club.wpia.gigi.TestCalendarUtil#testIsOfAge()
92      */
93     @Test
94     public void testChangeTooYoung() throws IOException {
95         Calendar c = GregorianCalendar.getInstance();
96         c.add(Calendar.YEAR, -User.MINIMUM_AGE);
97         c.add(Calendar.DAY_OF_MONTH, +2);
98         assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "day=" + c.get(Calendar.DAY_OF_MONTH) + "&month=" + (c.get(Calendar.MONTH) + 1) + "&year=" + c.get(Calendar.YEAR) + "&action=updateDoB", 0));
99     }
100
101     @Test
102     public void testChangeTooOld() throws IOException {
103         Calendar c = GregorianCalendar.getInstance();
104         c.add(Calendar.YEAR, -User.MAXIMUM_PLAUSIBLE_AGE);
105         c.add(Calendar.DAY_OF_MONTH, -2);
106         assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "day=" + c.get(Calendar.DAY_OF_MONTH) + "&month=" + (c.get(Calendar.MONTH) + 1) + "&year=" + c.get(Calendar.YEAR) + "&action=updateDoB", 0));
107     }
108
109     @Test
110     public void testChangeResidenceCountry() throws IOException {
111         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "residenceCountry=DE&action=updateResidenceCountry", 0));
112         User user = User.getById(id);
113         assertEquals("DE", user.getResidenceCountry().getCode());
114     }
115
116     @Test
117     public void testChangeResidenceCountryToNull() throws IOException {
118         User user = User.getById(id);
119         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "residenceCountry=invalid&action=updateResidenceCountry", 0));
120         assertEquals(null, user.getResidenceCountry());
121     }
122
123     @Test
124     public void testModifyUserGroup() throws IOException {
125         User user = User.getById(id);
126         // test add group
127         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=" + URLEncoder.encode(Group.LOCATE_AGENT.getDBName(), "UTF-8"), 0));
128
129         user = User.getById(id);
130         user.refreshGroups();
131         assertTrue(user.isInGroup(Group.LOCATE_AGENT));
132
133         // test remove group
134         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=removeGroup&groupToModify=" + URLEncoder.encode(Group.LOCATE_AGENT.getDBName(), "UTF-8"), 0));
135
136         user = User.getById(id);
137         user.refreshGroups();
138         assertFalse(user.isInGroup(Group.LOCATE_AGENT));
139
140         // test add group that only support can add
141         assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=" + URLEncoder.encode(Group.SUPPORTER.getDBName(), "UTF-8"), 0));
142
143         user = User.getById(id);
144         user.refreshGroups();
145         assertFalse(user.isInGroup(Group.SUPPORTER));
146
147         // test add invalid group
148         assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=non-existing", 0));
149     }
150
151     @Test
152     public void testHyphen() throws IOException {
153
154         String fn = "Hans-Dieter";
155         String fnc = fn;
156         String ln = "Müller-Schmitz";
157         String lnc = ln;
158         testAddName(fn, ln, fnc, lnc);
159
160         fn = "Hans-   Dieter";
161         ln = "Müller-    Schmitz";
162         testAddName(fn, ln, fnc, lnc);
163
164         fn = "Hans    -Dieter";
165         ln = "Müller    -Schmitz";
166         testAddName(fn, ln, fnc, lnc);
167
168         fn = "Hans    -     Dieter";
169         ln = "Müller   -   Schmitz";
170         testAddName(fn, ln, fnc, lnc);
171
172         String[] hyphen = {
173                 "\u002d", "\u058a", "\u05be", "\u1806", "\u2010", "\u2011", "\u2012", "\u2013", "\u2014", "\u2015", "\u2e3a", "\u2e3b", "\ufe58", "\ufe63", "\uff0d"
174         };
175
176         for (int i = 0; i < hyphen.length; i++) {
177             fn = "Hans    " + hyphen[i] + "     Dieter";
178             ln = "Müller   " + hyphen[i] + "   Schmitz";
179             testAddName(fn, ln, fnc, lnc);
180         }
181     }
182
183     @Test
184     public void testBlanks() throws IOException {
185
186         String fn = "Hans";
187         String fnc = fn;
188         String ln = "Müller";
189         String lnc = ln;
190         testAddName(fn, ln, fnc, lnc);
191
192         fn = "Hans  ";
193         ln = "Müller  ";
194         testAddName(fn, ln, fnc, lnc);
195
196         fn = "    Hans";
197         ln = "    Müller";
198         testAddName(fn, ln, fnc, lnc);
199
200         fn = "Hans    Dieter";
201         ln = "Müller    Schmitz";
202         testAddName(fn, ln, fnc, lnc, 4);
203
204         fn = "Hans    Dieter   ";
205         ln = "    Müller    Schmitz";
206         testAddName(fn, ln, fnc, lnc, 4);
207     }
208
209     public void testAddName(String fn, String ln, String fnc, String lnc) throws IOException {
210         testAddName(fn, ln, fnc, lnc, 2);
211     }
212
213     public void testAddName(String fn, String ln, String fnc, String lnc, int partLength) throws IOException {
214         int startn = User.getById(id).getNames().length;
215         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addName&name-type=western&fname=" + fn + "&lname=" + ln, 0));
216         User u = User.getById(id);
217
218         NamePart[] parts = u.getNames()[startn].getParts();
219         assertThat(Arrays.asList(parts), CoreMatchers.hasItem(new NamePart(NamePartType.FIRST_NAME, fnc)));
220         assertThat(Arrays.asList(parts), CoreMatchers.hasItem(new NamePart(NamePartType.LAST_NAME, lnc)));
221         assertEquals(partLength, parts.length);
222         assertEquals(startn + 1, User.getById(id).getNames().length);
223     }
224 }