mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-02 11:46:53 +00:00
Default to preferred server-local time if account has no phone number
This commit is contained in:
1 parent
4440022796
commit
d46818e465
2 files changed
+55
-50
No files matched your search
+41
-35
@@ -12,59 +12,65 @@ import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Optional;
|
||||
import com.google.i18n.phonenumbers.Phonenumber;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.NullSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.util.TestClock;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
class SchedulingUtilTest {
|
||||
|
||||
@Test
|
||||
void getNextRecommendedNotificationTime() {
|
||||
{
|
||||
final Account account = mock(Account.class);
|
||||
final Account account = mock(Account.class);
|
||||
|
||||
// The account has a phone number that can be resolved to a region with known timezones
|
||||
when(account.getNumber()).thenReturn(PhoneNumberUtil.getInstance().format(
|
||||
PhoneNumberUtil.getInstance().getExampleNumber("DE"), PhoneNumberUtil.PhoneNumberFormat.E164));
|
||||
// The account has a phone number that can be resolved to a region with known timezones
|
||||
when(account.getNumberOptional()).thenReturn(Optional.of(PhoneNumberUtil.getInstance().format(
|
||||
PhoneNumberUtil.getInstance().getExampleNumber("DE"), PhoneNumberUtil.PhoneNumberFormat.E164)));
|
||||
|
||||
final ZoneId berlinZoneId = ZoneId.of("Europe/Berlin");
|
||||
final ZonedDateTime beforeNotificationTime = ZonedDateTime.now(berlinZoneId).with(LocalTime.of(13, 0));
|
||||
final ZoneId berlinZoneId = ZoneId.of("Europe/Berlin");
|
||||
final ZonedDateTime beforeNotificationTime = ZonedDateTime.now(berlinZoneId).with(LocalTime.of(13, 0));
|
||||
|
||||
assertEquals(
|
||||
beforeNotificationTime.with(LocalTime.of(14, 0)).toInstant(),
|
||||
SchedulingUtil.getNextRecommendedNotificationTime(account, LocalTime.of(14, 0),
|
||||
Clock.fixed(beforeNotificationTime.toInstant(), berlinZoneId)));
|
||||
assertEquals(
|
||||
beforeNotificationTime.with(LocalTime.of(14, 0)).toInstant(),
|
||||
SchedulingUtil.getNextRecommendedNotificationTime(account, LocalTime.of(14, 0),
|
||||
Clock.fixed(beforeNotificationTime.toInstant(), berlinZoneId)));
|
||||
|
||||
final ZonedDateTime afterNotificationTime = ZonedDateTime.now(berlinZoneId).with(LocalTime.of(15, 0));
|
||||
final ZonedDateTime afterNotificationTime = ZonedDateTime.now(berlinZoneId).with(LocalTime.of(15, 0));
|
||||
|
||||
assertEquals(
|
||||
afterNotificationTime.with(LocalTime.of(14, 0)).plusDays(1).toInstant(),
|
||||
SchedulingUtil.getNextRecommendedNotificationTime(account, LocalTime.of(14, 0),
|
||||
Clock.fixed(afterNotificationTime.toInstant(), berlinZoneId)));
|
||||
}
|
||||
assertEquals(
|
||||
afterNotificationTime.with(LocalTime.of(14, 0)).plusDays(1).toInstant(),
|
||||
SchedulingUtil.getNextRecommendedNotificationTime(account, LocalTime.of(14, 0),
|
||||
Clock.fixed(afterNotificationTime.toInstant(), berlinZoneId)));
|
||||
}
|
||||
|
||||
{
|
||||
final Account account = mock(Account.class);
|
||||
@ParameterizedTest
|
||||
@NullSource
|
||||
@ValueSource(strings = {"Not a parseable number"})
|
||||
void getNextRecommendedNotificationTimeDefaultsToPreferredTime(@Nullable final String number) {
|
||||
final Account account = mock(Account.class);
|
||||
|
||||
// The account does not have a phone number that can be connected to a region/time zone
|
||||
when(account.getNumber()).thenReturn("Not a parseable number");
|
||||
// The account does not have a phone number that can be connected to a region/time zone
|
||||
when(account.getNumberOptional()).thenReturn(Optional.ofNullable(number));
|
||||
|
||||
final ZonedDateTime beforeNotificationTime = ZonedDateTime.now(ZoneId.systemDefault()).with(LocalTime.of(13, 59));
|
||||
final LocalTime preferredNotificationTime = LocalTime.of(14, 0);
|
||||
final ZonedDateTime beforeNotificationTime = ZonedDateTime.now(ZoneId.systemDefault()).with(LocalTime.of(13, 59));
|
||||
final LocalTime preferredNotificationTime = LocalTime.of(14, 0);
|
||||
|
||||
assertEquals(
|
||||
beforeNotificationTime.with(preferredNotificationTime).toInstant(),
|
||||
SchedulingUtil.getNextRecommendedNotificationTime(account, preferredNotificationTime,
|
||||
Clock.fixed(beforeNotificationTime.toInstant(), ZoneId.systemDefault())));
|
||||
assertEquals(
|
||||
beforeNotificationTime.with(preferredNotificationTime).toInstant(),
|
||||
SchedulingUtil.getNextRecommendedNotificationTime(account, preferredNotificationTime,
|
||||
Clock.fixed(beforeNotificationTime.toInstant(), ZoneId.systemDefault())));
|
||||
|
||||
final ZonedDateTime afterNotificationTime = ZonedDateTime.now(ZoneId.systemDefault()).with(LocalTime.of(14, 1));
|
||||
final ZonedDateTime afterNotificationTime = ZonedDateTime.now(ZoneId.systemDefault()).with(LocalTime.of(14, 1));
|
||||
|
||||
assertEquals(
|
||||
afterNotificationTime.with(preferredNotificationTime).plusDays(1).toInstant(),
|
||||
SchedulingUtil.getNextRecommendedNotificationTime(account, preferredNotificationTime,
|
||||
Clock.fixed(afterNotificationTime.toInstant(), ZoneId.systemDefault())));
|
||||
}
|
||||
assertEquals(
|
||||
afterNotificationTime.with(preferredNotificationTime).plusDays(1).toInstant(),
|
||||
SchedulingUtil.getNextRecommendedNotificationTime(account, preferredNotificationTime,
|
||||
Clock.fixed(afterNotificationTime.toInstant(), ZoneId.systemDefault())));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -72,8 +78,8 @@ class SchedulingUtilTest {
|
||||
final Account account = mock(Account.class);
|
||||
|
||||
// The account has a phone number that can be resolved to a region with known timezones
|
||||
when(account.getNumber()).thenReturn(PhoneNumberUtil.getInstance().format(
|
||||
PhoneNumberUtil.getInstance().getExampleNumber("DE"), PhoneNumberUtil.PhoneNumberFormat.E164));
|
||||
when(account.getNumberOptional()).thenReturn(Optional.of(PhoneNumberUtil.getInstance().format(
|
||||
PhoneNumberUtil.getInstance().getExampleNumber("DE"), PhoneNumberUtil.PhoneNumberFormat.E164)));
|
||||
|
||||
final LocalDateTime afterNotificationTime = LocalDateTime.of(2025, 3, 29, 15, 0);
|
||||
final ZoneId berlinZoneId = ZoneId.of("Europe/Berlin");
|
||||
|
||||
Reference in new issue
Block a user