1 package club.wpia.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.hamcrest.CoreMatchers;
14 import org.junit.Test;
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;
24 public class TestMyDetailsEdit extends ManagedTest {
26 String email = createUniqueName() + "@e.de";
28 int id = createVerifiedUser("Kurti", "Hansel", email, TEST_PASSWORD);
30 String cookie = login(email, TEST_PASSWORD);
32 public TestMyDetailsEdit() throws IOException {}
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);
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);
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));
55 assertEquals(startn + 1, user.getNames().length);
56 assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "removeName=" + n1.getId(), 0));
57 assertEquals(startn, user.getNames().length);
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);
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());
81 public void testChangeDOBInvalid() throws IOException {
82 assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "day=1&month=1&year=test&action=updateDoB", 0));
86 * Tests that changing the date of birth to a too recent one results in an
90 * when web interactions fail.
91 * @see club.wpia.gigi.TestCalendarUtil#testIsOfAge()
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));
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));
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());
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());
124 public void testModifyUserGroup() throws IOException {
125 User user = User.getById(id);
127 assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=" + URLEncoder.encode(Group.LOCATE_AGENT.getDBName(), "UTF-8"), 0));
129 user = User.getById(id);
130 user.refreshGroups();
131 assertTrue(user.isInGroup(Group.LOCATE_AGENT));
134 assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=removeGroup&groupToModify=" + URLEncoder.encode(Group.LOCATE_AGENT.getDBName(), "UTF-8"), 0));
136 user = User.getById(id);
137 user.refreshGroups();
138 assertFalse(user.isInGroup(Group.LOCATE_AGENT));
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));
143 user = User.getById(id);
144 user.refreshGroups();
145 assertFalse(user.isInGroup(Group.SUPPORTER));
147 // test add invalid group
148 assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=non-existing", 0));