Add test for urls
This commit is contained in:
parent
758e8646fc
commit
cba596c910
4 changed files with 48 additions and 8 deletions
|
@ -12,6 +12,7 @@
|
|||
"ignorePatterns": ["pnpm-lock.yaml"],
|
||||
"plugins": ["simple-import-sort", "@typescript-eslint"],
|
||||
"rules": {
|
||||
"import/no-extraneous-dependencies": ["error", {"devDependencies": true}],
|
||||
"quotes": ["error", "double"],
|
||||
"react/react-in-jsx-scope": "off", // next does not require react imports
|
||||
"import/extensions": "off", // file extension not required when importing
|
||||
|
|
|
@ -5,7 +5,7 @@ import { Response } from "retes/response";
|
|||
|
||||
import { SALEOR_AUTHORIZATION_BEARER_HEADER, SALEOR_SIGNATURE_HEADER } from "./const";
|
||||
import { getSaleorHeaders } from "./headers";
|
||||
import { jwksUrl } from "./urls";
|
||||
import { getJwksUrl } from "./urls";
|
||||
|
||||
export const withBaseURL: Middleware = (handler) => async (request) => {
|
||||
const { host, "x-forwarded-proto": protocol = "http" } = request.headers;
|
||||
|
@ -100,7 +100,7 @@ export const withWebhookSignatureVerified =
|
|||
};
|
||||
|
||||
const remoteJwks = jose.createRemoteJWKSet(
|
||||
new URL(jwksUrl(saleorDomain))
|
||||
new URL(getJwksUrl(saleorDomain))
|
||||
) as jose.FlattenedVerifyGetKey;
|
||||
|
||||
try {
|
||||
|
@ -176,7 +176,7 @@ export const withJWTVerified =
|
|||
}
|
||||
|
||||
try {
|
||||
const JWKS = jose.createRemoteJWKSet(new URL(jwksUrl(domain)));
|
||||
const JWKS = jose.createRemoteJWKSet(new URL(getJwksUrl(domain)));
|
||||
await jose.jwtVerify(token, JWKS);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
|
|
26
src/urls.test.ts
Normal file
26
src/urls.test.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { getGraphQLUrl, getJwksUrl } from "./urls";
|
||||
|
||||
describe("urls.ts", () => {
|
||||
describe("jwksUrl function", () => {
|
||||
test.each([
|
||||
["localhost:8000", "http://localhost:8000/.well-known/jwks.json"],
|
||||
[
|
||||
"https://my-saleor.saleor.cloud",
|
||||
"https://https://my-saleor.saleor.cloud/.well-known/jwks.json",
|
||||
],
|
||||
])("resolves %s to be %s", (input, expectedOutput) => {
|
||||
expect(getJwksUrl(input)).toBe(expectedOutput);
|
||||
});
|
||||
});
|
||||
|
||||
describe("graphQLUrl function", () => {
|
||||
test.each([
|
||||
["localhost:8000", "http://localhost:8000/graphql/"],
|
||||
["https://my-saleor.saleor.cloud", "https://https://my-saleor.saleor.cloud/graphql/"],
|
||||
])("resolves %s to be %s", (input, expectedOutput) => {
|
||||
expect(getGraphQLUrl(input)).toBe(expectedOutput);
|
||||
});
|
||||
});
|
||||
});
|
23
src/urls.ts
23
src/urls.ts
|
@ -1,8 +1,21 @@
|
|||
const urlProtocol = (saleorDomain: string): string =>
|
||||
/**
|
||||
* TODO: Validate proper URL
|
||||
*/
|
||||
const resolveUrlProtocol = (saleorDomain: string): string =>
|
||||
saleorDomain === "localhost:8000" ? "http" : "https";
|
||||
|
||||
export const jwksUrl = (saleorDomain: string): string =>
|
||||
`${urlProtocol(saleorDomain)}://${saleorDomain}/.well-known/jwks.json`;
|
||||
export const getJwksUrl = (saleorDomain: string): string =>
|
||||
`${resolveUrlProtocol(saleorDomain)}://${saleorDomain}/.well-known/jwks.json`;
|
||||
|
||||
export const graphQLUrl = (saleorDomain: string): string =>
|
||||
`${urlProtocol(saleorDomain)}://${saleorDomain}/graphql/`;
|
||||
export const getGraphQLUrl = (saleorDomain: string): string =>
|
||||
`${resolveUrlProtocol(saleorDomain)}://${saleorDomain}/graphql/`;
|
||||
|
||||
/**
|
||||
* @deprecated Remove in v1, left for compatibility
|
||||
*/
|
||||
export const jwksUrl = getJwksUrl;
|
||||
|
||||
/**
|
||||
* @deprecated Remove in v1, left for compatibility
|
||||
*/
|
||||
export const graphQLUrl = getGraphQLUrl;
|
||||
|
|
Loading…
Reference in a new issue