forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestAuth.js
More file actions
38 lines (34 loc) · 1.09 KB
/
Copy pathtestAuth.js
File metadata and controls
38 lines (34 loc) · 1.09 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
import request from "supertest";
import bcrypt from "bcrypt";
import User from "../../src/models/User.js";
// Registration is email-verification-first, so POST /api/auth/register no longer
// returns tokens — it creates a pending record and emails a verification link.
// For suites that just need an authenticated user, seed a verified user directly
// and log in. Returns the full Mongoose user doc plus the access token.
export async function seedUserAndLogin(app, overrides = {}) {
const creds = {
name: "Test User",
email: "seed_user@example.com",
password: "Qx7#vLmp92Zt",
role: "student",
...overrides,
};
const hashedPassword = await bcrypt.hash(creds.password, 12);
const user = await User.create({
name: creds.name,
email: creds.email,
password: hashedPassword,
role: creds.role,
isVerified: true,
});
const res = await request(app)
.post("/api/auth/login")
.send({ email: creds.email, password: creds.password });
return {
token: res.body.accessToken,
refreshToken: res.body.refreshToken,
user,
res,
creds,
};
}