-
Notifications
You must be signed in to change notification settings - Fork 73
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
13 changed files
with
231 additions
and
9 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 |
---|---|---|
|
@@ -176,4 +176,70 @@ describe("User Pool", () => { | |
expect(user?.Username).toEqual("1"); | ||
}); | ||
}); | ||
|
||
describe("listUsers", () => { | ||
let userPool: UserPool; | ||
let now: Date; | ||
|
||
beforeAll(async () => { | ||
now = new Date(); | ||
|
||
userPool = await createUserPool( | ||
{ UserPoolId: "local", UsernameAttributes: [] }, | ||
tmpCreateDataStore | ||
); | ||
|
||
await userPool.saveUser({ | ||
Username: "1", | ||
Password: "hunter2", | ||
UserStatus: "UNCONFIRMED", | ||
Attributes: [ | ||
{ Name: "email", Value: "[email protected]" }, | ||
{ Name: "phone_number", Value: "0411000111" }, | ||
], | ||
UserCreateDate: now.getTime(), | ||
UserLastModifiedDate: now.getTime(), | ||
Enabled: true, | ||
}); | ||
|
||
await userPool.saveUser({ | ||
Username: "2", | ||
Password: "password1", | ||
UserStatus: "UNCONFIRMED", | ||
Attributes: [], | ||
UserCreateDate: now.getTime(), | ||
UserLastModifiedDate: now.getTime(), | ||
Enabled: true, | ||
}); | ||
}); | ||
|
||
it("returns all users", async () => { | ||
const users = await userPool.listUsers(); | ||
|
||
expect(users).toEqual([ | ||
{ | ||
Username: "1", | ||
Password: "hunter2", | ||
UserStatus: "UNCONFIRMED", | ||
Attributes: [ | ||
{ Name: "sub", Value: "1" }, | ||
{ Name: "email", Value: "[email protected]" }, | ||
{ Name: "phone_number", Value: "0411000111" }, | ||
], | ||
UserCreateDate: now.getTime(), | ||
UserLastModifiedDate: now.getTime(), | ||
Enabled: true, | ||
}, | ||
{ | ||
Username: "2", | ||
Password: "password1", | ||
UserStatus: "UNCONFIRMED", | ||
Attributes: [{ Name: "sub", Value: "2" }], | ||
UserCreateDate: now.getTime(), | ||
UserLastModifiedDate: now.getTime(), | ||
Enabled: true, | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |
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
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
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
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,91 @@ | ||
import { advanceTo } from "jest-date-mock"; | ||
import { UserPool } from "../services"; | ||
import { Triggers } from "../services/triggers"; | ||
import { ListUsers, ListUsersTarget } from "./listUsers"; | ||
|
||
describe("ListUsers target", () => { | ||
let listUsers: ListUsersTarget; | ||
let mockDataStore: jest.Mocked<UserPool>; | ||
let mockCodeDelivery: jest.Mock; | ||
let mockTriggers: jest.Mocked<Triggers>; | ||
let now: Date; | ||
|
||
beforeEach(() => { | ||
now = new Date(2020, 1, 2, 3, 4, 5); | ||
advanceTo(now); | ||
|
||
mockDataStore = { | ||
getUserByUsername: jest.fn(), | ||
getUserPoolIdForClientId: jest.fn(), | ||
listUsers: jest.fn(), | ||
saveUser: jest.fn(), | ||
}; | ||
mockCodeDelivery = jest.fn(); | ||
mockTriggers = { | ||
enabled: jest.fn(), | ||
postConfirmation: jest.fn(), | ||
userMigration: jest.fn(), | ||
}; | ||
|
||
listUsers = ListUsers({ | ||
userPool: mockDataStore, | ||
codeDelivery: mockCodeDelivery, | ||
triggers: mockTriggers, | ||
}); | ||
}); | ||
|
||
it.todo("should validate UserPoolId parameter"); | ||
|
||
it("lists users and removes Cognito Local fields", async () => { | ||
mockDataStore.listUsers.mockResolvedValue([ | ||
{ | ||
Attributes: [], | ||
UserStatus: "CONFIRMED", | ||
Password: "hunter2", | ||
Username: "0000-0000", | ||
Enabled: true, | ||
UserCreateDate: new Date().getTime(), | ||
UserLastModifiedDate: new Date().getTime(), | ||
ConfirmationCode: "1234", | ||
}, | ||
{ | ||
Attributes: [], | ||
UserStatus: "CONFIRMED", | ||
Password: "password1", | ||
Username: "1111-1111", | ||
Enabled: true, | ||
UserCreateDate: new Date().getTime(), | ||
UserLastModifiedDate: new Date().getTime(), | ||
}, | ||
]); | ||
|
||
const output = await listUsers({ | ||
UserPoolId: "userPoolId", | ||
}); | ||
|
||
expect(output).toBeDefined(); | ||
expect(output.Users).toEqual([ | ||
{ | ||
Attributes: [], | ||
UserStatus: "CONFIRMED", | ||
Username: "0000-0000", | ||
Enabled: true, | ||
UserCreateDate: new Date().getTime(), | ||
UserLastModifiedDate: new Date().getTime(), | ||
}, | ||
{ | ||
Attributes: [], | ||
UserStatus: "CONFIRMED", | ||
Username: "1111-1111", | ||
Enabled: true, | ||
UserCreateDate: new Date().getTime(), | ||
UserLastModifiedDate: new Date().getTime(), | ||
}, | ||
]); | ||
}); | ||
|
||
it.todo("supports AttributesToGet to specify which attributes to return"); | ||
it.todo("supports Filter to filter users before returning"); | ||
it.todo("supports Limit to specify the number of users 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,43 @@ | ||
import { Services } from "../services"; | ||
import { UserAttribute } from "../services/userPool"; | ||
|
||
interface Input { | ||
UserPoolId: string; | ||
AttributesToGet?: string[]; // TODO: filter returned attributes | ||
Filter?: string; // TODO: filter users before returning | ||
Limit?: number; // TODO: limit number of returned users | ||
PaginationToken?: string; // TODO: support pagination | ||
} | ||
|
||
export interface DynamoDBUserRecord { | ||
Username: string; | ||
UserCreateDate: number; | ||
UserLastModifiedDate: number; | ||
Enabled: boolean; | ||
UserStatus: "CONFIRMED" | "UNCONFIRMED" | "RESET_REQUIRED"; | ||
Attributes: readonly UserAttribute[]; | ||
} | ||
|
||
interface Output { | ||
PaginationToken?: string; | ||
Users: readonly DynamoDBUserRecord[]; | ||
} | ||
|
||
export type ListUsersTarget = (body: Input) => Promise<Output>; | ||
|
||
export const ListUsers = ({ | ||
userPool, | ||
}: Services): ListUsersTarget => async () => { | ||
const users = await userPool.listUsers(); | ||
|
||
return { | ||
Users: users.map((user) => ({ | ||
Username: user.Username, | ||
UserCreateDate: user.UserCreateDate, | ||
UserLastModifiedDate: user.UserLastModifiedDate, | ||
Enabled: user.Enabled, | ||
UserStatus: user.UserStatus, | ||
Attributes: user.Attributes, | ||
})), | ||
}; | ||
}; |
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