-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91b37d6
commit b7478b4
Showing
2 changed files
with
163 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.", | ||
); | ||
}); | ||
}); |