forked from jagregory/cognito-local
-
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.
test(userpool): convert some integration tests to unit tests
- Loading branch information
Showing
2 changed files
with
188 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,12 +31,12 @@ describe("User Pool", () => { | |
describe("saveUser", () => { | ||
it("saves a user with their username as an additional attribute", async () => { | ||
const now = new Date().getTime(); | ||
const dataStore = await createUserPool( | ||
const userPool = await createUserPool( | ||
{ UsernameAttributes: [] }, | ||
tmpCreateDataStore | ||
); | ||
|
||
await dataStore.saveUser({ | ||
await userPool.saveUser({ | ||
Username: "1", | ||
Password: "hunter3", | ||
UserStatus: "UNCONFIRMED", | ||
|
@@ -69,12 +69,12 @@ describe("User Pool", () => { | |
|
||
it("updates a user", async () => { | ||
const now = new Date().getTime(); | ||
const dataStore = await createUserPool( | ||
const userPool = await createUserPool( | ||
{ UsernameAttributes: [] }, | ||
tmpCreateDataStore | ||
); | ||
|
||
await dataStore.saveUser({ | ||
await userPool.saveUser({ | ||
Username: "1", | ||
Password: "hunter3", | ||
UserStatus: "UNCONFIRMED", | ||
|
@@ -106,7 +106,7 @@ describe("User Pool", () => { | |
}, | ||
}); | ||
|
||
await dataStore.saveUser({ | ||
await userPool.saveUser({ | ||
Username: "1", | ||
Password: "hunter3", | ||
UserStatus: "CONFIRMED", | ||
|
@@ -139,84 +139,38 @@ describe("User Pool", () => { | |
}); | ||
|
||
describe("getUserByUsername", () => { | ||
describe.each` | ||
username_attributes | find_by_email | find_by_phone_number | ||
${[]} | ${false} | ${false} | ||
${["email"]} | ${true} | ${false} | ||
${["phone_number"]} | ${false} | ${true} | ||
${["email", "phone_number"]} | ${true} | ${true} | ||
`( | ||
"$username_attributes username attributes", | ||
({ username_attributes, find_by_email, find_by_phone_number }) => { | ||
let dataStore: UserPool; | ||
|
||
beforeAll(async () => { | ||
dataStore = await createUserPool( | ||
{ UsernameAttributes: username_attributes }, | ||
tmpCreateDataStore | ||
); | ||
|
||
await dataStore.saveUser({ | ||
Username: "1", | ||
Password: "hunter2", | ||
UserStatus: "UNCONFIRMED", | ||
Attributes: [ | ||
{ Name: "email", Value: "[email protected]" }, | ||
{ Name: "phone_number", Value: "0411000111" }, | ||
], | ||
UserCreateDate: new Date().getTime(), | ||
UserLastModifiedDate: new Date().getTime(), | ||
Enabled: true, | ||
}); | ||
}); | ||
|
||
it("returns null if user doesn't exist", async () => { | ||
const user = await dataStore.getUserByUsername("invalid"); | ||
|
||
expect(user).toBeNull(); | ||
}); | ||
|
||
it("returns existing user by their sub attribute", async () => { | ||
const user = await dataStore.getUserByUsername("1"); | ||
|
||
expect(user).not.toBeNull(); | ||
expect(user?.Username).toEqual("1"); | ||
}); | ||
|
||
if (find_by_email) { | ||
it("returns existing user by their email", async () => { | ||
const user = await dataStore.getUserByUsername( | ||
"[email protected]" | ||
); | ||
|
||
expect(user).not.toBeNull(); | ||
expect(user?.Username).toEqual("1"); | ||
}); | ||
} else { | ||
it("does not return the user by their email", async () => { | ||
const user = await dataStore.getUserByUsername( | ||
"[email protected]" | ||
); | ||
|
||
expect(user).toBeNull(); | ||
}); | ||
} | ||
|
||
if (find_by_phone_number) { | ||
it("returns existing user by their phone number", async () => { | ||
const user = await dataStore.getUserByUsername("0411000111"); | ||
|
||
expect(user).not.toBeNull(); | ||
expect(user?.Username).toEqual("1"); | ||
}); | ||
} else { | ||
it("does not return the user by their phone number", async () => { | ||
const user = await dataStore.getUserByUsername("0411000111"); | ||
|
||
expect(user).toBeNull(); | ||
}); | ||
} | ||
} | ||
); | ||
let userPool: UserPool; | ||
beforeAll(async () => { | ||
userPool = await createUserPool( | ||
{ UsernameAttributes: [] }, | ||
tmpCreateDataStore | ||
); | ||
|
||
await userPool.saveUser({ | ||
Username: "1", | ||
Password: "hunter2", | ||
UserStatus: "UNCONFIRMED", | ||
Attributes: [ | ||
{ Name: "email", Value: "[email protected]" }, | ||
{ Name: "phone_number", Value: "0411000111" }, | ||
], | ||
UserCreateDate: new Date().getTime(), | ||
UserLastModifiedDate: new Date().getTime(), | ||
Enabled: true, | ||
}); | ||
}); | ||
|
||
it("returns null if user doesn't exist", async () => { | ||
const user = await userPool.getUserByUsername("invalid"); | ||
|
||
expect(user).toBeNull(); | ||
}); | ||
|
||
it("returns existing user by their sub attribute", async () => { | ||
const user = await userPool.getUserByUsername("1"); | ||
|
||
expect(user).not.toBeNull(); | ||
expect(user?.Username).toEqual("1"); | ||
}); | ||
}); | ||
}); |
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,150 @@ | ||
import { CreateDataStore, DataStore } from "./dataStore"; | ||
import { createUserPool, UserPool } from "./userPool"; | ||
|
||
describe("User Pool", () => { | ||
let mockDataStore: jest.Mocked<DataStore>; | ||
|
||
beforeEach(() => { | ||
mockDataStore = { | ||
set: jest.fn(), | ||
get: jest.fn(), | ||
}; | ||
}); | ||
|
||
it("creates a database", async () => { | ||
const createDataStore = (jest.fn( | ||
() => mockDataStore | ||
) as unknown) as CreateDataStore; | ||
await createUserPool({ UsernameAttributes: [] }, createDataStore); | ||
|
||
expect(createDataStore).toHaveBeenCalledWith("local", { | ||
Options: { UsernameAttributes: [] }, | ||
Users: {}, | ||
}); | ||
}); | ||
|
||
describe("saveUser", () => { | ||
it("saves a user with their username as an additional attribute", async () => { | ||
const now = new Date().getTime(); | ||
const userPool = await createUserPool({ UsernameAttributes: [] }, () => | ||
Promise.resolve(mockDataStore) | ||
); | ||
|
||
await userPool.saveUser({ | ||
Username: "1", | ||
Password: "hunter3", | ||
UserStatus: "UNCONFIRMED", | ||
Attributes: [{ Name: "email", Value: "[email protected]" }], | ||
UserLastModifiedDate: now, | ||
UserCreateDate: now, | ||
Enabled: true, | ||
}); | ||
|
||
expect(mockDataStore.set).toHaveBeenCalledWith("Users.1", { | ||
Username: "1", | ||
Password: "hunter3", | ||
UserStatus: "UNCONFIRMED", | ||
Attributes: [ | ||
{ Name: "sub", Value: "1" }, | ||
{ Name: "email", Value: "[email protected]" }, | ||
], | ||
UserLastModifiedDate: now, | ||
UserCreateDate: now, | ||
Enabled: true, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("getUserByUsername", () => { | ||
describe.each` | ||
username_attributes | find_by_email | find_by_phone_number | ||
${[]} | ${false} | ${false} | ||
${["email"]} | ${true} | ${false} | ||
${["phone_number"]} | ${false} | ${true} | ||
${["email", "phone_number"]} | ${true} | ${true} | ||
`( | ||
"$username_attributes username attributes", | ||
({ username_attributes, find_by_email, find_by_phone_number }) => { | ||
let userPool: UserPool; | ||
|
||
beforeAll(async () => { | ||
const options = { UsernameAttributes: username_attributes }; | ||
const users = { | ||
"1": { | ||
Username: "1", | ||
Password: "hunter3", | ||
UserStatus: "UNCONFIRMED", | ||
Attributes: [ | ||
{ Name: "sub", Value: "1" }, | ||
{ Name: "email", Value: "[email protected]" }, | ||
{ Name: "phone_number", Value: "0411000111" }, | ||
], | ||
UserLastModifiedDate: new Date().getTime(), | ||
UserCreateDate: new Date().getTime(), | ||
Enabled: true, | ||
}, | ||
}; | ||
mockDataStore.get.mockImplementation((key) => { | ||
if (key === "Users") { | ||
return Promise.resolve(users); | ||
} else if (key === "Options") { | ||
return Promise.resolve(options); | ||
} | ||
|
||
return Promise.resolve(null); | ||
}); | ||
userPool = await createUserPool(options, () => | ||
Promise.resolve(mockDataStore) | ||
); | ||
}); | ||
|
||
it("returns null if user doesn't exist", async () => { | ||
const user = await userPool.getUserByUsername("invalid"); | ||
|
||
expect(user).toBeNull(); | ||
}); | ||
|
||
it("returns existing user by their sub attribute", async () => { | ||
const user = await userPool.getUserByUsername("1"); | ||
|
||
expect(user).not.toBeNull(); | ||
expect(user?.Username).toEqual("1"); | ||
}); | ||
|
||
if (find_by_email) { | ||
it("returns existing user by their email", async () => { | ||
const user = await userPool.getUserByUsername( | ||
"[email protected]" | ||
); | ||
|
||
expect(user).not.toBeNull(); | ||
expect(user?.Username).toEqual("1"); | ||
}); | ||
} else { | ||
it("does not return the user by their email", async () => { | ||
const user = await userPool.getUserByUsername( | ||
"[email protected]" | ||
); | ||
|
||
expect(user).toBeNull(); | ||
}); | ||
} | ||
|
||
if (find_by_phone_number) { | ||
it("returns existing user by their phone number", async () => { | ||
const user = await userPool.getUserByUsername("0411000111"); | ||
|
||
expect(user).not.toBeNull(); | ||
expect(user?.Username).toEqual("1"); | ||
}); | ||
} else { | ||
it("does not return the user by their phone number", async () => { | ||
const user = await userPool.getUserByUsername("0411000111"); | ||
|
||
expect(user).toBeNull(); | ||
}); | ||
} | ||
} | ||
); | ||
}); | ||
}); |