1 package org.cacert.gigi.pages.account;
3 import static org.junit.Assert.*;
5 import java.io.IOException;
6 import java.net.URLEncoder;
8 import java.util.Arrays;
9 import java.util.Calendar;
10 import java.util.GregorianCalendar;
11 import java.util.TimeZone;
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;
23 public class TestMyDetailsEdit extends ManagedTest {
25 String email = createUniqueName() + "@e.de";
27 int id = createVerifiedUser("Kurti", "Hansel", email, TEST_PASSWORD);
29 String cookie = login(email, TEST_PASSWORD);
31 public TestMyDetailsEdit() throws IOException {}
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);
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);
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));
54 assertEquals(startn + 1, user.getNames().length);
55 assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "removeName=" + n1.getId(), 0));
56 assertEquals(startn, user.getNames().length);
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);
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());
80 public void testChangeDOBInvalid() throws IOException {
81 assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "day=1&month=1&year=test&action=updateDoB", 0));
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));
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));
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());
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());
115 public void testModifyUserGroup() throws IOException {
116 User user = User.getById(id);
118 assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=" + URLEncoder.encode(Group.LOCATE_AGENT.getDBName(), "UTF-8"), 0));
120 user = User.getById(id);
121 user.refreshGroups();
122 assertTrue(user.isInGroup(Group.LOCATE_AGENT));
125 assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=removeGroup&groupToModify=" + URLEncoder.encode(Group.LOCATE_AGENT.getDBName(), "UTF-8"), 0));
127 user = User.getById(id);
128 user.refreshGroups();
129 assertFalse(user.isInGroup(Group.LOCATE_AGENT));
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));
134 user = User.getById(id);
135 user.refreshGroups();
136 assertFalse(user.isInGroup(Group.SUPPORTER));
138 // test add invalid group
139 assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=non-existing", 0));