Skip to content

Commit a63483a

Browse files
committed
Create new account generators and naming framework
1 parent 62ae5f7 commit a63483a

File tree

1 file changed

+206
-96
lines changed

1 file changed

+206
-96
lines changed

tests/util/account.test.util.js

Lines changed: 206 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,198 @@ const mongoose = require("mongoose");
55
const bcrypt = require("bcrypt");
66
const logger = require("../../services/logger.service");
77

8+
let counters = {
9+
emailCounter: 0,
10+
};
11+
12+
function incrementCounters() {
13+
for (const key in counters) {
14+
if (counters.hasOwnProperty(key)) {
15+
counters[key] = counters[key] + 1;
16+
}
17+
}
18+
}
19+
20+
function extractAccountInfo(acc) {
21+
let accDetails = {};
22+
23+
for (const val in acc) {
24+
// use .hasOwnProperty instead of 'in' to get rid of inherited properties such as 'should'
25+
if (Account.schema.paths.hasOwnProperty(val)) {
26+
accDetails[val] = acc[val];
27+
}
28+
}
29+
30+
return accDetails;
31+
}
32+
33+
function generateRandomValue(atr) {
34+
switch (atr) {
35+
case "_id":
36+
return mongoose.Types.ObjectId();
37+
case "firstName":
38+
// generates a random string between length 5 and 10 of random characters from a-z
39+
return Math.random().toString(36).replace(/[^a-z]+/g, "").substr(0, Math.floor(Math.random() * 6 + 5));
40+
case "lastName":
41+
return Math.random().toString(36).replace(/[^a-z]+/g, "").substr(0, Math.floor(Math.random() * 6 + 5));
42+
case "pronoun":
43+
// generate random string between length 2 and 4
44+
return Math.random().toString(36).replace(/[^a-z]+/g, "").substr(0, Math.floor(Math.random() * 3 + 2));
45+
case "email":
46+
const email = `abc.def${counters.emailCounter}@blahblah.com`;
47+
return email;
48+
case "password":
49+
return Math.random().toString(36).substr(0, 10);
50+
case "dietaryRestrictions":
51+
return Constants.SAMPLE_DIET_RESTRICTIONS[Math.floor(Math.random() * Constants.SAMPLE_DIET_RESTRICTIONS.length)];
52+
case "shirtSize":
53+
return Constants.SHIRT_SIZES[Math.floor(Math.random() * Constants.SHIRT_SIZES.length)];
54+
case "confirmed":
55+
// return false, because if an account is confirmed there should be a document of that account type,
56+
// which this does not create
57+
return Math.random() < 0.5;
58+
case "accountType":
59+
return Constants.EXTENDED_USER_TYPES[Math.floor(Math.random() * Constants.EXTENDED_USER_TYPES.length)];
60+
case "birthDate":
61+
return new Date();
62+
case "phoneNumber":
63+
return Math.floor(Math.random() * 10000000000);
64+
}
65+
}
66+
67+
function createAccount(acc = {}) {
68+
incrementCounters();
69+
70+
const extractedAcc = extractAccountInfo(acc);
71+
72+
for (const atr in Account.schema.paths) {
73+
if (!Account.schema.paths.hasOwnProperty(atr)) {
74+
continue;
75+
}
76+
77+
// if this value has been passed in, continue
78+
if (extractedAcc[atr] !== undefined) {
79+
continue;
80+
}
81+
82+
extractedAcc[atr] = generateRandomValue(atr);
83+
}
84+
85+
return extractedAcc;
86+
}
87+
88+
function createNAccounts(n, acc = {}) {
89+
let accounts = []
90+
for (let i = 0; i < n; i++) {
91+
accounts.push(createAccount(acc));
92+
}
93+
94+
return accounts;
95+
}
96+
97+
let hackerAccounts = {
98+
new: createNAccounts(10, {
99+
"accountType": Constants.HACKER
100+
}),
101+
stored: {
102+
team: createNAccounts(10, {
103+
"accountType": Constants.HACKER
104+
}),
105+
noTeam: createNAccounts(10, {
106+
"accountType": Constants.HACKER
107+
}),
108+
},
109+
invalid: createNAccounts(10, {
110+
"accountType": Constants.HACKER
111+
})
112+
};
113+
114+
let volunteerAccounts = {
115+
new: createNAccounts(5, {
116+
"accountType": Constants.VOLUNTEER
117+
}),
118+
stored: createNAccounts(5, {
119+
"accountType": Constants.VOLUNTEER
120+
}),
121+
invalid: createNAccounts(5, {
122+
"accountType": Constants.VOLUNTEER
123+
}),
124+
};
125+
126+
let staffAccounts = {
127+
stored: createNAccounts(5, {
128+
"accountType": Constants.STAFF
129+
})
130+
};
131+
132+
let sponsorT1Accounts = {
133+
new: createNAccounts(5, {
134+
"accountType": Constants.SPONSOR_T1
135+
}),
136+
stored: createNAccounts(5, {
137+
"accountType": Constants.SPONSOR_T1
138+
}),
139+
invalid: createNAccounts(5, {
140+
"accountType": Constants.SPONSOR_T1
141+
})
142+
};
143+
144+
let sponsorT2Accounts = {
145+
new: createNAccounts(5, {
146+
"accountType": Constants.SPONSOR_T2
147+
}),
148+
stored: createNAccounts(5, {
149+
"accountType": Constants.SPONSOR_T2
150+
}),
151+
invalid: createNAccounts(5, {
152+
"accountType": Constants.SPONSOR_T2
153+
})
154+
};
155+
156+
let sponsorT3Accounts = {
157+
new: createNAccounts(5, {
158+
"accountType": Constants.SPONSOR_T3
159+
}),
160+
stored: createNAccounts(5, {
161+
"accountType": Constants.SPONSOR_T3
162+
}),
163+
invalid: createNAccounts(5, {
164+
"accountType": Constants.SPONSOR_T3
165+
})
166+
};
167+
168+
let sponsorT4Accounts = {
169+
new: createNAccounts(5, {
170+
"accountType": Constants.SPONSOR_T4
171+
}),
172+
stored: createNAccounts(5, {
173+
"accountType": Constants.SPONSOR_T4
174+
}),
175+
invalid: createNAccounts(5, {
176+
"accountType": Constants.SPONSOR_T4
177+
})
178+
};
179+
180+
let sponsorT5Accounts = {
181+
new: createNAccounts(5, {
182+
"accountType": Constants.SPONSOR_T5
183+
}),
184+
stored: createNAccounts(5, {
185+
"accountType": Constants.SPONSOR_T5
186+
}),
187+
invalid: createNAccounts(5, {
188+
"accountType": Constants.SPONSOR_T5
189+
})
190+
};
191+
192+
let unlinkedAccounts = {
193+
new: [createAccount({
194+
"accountType": Constants.HACKER
195+
})],
196+
invalid: [createAccount()]
197+
};
198+
199+
8200
const newAccount1 = {
9201
"_id": mongoose.Types.ObjectId(),
10202
"firstName": "NEW",
@@ -18,32 +210,7 @@ const newAccount1 = {
18210
"birthDate": "1997-12-30",
19211
"phoneNumber": 1234567890,
20212
};
21-
const nonAccount1 = {
22-
"_id": mongoose.Types.ObjectId(),
23-
"firstName": "non",
24-
"lastName": "Account",
25-
"pronoun": "She/Her",
26-
"email": "notexist@blahblah.com",
27-
"password": "12345789",
28-
"dietaryRestrictions": ["none"],
29-
"shirtSize": "S",
30-
"birthDate": "1990-01-01",
31-
"phoneNumber": 1000000001,
32-
};
33-
const Admin1 = {
34-
"_id": mongoose.Types.ObjectId(),
35-
"firstName": "Admin1",
36-
"lastName": "Admin1",
37-
"pronoun": "Ze/Hir",
38-
"email": "Admin1@blahblah.com",
39-
"password": "Admin1",
40-
"dietaryRestrictions": ["none"],
41-
"shirtSize": "S",
42-
"confirmed": true,
43-
"accountType": Constants.STAFF,
44-
"birthDate": "1990-01-02",
45-
"phoneNumber": 1000000002,
46-
};
213+
47214
// hacker
48215
const Account1 = {
49216
"_id": mongoose.Types.ObjectId(),
@@ -221,37 +388,20 @@ const Hacker7 = {
221388
"phoneNumber": 1000000004,
222389
};
223390

224-
const customAccounts = [
225-
Admin1,
226-
Account1,
227-
Account2,
228-
Account3,
229-
Account4,
230-
Account5,
231-
Hacker3,
232-
Hacker4,
233-
Hacker5,
234-
Hacker6,
235-
Hacker7,
236-
NonConfirmedAccount1,
237-
NonConfirmedAccount2
238-
];
239-
240-
const generatedAccounts = generateAccounts(20);
241-
// 1-5 Are for admins
242-
// 6-10 Are for hackers (6 and 7 are new)
243-
// 11-15 Are for sponsors
244-
// 16-20 Are for volunteers
245-
246-
247-
const allAccounts = customAccounts.concat(generatedAccounts);
248-
249391
module.exports = {
250-
nonAccount1: nonAccount1,
392+
hackerAccounts: hackerAccounts,
393+
volunteerAccounts: volunteerAccounts,
394+
staffAccounts: staffAccounts,
395+
sponsorT1Accounts: sponsorT1Accounts,
396+
sponsorT2Accounts: sponsorT2Accounts,
397+
sponsorT3Accounts: sponsorT3Accounts,
398+
sponsorT4Accounts: sponsorT4Accounts,
399+
sponsorT5Accounts: sponsorT5Accounts,
400+
401+
251402
newAccount1: newAccount1,
252403
NonConfirmedAccount1: NonConfirmedAccount1,
253404
NonConfirmedAccount2: NonConfirmedAccount2,
254-
Admin1: Admin1,
255405
Account1: Account1,
256406
Account2: Account2,
257407
Account3: Account3,
@@ -262,54 +412,12 @@ module.exports = {
262412
Hacker5: Hacker5,
263413
Hacker6: Hacker6,
264414
Hacker7: Hacker7,
265-
customAccounts: customAccounts,
266-
generatedAccounts: generatedAccounts,
267-
allAccounts: allAccounts,
415+
268416
storeAll: storeAll,
269417
dropAll: dropAll,
270418
equals: equals
271419
};
272420

273-
function generateRandomShirtSize() {
274-
return Constants.SHIRT_SIZES[Math.floor(Math.random() * Constants.SHIRT_SIZES.length)];
275-
}
276-
277-
function generateAccounts(n) {
278-
let accounts = [];
279-
for (let i = 0; i < n; i++) {
280-
let birthMonth = Math.floor(Math.random() * 12) + 1;
281-
let birthDay = Math.floor(Math.random() * 28) + 1;
282-
let phoneNumber = Math.floor(Math.random() * 10000000000);
283-
284-
let acc = {
285-
"_id": mongoose.Types.ObjectId(),
286-
"firstName": "first" + String(i),
287-
"lastName": "last" + String(i),
288-
"pronoun": "They/" + String(i),
289-
"email": "test" + String(i) + "@blahblah.com",
290-
"password": "probsShouldBeHashed" + String(i),
291-
"dietaryRestrictions": [],
292-
"shirtSize": generateRandomShirtSize(),
293-
"confirmed": true,
294-
"birthDate": `1980-${birthMonth}-${birthDay}`,
295-
"phoneNumber": phoneNumber,
296-
};
297-
298-
if (i < n / 4) {
299-
acc.accountType = Constants.STAFF;
300-
} else if (i >= n / 4 && i < (n / 4) * 2) {
301-
acc.accountType = Constants.HACKER;
302-
} else if (i >= (n / 4) * 2 && i < (n / 4) * 3) {
303-
acc.accountType = Constants.SPONSOR;
304-
} else {
305-
acc.accountType = Constants.VOLUNTEER;
306-
}
307-
308-
accounts.push(acc);
309-
}
310-
return accounts;
311-
}
312-
313421
function encryptPassword(user) {
314422
let encryptedUser = JSON.parse(JSON.stringify(user));
315423
encryptedUser.password = bcrypt.hashSync(user.password, 10);
@@ -340,6 +448,8 @@ async function dropAll() {
340448
}
341449
}
342450

451+
// Try deleting this and see if anything fucks up
452+
343453
/**
344454
* Compare two accounts
345455
* @param {Account} acc1

0 commit comments

Comments
 (0)