Skip to content

Commit

Permalink
Allow device to unlink itself
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny-signal authored and ravi-signal committed Aug 15, 2024
1 parent fd10b97 commit 7605462
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ public DeviceInfoList getDevices(@ReadOnly @Auth AuthenticatedDevice auth) {
@Path("/{device_id}")
@ChangesLinkedDevices
public void removeDevice(@Mutable @Auth AuthenticatedDevice auth, @PathParam("device_id") byte deviceId) {
if (auth.getAuthenticatedDevice().getId() != Device.PRIMARY_ID) {
if (auth.getAuthenticatedDevice().getId() != Device.PRIMARY_ID &&
auth.getAuthenticatedDevice().getId() != deviceId) {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,46 @@ void unlinkPrimaryDevice() {
}
}

@Test
void removeDeviceBySelf() {
final byte deviceId = 2;

when(accountsManager.removeDevice(AuthHelper.VALID_ACCOUNT_3, deviceId))
.thenReturn(CompletableFuture.completedFuture(AuthHelper.VALID_ACCOUNT));

final Response response = resources
.getJerseyTest()
.target("/v1/devices/" + deviceId)
.request()
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_UUID_3, deviceId, AuthHelper.VALID_PASSWORD_3_LINKED))
.header(HttpHeaders.USER_AGENT, "Signal-Android/5.42.8675309 Android/30")
.delete();

assertThat(response.getStatus()).isEqualTo(204);
assertThat(response.hasEntity()).isFalse();

verify(accountsManager).removeDevice(AuthHelper.VALID_ACCOUNT_3, deviceId);
}

@Test
void removeDeviceByOther() {
final byte deviceId = 2;
final byte otherDeviceId = 3;

try (final Response response = resources
.getJerseyTest()
.target("/v1/devices/" + otherDeviceId)
.request()
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_UUID_3, deviceId, AuthHelper.VALID_PASSWORD_3_LINKED))
.header(HttpHeaders.USER_AGENT, "Signal-Android/5.42.8675309 Android/30")
.delete()) {

assertThat(response.getStatus()).isEqualTo(401);

verify(accountsManager, never()).removeDevice(any(), anyByte());
}
}

@Test
void checkVerificationToken() {
final UUID uuid = UUID.randomUUID();
Expand Down

0 comments on commit 7605462

Please sign in to comment.