forked from OWASP/NodeGoat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb-reset.js
135 lines (115 loc) · 4.08 KB
/
db-reset.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
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
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env nodejs
"use strict";
// This script initializes the database. You can set the environment variable
// before running it (default: development). ie:
// NODE_ENV=production node artifacts/db-reset.js
const { MongoClient } = require("mongodb");
const { db } = require("../config/config");
const USERS_TO_INSERT = [
{
"_id": 1,
"userName": "admin",
"firstName": "Node Goat",
"lastName": "Admin",
"password": "Admin_123",
//"password" : "$2a$10$8Zo/1e8KM8QzqOKqbDlYlONBOzukWXrM.IiyzqHRYDXqwB3gzDsba", // Admin_123
"isAdmin": true
}, {
"_id": 2,
"userName": "user1",
"firstName": "John",
"lastName": "Doe",
"benefitStartDate": "2030-01-10",
"password": "User1_123"
// "password" : "$2a$10$RNFhiNmt2TTpVO9cqZElb.LQM9e1mzDoggEHufLjAnAKImc6FNE86",// User1_123
}, {
"_id": 3,
"userName": "user2",
"firstName": "Will",
"lastName": "Smith",
"benefitStartDate": "2025-11-30",
"password": "User2_123"
//"password" : "$2a$10$Tlx2cNv15M0Aia7wyItjsepeA8Y6PyBYaNdQqvpxkIUlcONf1ZHyq", // User2_123
}];
const tryDropCollection = (db, name) => {
return new Promise((resolve, reject) => {
db.dropCollection(name, (err, data) => {
if (!err) {
console.log(`Dropped collection: ${name}`);
}
resolve(undefined);
});
});
}
const parseResponse = (err, res, comm) => {
if (err) {
console.log("ERROR:");
console.log(comm);
console.log(JSON.stringify(err));
process.exit(1);
}
console.log(comm);
console.log(JSON.stringify(res));
}
// Starting here
MongoClient.connect(db, (err, db) => {
if (err) {
console.log("ERROR: connect");
console.log(JSON.stringify(err));
process.exit(1);
}
console.log("Connected to the database");
const collectionNames = [
"users",
"allocations",
"contributions",
"memos",
"counters"
];
// remove existing data (if any), we don't want to look for errors here
console.log("Dropping existing collections");
const dropPromises = collectionNames.map((name) => tryDropCollection(db, name));
// Wait for all drops to finish (or fail) before continuing
Promise.all(dropPromises).then(() => {
const usersCol = db.collection("users");
const allocationsCol = db.collection("allocations");
const countersCol = db.collection("counters");
// reset unique id counter
countersCol.insert({
_id: "userId",
seq: 3
}, (err, data) => {
parseResponse(err, data, "countersCol.insert");
});
// insert admin and test users
console.log("Users to insert:");
USERS_TO_INSERT.forEach((user) => console.log(JSON.stringify(user)));
usersCol.insertMany(USERS_TO_INSERT, (err, data) => {
const finalAllocations = [];
// We can't continue if error here
if (err) {
console.log("ERROR: insertMany");
console.log(JSON.stringify(err));
process.exit(1);
}
parseResponse(err, data, "users.insertMany");
data.ops.forEach((user) => {
const stocks = Math.floor((Math.random() * 40) + 1);
const funds = Math.floor((Math.random() * 40) + 1);
finalAllocations.push({
userId: user._id,
stocks: stocks,
funds: funds,
bonds: 100 - (stocks + funds)
});
});
console.log("Allocations to insert:");
finalAllocations.forEach(allocation => console.log(JSON.stringify(allocation)));
allocationsCol.insertMany(finalAllocations, (err, data) => {
parseResponse(err, data, "allocations.insertMany");
console.log("Database reset performed successfully")
process.exit(0);
});
});
});
});