-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.test.js
74 lines (61 loc) · 2 KB
/
user.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const MongoClient = require("mongodb").MongoClient;
const User = require("./user")
describe("User Info Management", () => {
let client;
beforeAll(async () => {
client = await MongoClient.connect(
"mongodb+srv://m001-student:m001-mongodb-basics@sandbox.te4xf.mongodb.net/Sandbox?retryWrites=true&w=majority",
{ useNewUrlParser: true },
);
User.injectDB(client);
})
afterAll(async () => {
await client.close();
})
test("New user registration", async () => {
const res = await User.register("test", "123abc","test@gmail.com", "user");
expect(res.username).toBe("test");
expect(res.email).toBe("test@gmail.com");
expect(res.role).toBe("user");
})
test("Duplicate username", async () => {
const res = await User.register("admin", "123abc","admin@gmail.com", "admin");
expect(res).toBe(null)
})
test("User login invalid username", async () => {
const res = await User.login("useless name", "123abc")
expect(res).toBe(null)
})
test("User login invalid password", async () => {
const res = await User.login("admin", "wrong password")
expect(res).toBe(null)
})
test("User login successfully", async () => {
const res = await User.login("admin", "123abc")
expect(res.username).toBe("admin")
expect(res.email).toBe("admin@gmail.com"),
expect(res.role).toBe("admin")
})
test("User update Successfully", async () => {
const res = await User.update("test", "test@gmail.com", "admin");
expect(res.username).toBe("test");
expect(res.email).toBe("test@gmail.com");
expect(res.role).toBe("admin");
})
test("User update Failed", async () => {
const res = await User.update("none", "test@gmail.com", "admin");
expect(res).toBe(null);
})
test("User delete Successfully", async () => {
const res = await User.delete("test");
expect(res).toBe(true);
})
test("User delete Failed", async () => {
const res = await User.delete("none");
expect(res).toBe(null);
})
test("Get all users", async () => {
const res = await User.getAllUsers();
expect(res.length).toBe(7);
})
});