-
Notifications
You must be signed in to change notification settings - Fork 74
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
Showing
5 changed files
with
201 additions
and
0 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,98 @@ | ||
import { ClockFake } from "../../src/__tests__/clockFake"; | ||
import { withCognitoSdk } from "./setup"; | ||
|
||
const originalDate = new Date(); | ||
const roundedDate = new Date(originalDate.getTime()); | ||
roundedDate.setMilliseconds(0); | ||
|
||
const clock = new ClockFake(originalDate); | ||
|
||
describe( | ||
"CognitoIdentityServiceProvider.listGroups", | ||
withCognitoSdk( | ||
(Cognito) => { | ||
it("lists groups", async () => { | ||
const client = Cognito(); | ||
|
||
await client | ||
.createGroup({ | ||
GroupName: "abc", | ||
UserPoolId: "test1", | ||
}) | ||
.promise(); | ||
await client | ||
.createGroup({ | ||
GroupName: "def", | ||
UserPoolId: "test1", | ||
}) | ||
.promise(); | ||
await client | ||
.createGroup({ | ||
GroupName: "ghi", | ||
UserPoolId: "test2", | ||
}) | ||
.promise(); | ||
|
||
const result1 = await client | ||
.listGroups({ | ||
UserPoolId: "test1", | ||
}) | ||
.promise(); | ||
|
||
expect(result1).toEqual({ | ||
Groups: [ | ||
{ | ||
CreationDate: roundedDate, | ||
GroupName: "abc", | ||
LastModifiedDate: roundedDate, | ||
UserPoolId: "test1", | ||
}, | ||
{ | ||
CreationDate: roundedDate, | ||
GroupName: "def", | ||
LastModifiedDate: roundedDate, | ||
UserPoolId: "test1", | ||
}, | ||
], | ||
}); | ||
|
||
const result2 = await client | ||
.listGroups({ | ||
UserPoolId: "test2", | ||
}) | ||
.promise(); | ||
|
||
expect(result2).toEqual({ | ||
Groups: [ | ||
{ | ||
CreationDate: roundedDate, | ||
GroupName: "ghi", | ||
LastModifiedDate: roundedDate, | ||
UserPoolId: "test2", | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it("returns an empty collection when there are no groups", async () => { | ||
const client = Cognito(); | ||
|
||
const result = await client | ||
.listGroups({ | ||
UserPoolId: "test1", | ||
}) | ||
.promise(); | ||
|
||
expect(result).toEqual({ | ||
Groups: [], | ||
}); | ||
}); | ||
|
||
// TODO: getUserPool lazily creates a pool right now, so we can't handle invalid user pools | ||
it.todo("handles invalid user pool"); | ||
}, | ||
{ | ||
clock, | ||
} | ||
) | ||
); |
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,67 @@ | ||
import { MockUserPoolClient } from "../__tests__/mockUserPoolClient"; | ||
import { CognitoClient } from "../services"; | ||
import { ListGroups, ListGroupsTarget } from "./listGroups"; | ||
|
||
describe("ListGroups target", () => { | ||
let listGroups: ListGroupsTarget; | ||
let mockCognitoClient: jest.Mocked<CognitoClient>; | ||
let now: Date; | ||
|
||
beforeEach(() => { | ||
now = new Date(2020, 1, 2, 3, 4, 5); | ||
|
||
mockCognitoClient = { | ||
getAppClient: jest.fn(), | ||
getUserPool: jest.fn().mockResolvedValue(MockUserPoolClient), | ||
getUserPoolForClientId: jest.fn().mockResolvedValue(MockUserPoolClient), | ||
}; | ||
|
||
listGroups = ListGroups({ | ||
cognitoClient: mockCognitoClient, | ||
}); | ||
}); | ||
|
||
it("lists groups", async () => { | ||
MockUserPoolClient.listGroups.mockResolvedValue([ | ||
{ | ||
CreationDate: now.getTime(), | ||
Description: "Description", | ||
GroupName: "abc", | ||
LastModifiedDate: now.getTime(), | ||
Precedence: 1, | ||
RoleArn: "ARN", | ||
}, | ||
{ | ||
CreationDate: now.getTime(), | ||
GroupName: "def", | ||
LastModifiedDate: now.getTime(), | ||
}, | ||
]); | ||
|
||
const output = await listGroups({ | ||
UserPoolId: "userPoolId", | ||
}); | ||
|
||
expect(output).toBeDefined(); | ||
expect(output.Groups).toEqual([ | ||
{ | ||
CreationDate: now, | ||
Description: "Description", | ||
GroupName: "abc", | ||
LastModifiedDate: now, | ||
Precedence: 1, | ||
RoleArn: "ARN", | ||
UserPoolId: "userPoolId", | ||
}, | ||
{ | ||
CreationDate: now, | ||
GroupName: "def", | ||
LastModifiedDate: now, | ||
UserPoolId: "userPoolId", | ||
}, | ||
]); | ||
}); | ||
|
||
it.todo("supports Limit to specify the number of groups to return"); | ||
it.todo("supports PaginationToken to paginate results"); | ||
}); |
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,33 @@ | ||
import { | ||
ListGroupsRequest, | ||
ListGroupsResponse, | ||
} from "aws-sdk/clients/cognitoidentityserviceprovider"; | ||
import { Services } from "../services"; | ||
|
||
export type ListGroupsTarget = ( | ||
req: ListGroupsRequest | ||
) => Promise<ListGroupsResponse>; | ||
|
||
type ListGroupServices = Pick<Services, "cognitoClient">; | ||
|
||
export const ListGroups = ({ | ||
cognitoClient, | ||
}: ListGroupServices): ListGroupsTarget => async (req) => { | ||
// TODO: Limit support | ||
// TODO: PaginationToken support | ||
|
||
const userPool = await cognitoClient.getUserPool(req.UserPoolId); | ||
const groups = await userPool.listGroups(); | ||
|
||
return { | ||
Groups: groups.map((group) => ({ | ||
CreationDate: new Date(group.CreationDate), | ||
Description: group.Description, | ||
GroupName: group.GroupName, | ||
LastModifiedDate: new Date(group.LastModifiedDate), | ||
Precedence: group.Precedence, | ||
RoleArn: group.RoleArn, | ||
UserPoolId: req.UserPoolId, | ||
})), | ||
}; | ||
}; |
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