Skip to content

Commit

Permalink
fix: support unauthorized repository response
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Jul 6, 2021
1 parent 861c19a commit 022d278
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
15 changes: 13 additions & 2 deletions src/ForbiddenError.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
interface ForbiddenErrorAPIResponse {
interface ForbiddenErrorRepositoryAPIResponse {
type: string;
message: string;
oauth_initiate: string;
oauth_token: string;
}

interface ForbiddenErrorQueryAPIResponse {
error: string;
oauth_initiate: string;
oauth_token: string;
}

type ForbiddenErrorAPIResponse =
| ForbiddenErrorRepositoryAPIResponse
| ForbiddenErrorQueryAPIResponse;

export const isForbiddenErrorAPIResponse = (
input: unknown
): input is ForbiddenErrorAPIResponse => {
return (
typeof input === "object" &&
input !== null &&
"error" in input &&
("error" in input || "message" in input) &&
"oauth_initiate" in input &&
"oauth_token" in input
);
Expand Down
19 changes: 13 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1223,15 +1223,22 @@ export class Client {
break;
}

// Unauthorized
// - Missing access token for repository endpoint
// - Incorrect access token for repository endpoint
case 401:
// Forbidden
// - Missing access token
// - Incorrect access token
// - Missing access token for query endpoint
// - Incorrect access token for query endpoint
case 403: {
if (isForbiddenErrorAPIResponse(json)) {
throw new ForbiddenError(json.error, {
url,
response: json
});
throw new ForbiddenError(
"error" in json ? json.error : json.message,
{
url,
response: json
}
);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/__testutils__/createRepositoryEndpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as ava from "ava";
import * as crypto from "crypto";

export const createRepositoryEndpoint = (t: ava.ExecutionContext): string => {
const repositoryName = crypto.createHash("md5").update(t.title).digest("hex");

return `https://${repositoryName}.cdn.prismic.io/api/v2`;
};
26 changes: 24 additions & 2 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import { Response } from "node-fetch";

import { createMockQueryHandler } from "./__testutils__/createMockQueryHandler";
import { createMockRepositoryHandler } from "./__testutils__/createMockRepositoryHandler";
import { createQueryEndpoint } from "./__testutils__/createQueryEndpoint";
import { createQueryResponse } from "./__testutils__/createQueryResponse";
import { createRepositoryEndpoint } from "./__testutils__/createRepositoryEndpoint";
import { createRepositoryResponse } from "./__testutils__/createRepositoryResponse";
import { createTestClient } from "./__testutils__/createClient";
import { getMasterRef } from "./__testutils__/getMasterRef";

import * as prismic from "../src";
import { createQueryEndpoint } from "./__testutils__/createQueryEndpoint";

const server = mswNode.setupServer();
test.before(() => server.listen({ onUnhandledRequest: "error" }));
Expand Down Expand Up @@ -251,7 +252,28 @@ test.serial(
}
);

test("throws ForbiddenError if access token is invalid", async t => {
test("throws ForbiddenError if access token is invalid for repository metadata", async t => {
const repositoryResponse = {
message: "invalid access token",
oauth_initiate: "oauth_initiate",
oauth_token: "oauth_token"
};

server.use(
msw.rest.get(createRepositoryEndpoint(t), (_req, res, ctx) => {
return res(ctx.status(401), ctx.json(repositoryResponse));
})
);

const client = createTestClient(t);

await t.throwsAsync(async () => await client.getRepository(), {
instanceOf: prismic.ForbiddenError,
message: /invalid access token/i
});
});

test("throws ForbiddenError if access token is invalid for query", async t => {
const repositoryResponse = createRepositoryResponse();
const queryResponse = {
error: "invalid access token",
Expand Down

0 comments on commit 022d278

Please sign in to comment.