-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add current entitlements to whoami response
- Loading branch information
1 parent
d5b39cd
commit 33c0a27
Showing
6 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
service/src/main/java/org/whispersystems/textsecuregcm/entities/Entitlements.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.whispersystems.textsecuregcm.entities; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import org.whispersystems.textsecuregcm.util.InstantAdapter; | ||
|
||
import javax.annotation.Nullable; | ||
import java.time.Instant; | ||
import java.util.List; | ||
|
||
public record Entitlements( | ||
@Schema(description = "Active badges added via /v1/donation/redeem-receipt") | ||
List<BadgeEntitlement> badges, | ||
@Schema(description = "If present, the backup level set via /v1/archives/redeem-receipt") | ||
@Nullable BackupEntitlement backup) { | ||
|
||
public record BadgeEntitlement( | ||
@Schema(description = "The badge id") | ||
String id, | ||
|
||
@Schema(description = "When the badge expires, in number of seconds since epoch", implementation = Long.class) | ||
@JsonSerialize(using = InstantAdapter.EpochSecondSerializer.class) | ||
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT) | ||
@JsonProperty("expirationSeconds") | ||
Instant expiration, | ||
|
||
@Schema(description = "Whether the badge is currently configured to be visible") | ||
boolean visible) { | ||
} | ||
|
||
public record BackupEntitlement( | ||
@Schema(description = "The backup level of the account") | ||
long backupLevel, | ||
|
||
@Schema(description = "When the backup entitlement expires, in number of seconds since epoch", implementation = Long.class) | ||
@JsonSerialize(using = InstantAdapter.EpochSecondSerializer.class) | ||
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT) | ||
@JsonProperty("expirationSeconds") | ||
Instant expiration) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...java/org/whispersystems/textsecuregcm/controllers/AccountIdentityResponseBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2024 Signal Messenger, LLC | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
package org.whispersystems.textsecuregcm.controllers; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.time.Instant; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.whispersystems.textsecuregcm.entities.Entitlements; | ||
import org.whispersystems.textsecuregcm.storage.Account; | ||
import org.whispersystems.textsecuregcm.storage.AccountBadge; | ||
import org.whispersystems.textsecuregcm.util.TestClock; | ||
|
||
class AccountIdentityResponseBuilderTest { | ||
|
||
@Test | ||
void expiredBackupEntitlement() { | ||
final Instant expiration = Instant.ofEpochSecond(101); | ||
final Account account = mock(Account.class); | ||
when(account.getBackupVoucher()).thenReturn(new Account.BackupVoucher(6, expiration)); | ||
|
||
Entitlements.BackupEntitlement backup = new AccountIdentityResponseBuilder(account) | ||
.clock(TestClock.pinned(Instant.ofEpochSecond(101))) | ||
.build().entitlements().backup(); | ||
assertThat(backup).isNull(); | ||
|
||
backup = new AccountIdentityResponseBuilder(account) | ||
.clock(TestClock.pinned(Instant.ofEpochSecond(100))) | ||
.build().entitlements().backup(); | ||
assertThat(backup).isNotNull(); | ||
assertThat(backup.expiration()).isEqualTo(expiration); | ||
assertThat(backup.backupLevel()).isEqualTo(6); | ||
} | ||
|
||
@Test | ||
void expiredBadgeEntitlement() { | ||
final Account account = mock(Account.class); | ||
when(account.getBadges()).thenReturn(List.of( | ||
new AccountBadge("badge1", Instant.ofEpochSecond(10), false), | ||
new AccountBadge("badge2", Instant.ofEpochSecond(11), true))); | ||
|
||
// all should be expired | ||
assertThat(new AccountIdentityResponseBuilder(account) | ||
.clock(TestClock.pinned(Instant.ofEpochSecond(11))) | ||
.build().entitlements().badges()).isEmpty(); | ||
|
||
// first badge should be expired | ||
assertThat(new AccountIdentityResponseBuilder(account).clock(TestClock.pinned(Instant.ofEpochSecond(10))).build() | ||
.entitlements() | ||
.badges() | ||
.stream().map(Entitlements.BadgeEntitlement::id).toList()) | ||
.containsExactly("badge2"); | ||
|
||
// no badges should be expired | ||
assertThat(new AccountIdentityResponseBuilder(account).clock(TestClock.pinned(Instant.ofEpochSecond(9))).build() | ||
.entitlements() | ||
.badges() | ||
.stream().map(Entitlements.BadgeEntitlement::id).toList()) | ||
.containsExactly("badge1", "badge2"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters