-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathuser.test.ts
More file actions
123 lines (102 loc) · 3.38 KB
/
user.test.ts
File metadata and controls
123 lines (102 loc) · 3.38 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import mongoose from 'mongoose';
import bcrypt from 'bcryptjs';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { User } from '../user';
jest.setTimeout(30000); // give enough time for MongoMemoryServer
let mongoServer: MongoMemoryServer;
beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
const uri = mongoServer.getUri();
await mongoose.connect(uri);
});
afterAll(async () => {
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
await mongoServer.stop();
});
beforeEach(async () => {
await User.deleteMany({});
});
describe('User model', () => {
it('should hash password before saving', async () => {
const user = new User({
username: 'alice',
email: 'alice@example.com',
password: 'mypassword'
});
await user.save();
expect(user.password).not.toBe('mypassword');
const match = await bcrypt.compare('mypassword', user.password!);
expect(match).toBe(true);
});
it('should compare passwords correctly', async () => {
const user = new User({
username: 'bob',
email: 'bob@example.com',
password: 'secret'
});
await user.save();
const isMatch = await user.comparePassword('secret');
expect(isMatch).toBe(true);
const isNotMatch = await user.comparePassword('wrong');
expect(isNotMatch).toBe(false);
});
it('should expose virtual id as string', async () => {
const user = new User({
username: 'carol',
email: 'carol@example.com',
password: 'pass123'
});
await user.save();
expect(user.id).toBe(user._id.toHexString());
});
it('should include virtuals in toJSON output', () => {
const user = new User({
username: 'testuser',
email: 'test@example.com',
password: 'secret'
});
expect(user.id).toBe(user._id.toHexString());
const json = user.toJSON();
expect(json).toHaveProperty('id', user._id.toHexString());
expect(json).toHaveProperty('username', 'testuser');
expect(json).toHaveProperty('email', 'test@example.com');
});
it('should find user by email (case insensitive)', async () => {
await new User({
username: 'dave',
email: 'Dave@Example.com',
password: 'pass'
}).save();
const found = await User.findByEmail('dave@example.com');
expect(found).not.toBeNull();
expect(found!.username).toBe('dave');
});
it('should find user by username (case insensitive)', async () => {
await new User({
username: 'Eve',
email: 'eve@example.com',
password: 'pass'
}).save();
const found = await User.findByUsername('eve', { caseInsensitive: true });
expect(found).not.toBeNull();
expect(found!.email).toBe('eve@example.com');
});
it('should return null for wrong username/email', async () => {
const found = await User.findByEmail('nope@example.com');
expect(found).toBeNull();
});
it('should hash apiKeys on save and find matching key', async () => {
const user = new User({
username: 'frank',
email: 'frank@example.com',
apiKeys: [{ hashedKey: 'hashedApiKey' }]
});
await user.save();
const savedUser = await User.findOne({ email: 'frank@example.com' });
expect(savedUser).not.toBeNull();
const keyObj = await savedUser!.findMatchingKey('hashedApiKey');
expect(keyObj.isMatch).toBe(true);
expect(keyObj.keyDocument).not.toBeNull();
});
});