Skip to content

Commit

Permalink
test: fix unauthenticated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Oct 20, 2023
1 parent 5a7b955 commit 455671e
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 30 deletions.
11 changes: 11 additions & 0 deletions packages/manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ export type {
PrismicAuthCheckStatusResponse,
} from "./auth/createPrismicAuthManagerMiddleware";

export {
SliceMachineError,
UnauthorizedError,
UnauthenticatedError,
NotFoundError,
UnexpectedDataError,
InternalError,
PluginError,
PluginHookResultError,
} from "./errors";

export { DecodeError } from "./lib/DecodeError";

export type { SliceMachineConfig, PackageManager } from "./types";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect, it } from "vitest";
import { mockPrismicAuthAPI } from "./__testutils__/mockPrismicAuthAPI";
import { mockPrismicUserAPI } from "./__testutils__/mockPrismicUserAPI";

import { createPrismicAuthManager } from "../src";
import { createPrismicAuthManager, UnauthenticatedError } from "../src";

it("returns parsed cookies from the auth state file", async (ctx) => {
const prismicAuthManager = createPrismicAuthManager();
Expand Down Expand Up @@ -32,5 +32,5 @@ it("throws if the user is not logged in", async () => {

expect(async () => {
await prismicAuthManager.getAuthenticationCookies();
}).rejects.toThrow(/not logged in/i);
}).rejects.toThrow(UnauthenticatedError);
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect, it } from "vitest";
import { mockPrismicAuthAPI } from "./__testutils__/mockPrismicAuthAPI";
import { mockPrismicUserAPI } from "./__testutils__/mockPrismicUserAPI";

import { createPrismicAuthManager } from "../src";
import { createPrismicAuthManager, UnauthenticatedError } from "../src";

it("returns the user's Prismic authentication token", async (ctx) => {
const prismicAuthManager = createPrismicAuthManager();
Expand All @@ -28,5 +28,5 @@ it("throws if the user is not logged in", async () => {

expect(async () => {
await prismicAuthManager.getAuthenticationToken();
}).rejects.toThrow(/not logged in/i);
}).rejects.toThrow(UnauthenticatedError);
});
4 changes: 2 additions & 2 deletions packages/manager/test/PrismicAuthManager-getProfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createPrismicAuthLoginResponse } from "./__testutils__/createPrismicAut
import { mockPrismicAuthAPI } from "./__testutils__/mockPrismicAuthAPI";
import { mockPrismicUserAPI } from "./__testutils__/mockPrismicUserAPI";

import { createPrismicAuthManager } from "../src";
import { createPrismicAuthManager, UnauthenticatedError } from "../src";

