throw new GigiApiException("Entered date of birth is below the restricted age requirements.");
}
- if (CalendarUtil.isOfAge(dob, User.MAXIMUM_PLAUSIBLE_AGE)) {
+ if (CalendarUtil.isYearsInFuture(dob.end(), User.MAXIMUM_PLAUSIBLE_AGE)) {
throw new GigiApiException("Entered date of birth exceeds the maximum age set in our policies. Please check your DoB is correct and contact support if the issue persists.");
}
this.dob = dob;
ga.mergeInto(new GigiApiException("Entered date of birth is below the restricted age requirements."));
}
- if (CalendarUtil.isOfAge(myDoB.getDate(), User.MAXIMUM_PLAUSIBLE_AGE)) {
+ if (CalendarUtil.isYearsInFuture(myDoB.getDate().end(), User.MAXIMUM_PLAUSIBLE_AGE)) {
ga.mergeInto(new GigiApiException("Entered date of birth exceeds the maximum age set in our policies. Please check your DoB is correct and contact support if the issue persists."));
}
}
public static boolean isOfAge(DayDate dob, int age) {
+ return isYearsInFuture(dob.start(), age);
+ }
+
+ public static boolean isYearsInFuture(Date dt, int age) {
Calendar c = Calendar.getInstance();
- c.setTimeInMillis(dob.getTime());
- int year = c.get(Calendar.YEAR);
- int month = c.get(Calendar.MONTH);
- int day = c.get(Calendar.DAY_OF_MONTH);
- c.set(year, month, day);
+ c.setTime(dt);
c.add(Calendar.YEAR, age);
return System.currentTimeMillis() >= c.getTime().getTime();
*/
public class DayDate {
- public static final long MILLI_DAY = 24 * 60 * 60 * 1000;
+ private static final int MILLI_HOUR = 60 * 60 * 1000;
+
+ public static final long MILLI_DAY = 24 * MILLI_HOUR;
private long time;
public java.util.Date toDate() {
return new java.util.Date(time);
}
+
+ // Timezones currently reach from UTC-12 to UTC+14 that allows us to define
+ // the "earliest start" and "latest end" of a Date.
+ /**
+ * Gets this date's starting point.
+ *
+ * @return The earliest point in time where this date was in any timezone
+ */
+ public java.util.Date start() {
+ return new java.util.Date(time - 12 * MILLI_HOUR);
+ }
+
+ /**
+ * Gets this date's ending point.
+ *
+ * @return The latest point in time where this Date was any timezone
+ */
+ public java.util.Date end() {
+ return new java.util.Date(time + 14 * MILLI_HOUR + 24 * MILLI_HOUR);
+ }
}
import java.io.IOException;
import java.text.ParseException;
import java.util.Calendar;
-import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
gae.mergeInto(new GigiApiException("You must enter the date when you met the applicant."));
} else {
try {
- Date d = DateSelector.getDateFormat().parse(date);
- Calendar gc = GregorianCalendar.getInstance();
- gc.setTimeInMillis(System.currentTimeMillis());
- gc.add(Calendar.HOUR_OF_DAY, 12);
- if (d.getTime() > gc.getTimeInMillis()) {
+ DayDate d = new DayDate(DateSelector.getDateFormat().parse(date).getTime());
+ if (d.start().getTime() > System.currentTimeMillis()) {
gae.mergeInto(new GigiApiException("You must not enter a date in the future."));
}
+ Calendar gc = GregorianCalendar.getInstance();
gc.setTimeInMillis(System.currentTimeMillis());
gc.add(Calendar.MONTH, -LIMIT_MAX_MONTHS_VERIFICATION);
if (d.getTime() < gc.getTimeInMillis()) {
assertTrue(CalendarUtil.isOfAge(dob, 13));
assertTrue(CalendarUtil.isOfAge(dob, 14));
-
- dob = CalendarUtil.getDateFromComponents(year - 14, month, days + 1);
+ // We need one day as safety margin. Between 10:00 and 23:59 UTC there
+ // is a place on earth (UTC+1 to UTC+14) where a person having
+ // birthday "tomorrow" is already of that age. So we need the day after
+ // tomorrow for doing this check the easy way.
+ dob = CalendarUtil.getDateFromComponents(year - 14, month, days + 2);
assertFalse(CalendarUtil.isOfAge(dob, 14));
}
import club.wpia.gigi.dbObjects.Group;
import club.wpia.gigi.dbObjects.Name;
import club.wpia.gigi.dbObjects.NamePart;
-import club.wpia.gigi.dbObjects.User;
import club.wpia.gigi.dbObjects.NamePart.NamePartType;
-import club.wpia.gigi.pages.account.MyDetails;
+import club.wpia.gigi.dbObjects.User;
import club.wpia.gigi.testUtils.ManagedTest;
public class TestMyDetailsEdit extends ManagedTest {
assertNotNull(executeBasicWebInteraction(cookie, MyDetails.PATH, "day=1&month=1&year=test&action=updateDoB", 0));
}
+ /**
+ * Tests that changing the date of birth to a too recent one results in an
+ * error.
+ *
+ * @throws IOException
+ * when web interactions fail.
+ * @see club.wpia.gigi.TestCalendarUtil#testIsOfAge()
+ */
@Test
public void testChangeTooYoung() throws IOException {
Calendar c = GregorianCalendar.getInstance();
c.add(Calendar.YEAR, -User.MINIMUM_AGE);
- c.add(Calendar.DAY_OF_MONTH, +1);
+ c.add(Calendar.DAY_OF_MONTH, +2);
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));
}
public void testChangeTooOld() throws IOException {
Calendar c = GregorianCalendar.getInstance();
c.add(Calendar.YEAR, -User.MAXIMUM_PLAUSIBLE_AGE);
- c.add(Calendar.DAY_OF_MONTH, -1);
+ c.add(Calendar.DAY_OF_MONTH, -2);
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));
}