]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/account/TestMyDetailsEdit.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / tests / org / cacert / gigi / pages / account / TestMyDetailsEdit.java
1 package org.cacert.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.cacert.gigi.GigiApiException;
14 import org.cacert.gigi.dbObjects.Group;
15 import org.cacert.gigi.dbObjects.Name;
16 import org.cacert.gigi.dbObjects.NamePart;
17 import org.cacert.gigi.dbObjects.NamePart.NamePartType;
18 import org.cacert.gigi.dbObjects.User;
19 import org.cacert.gigi.testUtils.ManagedTest;
20 import org.hamcrest.CoreMatchers;
21 import org.junit.Test;
22
23 public class TestMyDetailsEdit extends ManagedTest {
24
25     String email = createUniqueName() + "@e.de";
26
27     int id = createVerifiedUser("Kurti", "Hansel", email, TEST_PASSWORD);
28
29     String cookie = login(email, TEST_PASSWORD);
30
31     public TestMyDetailsEdit() throws IOException {}
32
33     @Test
34     public void testAddName() throws IOException {
35         int startn = User.getById(id).getNames().length;
36         String newName = createUniqueName();
37         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "name-type=western&fname=" + newName + "&lname=Hansel&action=addName", 0));
38         User u = User.getById(id);
39
40         NamePart[] parts = u.getNames()[startn].getParts();
41         assertThat(Arrays.asList(parts), CoreMatchers.hasItem(new NamePart(NamePartType.FIRST_NAME, newName)));
42         assertThat(Arrays.asList(parts), CoreMatchers.hasItem(new NamePart(NamePartType.LAST_NAME, "Hansel")));
43         assertEquals(2, parts.length);
44         assertEquals(startn + 1, User.getById(id).getNames().length);
45     }
46
47     @Test
48     public void testDelName() throws IOException, GigiApiException {
49         User user = User.getById(id);
50         int startn = user.getNames().length;
51         String newName = createUniqueName();
52         Name n1 = new Name(user, new NamePart(NamePartType.SINGLE_NAME, newName));
53
54         assertEquals(startn + 1, user.getNames().length);
55         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "removeName=" + n1.getId(), 0));
56         assertEquals(startn, user.getNames().length);
57     }
58
59     @Test
60     public void testDelDefaultName() throws IOException {
61         User user = User.getById(id);
62         assertEquals(1, user.getNames().length);
63         assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "removeName=" + user.getNames()[0].getId(), 0));
64         assertEquals(1, user.getNames().length);
65     }
66
67     @Test
68     public void testChangeDOBValid() throws IOException {
69         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "day=1&month=2&year=2000&action=updateDoB", 0));
70         User u = User.getById(id);
71         Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
72         cal.set(Calendar.YEAR, 2000);
73         cal.set(Calendar.DAY_OF_MONTH, Calendar.FEBRUARY);
74         cal.set(Calendar.MONTH, 1);
75         Date d = new Date(cal.getTimeInMillis());
76         assertEquals(d.toString(), u.getDoB().toSQLDate().toString());
77     }
78
79     @Test
80     public void testChangeDOBInvalid() throws IOException {
81         assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "day=1&month=1&year=test&action=updateDoB", 0));
82     }
83
84     @Test
85     public void testChangeTooYoung() throws IOException {
86         Calendar c = GregorianCalendar.getInstance();
87         c.add(Calendar.YEAR, -User.MINIMUM_AGE);
88         c.add(Calendar.DAY_OF_MONTH, +1);
89         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));
90     }
91
92     @Test
93     public void testChangeTooOld() throws IOException {
94         Calendar c = GregorianCalendar.getInstance();
95         c.add(Calendar.YEAR, -User.MAXIMUM_PLAUSIBLE_AGE);
96         c.add(Calendar.DAY_OF_MONTH, -1);
97         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));
98     }
99
100     @Test
101     public void testChangeResidenceCountry() throws IOException {
102         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "residenceCountry=DE&action=updateResidenceCountry", 0));
103         User user = User.getById(id);
104         assertEquals("DE", user.getResidenceCountry().getCode());
105     }
106
107     @Test
108     public void testChangeResidenceCountryToNull() throws IOException {
109         User user = User.getById(id);
110         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "residenceCountry=invalid&action=updateResidenceCountry", 0));
111         assertEquals(null, user.getResidenceCountry());
112     }
113
114     @Test
115     public void testModifyUserGroup() throws IOException {
116         User user = User.getById(id);
117         // test add group
118         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=" + URLEncoder.encode(Group.LOCATE_AGENT.getDBName(), "UTF-8"), 0));
119
120         user = User.getById(id);
121         user.refreshGroups();
122         assertTrue(user.isInGroup(Group.LOCATE_AGENT));
123
124         // test remove group
125         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=removeGroup&groupToModify=" + URLEncoder.encode(Group.LOCATE_AGENT.getDBName(), "UTF-8"), 0));
126
127         user = User.getById(id);
128         user.refreshGroups();
129         assertFalse(user.isInGroup(Group.LOCATE_AGENT));
130
131         // test add group that only support can add
132         assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=" + URLEncoder.encode(Group.SUPPORTER.getDBName(), "UTF-8"), 0));
133
134         user = User.getById(id);
135         user.refreshGroups();
136         assertFalse(user.isInGroup(Group.SUPPORTER));
137
138         // test add invalid group
139         assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=non-existing", 0));
140     }
141 }