Skip to content

Commit

Permalink
test(userpool): convert some integration tests to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jagregory committed Apr 12, 2020
1 parent f184bcf commit 44e3cf0
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 84 deletions.
122 changes: 38 additions & 84 deletions integration-tests/userPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -106,7 +106,7 @@ describe("User Pool", () => {
},
});

await dataStore.saveUser({
await userPool.saveUser({
Username: "1",
Password: "hunter3",
UserStatus: "CONFIRMED",
Expand Down Expand Up @@ -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");
});
});
});
150 changes: 150 additions & 0 deletions src/services/userPool.test.ts
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();
});
}
}
);
});
});

0 comments on commit 44e3cf0

Please sign in to comment.