]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/account/TestMyDetailsEdit.java
Merge "fix: make MyDetails/SwitchToOrg work again"
[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.NamePart.NamePartType;
21 import club.wpia.gigi.dbObjects.User;
22 import club.wpia.gigi.testUtils.ManagedTest;
23
24 public class TestMyDetailsEdit extends ManagedTest {
25
26     String email = createUniqueName() + "@e.de";
27
28     int id = createVerifiedUser("Kurti", "Hansel", email, TEST_PASSWORD);
29
30     String cookie = login(email, TEST_PASSWORD);
31
32     public TestMyDetailsEdit() throws IOException {}
33
34     @Test
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);
40
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);
46     }
47
48     @Test
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));
54
55         assertEquals(startn + 1, user.getNames().length);
56         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "removeName=" + n1.getId(), 0));
57         assertEquals(startn, user.getNames().length);
58     }
59
60     @Test
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);
66     }
67
68     @Test
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());
78     }
79
80     @Test
81     public void testChangeDOBInvalid() throws IOException {
82         assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "day=1&month=1&year=test&action=updateDoB", 0));
83     }
84
85     /**
86      * Tests that changing the date of birth to a too recent one results in an
87      * error.
88      * 
89      * @throws IOException
90      *             when web interactions fail.
91      * @see club.wpia.gigi.TestCalendarUtil#testIsOfAge()
92      */
93     @Test
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));
99     }
100
101     @Test
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));
107     }
108
109     @Test
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());
114     }
115
116     @Test
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());
121     }
122
123     @Test
124     public void testModifyUserGroup() throws IOException {
125         User user = User.getById(id);
126         // test add group
127         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=" + URLEncoder.encode(Group.LOCATE_AGENT.getDBName(), "UTF-8"), 0));
128
129         user = User.getById(id);
130         user.refreshGroups();
131         assertTrue(user.isInGroup(Group.LOCATE_AGENT));
132
133         // test remove group
134         assertNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=removeGroup&groupToModify=" + URLEncoder.encode(Group.LOCATE_AGENT.getDBName(), "UTF-8"), 0));
135
136         user = User.getById(id);
137         user.refreshGroups();
138         assertFalse(user.isInGroup(Group.LOCATE_AGENT));
139
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));
142
143         user = User.getById(id);
144         user.refreshGroups();
145         assertFalse(user.isInGroup(Group.SUPPORTER));
146
147         // test add invalid group
148         assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "action=addGroup&groupToModify=non-existing", 0));
149     }
150 }