Untitled
unknown
plain_text
2 years ago
6.8 kB
14
Indexable
import { DataSource, Repository } from "typeorm";
import { v4 as uuid } from "uuid";
import { User } from "@lambdas/shared/models";
import { UserDbModel } from "@lambdas/shared/repositories/models/UserDbModel";
import { UserRepository } from "../../../lambdas/shared/repositories/legacy";
import client from "../../../testsHelpers/client";
import testDatabase from "../../../testsHelpers/testDatabase";
jest.mock("aws-sdk", () => ({
...jest.requireActual("aws-sdk"),
CognitoIdentityServiceProvider: class {
adminUpdateUserAttributes() {
return this;
}
adminSetUserPassword() {
return this;
}
adminDeleteUser() {
return this;
}
promise() {
return Promise.resolve({});
}
},
}));
describe("UserRepository tests", () => {
let userRepository: UserRepository;
let userBaseRepository: Repository<UserDbModel>;
let dataSource: DataSource;
beforeAll(async () => {
userRepository = new UserRepository({ client });
dataSource = await testDatabase.getDataSource();
if (!dataSource.isInitialized) {
await dataSource.initialize();
}
userBaseRepository = await testDatabase.getRepository(UserDbModel);
});
beforeEach(async () => {
await userRepository.insertSignedUpUser(
"userID",
"john",
"94",
"771231231"
);
});
afterEach(async () => {
await userRepository.deleteAllByIds(["userID"]);
});
it("findById should return inserted user", async () => {
const { records } = await userRepository.findById("userID");
expect(records[0].user.preferred_name).toEqual("john");
});
describe("updateUser", () => {
it("Should update user successfully", async () => {
const user = new User({
id: "userID",
preferred_name: "test",
mobile_country_code: "65",
mobile: "12341234",
isAdminUserSession: false,
});
const result = await dataSource.transaction((entityManager) =>
userRepository.updateUserWithEntityManager(user, entityManager, true)
);
expect(result.records).toHaveLength(1);
expect(result.records[0].preferred_name).toEqual("test");
expect(result.records[0].mobile_country_code).toEqual("65");
const userDbModel = await userBaseRepository.findOneBy({ id: "userID" });
expect(userDbModel?.preferredName).toEqual("test");
expect(userDbModel?.mobileCountryCode).toEqual("65");
});
});
});
describe("findAllByFilters", () => {
let userRepository: UserRepository;
beforeAll(() => {
userRepository = new UserRepository({ client });
});
describe("search by name", () => {
const user1 = new User({
id: uuid(),
full_name: "Bruce Springsteen",
preferred_name: "Bruce",
mobile_country_code: "65",
mobile: "12341234",
isAdminUserSession: false,
});
const user2 = new User({
id: uuid(),
full_name: "Bruce Lee",
preferred_name: "Bruce",
mobile_country_code: "65",
mobile: "56785678",
isAdminUserSession: false,
});
beforeEach(async () => {
await userRepository.insertSignedUpUser(
user1.id,
user1.full_name!,
user1.mobile_country_code!,
user1.mobile!
);
await userRepository.updateUser(user1);
await userRepository.insertSignedUpUser(
user2.id,
user2.full_name!,
user2.mobile_country_code!,
user2.mobile!
);
await userRepository.updateUser(user2);
});
afterEach(async () => {
await userRepository.deleteAllByIds([user1.id, user2.id]);
});
test("search users with name containing 'Bruce'", async () => {
const dataSource = await testDatabase.getDataSource();
const result = await userRepository.findAllByFilters({
name: "Bruce",
}, dataSource, false);
expect(result.records.map((r) => r.user.id).sort()).toEqual(
[user1.id, user2.id].sort()
);
});
test("search users with name containing 'Lee'", async () => {
const dataSource = await testDatabase.getDataSource();
const result = await userRepository.findAllByFilters({
name: "Lee",
}, dataSource, false);
expect(result.records.map((r) => r.user.id)).toEqual([user2.id]);
});
test('search users with name containing "ruc"', async () => {
const dataSource = await testDatabase.getDataSource();
const result = await userRepository.findAllByFilters({
name: "ruc",
}, dataSource, false);
expect(result.records.map((r) => r.user.id).sort()).toEqual(
[user1.id, user2.id].sort()
);
});
it('search users with name "Bruce Springsteen"', async () => {
const dataSource = await testDatabase.getDataSource();
const result = await userRepository.findAllByFilters({
name: "Bruce Springsteen",
}, dataSource, false);
expect(result.records.map((r) => r.user.id)).toEqual([user1.id]);
});
test('it strips out white spaces from pattern " Bruce "', async () => {
const dataSource = await testDatabase.getDataSource();
const result = await userRepository.findAllByFilters({
name: " Bruce ",
}, dataSource, false);
expect(result.records.map((r) => r.user.id).sort()).toEqual(
[user1.id, user2.id].sort()
);
});
test("the search is not case sensitive so searching 'bRuCe' should return the same results as searching 'Bruce'", async () => {
const dataSource = await testDatabase.getDataSource();
const result = await userRepository.findAllByFilters({
name: "bRuCe",
}, dataSource, false);
expect(result.records.map((r) => r.user.id).sort()).toEqual(
[user1.id, user2.id].sort()
);
});
test("searching with multiple tokens should return users matching both tokens in any order", async () => {
const dataSource = await testDatabase.getDataSource();
const reverseBruceSpringsteen = new User({
id: uuid(),
full_name: "Springsteen Bruce",
preferred_name: "Bruce",
mobile_country_code: "65",
mobile: "13571357",
isAdminUserSession: false,
});
await userRepository.insertSignedUpUser(
reverseBruceSpringsteen.id,
reverseBruceSpringsteen.full_name!,
reverseBruceSpringsteen.mobile_country_code!,
reverseBruceSpringsteen.mobile!
);
await userRepository.updateUser(reverseBruceSpringsteen);
const result = await userRepository.findAllByFilters({
name: "Springsteen Bruce",
}, dataSource, false);
expect(result.records.map((r) => r.user.id).sort()).toEqual(
[user1.id, reverseBruceSpringsteen.id].sort()
);
});
});
});
Editor is loading...