Skip to content

Commit f66dcce

Browse files
committed
fix(mobile): check notification permission on token rotation
1 parent 0d62bf5 commit f66dcce

2 files changed

Lines changed: 91 additions & 3 deletions

File tree

‎apps/mobile/src/features/agent-awareness/remoteRegistration.test.ts‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ const runBackgroundOperations = Effect.fn("TestRemoteRegistration.runBackgroundO
258258
describe("makeRelayDeviceRegistrationRequest", () => {
259259
beforeEach(() => {
260260
vi.restoreAllMocks();
261+
vi.mocked(Notifications.getPermissionsAsync)
262+
.mockReset()
263+
.mockResolvedValue({
264+
granted: true,
265+
} as Notifications.NotificationPermissionsStatus);
261266
vi.mocked(Notifications.getDevicePushTokenAsync).mockResolvedValue({
262267
type: "ios",
263268
data: "apns-token",
@@ -998,6 +1003,88 @@ describe("makeRelayDeviceRegistrationRequest", () => {
9981003
}).pipe(Effect.provide(relayTestLayer));
9991004
});
10001005

1006+
for (const platform of ["android", "ios"] as const) {
1007+
for (const permission of ["granted", "denied", "unavailable"] as const) {
1008+
it.effect(`honors ${permission} permission during ${platform} token rotation`, () => {
1009+
vi.spyOn(Platform, "OS", "get").mockReturnValue(platform);
1010+
vi.spyOn(Platform, "Version", "get").mockReturnValue(platform === "android" ? 36 : "18.0");
1011+
vi.mocked(Notifications.getDevicePushTokenAsync).mockResolvedValue({
1012+
type: platform,
1013+
data: "initial-push-token",
1014+
});
1015+
const registrations: unknown[] = [];
1016+
const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
1017+
const url = input instanceof Request ? input.url : String(input);
1018+
if (url.endsWith("/v1/mobile/devices")) {
1019+
registrations.push(
1020+
await (input instanceof Request ? input : new Response(init?.body)).json(),
1021+
);
1022+
}
1023+
return Response.json(
1024+
url.endsWith("/v1/client/dpop-token")
1025+
? {
1026+
access_token: "relay-dpop-token",
1027+
issued_token_type: "urn:ietf:params:oauth:token-type:access_token",
1028+
token_type: "DPoP",
1029+
expires_in: 300,
1030+
scope: "mobile:registration",
1031+
}
1032+
: { ok: true },
1033+
);
1034+
});
1035+
const testLayer = relayTestLayer.pipe(
1036+
Layer.provide(Layer.succeed(FetchHttpClient.Fetch, fetchMock)),
1037+
Layer.fresh,
1038+
);
1039+
Constants.expoConfig!.extra = { relay: { url: "https://relay.example.test" } };
1040+
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("clerk-token-user-a"), "user-a");
1041+
1042+
return Effect.gen(function* () {
1043+
yield* runBackgroundOperations();
1044+
expect(registrations).toHaveLength(1);
1045+
expect(registrations[0]).toMatchObject({
1046+
pushToken: "initial-push-token",
1047+
preferences: { notificationsEnabled: true },
1048+
});
1049+
registrations.length = 0;
1050+
vi.mocked(Notifications.getDevicePushTokenAsync).mockClear();
1051+
if (permission === "unavailable") {
1052+
vi.mocked(Notifications.getPermissionsAsync).mockRejectedValueOnce(
1053+
new Error("Permission lookup failed"),
1054+
);
1055+
} else {
1056+
vi.mocked(Notifications.getPermissionsAsync).mockResolvedValueOnce({
1057+
granted: permission === "granted",
1058+
} as Notifications.NotificationPermissionsStatus);
1059+
}
1060+
const tokenListener = vi
1061+
.mocked(Notifications.addPushTokenListener)
1062+
.mock.calls.at(-1)?.[0];
1063+
expect(tokenListener).toBeDefined();
1064+
tokenListener?.({ type: platform, data: "rotated-push-token" });
1065+
yield* runBackgroundOperations();
1066+
1067+
if (permission === "unavailable") {
1068+
expect(registrations).toHaveLength(0);
1069+
expect(getAgentAwarenessRegistrationStatus()).toBe("registered");
1070+
} else {
1071+
expect(registrations).toHaveLength(1);
1072+
expect(registrations[0]).toMatchObject({
1073+
platform,
1074+
preferences: { notificationsEnabled: permission === "granted" },
1075+
});
1076+
if (permission === "granted") {
1077+
expect(registrations[0]).toMatchObject({ pushToken: "rotated-push-token" });
1078+
} else {
1079+
expect(registrations[0]).not.toHaveProperty("pushToken");
1080+
}
1081+
}
1082+
expect(Notifications.getDevicePushTokenAsync).not.toHaveBeenCalled();
1083+
}).pipe(Effect.provide(testLayer));
1084+
});
1085+
}
1086+
}
1087+
10011088
it.effect("registers an Android FCM token without invoking Apple Live Activities", () => {
10021089
vi.spyOn(Platform, "OS", "get").mockReturnValue("android");
10031090
vi.spyOn(Platform, "Version", "get").mockReturnValue(36);

‎apps/mobile/src/features/agent-awareness/remoteRegistration.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,6 @@ function nativePushTokenRegistration(observedPushToken?: string) {
265265
if (!canRegisterPushNotifications() || !supportsAgentAwarenessPush()) {
266266
return { notificationsEnabled: false, pushToken: null };
267267
}
268-
if (observedPushToken) {
269-
return { notificationsEnabled: true, pushToken: observedPushToken };
270-
}
271268
const permissions = yield* Effect.tryPromise({
272269
try: () => Notifications.getPermissionsAsync(),
273270
catch: (cause) =>
@@ -279,6 +276,10 @@ function nativePushTokenRegistration(observedPushToken?: string) {
279276
if (!permissions.granted) {
280277
return { notificationsEnabled: false, pushToken: null };
281278
}
279+
// A token can rotate after notification permission has been revoked.
280+
if (observedPushToken) {
281+
return { notificationsEnabled: true, pushToken: observedPushToken };
282+
}
282283
const token = yield* Effect.tryPromise({
283284
try: () => Notifications.getDevicePushTokenAsync(),
284285
catch: (cause) =>

0 commit comments

Comments
 (0)