-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-utils.js
39 lines (32 loc) · 1.05 KB
/
test-utils.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
// Import necessary modules and functions for user creation and authentication
const User = require('../models/user');
const jwt = require('jsonwebtoken');
const { SECRET_KEY } = require('../config');
async function createAdminUserAndAuthenticate() {
try {
// Create an admin user with the provided data
const adminUserData = {
username: 'adminuser',
password: 'adminpassword',
isAdmin: true,
// Add other required fields as needed
};
// Create the admin user
const adminUser = await User.create(adminUserData);
// Generate a token for the admin user
const token = jwt.sign(
{ username: adminUser.username, isAdmin: adminUser.isAdmin },
SECRET_KEY
);
return token;
} catch (error) {
// Handle any errors that occur during user creation or token generation
console.error('Error creating admin user:', error);
throw error; // Rethrow the error to indicate failure
}
}
// Export the functions
module.exports = {
createAdminUserAndAuthenticate,
createNonAdminUserAndAuthenticate,
};