fixed more user unit tests
This commit is contained in:
+29
-29
@@ -8,8 +8,30 @@ const userService = new UserService();
|
||||
|
||||
let user;
|
||||
|
||||
|
||||
const waitForDatabase = () => {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
if (conn.readyState !== 1) {
|
||||
|
||||
conn.once("open", () => {
|
||||
|
||||
resolve();
|
||||
|
||||
})
|
||||
|
||||
} else {
|
||||
|
||||
resolve();
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
beforeEach(async(done) => {
|
||||
|
||||
await waitForDatabase();
|
||||
// user = await createUser();
|
||||
const {user: gotUser} = await createUser();
|
||||
user = gotUser;
|
||||
@@ -33,10 +55,9 @@ test("When giving email and password for login, should return user and token", a
|
||||
password
|
||||
}
|
||||
|
||||
const {user: receivedUser, token} = await userService.login(userData);
|
||||
const {user: receivedUser} = await userService.login(userData);
|
||||
|
||||
expect(receivedUser._id.toString()).toBe(user._id.toString());
|
||||
expect(token.toString().length).not.toBe(0);
|
||||
})
|
||||
|
||||
test("When giving wrong password for login, throw error", async() => {
|
||||
@@ -52,30 +73,12 @@ test("When giving wrong password for login, throw error", async() => {
|
||||
await expect(userService.login(userData)).rejects.toThrow();
|
||||
})
|
||||
|
||||
test("When giving user and token for logout, should remove token", async() => {
|
||||
test("When giving user should logout and not throw error", async() => {
|
||||
|
||||
const userID = user._id;
|
||||
const token = user.tokens[0].token;
|
||||
|
||||
await userService.logout(user, token);
|
||||
const updatedUser = await User.findById(userID);
|
||||
|
||||
expect(updatedUser.tokens.length).toBe(0);
|
||||
await userService.logout(user);
|
||||
})
|
||||
|
||||
|
||||
test("When giving the wrong token, should not remove token", async() => {
|
||||
|
||||
const userID = user._id;
|
||||
const wrongToken = "123456789012";
|
||||
|
||||
await userService.logout(user, wrongToken);
|
||||
const updatedUser = await User.findById(userID);
|
||||
|
||||
expect(updatedUser.tokens.length).toBe(1);
|
||||
})
|
||||
|
||||
test("When giving user, should logout all", async() => {
|
||||
test("When giving user, should logout all, show 0 tokens, and not throw error", async() => {
|
||||
|
||||
const userID = user._id;
|
||||
|
||||
@@ -93,10 +96,9 @@ test("When giving user data, should create new user", async() => {
|
||||
password: "12345678",
|
||||
}
|
||||
|
||||
const {user: createdUser, token} = await userService.create(userData);
|
||||
const {user: createdUser} = await userService.create(userData);
|
||||
|
||||
expect(createdUser.email).toBe(userData.email);
|
||||
expect(token.toString().length).not.toBe(0);
|
||||
})
|
||||
|
||||
test("When creating user with duplicate email, should throw an error", async() => {
|
||||
@@ -120,18 +122,16 @@ test("When giving user, token, old password, and new password. Should change pas
|
||||
|
||||
test("When giving wrong old password for change password, should throw an error", async() => {
|
||||
|
||||
const token = user.tokens[0].token;
|
||||
const wrongOldPassword = "1649560569";
|
||||
const newPassword = "87654321";
|
||||
|
||||
await expect(userService.changePassword(user, token, wrongOldPassword, newPassword)).rejects.toThrow();
|
||||
await expect(userService.changePassword(user._id, wrongOldPassword, newPassword)).rejects.toThrow();
|
||||
})
|
||||
|
||||
test("When giving new password with insufficent length, should throw an error", async() => {
|
||||
|
||||
const token = user.tokens[0].token;
|
||||
const oldPassword = "12345678";
|
||||
const newPassword = "8765";
|
||||
|
||||
await expect(userService.changePassword(user, token, oldPassword, newPassword)).rejects.toThrow();
|
||||
await expect(userService.changePassword(user._id, oldPassword, newPassword)).rejects.toThrow();
|
||||
})
|
||||
Reference in New Issue
Block a user