mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-21 07:28:05 +01:00
Stop monitoring device "enabled" state changes from auth enablement refresh requirement provider
Device enabled states no longer affect anything at an authentication level
This commit is contained in:
committed by
Jon Chambers
parent
2f76738b50
commit
2d1610b075
@@ -131,54 +131,8 @@ class AuthEnablementRefreshRequirementProviderTest {
|
||||
.forEach(device -> when(clientPresenceManager.isPresent(uuid, device.getId())).thenReturn(true));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void testDeviceEnabledChanged(final Map<Byte, Boolean> initialEnabled, final Map<Byte, Boolean> finalEnabled) {
|
||||
assert initialEnabled.size() == finalEnabled.size();
|
||||
|
||||
assert account.getPrimaryDevice().hasMessageDeliveryChannel();
|
||||
|
||||
initialEnabled.forEach((deviceId, enabled) ->
|
||||
DevicesHelper.setEnabled(account.getDevice(deviceId).orElseThrow(), enabled));
|
||||
|
||||
final Response response = resources.getJerseyTest()
|
||||
.target("/v1/test/account/devices/enabled")
|
||||
.request()
|
||||
.header("Authorization",
|
||||
"Basic " + Base64.getEncoder().encodeToString("user:pass".getBytes(StandardCharsets.UTF_8)))
|
||||
.post(Entity.entity(finalEnabled, MediaType.APPLICATION_JSON));
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
|
||||
final boolean expectDisplacedPresence = !initialEnabled.equals(finalEnabled);
|
||||
|
||||
assertAll(
|
||||
initialEnabled.keySet().stream()
|
||||
.map(deviceId -> () -> verify(clientPresenceManager, times(expectDisplacedPresence ? 1 : 0))
|
||||
.disconnectPresence(account.getUuid(), deviceId)));
|
||||
|
||||
assertAll(
|
||||
finalEnabled.keySet().stream()
|
||||
.map(deviceId -> () -> verify(clientPresenceManager, times(expectDisplacedPresence ? 1 : 0))
|
||||
.disconnectPresence(account.getUuid(), deviceId)));
|
||||
}
|
||||
|
||||
static Stream<Arguments> testDeviceEnabledChanged() {
|
||||
final byte deviceId2 = 2;
|
||||
final byte deviceId3 = 3;
|
||||
return Stream.of(
|
||||
Arguments.of(Map.of(deviceId2, false, deviceId3, false), Map.of(deviceId2, true, deviceId3, true)),
|
||||
Arguments.of(Map.of(deviceId2, true, deviceId3, true), Map.of(deviceId2, false, deviceId3, false)),
|
||||
Arguments.of(Map.of(deviceId2, true, deviceId3, true), Map.of(deviceId2, true, deviceId3, true)),
|
||||
Arguments.of(Map.of(deviceId2, false, deviceId3, true), Map.of(deviceId2, true, deviceId3, true)),
|
||||
Arguments.of(Map.of(deviceId2, true, deviceId3, false), Map.of(deviceId2, true, deviceId3, true))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeviceAdded() {
|
||||
assert account.getPrimaryDevice().hasMessageDeliveryChannel();
|
||||
|
||||
final int initialDeviceCount = account.getDevices().size();
|
||||
|
||||
final List<String> addedDeviceNames = List.of(
|
||||
@@ -204,8 +158,6 @@ class AuthEnablementRefreshRequirementProviderTest {
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {1, 2})
|
||||
void testDeviceRemoved(final int removedDeviceCount) {
|
||||
assert account.getPrimaryDevice().hasMessageDeliveryChannel();
|
||||
|
||||
final List<Byte> initialDeviceIds = account.getDevices().stream().map(Device::getId).toList();
|
||||
|
||||
final List<Byte> deletedDeviceIds = account.getDevices().stream()
|
||||
@@ -358,40 +310,9 @@ class AuthEnablementRefreshRequirementProviderTest {
|
||||
return "You’re in!";
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/account/enabled/{enabled}")
|
||||
@ChangesDeviceEnabledState
|
||||
public String setAccountEnabled(@Auth TestPrincipal principal, @PathParam("enabled") final boolean enabled) {
|
||||
|
||||
final Device device = principal.getAccount().getPrimaryDevice();
|
||||
|
||||
DevicesHelper.setEnabled(device, enabled);
|
||||
|
||||
assert device.hasMessageDeliveryChannel() == enabled;
|
||||
|
||||
return String.format("Set account to %s", enabled);
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/account/devices/enabled")
|
||||
@ChangesDeviceEnabledState
|
||||
public String setEnabled(@Auth TestPrincipal principal, Map<Byte, Boolean> deviceIdsEnabled) {
|
||||
|
||||
final StringBuilder response = new StringBuilder();
|
||||
|
||||
for (Entry<Byte, Boolean> deviceIdEnabled : deviceIdsEnabled.entrySet()) {
|
||||
final Device device = principal.getAccount().getDevice(deviceIdEnabled.getKey()).orElseThrow();
|
||||
DevicesHelper.setEnabled(device, deviceIdEnabled.getValue());
|
||||
|
||||
response.append(String.format("Set device enabled %s", deviceIdEnabled));
|
||||
}
|
||||
|
||||
return response.toString();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/account/devices")
|
||||
@ChangesDeviceEnabledState
|
||||
@ChangesLinkedDevices
|
||||
public String addDevices(@Auth TestPrincipal auth, List<byte[]> deviceNames) {
|
||||
|
||||
deviceNames.forEach(name -> {
|
||||
@@ -406,7 +327,7 @@ class AuthEnablementRefreshRequirementProviderTest {
|
||||
|
||||
@DELETE
|
||||
@Path("/account/devices/{deviceIds}")
|
||||
@ChangesDeviceEnabledState
|
||||
@ChangesLinkedDevices
|
||||
public String removeDevices(@Auth TestPrincipal auth, @PathParam("deviceIds") String deviceIds) {
|
||||
|
||||
Arrays.stream(deviceIds.split(","))
|
||||
@@ -415,17 +336,5 @@ class AuthEnablementRefreshRequirementProviderTest {
|
||||
|
||||
return "Removed device(s) " + deviceIds;
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/account/disablePrimaryDeviceAndDeleteDevice/{deviceId}")
|
||||
@ChangesDeviceEnabledState
|
||||
public String disablePrimaryDeviceAndRemoveDevice(@Auth TestPrincipal auth, @PathParam("deviceId") byte deviceId) {
|
||||
|
||||
DevicesHelper.setEnabled(auth.getAccount().getPrimaryDevice(), false);
|
||||
|
||||
auth.getAccount().removeDevice(deviceId);
|
||||
|
||||
return "Removed device " + deviceId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
* Copyright 2024 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
package org.whispersystems.textsecuregcm.auth;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.Device;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class ContainerRequestUtilTest {
|
||||
|
||||
@Test
|
||||
void testBuildDevicesEnabled() {
|
||||
|
||||
final byte disabledDeviceId = 3;
|
||||
|
||||
final Account account = mock(Account.class);
|
||||
|
||||
final List<Device> devices = new ArrayList<>();
|
||||
when(account.getDevices()).thenReturn(devices);
|
||||
|
||||
IntStream.range(1, 5)
|
||||
.forEach(id -> {
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getId()).thenReturn((byte) id);
|
||||
when(device.hasMessageDeliveryChannel()).thenReturn(id != disabledDeviceId);
|
||||
devices.add(device);
|
||||
});
|
||||
|
||||
final Map<Byte, Boolean> devicesEnabled = ContainerRequestUtil.AccountInfo.fromAccount(account).devicesEnabled();
|
||||
|
||||
assertEquals(4, devicesEnabled.size());
|
||||
|
||||
assertAll(devicesEnabled.entrySet().stream()
|
||||
.map(deviceAndEnabled -> () -> {
|
||||
if (deviceAndEnabled.getKey().equals(disabledDeviceId)) {
|
||||
assertFalse(deviceAndEnabled.getValue());
|
||||
} else {
|
||||
assertTrue(deviceAndEnabled.getValue());
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.whispersystems.textsecuregcm.tests.util.DevicesHelper.createDevice;
|
||||
import static org.whispersystems.textsecuregcm.tests.util.DevicesHelper.setEnabled;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
import java.lang.annotation.Annotation;
|
||||
@@ -28,12 +27,8 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.whispersystems.textsecuregcm.storage.Device.DeviceCapabilities;
|
||||
import org.whispersystems.textsecuregcm.tests.util.AccountsHelper;
|
||||
import org.whispersystems.textsecuregcm.util.TestClock;
|
||||
@@ -207,12 +202,6 @@ class AccountTest {
|
||||
final byte deviceId3 = 3;
|
||||
assertThat(account.getNextDeviceId()).isEqualTo(deviceId3);
|
||||
|
||||
account.addDevice(createDevice(deviceId3));
|
||||
|
||||
setEnabled(account.getDevice(deviceId2).orElseThrow(), false);
|
||||
|
||||
assertThat(account.getNextDeviceId()).isEqualTo((byte) 4);
|
||||
|
||||
account.removeDevice(deviceId2);
|
||||
|
||||
assertThat(account.getNextDeviceId()).isEqualTo(deviceId2);
|
||||
|
||||
@@ -27,8 +27,6 @@ public class DevicesHelper {
|
||||
device.setUserAgent("OWT");
|
||||
device.setRegistrationId(registrationId);
|
||||
|
||||
setEnabled(device, true);
|
||||
|
||||
return device;
|
||||
}
|
||||
|
||||
@@ -38,20 +36,6 @@ public class DevicesHelper {
|
||||
device.setUserAgent("OWT");
|
||||
device.setRegistrationId(registrationId);
|
||||
|
||||
setEnabled(device, false);
|
||||
|
||||
return device;
|
||||
}
|
||||
|
||||
public static void setEnabled(Device device, boolean enabled) {
|
||||
if (enabled) {
|
||||
device.setGcmId("testGcmId" + RANDOM.nextLong());
|
||||
} else {
|
||||
device.setGcmId(null);
|
||||
}
|
||||
|
||||
// fail fast, to guard against a change to the isEnabled() implementation causing unexpected test behavior
|
||||
assert enabled == device.hasMessageDeliveryChannel();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user