it("returns the profile for the user", async (ctx) => {
const prismicAuthManager = createPrismicAuthManager();
Expand All @@ -26,5 +26,5 @@ it("throws if the user is not logged in", async () => {

expect(async () => {
await prismicAuthManager.getAuthenticationToken();
}).rejects.toThrow(/not logged in/i);
}).rejects.toThrow(UnauthenticatedError);
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createPrismicAuthLoginResponse } from "./__testutils__/createPrismicAut
import { mockPrismicAuthAPI } from "./__testutils__/mockPrismicAuthAPI";
import { mockPrismicUserAPI } from "./__testutils__/mockPrismicUserAPI";

import { createPrismicAuthManager } from "../src";
import { createPrismicAuthManager, UnauthenticatedError } from "../src";

it("refreshes the auth token in the auth state file", async (ctx) => {
const prismicAuthManager = createPrismicAuthManager();
Expand Down Expand Up @@ -49,5 +49,5 @@ it("throws if the user is not logged in", async () => {

expect(async () => {
await prismicAuthManager.getAuthenticationToken();
}).rejects.toThrow(/not logged in/i);
}).rejects.toThrow(UnauthenticatedError);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { mockCustomTypesAPI } from "./__testutils__/mockCustomTypesAPI";
import { mockPrismicAuthAPI } from "./__testutils__/mockPrismicAuthAPI";
import { mockPrismicUserAPI } from "./__testutils__/mockPrismicUserAPI";

import { createSliceMachineManager } from "../src";
import { createSliceMachineManager, UnauthenticatedError } from "../src";

it("fetches Custom Types from the Custom Types API", async (ctx) => {
const adapter = createTestPlugin();
Expand Down Expand Up @@ -56,5 +56,5 @@ it("throws if the user is not logged in", async () => {

await expect(async () => {
await manager.customTypes.fetchRemoteCustomTypes();
}).rejects.toThrow(/not logged in/i);
}).rejects.toThrow(UnauthenticatedError);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { mockCustomTypesAPI } from "./__testutils__/mockCustomTypesAPI";
import { mockPrismicAuthAPI } from "./__testutils__/mockPrismicAuthAPI";
import { mockPrismicUserAPI } from "./__testutils__/mockPrismicUserAPI";

import { createSliceMachineManager } from "../src";
import { createSliceMachineManager, UnauthenticatedError } from "../src";

it("pushes a Custom Type using the Custom Types API", async (ctx) => {
const model = ctx.mockPrismic.model.customType();
Expand Down Expand Up @@ -110,5 +110,5 @@ it("throws if not logged in", async () => {

await expect(async () => {
await manager.customTypes.pushCustomType({ id: "id" });
}).rejects.toThrow(/not logged in/i);
}).rejects.toThrow(UnauthenticatedError);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { mockPrismicAuthAPI } from "./__testutils__/mockPrismicAuthAPI";
import { mockPrismicRepositoryAPI } from "./__testutils__/mockPrismicRepositoryAPI";
import { mockPrismicUserAPI } from "./__testutils__/mockPrismicUserAPI";

import { createSliceMachineManager } from "../src";
import { createSliceMachineManager, UnauthenticatedError } from "../src";

// TODO: This test times out. It seems like a bug in MSW or node-fetch.
// `create()` times out when it tries to read the network response's text via
Expand Down Expand Up @@ -104,5 +104,5 @@ it("throws if not logged in", async () => {
domain: "foo",
framework: "other",
});
}).rejects.toThrow(/not logged in/i);
}).rejects.toThrow(UnauthenticatedError);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { mockPrismicAuthAPI } from "./__testutils__/mockPrismicAuthAPI";
import { mockPrismicRepositoryAPI } from "./__testutils__/mockPrismicRepositoryAPI";
import { mockPrismicUserAPI } from "./__testutils__/mockPrismicUserAPI";

import { createSliceMachineManager } from "../src";
import { createSliceMachineManager, UnauthenticatedError } from "../src";

// TODO: This test is unfinished. Is this method necessary? It doesn't seem to
// be used anywhere.
Expand Down Expand Up @@ -102,5 +102,5 @@ it("throws if not logged in", async () => {
domain: "foo",
password: "bar",
});
}).rejects.toThrow(/not logged in/i);
}).rejects.toThrow(UnauthenticatedError);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { mockPrismicAuthAPI } from "./__testutils__/mockPrismicAuthAPI";
import { mockPrismicRepositoryAPI } from "./__testutils__/mockPrismicRepositoryAPI";
import { mockPrismicUserAPI } from "./__testutils__/mockPrismicUserAPI";

import { createSliceMachineManager } from "../src";
import { createSliceMachineManager, UnauthenticatedError } from "../src";

// TODO: The endpoint is called more than once. Why?
it.skip("pushes a given set of documents to a given repository", async (ctx) => {
Expand Down Expand Up @@ -153,5 +153,5 @@ it("throws if not logged in", async () => {
domain: "foo",
framework: "other",
});
}).rejects.toThrow(/not logged in/i);
}).rejects.toThrow(UnauthenticatedError);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createTestProject } from "./__testutils__/createTestProject";
import { mockPrismicAuthAPI } from "./__testutils__/mockPrismicAuthAPI";
import { mockPrismicUserAPI } from "./__testutils__/mockPrismicUserAPI";

import { createSliceMachineManager } from "../src";
import { createSliceMachineManager, UnauthenticatedError } from "../src";

