Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrod-lowe committed Aug 24, 2024
1 parent 91b37d6 commit b7478b4
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 5 deletions.
5 changes: 0 additions & 5 deletions graphql/query/getGame/appsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ export function response(context: Context) {
return;
}

if (!context.result) {
util.appendError("Game not found." as string);
return;
}

return context.result;
}

Expand Down
163 changes: 163 additions & 0 deletions graphql/tests/getGame.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { util, Context, AppSyncIdentityCognito } from "@aws-appsync/utils";
import { request, response } from "../query/getGame/appsync";

// Mock specific functions in the util object
jest.mock("@aws-appsync/utils", () => ({
util: {
autoId: jest.fn(),
time: {
nowISO8601: jest.fn(),
},
error: jest.fn().mockImplementation((message: string) => {
throw new Error(message);
}),
appendError: jest.fn(),
dynamodb: {
toMapValues: jest.fn().mockImplementation((input) =>
Object.entries(input).reduce(
(acc: Record<string, { S: string }>, [key, value]) => {
acc[key] = { S: value as string };
return acc;
},
{},
),
),
},
},
}));

describe("request", () => {
beforeEach(() => {
jest.clearAllMocks();
});

it("should return a DynamoDBGetItemRequest when identity and id are present", () => {
const context: Context<{ id: string }> = {
arguments: { id: "test-id" },
identity: { sub: "test-sub" } as AppSyncIdentityCognito,
} as Context<{ id: string }>;

const result = request(context);

expect(result).toEqual({
operation: "GetItem",
key: {
PK: { S: "GAME#test-id" },
SK: { S: "GAME" },
},
});
});

it("should throw an error when identity is missing", () => {
const context: Context<{ id: string }> = {
arguments: { id: "test-id" },
} as Context<{ id: string }>;

expect(() => request(context)).toThrow(
"Unauthorized: Identity information is missing.",
);
});

it("should throw an error when identity sub is missing", () => {
const context: Context<{ id: string }> = {
arguments: { id: "test-id" },
identity: {} as AppSyncIdentityCognito,
} as Context<{ id: string }>;

expect(() => request(context)).toThrow("Unauthorized: User ID is missing.");
});
});

describe("response", () => {
beforeEach(() => {
jest.clearAllMocks();
});

it("should append error if context.error is present", () => {
const context: Context = {
error: { message: "Some error", type: "SomeType" },
result: {},
} as Context;

response(context);

expect(util.appendError).toHaveBeenCalledWith("Some error", "SomeType", {});
});

it("should append error if identity sub is missing", () => {
const context: Context = {
identity: {} as AppSyncIdentityCognito,
result: {},
} as Context;

response(context);

expect(util.appendError).toHaveBeenCalledWith(
"Unauthorized: User ID is missing.",
);
});

it("should append error if user does not have access", () => {
const context: Context = {
identity: { sub: "unauthorized-sub" } as AppSyncIdentityCognito,
result: { fireflyUserId: "some-other-id", players: [] },
} as Context;

response(context);

expect(util.appendError).toHaveBeenCalledWith(
"Unauthorized: User does not have access to the game.",
);
});

it("should append error if context.result is null", () => {
const context: Context = {
identity: { sub: "test-sub" } as AppSyncIdentityCognito,
result: null,
} as Context;

response(context);

expect(util.appendError).toHaveBeenCalledWith(
"Unauthorized: User does not have access to the game.",
);
});

it("should return context.result if user has access", () => {
const context: Context = {
identity: { sub: "authorized-sub" } as AppSyncIdentityCognito,
result: { fireflyUserId: "authorized-sub", players: [] },
} as Context;

const result = response(context);

expect(result).toEqual({ fireflyUserId: "authorized-sub", players: [] });
});

it("should return context.result if the caller is one of the players", () => {
const context: Context = {
identity: { sub: "player-sub" } as AppSyncIdentityCognito,
result: { fireflyUserId: "some-other-id", players: ["player-sub"] },
} as Context;

const result = response(context);

expect(result).toEqual({
fireflyUserId: "some-other-id",
players: ["player-sub"],
});
});

it("should append error if players are set but the caller is not a player or firefly", () => {
const context: Context = {
identity: { sub: "unauthorized-sub" } as AppSyncIdentityCognito,
result: { fireflyUserId: "some-other-id", players: ["player-sub"] },
} as Context;

response(context);

expect(util.appendError).toHaveBeenCalledWith(
"Unauthorized: User does not have access to the game.",
);
});
});

0 comments on commit b7478b4

Please sign in to comment.