]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/account/TestMyDetailsEdit.java
upd: rename package name and all references to it
[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.User;
21 import club.wpia.gigi.dbObjects.NamePart.NamePartType;
22 import club.wpia.gigi.pages.account.MyDetails;
23 import club.wpia.gigi.testUtils.ManagedTest;
24
25 public class TestMyDetailsEdit extends ManagedTest {
26
27     String email = createUniqueName() + "@e.de";
28
29     int id = createVerifiedUser("Kurti", "Hansel", email, TEST_PASSWORD);
30
31     String cookie = login(email, TEST_PASSWORD);
32
33     public TestMyDetailsEdit() throws IOException {}
34
35     @Test
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);
41
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);
47     }
48
49     @Test
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));
55
56         assertEquals(startn + 1, user.getNames().length);
57         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "removeName=" + n1.getId(), 0));
58         assertEquals(startn, user.getNames().length);
59     }
60
61     @Test
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);
67     }
68
69     @Test
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());
79     }
80
81     @Test
82     public void testChangeDOBInvalid() throws IOException {
83         assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "day=1&month=1&year=test&action=updateDoB", 0));
84     }
85
86     @Test
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));
92     }
93
94     @Test
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));
100     }
101
102     @Test
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());
107     }
108
109     @Test
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());
114     }
115
116     @Test
117     public void testModifyUserGroup() throws IOException {
118         User user = User.getById(id);
119         // test add group
120         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=" + URLEncoder.encode(Group.LOCATE_AGENT.getDBName(), "UTF-8"), 0));
121
122         user = User.getById(id);
123         user.refreshGroups();
124         assertTrue(user.isInGroup(Group.LOCATE_AGENT));
125
126         // test remove group
127         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=removeGroup&groupToModify=" + URLEncoder.encode(Group.LOCATE_AGENT.getDBName(), "UTF-8"), 0));
128
129         user = User.getById(id);
130         user.refreshGroups();
131         assertFalse(user.isInGroup(Group.LOCATE_AGENT));
132
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));
135
136         user = User.getById(id);
137         user.refreshGroups();
138         assertFalse(user.isInGroup(Group.SUPPORTER));
139
140         // test add invalid group
141         assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=non-existing", 0));
142     }
143 }