it("returns all repositories for the logged in user", async (ctx) => {
const adapter = createTestPlugin();
Expand Down Expand Up @@ -88,5 +88,5 @@ it("throws if not logged in", async () => {

await expect(async () => {
await manager.prismicRepository.readAll();
}).rejects.toThrow(/not logged in/i);
}).rejects.toThrow(UnauthenticatedError);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { mockCustomTypesAPI } from "./__testutils__/mockCustomTypesAPI";
import { mockPrismicAuthAPI } from "./__testutils__/mockPrismicAuthAPI";
import { mockPrismicUserAPI } from "./__testutils__/mockPrismicUserAPI";

import { createSliceMachineManager } from "../src";
import { createSliceMachineManager, UnauthenticatedError } from "../src";

it("fetches Slices from the Custom Types API", async (ctx) => {
const adapter = createTestPlugin();
Expand Down Expand Up @@ -56,5 +56,5 @@ it("throws if the user is not logged in", async () => {

await expect(async () => {
await manager.slices.fetchRemoteSlices();
}).rejects.toThrow(/not logged in/i);
}).rejects.toThrow(UnauthenticatedError);
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createTestProject } from "./__testutils__/createTestProject";
import { mockPrismicAuthAPI } from "./__testutils__/mockPrismicAuthAPI";
import { mockPrismicUserAPI } from "./__testutils__/mockPrismicUserAPI";

import { createSliceMachineManager } from "../src";
import { createSliceMachineManager, UnauthenticatedError } from "../src";

it("returns parsed cookies from the auth state file", async (ctx) => {
const adapter = createTestPlugin();
Expand Down Expand Up @@ -43,5 +43,5 @@ it("throws if the user is not logged in", async () => {

expect(async () => {
await manager.user.getAuthenticationCookies();
}).rejects.toThrow(/not logged in/i);
}).rejects.toThrow(UnauthenticatedError);
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createTestProject } from "./__testutils__/createTestProject";
import { mockPrismicAuthAPI } from "./__testutils__/mockPrismicAuthAPI";
import { mockPrismicUserAPI } from "./__testutils__/mockPrismicUserAPI";

import { createSliceMachineManager } from "../src";
import { createSliceMachineManager, UnauthenticatedError } from "../src";

it("returns the user's Prismic authentication token", async (ctx) => {
const adapter = createTestPlugin();
Expand Down Expand Up @@ -40,5 +40,5 @@ it("throws if the user is not logged in", async () => {

expect(async () => {
await manager.user.getAuthenticationToken();
}).rejects.toThrow(/not logged in/i);
}).rejects.toThrow(UnauthenticatedError);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createTestProject } from "./__testutils__/createTestProject";
import { mockPrismicAuthAPI } from "./__testutils__/mockPrismicAuthAPI";
import { mockPrismicUserAPI } from "./__testutils__/mockPrismicUserAPI";

import { createSliceMachineManager } from "../src";
import { createSliceMachineManager, UnauthenticatedError } from "../src";

it("returns the profile for the user", async (ctx) => {
const adapter = createTestPlugin();
Expand Down Expand Up @@ -38,5 +38,5 @@ it("throws if the user is not logged in", async () => {

expect(async () => {
await manager.user.getAuthenticationToken();
}).rejects.toThrow(/not logged in/i);
}).rejects.toThrow(UnauthenticatedError);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createTestProject } from "./__testutils__/createTestProject";
import { mockPrismicAuthAPI } from "./__testutils__/mockPrismicAuthAPI";
import { mockPrismicUserAPI } from "./__testutils__/mockPrismicUserAPI";

import { createSliceMachineManager } from "../src";
import { createSliceMachineManager, UnauthenticatedError } from "../src";

it("refreshes the auth token in the auth state file", async (ctx) => {
const adapter = createTestPlugin();
Expand Down Expand Up @@ -66,5 +66,5 @@ it("throws if the user is not logged in", async () => {

expect(async () => {
await manager.user.getAuthenticationToken();
}).rejects.toThrow(/not logged in/i);
}).rejects.toThrow(UnauthenticatedError);
});

0 comments on commit 455671e

Please sign in to comment.