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.User;
21 import club.wpia.gigi.dbObjects.NamePart.NamePartType;
22 import club.wpia.gigi.pages.account.MyDetails;
23 import club.wpia.gigi.testUtils.ManagedTest;
25 public class TestMyDetailsEdit extends ManagedTest {
27 String email = createUniqueName() + "@e.de";
29 int id = createVerifiedUser("Kurti", "Hansel", email, TEST_PASSWORD);
31 String cookie = login(email, TEST_PASSWORD);
33 public TestMyDetailsEdit() throws IOException {}
36 public void testAddName() throws IOException {
37 int startn = User.getById(id).getNames().length;
38 String newName = createUniqueName();
39 assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "name-type=western&fname=" + newName + "&lname=Hansel&action=addName", 0));
40 User u = User.getById(id);
42 NamePart[] parts = u.getNames()[startn].getParts();
43 assertThat(Arrays.asList(parts), CoreMatchers.hasItem(new NamePart(NamePartType.FIRST_NAME, newName)));
44 assertThat(Arrays.asList(parts), CoreMatchers.hasItem(new NamePart(NamePartType.LAST_NAME, "Hansel")));
45 assertEquals(2, parts.length);
46 assertEquals(startn + 1, User.getById(id).getNames().length);
50 public void testDelName() throws IOException, GigiApiException {
51 User user = User.getById(id);
52 int startn = user.getNames().length;
53 String newName = createUniqueName();
54 Name n1 = new Name(user, new NamePart(NamePartType.SINGLE_NAME, newName));
56 assertEquals(startn + 1, user.getNames().length);
57 assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "removeName=" + n1.getId(), 0));
58 assertEquals(startn, user.getNames().length);
62 public void testDelDefaultName() throws IOException {
63 User user = User.getById(id);
64 assertEquals(1, user.getNames().length);
65 assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "removeName=" + user.getNames()[0].getId(), 0));
66 assertEquals(1, user.getNames().length);
70 public void testChangeDOBValid() throws IOException {
71 assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "day=1&month=2&year=2000&action=updateDoB", 0));
72 User u = User.getById(id);
73 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
74 cal.set(Calendar.YEAR, 2000);
75 cal.set(Calendar.DAY_OF_MONTH, Calendar.FEBRUARY);
76 cal.set(Calendar.MONTH, 1);
77 Date d = new Date(cal.getTimeInMillis());
78 assertEquals(d.toString(), u.getDoB().toSQLDate().toString());
82 public void testChangeDOBInvalid() throws IOException {
83 assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "day=1&month=1&year=test&action=updateDoB", 0));
87 public void testChangeTooYoung() throws IOException {
88 Calendar c = GregorianCalendar.getInstance();
89 c.add(Calendar.YEAR, -User.MINIMUM_AGE);
90 c.add(Calendar.DAY_OF_MONTH, +1);
91 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));
95 public void testChangeTooOld() throws IOException {
96 Calendar c = GregorianCalendar.getInstance();
97 c.add(Calendar.YEAR, -User.MAXIMUM_PLAUSIBLE_AGE);
98 c.add(Calendar.DAY_OF_MONTH, -1);
99 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));
103 public void testChangeResidenceCountry() throws IOException {
104 assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "residenceCountry=DE&action=updateResidenceCountry", 0));
105 User user = User.getById(id);
106 assertEquals("DE", user.getResidenceCountry().getCode());
110 public void testChangeResidenceCountryToNull() throws IOException {
111 User user = User.getById(id);
112 assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "residenceCountry=invalid&action=updateResidenceCountry", 0));
113 assertEquals(null, user.getResidenceCountry());
117 public void testModifyUserGroup() throws IOException {
118 User user = User.getById(id);
120 assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=" + URLEncoder.encode(Group.LOCATE_AGENT.getDBName(), "UTF-8"), 0));
122 user = User.getById(id);
123 user.refreshGroups();
124 assertTrue(user.isInGroup(Group.LOCATE_AGENT));
127 assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=removeGroup&groupToModify=" + URLEncoder.encode(Group.LOCATE_AGENT.getDBName(), "UTF-8"), 0));
129 user = User.getById(id);
130 user.refreshGroups();
131 assertFalse(user.isInGroup(Group.LOCATE_AGENT));
133 // test add group that only support can add
134 assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=" + URLEncoder.encode(Group.SUPPORTER.getDBName(), "UTF-8"), 0));
136 user = User.getById(id);
137 user.refreshGroups();
138 assertFalse(user.isInGroup(Group.SUPPORTER));
140 // test add invalid group
141 assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=non-existing", 0));