feat: create lib function to verify FCM token

This commit is contained in:
2024-06-15 05:47:26 -04:00
parent 20ca88fda9
commit 7675867549
10 changed files with 219 additions and 1 deletions

36
src/mocks/fcm.ts Normal file
View File

@@ -0,0 +1,36 @@
import { HttpResponse, http } from "msw";
import type { FcmMessagePayload } from "~/libs/fcm/sendFcmMessage";
export function mockFcmMessageResponse() {
return http.post<{}, { message: FcmMessagePayload; validate_only: boolean }>(
"https://fcm.googleapis.com/v1/projects/aniplay-73b59/messages:send",
async ({ request }) => {
const { message } = await request.json();
const { name, token } = message;
if (name === "token_verification") {
if (token?.length === 163) {
return HttpResponse.json({ name });
}
return HttpResponse.json({
error: {
code: 400,
message:
"The registration token is not a valid FCM registration token",
status: "INVALID_ARGUMENT",
details: [
{
"@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
errorCode: "INVALID_ARGUMENT",
},
],
},
});
}
return HttpResponse.json(message);
},
);
}

30
src/mocks/gToken.ts Normal file
View File

@@ -0,0 +1,30 @@
import type { TokenOptions } from "gtoken";
import { mock } from "bun:test";
const emailRegex =
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
class MockGoogleToken {
private email: string | undefined;
constructor(options: TokenOptions) {
this.email = options.email;
}
getToken() {
if (!this.email) {
return Promise.reject("No email provided");
}
if (!emailRegex.test(this.email)) {
return Promise.reject("Invalid email");
}
return Promise.resolve({
access_token: "asd",
});
}
}
mock.module("gtoken", () => ({ GoogleToken: MockGoogleToken }));

View File

@@ -8,6 +8,7 @@ import { getAnifyTitle } from "./anify/title";
import { getAnilistSearchResults } from "./anilist/search";
import { getAnilistTitle } from "./anilist/title";
import { updateAnilistWatchStatus } from "./anilist/updateWatchStatus";
import { mockFcmMessageResponse } from "./fcm";
export const handlers = [
getAnilistSearchResults(),
@@ -20,4 +21,5 @@ export const handlers = [
getAnifyEpisodes(),
getAnifySources(),
getAnifyTitle(),
mockFcmMessageResponse(),
];