From 62ae5f7eb5cd17bda304a077bcce1b3b8c915478 Mon Sep 17 00:00:00 2001 From: Richard Zhang Date: Sun, 13 Jan 2019 02:09:50 -0500 Subject: [PATCH 01/11] Add sample diet restriction constants --- constants/general.constant.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/constants/general.constant.js b/constants/general.constant.js index 913a1a5b..c2294dfc 100644 --- a/constants/general.constant.js +++ b/constants/general.constant.js @@ -24,6 +24,23 @@ const HACKER_STATUSES = [ HACKER_STATUS_CHECKED_IN ]; +const SAMPLE_DIET_RESTRICTIONS = [ + "None", + "Vegan", + "Vegetarian", + "Keto", + "Gluten free", + "Pescetarian", + "Peanut allergy", + "Milk allergy", + "Egg allergy", + "Allergy", + "No beef", + "No porc", + "No fish", + "No shellfish" +]; + const HACKER = "Hacker"; const VOLUNTEER = "Volunteer"; const STAFF = "Staff"; @@ -123,5 +140,6 @@ module.exports = { POST_ROLES: POST_ROLES, CACHE_TIMEOUT_STATS: CACHE_TIMEOUT_STATS, CACHE_KEY_STATS: CACHE_KEY_STATS, - MAX_TEAM_SIZE: MAX_TEAM_SIZE + MAX_TEAM_SIZE: MAX_TEAM_SIZE, + SAMPLE_DIET_RESTRICTIONS, }; \ No newline at end of file From a63483a36aca764b6cec1de122ae68560c1f8467 Mon Sep 17 00:00:00 2001 From: Richard Zhang Date: Sun, 13 Jan 2019 02:10:23 -0500 Subject: [PATCH 02/11] Create new account generators and naming framework --- tests/util/account.test.util.js | 302 ++++++++++++++++++++++---------- 1 file changed, 206 insertions(+), 96 deletions(-) diff --git a/tests/util/account.test.util.js b/tests/util/account.test.util.js index d5628a57..e4ba8441 100644 --- a/tests/util/account.test.util.js +++ b/tests/util/account.test.util.js @@ -5,6 +5,198 @@ const mongoose = require("mongoose"); const bcrypt = require("bcrypt"); const logger = require("../../services/logger.service"); +let counters = { + emailCounter: 0, +}; + +function incrementCounters() { + for (const key in counters) { + if (counters.hasOwnProperty(key)) { + counters[key] = counters[key] + 1; + } + } +} + +function extractAccountInfo(acc) { + let accDetails = {}; + + for (const val in acc) { + // use .hasOwnProperty instead of 'in' to get rid of inherited properties such as 'should' + if (Account.schema.paths.hasOwnProperty(val)) { + accDetails[val] = acc[val]; + } + } + + return accDetails; +} + +function generateRandomValue(atr) { + switch (atr) { + case "_id": + return mongoose.Types.ObjectId(); + case "firstName": + // generates a random string between length 5 and 10 of random characters from a-z + return Math.random().toString(36).replace(/[^a-z]+/g, "").substr(0, Math.floor(Math.random() * 6 + 5)); + case "lastName": + return Math.random().toString(36).replace(/[^a-z]+/g, "").substr(0, Math.floor(Math.random() * 6 + 5)); + case "pronoun": + // generate random string between length 2 and 4 + return Math.random().toString(36).replace(/[^a-z]+/g, "").substr(0, Math.floor(Math.random() * 3 + 2)); + case "email": + const email = `abc.def${counters.emailCounter}@blahblah.com`; + return email; + case "password": + return Math.random().toString(36).substr(0, 10); + case "dietaryRestrictions": + return Constants.SAMPLE_DIET_RESTRICTIONS[Math.floor(Math.random() * Constants.SAMPLE_DIET_RESTRICTIONS.length)]; + case "shirtSize": + return Constants.SHIRT_SIZES[Math.floor(Math.random() * Constants.SHIRT_SIZES.length)]; + case "confirmed": + // return false, because if an account is confirmed there should be a document of that account type, + // which this does not create + return Math.random() < 0.5; + case "accountType": + return Constants.EXTENDED_USER_TYPES[Math.floor(Math.random() * Constants.EXTENDED_USER_TYPES.length)]; + case "birthDate": + return new Date(); + case "phoneNumber": + return Math.floor(Math.random() * 10000000000); + } +} + +function createAccount(acc = {}) { + incrementCounters(); + + const extractedAcc = extractAccountInfo(acc); + + for (const atr in Account.schema.paths) { + if (!Account.schema.paths.hasOwnProperty(atr)) { + continue; + } + + // if this value has been passed in, continue + if (extractedAcc[atr] !== undefined) { + continue; + } + + extractedAcc[atr] = generateRandomValue(atr); + } + + return extractedAcc; +} + +function createNAccounts(n, acc = {}) { + let accounts = [] + for (let i = 0; i < n; i++) { + accounts.push(createAccount(acc)); + } + + return accounts; +} + +let hackerAccounts = { + new: createNAccounts(10, { + "accountType": Constants.HACKER + }), + stored: { + team: createNAccounts(10, { + "accountType": Constants.HACKER + }), + noTeam: createNAccounts(10, { + "accountType": Constants.HACKER + }), + }, + invalid: createNAccounts(10, { + "accountType": Constants.HACKER + }) +}; + +let volunteerAccounts = { + new: createNAccounts(5, { + "accountType": Constants.VOLUNTEER + }), + stored: createNAccounts(5, { + "accountType": Constants.VOLUNTEER + }), + invalid: createNAccounts(5, { + "accountType": Constants.VOLUNTEER + }), +}; + +let staffAccounts = { + stored: createNAccounts(5, { + "accountType": Constants.STAFF + }) +}; + +let sponsorT1Accounts = { + new: createNAccounts(5, { + "accountType": Constants.SPONSOR_T1 + }), + stored: createNAccounts(5, { + "accountType": Constants.SPONSOR_T1 + }), + invalid: createNAccounts(5, { + "accountType": Constants.SPONSOR_T1 + }) +}; + +let sponsorT2Accounts = { + new: createNAccounts(5, { + "accountType": Constants.SPONSOR_T2 + }), + stored: createNAccounts(5, { + "accountType": Constants.SPONSOR_T2 + }), + invalid: createNAccounts(5, { + "accountType": Constants.SPONSOR_T2 + }) +}; + +let sponsorT3Accounts = { + new: createNAccounts(5, { + "accountType": Constants.SPONSOR_T3 + }), + stored: createNAccounts(5, { + "accountType": Constants.SPONSOR_T3 + }), + invalid: createNAccounts(5, { + "accountType": Constants.SPONSOR_T3 + }) +}; + +let sponsorT4Accounts = { + new: createNAccounts(5, { + "accountType": Constants.SPONSOR_T4 + }), + stored: createNAccounts(5, { + "accountType": Constants.SPONSOR_T4 + }), + invalid: createNAccounts(5, { + "accountType": Constants.SPONSOR_T4 + }) +}; + +let sponsorT5Accounts = { + new: createNAccounts(5, { + "accountType": Constants.SPONSOR_T5 + }), + stored: createNAccounts(5, { + "accountType": Constants.SPONSOR_T5 + }), + invalid: createNAccounts(5, { + "accountType": Constants.SPONSOR_T5 + }) +}; + +let unlinkedAccounts = { + new: [createAccount({ + "accountType": Constants.HACKER + })], + invalid: [createAccount()] +}; + + const newAccount1 = { "_id": mongoose.Types.ObjectId(), "firstName": "NEW", @@ -18,32 +210,7 @@ const newAccount1 = { "birthDate": "1997-12-30", "phoneNumber": 1234567890, }; -const nonAccount1 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "non", - "lastName": "Account", - "pronoun": "She/Her", - "email": "notexist@blahblah.com", - "password": "12345789", - "dietaryRestrictions": ["none"], - "shirtSize": "S", - "birthDate": "1990-01-01", - "phoneNumber": 1000000001, -}; -const Admin1 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "Admin1", - "lastName": "Admin1", - "pronoun": "Ze/Hir", - "email": "Admin1@blahblah.com", - "password": "Admin1", - "dietaryRestrictions": ["none"], - "shirtSize": "S", - "confirmed": true, - "accountType": Constants.STAFF, - "birthDate": "1990-01-02", - "phoneNumber": 1000000002, -}; + // hacker const Account1 = { "_id": mongoose.Types.ObjectId(), @@ -221,37 +388,20 @@ const Hacker7 = { "phoneNumber": 1000000004, }; -const customAccounts = [ - Admin1, - Account1, - Account2, - Account3, - Account4, - Account5, - Hacker3, - Hacker4, - Hacker5, - Hacker6, - Hacker7, - NonConfirmedAccount1, - NonConfirmedAccount2 -]; - -const generatedAccounts = generateAccounts(20); -// 1-5 Are for admins -// 6-10 Are for hackers (6 and 7 are new) -// 11-15 Are for sponsors -// 16-20 Are for volunteers - - -const allAccounts = customAccounts.concat(generatedAccounts); - module.exports = { - nonAccount1: nonAccount1, + hackerAccounts: hackerAccounts, + volunteerAccounts: volunteerAccounts, + staffAccounts: staffAccounts, + sponsorT1Accounts: sponsorT1Accounts, + sponsorT2Accounts: sponsorT2Accounts, + sponsorT3Accounts: sponsorT3Accounts, + sponsorT4Accounts: sponsorT4Accounts, + sponsorT5Accounts: sponsorT5Accounts, + + newAccount1: newAccount1, NonConfirmedAccount1: NonConfirmedAccount1, NonConfirmedAccount2: NonConfirmedAccount2, - Admin1: Admin1, Account1: Account1, Account2: Account2, Account3: Account3, @@ -262,54 +412,12 @@ module.exports = { Hacker5: Hacker5, Hacker6: Hacker6, Hacker7: Hacker7, - customAccounts: customAccounts, - generatedAccounts: generatedAccounts, - allAccounts: allAccounts, + storeAll: storeAll, dropAll: dropAll, equals: equals }; -function generateRandomShirtSize() { - return Constants.SHIRT_SIZES[Math.floor(Math.random() * Constants.SHIRT_SIZES.length)]; -} - -function generateAccounts(n) { - let accounts = []; - for (let i = 0; i < n; i++) { - let birthMonth = Math.floor(Math.random() * 12) + 1; - let birthDay = Math.floor(Math.random() * 28) + 1; - let phoneNumber = Math.floor(Math.random() * 10000000000); - - let acc = { - "_id": mongoose.Types.ObjectId(), - "firstName": "first" + String(i), - "lastName": "last" + String(i), - "pronoun": "They/" + String(i), - "email": "test" + String(i) + "@blahblah.com", - "password": "probsShouldBeHashed" + String(i), - "dietaryRestrictions": [], - "shirtSize": generateRandomShirtSize(), - "confirmed": true, - "birthDate": `1980-${birthMonth}-${birthDay}`, - "phoneNumber": phoneNumber, - }; - - if (i < n / 4) { - acc.accountType = Constants.STAFF; - } else if (i >= n / 4 && i < (n / 4) * 2) { - acc.accountType = Constants.HACKER; - } else if (i >= (n / 4) * 2 && i < (n / 4) * 3) { - acc.accountType = Constants.SPONSOR; - } else { - acc.accountType = Constants.VOLUNTEER; - } - - accounts.push(acc); - } - return accounts; -} - function encryptPassword(user) { let encryptedUser = JSON.parse(JSON.stringify(user)); encryptedUser.password = bcrypt.hashSync(user.password, 10); @@ -340,6 +448,8 @@ async function dropAll() { } } +// Try deleting this and see if anything fucks up + /** * Compare two accounts * @param {Account} acc1 From e06498dcd276b528f6aed3fe07ec1ba57f3a31c6 Mon Sep 17 00:00:00 2001 From: Richard Zhang Date: Sun, 13 Jan 2019 02:10:35 -0500 Subject: [PATCH 03/11] WIP converting accounts to new format --- tests/account.test.js | 79 +++++++++++++++------------ tests/auth.test.js | 4 +- tests/hacker.test.js | 42 +++++++------- tests/role.test.js | 9 ++- tests/search.service.spec.js | 23 ++++---- tests/sponsor.test.js | 17 +++--- tests/team.test.js | 17 +++--- tests/util/hacker.test.util.js | 7 +-- tests/util/resetPassword.test.util.js | 2 +- tests/util/roleBinding.test.util.js | 16 +++++- tests/volunteer.test.js | 17 +++--- 11 files changed, 129 insertions(+), 104 deletions(-) diff --git a/tests/account.test.js b/tests/account.test.js index 687265b6..5a120d8a 100644 --- a/tests/account.test.js +++ b/tests/account.test.js @@ -19,19 +19,26 @@ const util = { accountConfirmation: require("./util/accountConfirmation.test.util"), reset: require("./util/resetPassword.test.util") }; -// hacker role binding -const storedAccount1 = util.account.Account1; +const agent = chai.request.agent(server.app); +// tokens +const confirmationToken = util.accountConfirmation.ConfirmationToken; +const fakeToken = util.accountConfirmation.FakeToken; +const resetToken = util.reset.ResetToken; +// accounts +const Admin0 = util.account.staffAccounts[0]; +const teamHackerAccount0 = util.account.hackerAccounts.stored.team[0]; + + + + //This account has a confirmation token in the db const storedAccount2 = util.account.NonConfirmedAccount1; //This account does not have a confirmation token in the DB const storedAccount3 = util.account.NonConfirmedAccount2; // admin role binding -const Admin1 = util.account.Admin1; + const newAccount1 = util.account.newAccount1; -const agent = chai.request.agent(server.app); -const confirmationToken = util.accountConfirmation.ConfirmationToken; -const fakeToken = util.accountConfirmation.FakeToken; -const resetToken = util.reset.ResetToken; + describe("GET user account", function () { // fail on authentication @@ -50,7 +57,7 @@ describe("GET user account", function () { // fail due to invalid login it("should fail due to invalid password", function (done) { agent.post("/api/auth/login").type("application/json").send({ - email: Admin1.email, + email: Admin0.email, password: "FakePassword" }).end((err, res) => { res.should.have.status(401); @@ -62,7 +69,7 @@ describe("GET user account", function () { // success case it("should list the user's account on /api/account/self GET", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -92,13 +99,13 @@ describe("GET user account", function () { // success case - admin case it("should list another account specified by id using admin priviledge on /api/account/:id/ GET", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/account/${storedAccount1._id}`) + .get(`/api/account/${teamHackerAccount0._id}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -111,7 +118,7 @@ describe("GET user account", function () { res.body.should.have.property("data"); // use acc.toStrippedJSON to deal with hidden passwords and convert _id to id - const acc = new Account(storedAccount1); + const acc = new Account(teamHackerAccount0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(acc.toStrippedJSON())); done(); }); @@ -119,13 +126,13 @@ describe("GET user account", function () { }); // success case - user case it("should list an account specified by id on /api/account/:id/ GET", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/account/${storedAccount1._id}`) + .get(`/api/account/${teamHackerAccount0._id}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -138,7 +145,7 @@ describe("GET user account", function () { res.body.should.have.property("data"); // use acc.toStrippedJSON to deal with hidden passwords and convert _id to id - const acc = new Account(storedAccount1); + const acc = new Account(teamHackerAccount0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(acc.toStrippedJSON())); done(); }); @@ -147,13 +154,13 @@ describe("GET user account", function () { // // fail case on authorization it("should fail to list an account specified by id on /api/account/:id/ GET due to lack of authorization", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/account/${Admin1._id}`) + .get(`/api/account/${Admin0._id}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -176,7 +183,7 @@ describe("POST create account", function () { chai.request(server.app) .post(`/api/account/`) .type("application/json") - .send(newAccount1) + .send(teamHackerAccount0) .end(function (err, res) { res.should.have.status(200); res.should.be.json; @@ -184,7 +191,7 @@ describe("POST create account", function () { res.body.message.should.equal(Constants.Success.ACCOUNT_CREATE); // use acc.toStrippedJSON to deal with hidden passwords and convert _id to id - const acc = (new Account(newAccount1)).toStrippedJSON(); + const acc = (new Account(teamHackerAccount0)).toStrippedJSON(); // delete id as those are generated delete acc.id; delete res.body.data.id; @@ -197,7 +204,7 @@ describe("POST create account", function () { chai.request(server.app) .post(`/api/account/`) .type("application/json") - .send(storedAccount1) + .send(teamHackerAccount0) .end(function (err, res) { res.should.have.status(422); done(); @@ -243,13 +250,13 @@ describe("POST confirm account", function () { describe("PATCH update account", function () { const updatedInfo = { - "_id": storedAccount1._id, + "_id": teamHackerAccount0._id, "firstName": "new", "lastName": "name" }; const failUpdatedInfo = { - "_id": Admin1._id, + "_id": Admin0._id, "firstName": "fail", "lastName": "fail" }; @@ -269,7 +276,7 @@ describe("PATCH update account", function () { // succeed on :all case it("should SUCCEED and use admin to update another account", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -294,7 +301,7 @@ describe("PATCH update account", function () { // succeed on :self case it("should SUCCEED and update the user's own account", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -319,7 +326,7 @@ describe("PATCH update account", function () { // fail due to lack of authorization it("should Fail to update an account due to lack of authorization", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -362,7 +369,7 @@ describe("POST reset password", function () { describe("PATCH change password for logged in user", function () { const successChangePassword = { - "oldPassword": Admin1.password, + "oldPassword": Admin0.password, "newPassword": "password12345" }; const failChangePassword = { @@ -385,7 +392,7 @@ describe("PATCH change password for logged in user", function () { }); // success case it("should change the logged in user's password to a new password", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -405,7 +412,7 @@ describe("PATCH change password for logged in user", function () { }); // fail case because old password in incorrect it("should fail to change the logged in user's password to a new password because old password is incorrect", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -427,13 +434,13 @@ describe("PATCH change password for logged in user", function () { describe("GET retrieve permissions", function () { it("should SUCCEED and retrieve the rolebindings for the user", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } agent - .get("/api/auth/rolebindings/" + storedAccount1._id) + .get("/api/auth/rolebindings/" + teamHackerAccount0._id) .type("application/json") .end(function (err, res) { res.should.have.status(200); @@ -443,14 +450,14 @@ describe("GET retrieve permissions", function () { res.body.data.should.be.a("object"); res.body.data.should.have.property("roles"); res.body.data.should.have.property("accountId"); - res.body.data.accountId.should.equal(storedAccount1._id.toHexString()); + res.body.data.accountId.should.equal(teamHackerAccount0._id.toHexString()); done(); }); }); }); it("should FAIL to retrieve the rolebindings as the account is not authenticated", function (done) { chai.request(server.app) - .get("/api/auth/rolebindings/" + storedAccount1._id) + .get("/api/auth/rolebindings/" + teamHackerAccount0._id) .type("application/json") .end(function (err, res) { res.should.have.status(401); @@ -481,7 +488,7 @@ describe("GET resend confirmation email", function () { }); }); it("should FAIL as the account is already confirmed", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -520,7 +527,7 @@ describe("GET resend confirmation email", function () { describe("POST invite account", function () { it("Should succeed to invite a user to create an account", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -558,7 +565,7 @@ describe("GET invites", function () { }); }); it("Should FAIL to get all invites due to Authorization", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -574,7 +581,7 @@ describe("GET invites", function () { }); }); it("Should SUCCEED to get all invites", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); diff --git a/tests/auth.test.js b/tests/auth.test.js index d0a428bc..78b64ce0 100644 --- a/tests/auth.test.js +++ b/tests/auth.test.js @@ -24,11 +24,11 @@ const constants = { const roles = require("../constants/role.constant"); // hacker role binding -const storedAccount1 = util.account.Account1; +const teamHackerAccount0 = util.account.hackerAccounts.stored.team[0]; describe("GET roles", function () { it("should list all roles GET", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); diff --git a/tests/hacker.test.js b/tests/hacker.test.js index 0204e5f8..2d6b76a9 100644 --- a/tests/hacker.test.js +++ b/tests/hacker.test.js @@ -22,13 +22,15 @@ const util = { }; const StorageService = require("../services/storage.service"); -const Admin1 = util.account.Admin1; +const Admin0 = util.account.Admin0; +const teamHackerAccount0 = util.account.hackerAccounts.stored.team[0]; + + const Volunteer1 = util.account.Account4; // storedAccount1 and storedHacker1 are linked together, and have hacker priviledges // newHackerDuplicateAccountLink1 is also linked with Account1 // storedHacker1 has status confirmed -const storedAccount1 = util.account.Account1; const storedHacker1 = util.hacker.HackerA; const newHackerDuplicateAccountLink1 = util.hacker.duplicateAccountLinkHacker1; @@ -61,7 +63,7 @@ describe("GET hacker", function () { // success case it("should list the user's hacker info on /api/hacker/self GET", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -87,7 +89,7 @@ describe("GET hacker", function () { // fail case due to wrong account type it("should fail to list the hacker info of an admin due to wrong account type /api/account/self GET", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -106,7 +108,7 @@ describe("GET hacker", function () { // succeed on admin case it("should list a hacker's information using admin power on /api/hacker/:id GET", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -134,7 +136,7 @@ describe("GET hacker", function () { // succeed on :self case it("should list the user's hacker information on /api/hacker/:id GET", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -188,7 +190,7 @@ describe("GET hacker", function () { // fail due to lack of hacker it("should fail to list an invalid hacker /api/hacker/:id GET", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -231,7 +233,7 @@ describe("POST create hacker", function () { // succeed on admin case it("should SUCCEED and create a new hacker (with an account that has been confirmed) using admin credentials", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -318,7 +320,7 @@ describe("POST create hacker", function () { // fail on unconfirmed account, using admin it("should FAIL to create a new hacker if the account hasn't been confirmed", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -339,7 +341,7 @@ describe("POST create hacker", function () { // fail due to duplicate accountId it("should FAIL to create new hacker due to duplicate account link", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -360,7 +362,7 @@ describe("POST create hacker", function () { // fail on invalid input it("should FAIL to create new hacker due to invalid input", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -398,7 +400,7 @@ describe("PATCH update one hacker", function () { // should succeed on admin case it("should SUCCEED and update a hacker using admin power", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -424,7 +426,7 @@ describe("PATCH update one hacker", function () { }); it("should SUCCEED and update a hacker STATUS as an Admin", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -450,7 +452,7 @@ describe("PATCH update one hacker", function () { }); it("should FAIL and NOT update a hacker STATUS as a Hacker", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -501,7 +503,7 @@ describe("PATCH update one hacker", function () { // hacker should fail to checkin hacker it("should FAIL to check in hacker as a hacker", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -577,7 +579,7 @@ describe("PATCH update one hacker", function () { // fail due to lack of hacker it("should fail to change an invalid hacker's info", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -632,7 +634,7 @@ describe("PATCH update one hacker", function () { // Succeed and change confirmed to accepted it("should succeed for hacker to update their own status from confirmed to accepted", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -752,7 +754,7 @@ describe("POST add a hacker resume", function () { describe("GET Hacker stats", function () { it("It should FAIL and get hacker stats (invalid validation)", function (done) { //this takes a lot of time for some reason - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { return done(error); } @@ -768,7 +770,7 @@ describe("GET Hacker stats", function () { }); it("It should SUCCEED and get hacker stats", function (done) { //this takes a lot of time for some reason - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { return done(error); } @@ -804,7 +806,7 @@ describe("GET Hacker stats", function () { }); it("It should FAIL and get hacker stats due to invalid Authorization", function (done) { //this takes a lot of time for some reason - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { return done(error); } diff --git a/tests/role.test.js b/tests/role.test.js index dc811e59..34796c8e 100644 --- a/tests/role.test.js +++ b/tests/role.test.js @@ -18,6 +18,9 @@ const Constants = { Success: require("../constants/success.constant"), }; +const Admin0 = util.account.staffAccounts[0]; +const Hacker0 = util.account.hackerAccounts.stored.team[0]; + describe("POST create role", function () { it("should Fail to create a role because staff is not logged in", function (done) { chai.request(server.app) @@ -36,7 +39,7 @@ describe("POST create role", function () { // should succeed on logged in admin it("should SUCCEED and add new role", function (done) { - util.auth.login(agent, util.account.Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -74,7 +77,7 @@ describe("POST create role", function () { // should fail due to lack of authorization it("should Fail to add new role due to lack of authorization", function (done) { - util.auth.login(agent, util.account.Account1, (error) => { + util.auth.login(agent, Hacker0, (error) => { if (error) { agent.close(); return done(error); @@ -95,7 +98,7 @@ describe("POST create role", function () { // should succeed despite duplicate routes it("should Suceed to add new role despite to duplicate routes", function (done) { - util.auth.login(agent, util.account.Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); diff --git a/tests/search.service.spec.js b/tests/search.service.spec.js index d14785fd..b4f7a0f9 100644 --- a/tests/search.service.spec.js +++ b/tests/search.service.spec.js @@ -38,7 +38,8 @@ const badQuery = [{ value: "passowrd" }]; -const Admin1 = util.account.Admin1; +const Admin0 = util.account.staffAccounts[0]; + const HackerA = util.account.Account2; describe("Searching for hackers", function () { @@ -87,7 +88,7 @@ describe("Searching for hackers", function () { }); }); it("Should return all female hackers", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -107,7 +108,7 @@ describe("Searching for hackers", function () { }); }); it("Should return an error as hackers don't have password stored", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -126,7 +127,7 @@ describe("Searching for hackers", function () { }); it("Should return an error as staff aren't searchable", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -145,7 +146,7 @@ describe("Searching for hackers", function () { }); }); it("Should throw an error because model is not lowercase", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -164,7 +165,7 @@ describe("Searching for hackers", function () { }); }); it("Should throw an error because out of a fake model", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -183,7 +184,7 @@ describe("Searching for hackers", function () { }); }); it("Should only return 1 hacker (page size)", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -203,7 +204,7 @@ describe("Searching for hackers", function () { }); }); it("Should only return 1 hacker (pagination)", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -225,7 +226,7 @@ describe("Searching for hackers", function () { }); }); it("Should throw an error because out of bounds (page size)", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -244,7 +245,7 @@ describe("Searching for hackers", function () { }); }); it("Should throw an error because out of bounds (pagination)", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -265,7 +266,7 @@ describe("Searching for hackers", function () { }); it("Should expand the accountId when expand is set to true", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); diff --git a/tests/sponsor.test.js b/tests/sponsor.test.js index 3ab980f3..54fd3ee1 100644 --- a/tests/sponsor.test.js +++ b/tests/sponsor.test.js @@ -18,6 +18,11 @@ const util = { account: require("./util/account.test.util"), }; +const Admin0 = util.account.staffAccounts[0]; +const HackerAccount0 = util.account.hackerAccounts.stored.team[0]; + + + // associated account let storedAccount = util.account.Account3; // configure sponsor data to be the same as output from query @@ -29,13 +34,9 @@ delete storedSponsor._id; let duplicateAccount = util.account.Account3; let duplicateSponsor = util.sponsor.duplicateAccountLinkSponsor1; -let authorizationFailAccount = util.account.Account1; - const newSponsor = util.sponsor.newSponsor1; const newSponsorAccount = util.account.Account5; -const Admin1 = util.account.Admin1; -const hackerAccount1 = util.account.Account1; describe("GET user sponsor", function () { it("should fail list a sponsor's information due to authentication from /api/sponsor/:id GET", function (done) { @@ -54,7 +55,7 @@ describe("GET user sponsor", function () { // admin success it("should succeed to list a sponsor's info using admin power on /api/sponsor/:id GET", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -106,7 +107,7 @@ describe("GET user sponsor", function () { // failure due to lack of auth it("should fail to list a user's sponsor info due to lack of authorization /api/sponsor/:id GET", function (done) { - util.auth.login(agent, authorizationFailAccount, (error) => { + util.auth.login(agent, HackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -130,7 +131,7 @@ describe("GET user sponsor", function () { // failure due to lack of this sponsor it("should fail to list non existant info on /api/sponsor/:id GET", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -220,7 +221,7 @@ describe("POST create sponsor", function () { // unauthorized case it("should FAIL to create a new sponsor", function (done) { - util.auth.login(agent, hackerAccount1, (error) => { + util.auth.login(agent, HackerAccount0, (error) => { if (error) { agent.close(); return done(error); diff --git a/tests/team.test.js b/tests/team.test.js index eee33fda..66e6c025 100644 --- a/tests/team.test.js +++ b/tests/team.test.js @@ -20,6 +20,9 @@ const Constants = { const agent = chai.request.agent(server.app); +const Admin0 = util.account.staffAccounts[0]; +const Hacker0 = util.account.hackerAccounts.stored.team[0]; + describe("GET team", function () { it("should FAIL to list a team's information due to lack of authentication", function (done) { chai.request(server.app) @@ -127,7 +130,7 @@ describe("POST create team", function () { }); it("should FAIL to create a new team due to logged in user not being a hacker", function (done) { - util.auth.login(agent, util.account.Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -172,7 +175,7 @@ describe("POST create team", function () { }); it("should Fail to create a new team due to hacker already being in a team", function (done) { - util.auth.login(agent, util.account.Account1, (error) => { + util.auth.login(agent, Hacker0, (error) => { if (error) { agent.close(); return done(error); @@ -264,7 +267,7 @@ describe("PATCH join team", function () { }); it("should FAIL to join a hacker to a team that doesn't exist.", function (done) { - util.auth.login(agent, util.account.Account1, (error) => { + util.auth.login(agent, Hacker0, (error) => { if (error) { agent.close(); return done(error); @@ -288,7 +291,7 @@ describe("PATCH join team", function () { }); it("should FAIL to join a hacker to a team that is full.", function (done) { - util.auth.login(agent, util.account.Account1, (error) => { + util.auth.login(agent, Hacker0, (error) => { if (error) { agent.close(); return done(error); @@ -336,7 +339,7 @@ describe("PATCH join team", function () { }); it("should SUCCEED and join a hacker on a team to aother team.", function (done) { - util.auth.login(agent, util.account.Account1, (error) => { + util.auth.login(agent, Hacker0, (error) => { if (error) { agent.close(); return done(error); @@ -429,7 +432,7 @@ describe("PATCH change team info", function () { }); it("should SUCCEED and leave a team.", function (done) { - util.auth.login(agent, util.account.Account1, (error) => { + util.auth.login(agent, Hacker0, (error) => { if (error) { agent.close(); return done(error); @@ -449,7 +452,7 @@ describe("PATCH change team info", function () { }); it("should SUCCEED for an admin to change a team information", function (done) { - util.auth.login(agent, util.account.Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); diff --git a/tests/util/hacker.test.util.js b/tests/util/hacker.test.util.js index 2ea1b3d1..300c285a 100644 --- a/tests/util/hacker.test.util.js +++ b/tests/util/hacker.test.util.js @@ -58,7 +58,7 @@ const badCodeOfConductHacker1 = { const duplicateAccountLinkHacker1 = { "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.Account1._id, + "accountId": Util.Account.hackerAccounts.stored.team[0], "status": "Applied", "school": "University of Blah", "degree": "Undergraduate", @@ -135,7 +135,7 @@ const newHacker2 = { const HackerA = { "_id": Constants.MongoId.hackerAId, - "accountId": Util.Account.Account1._id, + "accountId": Util.Account.hackerAccounts.stored.team[0], "status": "Confirmed", "school": "University of Blah", "degree": "Masters", @@ -353,9 +353,6 @@ module.exports = { Hackers: Hackers, storeAll: storeAll, dropAll: dropAll -}; - -function storeAll(attributes) { const hackerDocs = []; const hackerIds = []; for (var i = 0; i < attributes.length; i++) { diff --git a/tests/util/resetPassword.test.util.js b/tests/util/resetPassword.test.util.js index 567a0e89..8a1bc1a4 100644 --- a/tests/util/resetPassword.test.util.js +++ b/tests/util/resetPassword.test.util.js @@ -14,7 +14,7 @@ const logger = require("../../services/logger.service"); const ResetPasswordToken1 = { "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.Account1._id + "accountId": Util.Account.hackerAccounts.stored.team[0] }; const ResetToken = Services.resetPassword.generateToken(ResetPasswordToken1._id, ResetPasswordToken1.accountId); diff --git a/tests/util/roleBinding.test.util.js b/tests/util/roleBinding.test.util.js index d43a3a7a..15f1ca82 100644 --- a/tests/util/roleBinding.test.util.js +++ b/tests/util/roleBinding.test.util.js @@ -76,12 +76,22 @@ const RoleBindingNewVolunteer1 = { roles: [Constants.Role.accountRole._id, Constants.Role.allRolesObject["postVolunteer"]], }; -const RoleBindingAdmin1 = { - accountId: Util.Account.Admin1._id, +const RoleBindingAdmin0 = { + accountId: Util.Account.staffAccounts[0], roles: [Constants.Role.adminRole._id], }; +// function setUpAccountRoleBinding???(???) { +// /* +// TODO +// */ +// } + + const RoleBindings = [ + RoleBindingAdmin0, + + RoleBinding1, RoleBinding2, RoleBinding3, @@ -96,7 +106,7 @@ const RoleBindings = [ RoleBindingSponsor2, RoleBindingVolunteer1, RoleBindingNewVolunteer1, - RoleBindingAdmin1, + RoleBindingHacker3, ]; diff --git a/tests/volunteer.test.js b/tests/volunteer.test.js index dcb2a39f..779a4a66 100644 --- a/tests/volunteer.test.js +++ b/tests/volunteer.test.js @@ -17,15 +17,16 @@ const util = { auth: require("./util/auth.test.util") }; -const Admin1 = util.account.Admin1; +const Admin0 = util.account.Admin0; +const Hacker0 = util.account.hackerAccounts.stored.team[0]; + + const adminVolunteer = util.volunteer.adminVolunteer1; const newVolunteerAccount = util.account.generatedAccounts[15]; const newVolunteer = util.volunteer.newVolunteer1; const duplicateVolunteer = util.volunteer.duplicateVolunteer1; -const hackerAccount = util.account.Account1; - describe("GET volunteer", function () { it("should FAIL to get volunteer due to lack of authentication", function (done) { chai.request(server.app) @@ -62,13 +63,13 @@ describe("GET volunteer", function () { }); it("should Fail to GET volunteer due to non existant volunteer id", function (done) { - util.auth.login(agent, util.account.Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/volunteer/${util.account.Admin1._id}`) + .get(`/api/volunteer/${Admin0._id}`) .end(function (err, res) { res.should.have.status(404); res.should.be.json; @@ -83,7 +84,7 @@ describe("GET volunteer", function () { // success case it("should GET volunteer info by id with admin credentials", function (done) { - util.auth.login(agent, util.account.Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -153,7 +154,7 @@ describe("POST create volunteer", function () { // fail on admin case it("fail to create a volunteer when the logged in account is not a volunteer /api/volunteer POST", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -225,7 +226,7 @@ describe("POST create volunteer", function () { // fail on non-volunteer type account trying to create volunteer it("should fail to create a volunteer due to authorization /api/volunteer POST", function (done) { - util.auth.login(agent, hackerAccount, (error) => { + util.auth.login(agent, Hacker0, (error) => { if (error) { agent.close(); return done(error); From 9537c869f1279f2f3a680fff9d01ddca4418def7 Mon Sep 17 00:00:00 2001 From: Richard Zhang Date: Mon, 14 Jan 2019 14:09:15 -0500 Subject: [PATCH 04/11] WIP account test refactor --- tests/account.test.js | 4 +- tests/hacker.test.js | 24 +- tests/search.service.spec.js | 4 +- tests/sponsor.test.js | 31 +- tests/team.test.js | 34 +- tests/util/account.test.util.js | 177 +---------- tests/util/accountConfirmation.test.util.js | 8 +- tests/util/hacker.test.util.js | 329 ++++++++++---------- tests/util/roleBinding.test.util.js | 73 ----- tests/util/sponsor.test.util.js | 37 ++- tests/util/staff.test.util.js | 6 +- tests/util/volunteer.test.util.js | 8 +- tests/volunteer.test.js | 19 +- 13 files changed, 271 insertions(+), 483 deletions(-) diff --git a/tests/account.test.js b/tests/account.test.js index 5a120d8a..44a30f8e 100644 --- a/tests/account.test.js +++ b/tests/account.test.js @@ -37,7 +37,7 @@ const storedAccount2 = util.account.NonConfirmedAccount1; const storedAccount3 = util.account.NonConfirmedAccount2; // admin role binding -const newAccount1 = util.account.newAccount1; +const newAccount0 = util.account.unlinkedAccounts.new[0]; describe("GET user account", function () { @@ -536,7 +536,7 @@ describe("POST invite account", function () { .post("/api/account/invite") .type("application/json") .send({ - email: newAccount1.email, + email: newAccount0.email, accountType: Constants.General.VOLUNTEER }) // does not have password because of to stripped json diff --git a/tests/hacker.test.js b/tests/hacker.test.js index 2d6b76a9..6463058d 100644 --- a/tests/hacker.test.js +++ b/tests/hacker.test.js @@ -22,11 +22,11 @@ const util = { }; const StorageService = require("../services/storage.service"); -const Admin0 = util.account.Admin0; +const Admin0 = util.account.staffAccounts.stored[0]; const teamHackerAccount0 = util.account.hackerAccounts.stored.team[0]; - - -const Volunteer1 = util.account.Account4; +// linked with hackerB - fix later +const noTeamHackerAccount0 = util.account.hackerAccounts.stored.noTeam[0]; +const volunteerAccount0 = util.account.volunteerAccounts.stored[0]; // storedAccount1 and storedHacker1 are linked together, and have hacker priviledges // newHackerDuplicateAccountLink1 is also linked with Account1 @@ -34,8 +34,6 @@ const Volunteer1 = util.account.Account4; const storedHacker1 = util.hacker.HackerA; const newHackerDuplicateAccountLink1 = util.hacker.duplicateAccountLinkHacker1; -// storedAccount2 and storedHacker2 are linked together, and have hacker priviledges -const storedAccount2 = util.account.Account2; const storedHacker2 = util.hacker.HackerB; const newHacker1 = util.hacker.newHacker1; @@ -165,7 +163,7 @@ describe("GET hacker", function () { // fail due to lack of authorization it("should fail to list a hacker information due to lack of authorization on /api/hacker/:id GET", function (done) { - util.auth.login(agent, storedAccount2, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -476,7 +474,7 @@ describe("PATCH update one hacker", function () { // volunteer should successfully checkin hacker it("should SUCCEED and check in hacker as a volunteer", function (done) { - util.auth.login(agent, Volunteer1, (error) => { + util.auth.login(agent, volunteerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -527,7 +525,7 @@ describe("PATCH update one hacker", function () { // should succeed on hacker case it("should SUCCEED and update the user's hacker info", function (done) { - util.auth.login(agent, storedAccount2, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -554,7 +552,7 @@ describe("PATCH update one hacker", function () { // should fail due to authorization it("should Fail to update hacker info due to lack of authorization", function (done) { - util.auth.login(agent, storedAccount2, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -603,7 +601,7 @@ describe("PATCH update one hacker", function () { // Succeed and change accepted to confirm it("should succeed for hacker to update their own status from accepted to confirmed", function (done) { - util.auth.login(agent, storedAccount2, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -665,7 +663,7 @@ describe("PATCH update one hacker", function () { // fail for a hacker that's not accepted it("should fail to update hacker status when hacker status is not accepted or confirmed", function (done) { - util.auth.login(agent, util.account.Hacker3, (error) => { + util.auth.login(agent, util.account.waitlistedHacker0, (error) => { if (error) { agent.close(); return done(error); @@ -693,7 +691,7 @@ describe("PATCH update one hacker", function () { // fail for a hacker that's not accepted it("should fail for hacker trying to confirm someone else", function (done) { - util.auth.login(agent, util.account.Hacker3, (error) => { + util.auth.login(agent, util.account.waitlistedHacker0, (error) => { if (error) { agent.close(); return done(error); diff --git a/tests/search.service.spec.js b/tests/search.service.spec.js index b4f7a0f9..356a0b32 100644 --- a/tests/search.service.spec.js +++ b/tests/search.service.spec.js @@ -40,7 +40,7 @@ const badQuery = [{ const Admin0 = util.account.staffAccounts[0]; -const HackerA = util.account.Account2; +const noTeamHackerAccount0 = util.account.hackerAccounts.stored.noTeam[0]; describe("Searching for hackers", function () { it("Should FAIL to search due to invalid authentication", function (done) { @@ -68,7 +68,7 @@ describe("Searching for hackers", function () { }); it("Should FAIL to search due to invalid authorization", function (done) { - util.auth.login(agent, HackerA, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); diff --git a/tests/sponsor.test.js b/tests/sponsor.test.js index 54fd3ee1..cc4a2e54 100644 --- a/tests/sponsor.test.js +++ b/tests/sponsor.test.js @@ -20,28 +20,19 @@ const util = { const Admin0 = util.account.staffAccounts[0]; const HackerAccount0 = util.account.hackerAccounts.stored.team[0]; +const T1SponsorAccount0 = util.account.sponsorT1Accounts.stored[0]; +const newT2SponsorAccount0 = util.account.sponsorT2Accounts.new[0]; +const T1Sponsor0 = util.sponsor.T1Sponsor0; +const newT2Sponsor0 = util.sponsor.newT2Sponsor0; - -// associated account -let storedAccount = util.account.Account3; -// configure sponsor data to be the same as output from query -// stringify and parse for deep copy -let storedSponsor = JSON.parse(JSON.stringify(util.sponsor.Sponsor1)); -storedSponsor.id = storedSponsor._id; -delete storedSponsor._id; - -let duplicateAccount = util.account.Account3; let duplicateSponsor = util.sponsor.duplicateAccountLinkSponsor1; -const newSponsor = util.sponsor.newSponsor1; -const newSponsorAccount = util.account.Account5; - describe("GET user sponsor", function () { it("should fail list a sponsor's information due to authentication from /api/sponsor/:id GET", function (done) { chai.request(server.app) - .get(`/api/sponsor/` + util.sponsor.Sponsor1._id) + .get(`/api/sponsor/` + T1Sponsor0._id) // does not have password because of to stripped json .end(function (err, res) { res.should.have.status(401); @@ -61,7 +52,7 @@ describe("GET user sponsor", function () { return done(error); } return agent - .get(`/api/sponsor/${util.sponsor.Sponsor1._id}`) + .get(`/api/sponsor/${T1Sponsor0._id}`) .end(function (err, res) { if (err) { return done(err); @@ -73,7 +64,7 @@ describe("GET user sponsor", function () { res.body.should.have.property("data"); res.body.data.should.be.a("object"); - chai.expect(res.body.data).to.deep.equal(storedSponsor); + chai.expect(res.body.data).to.deep.equal(sponsorT1Account0); done(); }); }); @@ -81,13 +72,13 @@ describe("GET user sponsor", function () { // regular user access success it("should succeed to list a user's sponsor info on /api/sponsor/:id GET", function (done) { - util.auth.login(agent, storedAccount, (error) => { + util.auth.login(agent, sponsorT1Account0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/sponsor/${util.sponsor.Sponsor1._id}`) + .get(`/api/sponsor/${util.sponsor.T1Sponsor0._id}`) .end(function (err, res) { if (err) { return done(err); @@ -99,7 +90,7 @@ describe("GET user sponsor", function () { res.body.should.have.property("data"); res.body.data.should.be.a("object"); - chai.expect(res.body.data).to.deep.equal(storedSponsor); + chai.expect(res.body.data).to.deep.equal(T1SponsorAccount0); done(); }); }); @@ -200,7 +191,7 @@ describe("POST create sponsor", function () { // fail case - duplicate accountId it("should fail to create a sponsor due to duplicate accountId", function (done) { - util.auth.login(agent, duplicateAccount, (error) => { + util.auth.login(agent, sponsorT1Account0, (error) => { if (error) { agent.close(); return done(error); diff --git a/tests/team.test.js b/tests/team.test.js index 66e6c025..95462362 100644 --- a/tests/team.test.js +++ b/tests/team.test.js @@ -21,7 +21,9 @@ const Constants = { const agent = chai.request.agent(server.app); const Admin0 = util.account.staffAccounts[0]; -const Hacker0 = util.account.hackerAccounts.stored.team[0]; +const teamHackerAccount0 = util.account.hackerAccounts.stored.team[0]; +const noTeamHackerAccount0 = util.account.hackerAccounts.stored.noTeam[0]; +const sponsorT1Account0 = util.account.sponsorT1Accounts.stored[0]; describe("GET team", function () { it("should FAIL to list a team's information due to lack of authentication", function (done) { @@ -39,7 +41,7 @@ describe("GET team", function () { }); it("should Fail and list a team's information from /api/team/ GET due to non existant team id", function (done) { - util.auth.login(agent, util.account.Hacker4, (error) => { + util.auth.login(agent, util.account.hackerAccounts.stored.team[0], (error) => { if (error) { agent.close(); return done(error); @@ -59,7 +61,7 @@ describe("GET team", function () { }); it("should SUCCEED and list a team's information from /api/team/ GET", function (done) { - util.auth.login(agent, util.account.Hacker3, (error) => { + util.auth.login(agent, util.account.waitlistedHacker0, (error) => { if (error) { agent.close(); return done(error); @@ -108,7 +110,7 @@ describe("POST create team", function () { }); it("should FAIL to create a new team due to lack of authorization", function (done) { - util.auth.login(agent, util.account.Account3, (error) => { + util.auth.login(agent, sponsorT1Account0, (error) => { if (error) { agent.close(); return done(error); @@ -152,7 +154,7 @@ describe("POST create team", function () { }); it("should FAIL to create a new team due to duplicate team name", function (done) { - util.auth.login(agent, util.account.Account2, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -175,7 +177,7 @@ describe("POST create team", function () { }); it("should Fail to create a new team due to hacker already being in a team", function (done) { - util.auth.login(agent, Hacker0, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -196,7 +198,7 @@ describe("POST create team", function () { }); it("should SUCCEED and create a new team", function (done) { - util.auth.login(agent, util.account.Account2, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -243,7 +245,7 @@ describe("PATCH join team", function () { }); it("should FAIL to join a volunteer to a team.", function (done) { - util.auth.login(agent, util.account.Account5, (error) => { + util.auth.login(agent, util.account.volunteerAccounts.stored[0], (error) => { if (error) { agent.close(); return done(error); @@ -267,7 +269,7 @@ describe("PATCH join team", function () { }); it("should FAIL to join a hacker to a team that doesn't exist.", function (done) { - util.auth.login(agent, Hacker0, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -291,7 +293,7 @@ describe("PATCH join team", function () { }); it("should FAIL to join a hacker to a team that is full.", function (done) { - util.auth.login(agent, Hacker0, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -315,7 +317,7 @@ describe("PATCH join team", function () { }); it("should SUCCEED and join a hacker without a team to a team.", function (done) { - util.auth.login(agent, util.account.Account2, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -339,7 +341,7 @@ describe("PATCH join team", function () { }); it("should SUCCEED and join a hacker on a team to aother team.", function (done) { - util.auth.login(agent, Hacker0, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -383,7 +385,7 @@ describe("PATCH change team info", function () { }); it("should FAIL for a hacker to change another team's information due to invalid authorization", function (done) { - util.auth.login(agent, util.account.Hacker4, (error) => { + util.auth.login(agent, util.account.hackerAccounts.stored.team[1], (error) => { if (error) { agent.close(); return done(error); @@ -406,8 +408,8 @@ describe("PATCH change team info", function () { }); }); - it("should SUCCEED to change the hacker's team information", function (done) { - util.auth.login(agent, util.account.Hacker4, (error) => { + it("should SUCCEED to change the hacker's team information (even though the hackers are different, the team is the same)", function (done) { + util.auth.login(agent, util.account.hackerAccounts.stored.team[1], (error) => { if (error) { agent.close(); return done(error); @@ -432,7 +434,7 @@ describe("PATCH change team info", function () { }); it("should SUCCEED and leave a team.", function (done) { - util.auth.login(agent, Hacker0, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); diff --git a/tests/util/account.test.util.js b/tests/util/account.test.util.js index e4ba8441..72ed016c 100644 --- a/tests/util/account.test.util.js +++ b/tests/util/account.test.util.js @@ -193,46 +193,21 @@ let unlinkedAccounts = { new: [createAccount({ "accountType": Constants.HACKER })], - invalid: [createAccount()] + invalid: [createAccount()], + stored: [createAccount({ + "accountType": Constants.HACKER + }), createAccount({ + "accountType": Constants.HACKER + })] }; -const newAccount1 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "NEW", - "lastName": "Account", - "pronoun": "He/Him", - "email": "newexist@blahblah.com", - "password": "1234567890", - "dietaryRestrictions": ["none"], - "shirtSize": "S", - "accountType": Constants.HACKER, - "birthDate": "1997-12-30", - "phoneNumber": 1234567890, -}; - -// hacker -const Account1 = { +const waitlistedHacker0 = { "_id": mongoose.Types.ObjectId(), - "firstName": "ABC", - "lastName": "DEF", - "pronoun": "Ze/Zir", - "email": "abc.def1@blahblah.com", - "password": "probsShouldBeHashed1", - "dietaryRestrictions": ["none"], - "shirtSize": "S", - "confirmed": true, - "accountType": Constants.HACKER, - "birthDate": "1990-01-03", - "phoneNumber": 1000000003, -}; -// hacker -const Account2 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "abc", - "lastName": "def", + "firstName": "abcd", + "lastName": "defg3", "pronoun": "They/Them", - "email": "abc.def2@blahblah.com", + "email": "abc.def7@blahblah.com", "password": "probsShouldBeHashed2", "dietaryRestrictions": ["vegetarian"], "shirtSize": "M", @@ -241,51 +216,6 @@ const Account2 = { "birthDate": "1990-01-04", "phoneNumber": 1000000004, }; -// sponsor -const Account3 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "XYZ", - "lastName": "UST", - "pronoun": "Xey/Xem", - "email": "abc.def3@blahblah.com", - "password": "probsShouldBeHashed3", - "dietaryRestrictions": ["vegan"], - "shirtSize": "L", - "confirmed": true, - "birthDate": "1990-01-05", - "phoneNumber": 1000000005, - "accountType": Constants.SPONSOR_T1 -}; -// volunteer -const Account4 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "xyz", - "lastName": "ust", - "pronoun": "Sie/Hir", - "email": "abc.def4@blahblah.com", - "password": "probsShouldBeHashed4", - "dietaryRestrictions": ["vegetarian", "lactose intolerant"], - "shirtSize": "XL", - "confirmed": true, - "accountType": Constants.VOLUNTEER, - "birthDate": "1980-01-30", - "phoneNumber": 1000000006, -}; -// sponsor -const Account5 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "LMAO", - "lastName": "ROFL", - "pronoun": "It/It", - "email": "abc.def0@blahblah.com", - "password": "probsShouldBeHashed5", - "dietaryRestrictions": ["something1", "something2"], - "shirtSize": "XXL", - "confirmed": true, - "accountType": Constants.SPONSOR_T2, - "birthDate": "1980-06-30", - "phoneNumber": 1000000236 -}; // non confirmed account for hacker const NonConfirmedAccount1 = { @@ -315,79 +245,6 @@ const NonConfirmedAccount2 = { "accountType": Constants.HACKER, }; -// hacker waitlisted -const Hacker3 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "abcd", - "lastName": "defg3", - "pronoun": "They/Them", - "email": "abc.def7@blahblah.com", - "password": "probsShouldBeHashed2", - "dietaryRestrictions": ["vegetarian"], - "shirtSize": "M", - "confirmed": true, - "accountType": Constants.HACKER, - "birthDate": "1990-01-04", - "phoneNumber": 1000000004, -}; - -const Hacker4 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "abcd", - "lastName": "defg4", - "pronoun": "They/Them", - "email": "abc.def.hacker4@blahblah.com", - "password": "probsShouldBeHashed2", - "dietaryRestrictions": ["vegetarian"], - "shirtSize": "M", - "confirmed": true, - "accountType": Constants.HACKER, - "birthDate": "1990-01-04", - "phoneNumber": 1000000004, -}; -const Hacker5 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "abcd", - "lastName": "defg5", - "pronoun": "They/Them", - "email": "abc.def.hacker5@blahblah.com", - "password": "probsShouldBeHashed2", - "dietaryRestrictions": ["vegetarian"], - "shirtSize": "M", - "confirmed": true, - "accountType": Constants.HACKER, - "birthDate": "1990-01-04", - "phoneNumber": 1000000004, -}; -const Hacker6 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "abcd", - "lastName": "defg6", - "pronoun": "They/Them", - "email": "abc.def.hacker6@blahblah.com", - "password": "probsShouldBeHashed2", - "dietaryRestrictions": ["vegetarian"], - "shirtSize": "M", - "confirmed": true, - "accountType": Constants.HACKER, - "birthDate": "1990-01-04", - "phoneNumber": 1000000004, -}; -const Hacker7 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "abcd", - "lastName": "defg7", - "pronoun": "They/Them", - "email": "abc.def.hacker7@blahblah.com", - "password": "probsShouldBeHashed2", - "dietaryRestrictions": ["vegetarian"], - "shirtSize": "M", - "confirmed": true, - "accountType": Constants.HACKER, - "birthDate": "1990-01-04", - "phoneNumber": 1000000004, -}; - module.exports = { hackerAccounts: hackerAccounts, volunteerAccounts: volunteerAccounts, @@ -397,21 +254,11 @@ module.exports = { sponsorT3Accounts: sponsorT3Accounts, sponsorT4Accounts: sponsorT4Accounts, sponsorT5Accounts: sponsorT5Accounts, + unlinkedAccounts: unlinkedAccounts, - - newAccount1: newAccount1, + waitlistedHacker0: waitlistedHacker0, NonConfirmedAccount1: NonConfirmedAccount1, NonConfirmedAccount2: NonConfirmedAccount2, - Account1: Account1, - Account2: Account2, - Account3: Account3, - Account4: Account4, - Account5: Account5, - Hacker3: Hacker3, - Hacker4: Hacker4, - Hacker5: Hacker5, - Hacker6: Hacker6, - Hacker7: Hacker7, storeAll: storeAll, dropAll: dropAll, diff --git a/tests/util/accountConfirmation.test.util.js b/tests/util/accountConfirmation.test.util.js index 949add4c..de005027 100644 --- a/tests/util/accountConfirmation.test.util.js +++ b/tests/util/accountConfirmation.test.util.js @@ -16,9 +16,9 @@ const logger = require("../../services/logger.service"); const HackerConfirmation = { "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.Account2._id, + "accountId": Util.Account.hackerAccounts.stored.noTeam[0], "accountType": Constants.HACKER, - "email": Util.Account.Account2.email + "email": Util.Account.hackerAccounts.stored.noTeam[0].email }; const HackerConfirmation2 = { @@ -30,7 +30,7 @@ const HackerConfirmation2 = { const HackerConfirmation3 = { "_id": mongoose.Types.ObjectId(), - "email": Util.Account.newAccount1.email + "email": Util.Account.unlinkedAccounts.new[0].email }; const HackerConfirmation4 = { @@ -42,7 +42,7 @@ const HackerConfirmation4 = { // Using a real ID which is stored but corresponds to another account const FakeHackerToken = { "_id": HackerConfirmation._id, - "accountId": Util.Account.Account3._id, + "accountId": Util.Account.sponsorT1Accounts.stored[0]._id, "accountType": Constants.HACKER }; diff --git a/tests/util/hacker.test.util.js b/tests/util/hacker.test.util.js index 300c285a..b90e83cf 100644 --- a/tests/util/hacker.test.util.js +++ b/tests/util/hacker.test.util.js @@ -10,32 +10,13 @@ const mongoose = require("mongoose"); const Hacker = require("../../models/hacker.model"); const logger = require("../../services/logger.service"); -const invalidHacker1 = { - "_id": mongoose.Types.ObjectId(), - // invalid mongoID - "accountId": "UtilAccountAccount1_id", - // invalid missing school attribute - "degree": "Undersaduate", - "gender": "Female", - "needsBus": true, - "application": { - // invalid portflio with no resume - "portfolioURL": {}, - // invalid jobInterest - "jobInterest": "ASDF", - }, - "ethnicity": ["Asian", "Caucasian"], - "major": "CS", - "graduationYear": 2020, - "codeOfConduct": true, -}; - -// duplicate of newHack1, but with false for code of conduct -const badCodeOfConductHacker1 = { - "accountId": Util.Account.generatedAccounts[6]._id, - "school": "University of ASDF", +const TeamHacker0 = { + "_id": Constants.MongoId.hackerAId, + "accountId": Util.Account.hackerAccounts.stored.team[0], + "status": "Confirmed", + "school": "University of Blah", "degree": "Masters", - "gender": "Female", + "gender": "Male", "needsBus": true, "application": { "portfolioURL": { @@ -50,119 +31,128 @@ const badCodeOfConductHacker1 = { "jobInterest": "Full-time", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": ["Caucasian"], + "ethnicity": ["Native American"], "major": "EE", "graduationYear": 2019, - "codeOfConduct": false, + "codeOfConduct": true, + "teamId": Constants.MongoId.team1Id, }; -const duplicateAccountLinkHacker1 = { - "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.hackerAccounts.stored.team[0], - "status": "Applied", - "school": "University of Blah", - "degree": "Undergraduate", - "gender": "Male", - "needsBus": true, +const TeamHacker1 = { + "_id": Constants.MongoId.hackerDId, + "accountId": Util.Account.hackerAccounts.stored.team[1]._id, + "status": "Waitlisted", + "school": "University of Blah1", + "degree": "Masters", + "gender": "Female", + "needsBus": false, "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume100", - "github": "www.github.com/Person1", + "resume": "www.gcloud.com/myResume2", + "github": "www.github.com/Personasdf", "dropler": undefined, - "personal": "www.person1.com", - "linkedIn": "www.linkedin.com/in/Person1", + "personal": undefined, + "linkedIn": undefined, "other": undefined }, - "jobInterest": "Full-time", + "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": ["Caucasian"], - "major": "CS", + "ethnicity": "European", + "major": "EE", "graduationYear": 2019, "codeOfConduct": true, + "teamId": Constants.MongoId.team3Id, }; -const newHacker1 = { - "accountId": Util.Account.generatedAccounts[6]._id, - "school": "University of ASDF", +const TeamHacker2 = { + "_id": Constants.MongoId.hackerEId, + "accountId": Util.Account.hackerAccounts.stored.team[2]._id, + "status": "Waitlisted", + "school": "University of Blah1", "degree": "Masters", "gender": "Female", - "needsBus": true, + "needsBus": false, "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume100", - "github": "www.github.com/Person1", + "resume": "www.gcloud.com/myResume2", + "github": "www.github.com/Personasdf", "dropler": undefined, - "personal": "www.person1.com", - "linkedIn": "www.linkedin.com/in/Person1", + "personal": undefined, + "linkedIn": undefined, "other": undefined }, - "jobInterest": "Full-time", + "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": ["Caucasian"], + "ethnicity": "European", "major": "EE", "graduationYear": 2019, "codeOfConduct": true, + "teamId": Constants.MongoId.team3Id, }; -const newHacker2 = { - "accountId": Util.Account.NonConfirmedAccount1._id, - "school": "University of YIKES", - "degree": "PhD", +const TeamHacker3 = { + "_id": Constants.MongoId.hackerFId, + "accountId": Util.Account.hackerAccounts.stored.team[3]._id, + "status": "Waitlisted", + "school": "University of Blah1", + "degree": "Masters", "gender": "Female", - "needsBus": true, + "needsBus": false, "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume100", - "github": "www.github.com/Person1", + "resume": "www.gcloud.com/myResume2", + "github": "www.github.com/Personasdf", "dropler": undefined, - "personal": "www.person1.com", - "linkedIn": "www.linkedin.com/in/Person1", + "personal": undefined, + "linkedIn": undefined, "other": undefined }, - "jobInterest": "Full-time", + "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": ["African American"], + "ethnicity": "European", "major": "EE", "graduationYear": 2019, "codeOfConduct": true, + "teamId": Constants.MongoId.team3Id, }; -const HackerA = { - "_id": Constants.MongoId.hackerAId, - "accountId": Util.Account.hackerAccounts.stored.team[0], - "status": "Confirmed", - "school": "University of Blah", +const TeamHacker4 = { + "_id": Constants.MongoId.hackerGId, + "accountId": Util.Account.hackerAccounts.stored.team[4]._id, + "status": "Waitlisted", + "school": "University of Blah1", "degree": "Masters", - "gender": "Male", - "needsBus": true, + "gender": "Female", + "needsBus": false, "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume100", - "github": "www.github.com/Person1", + "resume": "www.gcloud.com/myResume2", + "github": "www.github.com/Personasdf", "dropler": undefined, - "personal": "www.person1.com", - "linkedIn": "www.linkedin.com/in/Person1", + "personal": undefined, + "linkedIn": undefined, "other": undefined }, - "jobInterest": "Full-time", + "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": ["Native American"], + "ethnicity": "European", "major": "EE", "graduationYear": 2019, "codeOfConduct": true, - "teamId": Constants.MongoId.team1Id, + "teamId": Constants.MongoId.team3Id, }; -const HackerB = { + +const NoTeamHacker0 = { "_id": Constants.MongoId.hackerBId, - "accountId": Util.Account.Account2._id, + "accountId": Util.Account.hackerAccounts.stored.noTeam[0]._id, "status": "Accepted", "school": "University of Blah1", "degree": "Masters", @@ -187,121 +177,132 @@ const HackerB = { "codeOfConduct": true, }; -const HackerC = { - "_id": Constants.MongoId.hackerCId, - "accountId": Util.Account.Hacker3._id, - "status": "Waitlisted", - "school": "University of Blah1", +const newHacker0 = { + "accountId": Util.Account.hackerAccounts.new[0]._id, + "school": "University of ASDF", "degree": "Masters", "gender": "Female", - "needsBus": false, + "needsBus": true, "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume2", - "github": "www.github.com/Personasdf", + "resume": "www.gcloud.com/myResume100", + "github": "www.github.com/Person1", "dropler": undefined, - "personal": undefined, - "linkedIn": undefined, + "personal": "www.person1.com", + "linkedIn": "www.linkedin.com/in/Person1", "other": undefined }, - "jobInterest": "Internship", + "jobInterest": "Full-time", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": "European", + "ethnicity": ["Caucasian"], "major": "EE", "graduationYear": 2019, "codeOfConduct": true, - "teamId": Constants.MongoId.team2Id, }; -const HackerD = { - "_id": Constants.MongoId.hackerDId, - "accountId": Util.Account.Hacker4._id, - "status": "Waitlisted", - "school": "University of Blah1", - "degree": "Masters", +const newHacker1 = { + "accountId": Util.Account.hackerAccounts.new[1]._id, + "school": "University of YIKES", + "degree": "PhD", "gender": "Female", - "needsBus": false, + "needsBus": true, "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume2", - "github": "www.github.com/Personasdf", + "resume": "www.gcloud.com/myResume100", + "github": "www.github.com/Person1", "dropler": undefined, - "personal": undefined, - "linkedIn": undefined, + "personal": "www.person1.com", + "linkedIn": "www.linkedin.com/in/Person1", "other": undefined }, - "jobInterest": "Internship", + "jobInterest": "Full-time", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": "European", + "ethnicity": ["African American"], "major": "EE", "graduationYear": 2019, "codeOfConduct": true, - "teamId": Constants.MongoId.team3Id, }; -const HackerE = { - "_id": Constants.MongoId.hackerEId, - "accountId": Util.Account.Hacker5._id, - "status": "Waitlisted", - "school": "University of Blah1", +// duplicate of newHack1, but with false for code of conduct +const invalidHacker0 = { + "accountId": Util.Account.hackerAccounts.invalid[0]._id, + "school": "University of ASDF", "degree": "Masters", "gender": "Female", - "needsBus": false, + "needsBus": true, "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume2", - "github": "www.github.com/Personasdf", + "resume": "www.gcloud.com/myResume100", + "github": "www.github.com/Person1", "dropler": undefined, - "personal": undefined, - "linkedIn": undefined, + "personal": "www.person1.com", + "linkedIn": "www.linkedin.com/in/Person1", "other": undefined }, - "jobInterest": "Internship", + "jobInterest": "Full-time", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": "European", + "ethnicity": ["Caucasian"], "major": "EE", "graduationYear": 2019, - "codeOfConduct": true, - "teamId": Constants.MongoId.team3Id, + "codeOfConduct": false, }; -const HackerF = { - "_id": Constants.MongoId.hackerFId, - "accountId": Util.Account.Hacker6._id, - "status": "Waitlisted", - "school": "University of Blah1", - "degree": "Masters", +const invalidHacker1 = { + "_id": mongoose.Types.ObjectId(), + // invalid mongoID + "accountId": Util.Account.hackerAccounts.invalid[1]._invalidId, + // invalid missing school attribute + "degree": "Undersaduate", "gender": "Female", - "needsBus": false, + "needsBus": true, + "application": { + // invalid portflio with no resume + "portfolioURL": {}, + // invalid jobInterest + "jobInterest": "ASDF", + }, + "ethnicity": ["Asian", "Caucasian"], + "major": "CS", + "graduationYear": 2020, + "codeOfConduct": true, +}; + +const duplicateAccountLinkHacker0 = { + "_id": mongoose.Types.ObjectId(), + "accountId": Util.Account.hackerAccounts.stored.team[0], + "status": "Applied", + "school": "University of Blah", + "degree": "Undergraduate", + "gender": "Male", + "needsBus": true, "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume2", - "github": "www.github.com/Personasdf", + "resume": "www.gcloud.com/myResume100", + "github": "www.github.com/Person1", "dropler": undefined, - "personal": undefined, - "linkedIn": undefined, + "personal": "www.person1.com", + "linkedIn": "www.linkedin.com/in/Person1", "other": undefined }, - "jobInterest": "Internship", + "jobInterest": "Full-time", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": "European", - "major": "EE", + "ethnicity": ["Caucasian"], + "major": "CS", "graduationYear": 2019, "codeOfConduct": true, - "teamId": Constants.MongoId.team3Id, }; -const HackerG = { - "_id": Constants.MongoId.hackerGId, - "accountId": Util.Account.Hacker7._id, +const waitlistedHacker0 = { + "_id": Constants.MongoId.hackerCId, + "accountId": Util.Account.waitlistedHacker0._id, "status": "Waitlisted", "school": "University of Blah1", "degree": "Masters", @@ -324,35 +325,49 @@ const HackerG = { "major": "EE", "graduationYear": 2019, "codeOfConduct": true, - "teamId": Constants.MongoId.team3Id, + "teamId": Constants.MongoId.team2Id, }; const Hackers = [ - HackerA, - HackerB, - HackerC, - HackerD, - HackerE, - HackerF, - HackerG, + TeamHacker0, + TeamHacker1, + TeamHacker2, + TeamHacker3, + TeamHacker4, + + NoTeamHacker0, + + invalidHacker0, + invalidHacker1, + + duplicateAccountLinkHacker0, + waitlistedHacker0 ]; module.exports = { - duplicateAccountLinkHacker1: duplicateAccountLinkHacker1, - invalidHacker1: invalidHacker1, + TeamHacker0: TeamHacker0, + TeamHacker1: TeamHacker1, + TeamHacker2: TeamHacker2, + TeamHacker3: TeamHacker3, + TeamHacker4: TeamHacker4, + + NoTeamHacker0: NoTeamHacker0, + + newHacker0: newHacker0, newHacker1: newHacker1, - newHacker2: newHacker2, - badCodeOfConductHacker1: badCodeOfConductHacker1, - HackerA: HackerA, - HackerB: HackerB, - HackerC: HackerC, - HackerD: HackerD, - HackerE: HackerE, - HackerF: HackerF, - HackerG: HackerG, + + invalidHacker0: invalidHacker0, + invalidHacker1: invalidHacker1, + + duplicateAccountLinkHacker0: duplicateAccountLinkHacker0, + waitlistedHacker0: waitlistedHacker0, + Hackers: Hackers, storeAll: storeAll, dropAll: dropAll +}; + +function storeAll(attributes) { const hackerDocs = []; const hackerIds = []; for (var i = 0; i < attributes.length; i++) { diff --git a/tests/util/roleBinding.test.util.js b/tests/util/roleBinding.test.util.js index 15f1ca82..2d664876 100644 --- a/tests/util/roleBinding.test.util.js +++ b/tests/util/roleBinding.test.util.js @@ -8,79 +8,6 @@ const Constants = { }; const logger = require("../../services/logger.service"); -const RoleBinding1 = { - accountId: Util.Account.allAccounts[6]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.adminRole._id], -}; -const RoleBinding2 = { - accountId: Util.Account.allAccounts[7]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.hackerRole._id], -}; -const RoleBinding3 = { - accountId: Util.Account.allAccounts[8]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.volunteerRole._id], -}; -const RoleBinding4 = { - accountId: Util.Account.allAccounts[9]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.sponsorT1Role._id], -}; -const RoleBinding5 = { - accountId: Util.Account.allAccounts[10]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.sponsorT2Role._id], -}; -const RoleBinding6 = { - accountId: Util.Account.allAccounts[11]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.allRolesObject.getSelfAccount._id], -}; - -const RoleBinding7 = { - accountId: Util.Account.allAccounts[12]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.allRolesObject.getAnyByIdHacker._id, Constants.Role.allRolesObject.patchSelfByIdHacker._id], -}; - -const RoleBindingHacker1 = { - accountId: Util.Account.Account1._id, - roles: [Constants.Role.accountRole._id, Constants.Role.hackerRole._id], -}; -const RoleBindingHacker2 = { - accountId: Util.Account.Account2._id, - roles: [Constants.Role.accountRole._id, Constants.Role.hackerRole._id], -}; -const RoleBindingHacker3 = { - accountId: Util.Account.Hacker3._id, - roles: [Constants.Role.hackerRole._id], -}; - -const RoleBindingNewHacker1 = { - accountId: Util.Account.allAccounts[13]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.allRolesObject["postHacker"]], -}; - -const RoleBindingSponsor1 = { - accountId: Util.Account.Account3._id, - roles: [Constants.Role.accountRole._id, Constants.Role.sponsorT1Role._id], -}; - -const RoleBindingSponsor2 = { - accountId: Util.Account.Account5._id, - roles: [Constants.Role.accountRole._id, Constants.Role.sponsorT1Role._id], -}; - -const RoleBindingVolunteer1 = { - accountId: Util.Account.Account4._id, - roles: [Constants.Role.accountRole._id, Constants.Role.volunteerRole._id], -}; - -const RoleBindingNewVolunteer1 = { - accountId: Util.Account.generatedAccounts[15]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.allRolesObject["postVolunteer"]], -}; - -const RoleBindingAdmin0 = { - accountId: Util.Account.staffAccounts[0], - roles: [Constants.Role.adminRole._id], -}; - // function setUpAccountRoleBinding???(???) { // /* // TODO diff --git a/tests/util/sponsor.test.util.js b/tests/util/sponsor.test.util.js index 0301d1e0..3623d7cb 100644 --- a/tests/util/sponsor.test.util.js +++ b/tests/util/sponsor.test.util.js @@ -7,33 +7,35 @@ const Sponsor = require("../../models/sponsor.model"); const mongoose = require("mongoose"); const logger = require("../../services/logger.service"); -const newSponsor1 = { +const T1Sponsor0 = { + "_id": mongoose.Types.ObjectId(), + "accountId": Util.Account.sponsorT1Accounts.stored[0]._id, + "tier": 1, + "company": "Best company NA", + "contractURL": "https://linkto.con", + "nominees": [Util.Hacker.HackerA._id], +}; + +const newT2Sponsor0 = { // no _id as that will be generated - "accountId": Util.Account.Account5._id, - "tier": 5, + "accountId": Util.Account.sponsorT2Accounts.new[0]._id, + "tier": 2, "company": "Best company EU", "contractURL": "https://linktocontract2.con", "nominees": [Util.Hacker.HackerB._id] }; + const duplicateAccountLinkSponsor1 = { "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.Account3._id, + "accountId": Util.Account.sponsorT1Accounts.stored[0]._id, "tier": 3, "company": "Best company NA1", "contractURL": "https://linkto1.con", "nominees": [Util.Hacker.HackerA._id], }; -const Sponsor1 = { - "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.Account3._id, - "tier": 3, - "company": "Best company NA", - "contractURL": "https://linkto.con", - "nominees": [Util.Hacker.HackerA._id], -}; const Sponsors = [ - Sponsor1, + Sponsor0, ]; function storeAll(attributes) { @@ -60,9 +62,14 @@ async function dropAll() { } module.exports = { - newSponsor1: newSponsor1, + T1Sponsor0: T1Sponsor0, + newT2Sponsor0: newT2Sponsor0, + + duplicateAccountLinkSponsor1: duplicateAccountLinkSponsor1, - Sponsor1: Sponsor1, + + + Sponsors: Sponsors, storeAll: storeAll, dropAll: dropAll, diff --git a/tests/util/staff.test.util.js b/tests/util/staff.test.util.js index 4edf3d87..3f7300c9 100644 --- a/tests/util/staff.test.util.js +++ b/tests/util/staff.test.util.js @@ -6,12 +6,12 @@ const Staff = require("../../models/staff.model"); const mongoose = require("mongoose"); const logger = require("../../services/logger.service"); -const Staff1 = { +const Staff0 = { "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.Account4._id + "accountId": Util.Account.staffAccounts.stored[0]; }; const Staffs = [ - Staff1, + Staff0, ]; function storeAll(attributes) { diff --git a/tests/util/volunteer.test.util.js b/tests/util/volunteer.test.util.js index f5fb48f4..74e706ae 100644 --- a/tests/util/volunteer.test.util.js +++ b/tests/util/volunteer.test.util.js @@ -11,19 +11,19 @@ const newVolunteer1 = { }; const duplicateVolunteer1 = { - "accountId": Util.Account.Account4._id + "accountId": Util.Account.volunteerAccounts.stored[0]._id }; const adminVolunteer1 = { "accountId": Util.Account.Admin1._id }; -const Volunteer1 = { +const Volunteer0 = { "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.Account4._id + "accountId": Util.Account.volunteerAccounts.stored[0]._id }; const Volunteers = [ - Volunteer1, + Volunteer0, ]; function storeAll(attributes) { diff --git a/tests/volunteer.test.js b/tests/volunteer.test.js index 779a4a66..d3fc5b70 100644 --- a/tests/volunteer.test.js +++ b/tests/volunteer.test.js @@ -18,7 +18,8 @@ const util = { }; const Admin0 = util.account.Admin0; -const Hacker0 = util.account.hackerAccounts.stored.team[0]; +const HackerAccount0 = util.account.hackerAccounts.stored.team[0]; +const VolunteerAccount0 = util.account.volunteerAccounts.stored[0]; const adminVolunteer = util.volunteer.adminVolunteer1; @@ -30,7 +31,7 @@ const duplicateVolunteer = util.volunteer.duplicateVolunteer1; describe("GET volunteer", function () { it("should FAIL to get volunteer due to lack of authentication", function (done) { chai.request(server.app) - .get(`/api/volunteer/${util.volunteer.Volunteer1._id}`) + .get(`/api/volunteer/${util.volunteer.Volunteer0._id}`) .end(function (err, res) { res.should.have.status(401); res.should.be.json; @@ -43,13 +44,13 @@ describe("GET volunteer", function () { }); it("should Fail to GET volunteer due inappropriate authorization", function (done) { - util.auth.login(agent, util.account.Hacker5, (error) => { + util.auth.login(agent, util.account.hackerAccounts.stored.team[2], (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/volunteer/${util.volunteer.Volunteer1._id}`) + .get(`/api/volunteer/${util.volunteer.Volunteer0._id}`) .end(function (err, res) { res.should.have.status(403); res.should.be.json; @@ -90,7 +91,7 @@ describe("GET volunteer", function () { return done(error); } return agent - .get(`/api/volunteer/${util.volunteer.Volunteer1._id}`) + .get(`/api/volunteer/${util.volunteer.Volunteer0._id}`) .end(function (err, res) { if (err) { return done(err); @@ -101,7 +102,7 @@ describe("GET volunteer", function () { res.body.message.should.equal(Constants.Success.VOLUNTEER_GET_BY_ID); res.body.should.have.property("data"); - let volunteer = new Volunteer(util.volunteer.Volunteer1); + let volunteer = new Volunteer(util.volunteer.Volunteer0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(volunteer.toJSON())); done(); }); @@ -110,13 +111,13 @@ describe("GET volunteer", function () { // success case it("should GET the user's volunteer info by id", function (done) { - util.auth.login(agent, util.account.Account4, (error) => { + util.auth.login(agent, VolunteerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/volunteer/${util.volunteer.Volunteer1._id}`) + .get(`/api/volunteer/${util.volunteer.Volunteer0._id}`) .end(function (err, res) { if (err) { return done(err); @@ -127,7 +128,7 @@ describe("GET volunteer", function () { res.body.message.should.equal(Constants.Success.VOLUNTEER_GET_BY_ID); res.body.should.have.property("data"); - let volunteer = new Volunteer(util.volunteer.Volunteer1); + let volunteer = new Volunteer(util.volunteer.Volunteer0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(volunteer.toJSON())); done(); }); From 71a616a53547bdd2b3be278ff36a38980c52f280 Mon Sep 17 00:00:00 2001 From: Richard Zhang Date: Mon, 14 Jan 2019 15:15:50 -0500 Subject: [PATCH 05/11] Fix errors that interpreter points out, add roleBindings --- tests/account.test.js | 2 +- tests/hacker.test.js | 82 ++++++++++++------------ tests/setup.spec.js | 25 +++++++- tests/util/account.test.util.js | 17 +++-- tests/util/bus.test.util.js | 2 +- tests/util/hacker.test.util.js | 28 +++++++++ tests/util/roleBinding.test.util.js | 98 +++++++++++++++++++---------- tests/util/sponsor.test.util.js | 11 ++-- tests/util/staff.test.util.js | 4 +- tests/util/team.test.util.js | 8 +-- tests/util/volunteer.test.util.js | 21 ++++--- tests/volunteer.test.js | 31 ++++----- 12 files changed, 209 insertions(+), 120 deletions(-) diff --git a/tests/account.test.js b/tests/account.test.js index 44a30f8e..9de890c5 100644 --- a/tests/account.test.js +++ b/tests/account.test.js @@ -25,7 +25,7 @@ const confirmationToken = util.accountConfirmation.ConfirmationToken; const fakeToken = util.accountConfirmation.FakeToken; const resetToken = util.reset.ResetToken; // accounts -const Admin0 = util.account.staffAccounts[0]; +const Admin0 = util.account.staffAccounts.stored[0]; const teamHackerAccount0 = util.account.hackerAccounts.stored.team[0]; diff --git a/tests/hacker.test.js b/tests/hacker.test.js index 6463058d..8d1def35 100644 --- a/tests/hacker.test.js +++ b/tests/hacker.test.js @@ -23,33 +23,29 @@ const util = { const StorageService = require("../services/storage.service"); const Admin0 = util.account.staffAccounts.stored[0]; -const teamHackerAccount0 = util.account.hackerAccounts.stored.team[0]; -// linked with hackerB - fix later -const noTeamHackerAccount0 = util.account.hackerAccounts.stored.noTeam[0]; -const volunteerAccount0 = util.account.volunteerAccounts.stored[0]; -// storedAccount1 and storedHacker1 are linked together, and have hacker priviledges -// newHackerDuplicateAccountLink1 is also linked with Account1 -// storedHacker1 has status confirmed -const storedHacker1 = util.hacker.HackerA; -const newHackerDuplicateAccountLink1 = util.hacker.duplicateAccountLinkHacker1; +const volunteerAccount0 = util.account.volunteerAccounts.stored[0]; -const storedHacker2 = util.hacker.HackerB; +const newHackerAccount0 = util.account.hackerAccounts.new[0]; +const newHacker0 = util.hacker.newHacker0; +const invalidHacker0 = util.hacker.invalidHacker0; const newHacker1 = util.hacker.newHacker1; -// badConductHacker1 is the same as newHacker1, even linking to the same account -// the difference is that badConductHacker1 does not accept the code of conducts -const badConductHacker1 = util.hacker.badCodeOfConductHacker1; -const newHackerAccount1 = util.account.allAccounts[13]; -const newHacker2 = util.hacker.newHacker2; +const noTeamHackerAccount0 = util.account.hackerAccounts.stored.noTeam[0]; +const noTeamHacker0 = util.hacker.NoTeamHacker0; + +const teamHackerAccount0 = util.account.hackerAccounts.stored.team[0]; +const TeamHacker0 = util.hacker.TeamHacker0; +const duplicateAccountLinkHacker0 = util.hacker.duplicateAccountLinkHacker0; + const invalidHacker1 = util.hacker.invalidHacker1; describe("GET hacker", function () { // fail on authentication it("should fail to list a hacker's information on /api/hacker/:id GET due to authentication", function (done) { chai.request(server.app) - .get(`/api/hacker/` + storedHacker1._id) + .get(`/api/hacker/` + TeamHacker0._id) .end(function (err, res) { res.should.have.status(401); res.should.be.json; @@ -78,7 +74,7 @@ describe("GET hacker", function () { res.body.message.should.equal(Constants.Success.HACKER_READ); res.body.should.have.property("data"); - let hacker = new Hacker(storedHacker1); + let hacker = new Hacker(TeamHacker0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); done(); }); @@ -112,7 +108,7 @@ describe("GET hacker", function () { return done(error); } return agent - .get(`/api/hacker/${storedHacker1._id}`) + .get(`/api/hacker/${TeamHacker0._id}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -124,7 +120,7 @@ describe("GET hacker", function () { res.body.message.should.equal(Constants.Success.HACKER_READ); res.body.should.have.property("data"); - let hacker = new Hacker(storedHacker1); + let hacker = new Hacker(TeamHacker0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); done(); @@ -140,7 +136,7 @@ describe("GET hacker", function () { return done(error); } return agent - .get(`/api/hacker/${storedHacker1._id}`) + .get(`/api/hacker/${TeamHacker0._id}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -152,7 +148,7 @@ describe("GET hacker", function () { res.body.message.should.equal(Constants.Success.HACKER_READ); res.body.should.have.property("data"); - let hacker = new Hacker(storedHacker1); + let hacker = new Hacker(TeamHacker0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); @@ -169,7 +165,7 @@ describe("GET hacker", function () { return done(error); } return agent - .get(`/api/hacker/${storedHacker1._id}`) + .get(`/api/hacker/${TeamHacker0._id}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -239,7 +235,7 @@ describe("POST create hacker", function () { return agent .post(`/api/hacker/`) .type("application/json") - .send(newHacker1) + .send(newHacker0) .end(function (err, res) { res.should.have.status(200); res.should.be.json; @@ -250,7 +246,7 @@ describe("POST create hacker", function () { // create JSON version of model // delete id as they will be different between model objects // update status to be applied on the comparator hacker object - const hacker = (new Hacker(newHacker1)).toJSON(); + const hacker = (new Hacker(newHacker0)).toJSON(); hacker.status = Constants.General.HACKER_STATUS_APPLIED; delete res.body.data.id; delete hacker.id; @@ -263,7 +259,7 @@ describe("POST create hacker", function () { // succeed on user case it("should SUCCEED and create a new hacker for user (with an account that has been confirmed)", function (done) { - util.auth.login(agent, newHackerAccount1, (error) => { + util.auth.login(agent, newHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -282,7 +278,7 @@ describe("POST create hacker", function () { // create JSON version of model // delete id as they will be different between model objects // update status to be applied on the comparator hacker object - const hacker = (new Hacker(newHacker1)).toJSON(); + const hacker = (new Hacker(newHacker0)).toJSON(); hacker.status = Constants.General.HACKER_STATUS_APPLIED; delete res.body.data.id; delete hacker.id; @@ -294,7 +290,7 @@ describe("POST create hacker", function () { // should fail due to 'false' on code of conduct it("should FAIL if the new hacker does not accept code of conduct", function (done) { - util.auth.login(agent, newHackerAccount1, (error) => { + util.auth.login(agent, newHacker0, (error) => { if (error) { agent.close(); return done(error); @@ -302,7 +298,7 @@ describe("POST create hacker", function () { return agent .post(`/api/hacker/`) .type("application/json") - .send(badConductHacker1) + .send(invalidHacker0) .end(function (err, res) { res.should.have.status(422); res.should.be.json; @@ -326,7 +322,7 @@ describe("POST create hacker", function () { return agent .post(`/api/hacker/`) .type("application/json") - .send(newHacker2) + .send(util.hacker.unconfirmedAccountHacker0) .end(function (err, res) { res.should.be.json; res.body.should.have.property("message"); @@ -347,7 +343,7 @@ describe("POST create hacker", function () { return agent .post(`/api/hacker/`) .type("application/json") - .send(newHackerDuplicateAccountLink1) + .send(duplicateAccountLinkHacker0) .end(function (err, res) { res.should.have.status(409); res.body.should.have.property("message"); @@ -382,7 +378,7 @@ describe("PATCH update one hacker", function () { // fail on authentication it("should fail to update a hacker on /api/hacker/:id GET due to authentication", function (done) { chai.request(server.app) - .patch(`/api/hacker/${storedHacker1._id}`) + .patch(`/api/hacker/${TeamHacker0._id}`) .type("application/json") .send({ gender: "Other" @@ -404,7 +400,7 @@ describe("PATCH update one hacker", function () { return done(error); } return agent - .patch(`/api/hacker/${storedHacker1._id}`) + .patch(`/api/hacker/${TeamHacker0._id}`) .type("application/json") .send({ gender: "Other" @@ -430,7 +426,7 @@ describe("PATCH update one hacker", function () { return done(error); } return agent - .patch(`/api/hacker/status/${storedHacker1._id}`) + .patch(`/api/hacker/status/${TeamHacker0._id}`) .type("application/json") .send({ status: "Accepted" @@ -456,7 +452,7 @@ describe("PATCH update one hacker", function () { return done(error); } return agent - .patch(`/api/hacker/status/${storedHacker1._id}`) + .patch(`/api/hacker/status/${TeamHacker0._id}`) .type("application/json") .send({ status: "Accepted" @@ -480,7 +476,7 @@ describe("PATCH update one hacker", function () { return done(error); } return agent - .patch(`/api/hacker/checkin/${storedHacker1._id}`) + .patch(`/api/hacker/checkin/${TeamHacker0._id}`) .type("application/json") .send({ status: "Checked-in" @@ -507,7 +503,7 @@ describe("PATCH update one hacker", function () { return done(error); } return agent - .patch(`/api/hacker/checkin/${storedHacker1._id}`) + .patch(`/api/hacker/checkin/${TeamHacker0._id}`) .type("application/json") .send({ status: "Checked-in" @@ -531,7 +527,7 @@ describe("PATCH update one hacker", function () { return done(error); } return agent - .patch(`/api/hacker/${storedHacker2._id}`) + .patch(`/api/hacker/${noTeamHacker0._id}`) .type("application/json") .send({ gender: "Other" @@ -558,7 +554,7 @@ describe("PATCH update one hacker", function () { return done(error); } return agent - .patch(`/api/hacker/${storedHacker1._id}`) + .patch(`/api/hacker/${noTeamHacker0._id}`) .type("application/json") .send({ gender: "Other" @@ -607,7 +603,7 @@ describe("PATCH update one hacker", function () { return done(error); } return agent - .patch(`/api/hacker/confirmation/${storedHacker2._id}`) + .patch(`/api/hacker/confirmation/${noTeamHacker0._id}`) .type("application/json") .send({ confirm: true @@ -638,7 +634,7 @@ describe("PATCH update one hacker", function () { return done(error); } return agent - .patch(`/api/hacker/confirmation/${storedHacker1._id}`) + .patch(`/api/hacker/confirmation/${noTeamHacker0._id}`) .type("application/json") .send({ confirm: false @@ -697,7 +693,7 @@ describe("PATCH update one hacker", function () { return done(error); } return agent - .patch(`/api/hacker/confirmation/${storedHacker1._id}`) + .patch(`/api/hacker/confirmation/${noTeamHacker0._id}`) .type("application/json") .send({ confirm: true @@ -720,12 +716,12 @@ describe("PATCH update one hacker", function () { describe("POST add a hacker resume", function () { it("It should SUCCEED and upload a resume for a hacker", function (done) { //this takes a lot of time for some reason - util.auth.login(agent, storedHacker1, (error) => { + util.auth.login(agent, noTeamHacker0, (error) => { if (error) { return done(error); } return agent - .post(`/api/hacker/resume/${storedHacker1._id}`) + .post(`/api/hacker/resume/${noTeamHacker0._id}`) .type("multipart/form-data") .attach("resume", fs.createReadStream(path.join(__dirname, "testResume.pdf")), { contentType: "application/pdf" diff --git a/tests/setup.spec.js b/tests/setup.spec.js index d7b63ba4..b747a55d 100644 --- a/tests/setup.spec.js +++ b/tests/setup.spec.js @@ -53,7 +53,19 @@ afterEach(function (done) { }); }); async function storeAll() { - await Util.Account.storeAll(Util.Account.allAccounts); + await Util.Account.storeAll(Util.Account.hackerAccounts.stored.team); + await Util.Account.storeAll(Util.Account.hackerAccounts.stored.noTeam); + await Util.Account.storeAll(Util.Account.volunteerAccounts.stored); + await Util.Account.storeAll(Util.Account.staffAccounts.stored); + await Util.Account.storeAll(Util.Account.sponsorT1Accounts.stored); + await Util.Account.storeAll(Util.Account.sponsorT2Accounts.stored); + await Util.Account.storeAll(Util.Account.sponsorT3Accounts.stored); + await Util.Account.storeAll(Util.Account.sponsorT4Accounts.stored); + await Util.Account.storeAll(Util.Account.sponsorT5Accounts.stored); + await Util.Account.storeAll(Util.Account.unlinkedAccounts.stored); + + await Util.Account.storeAll(Util.Account.extraAccounts); + await Util.Hacker.storeAll(Util.Hacker.Hackers); await Util.Sponsor.storeAll(Util.Sponsor.Sponsors); await Util.Team.storeAll(Util.Team.Teams); @@ -63,7 +75,16 @@ async function storeAll() { await Util.Bus.storeAll(Util.Bus.Busses); await Util.Volunteer.storeAll(Util.Volunteer.Volunteers); await Util.Role.storeAll(Constants.Role.allRolesArray); - await Util.RoleBinding.storeAll(Util.RoleBinding.RoleBindings); + + await Util.RoleBinding.storeAll(Util.RoleBinding.TeamHackerRB); + await Util.RoleBinding.storeAll(Util.RoleBinding.NoTeamHackerRB); + await Util.RoleBinding.storeAll(Util.RoleBinding.VolunteerRB); + await Util.RoleBinding.storeAll(Util.RoleBinding.StaffRB); + await Util.RoleBinding.storeAll(Util.RoleBinding.SponsorT1RB); + await Util.RoleBinding.storeAll(Util.RoleBinding.SponsorT2RB); + await Util.RoleBinding.storeAll(Util.RoleBinding.SponsorT3RB); + await Util.RoleBinding.storeAll(Util.RoleBinding.SponsorT4RB); + await Util.RoleBinding.storeAll(Util.RoleBinding.SponsorT5RB); } async function dropAll() { diff --git a/tests/util/account.test.util.js b/tests/util/account.test.util.js index 72ed016c..e8a5fede 100644 --- a/tests/util/account.test.util.js +++ b/tests/util/account.test.util.js @@ -86,7 +86,7 @@ function createAccount(acc = {}) { } function createNAccounts(n, acc = {}) { - let accounts = [] + let accounts = []; for (let i = 0; i < n; i++) { accounts.push(createAccount(acc)); } @@ -201,7 +201,6 @@ let unlinkedAccounts = { })] }; - const waitlistedHacker0 = { "_id": mongoose.Types.ObjectId(), "firstName": "abcd", @@ -245,6 +244,13 @@ const NonConfirmedAccount2 = { "accountType": Constants.HACKER, }; +const NonConfirmedAccount3 = createAccount({ + "confirmed": false, + "accountType": Constants.HACKER +}); + +const extraAccounts = [waitlistedHacker0, NonConfirmedAccount1, NonConfirmedAccount2, NonConfirmedAccount3]; + module.exports = { hackerAccounts: hackerAccounts, volunteerAccounts: volunteerAccounts, @@ -259,10 +265,13 @@ module.exports = { waitlistedHacker0: waitlistedHacker0, NonConfirmedAccount1: NonConfirmedAccount1, NonConfirmedAccount2: NonConfirmedAccount2, + NonConfirmedAccount3: NonConfirmedAccount3, + + extraAccounts: extraAccounts, storeAll: storeAll, dropAll: dropAll, - equals: equals + equals: equals, }; function encryptPassword(user) { @@ -312,5 +321,5 @@ function equals(acc1, acc2) { const email = (acc1.email === acc2.email); const dietaryRestrictions = (acc1.dietaryRestrictions.join(",") === acc2.dietaryRestrictions.join(",")); const shirtSize = (acc1.shirtSize === acc2.shirtSize); - return [id, firstName, lastName, email, dietaryRestrictions, shirtSize]; + return [id, firstName, lastName, email, dietaryRestrictions, shirtSize, pronoun]; } \ No newline at end of file diff --git a/tests/util/bus.test.util.js b/tests/util/bus.test.util.js index 283c8260..0603ecf1 100644 --- a/tests/util/bus.test.util.js +++ b/tests/util/bus.test.util.js @@ -15,7 +15,7 @@ const Bus1 = { "addr2": "addr2-1" }, "capacity": 10, - "hackers": [Util.Hacker.HackerA._id] + "hackers": [Util.Hacker.TeamHacker0._id] }; const Busses = [ Bus1, diff --git a/tests/util/hacker.test.util.js b/tests/util/hacker.test.util.js index b90e83cf..8485427d 100644 --- a/tests/util/hacker.test.util.js +++ b/tests/util/hacker.test.util.js @@ -328,6 +328,33 @@ const waitlistedHacker0 = { "teamId": Constants.MongoId.team2Id, }; +const unconfirmedAccountHacker0 = { + "_id": Constants.MongoId.hackerCId, + "accountId": Util.Account.NonConfirmedAccount3._id, + "status": "Waitlisted", + "school": "University of Blah1", + "degree": "Masters", + "gender": "Female", + "needsBus": false, + "application": { + "portfolioURL": { + //gcloud bucket link + "resume": "www.gcloud.com/myResume123", + "github": "www.github.com/Personasdf", + "dropler": undefined, + "personal": undefined, + "linkedIn": undefined, + "other": undefined + }, + "jobInterest": "Internship", + "skills": ["CSS", "HTML", "JS"], + }, + "ethnicity": "European", + "major": "EE", + "graduationYear": 2019, + "codeOfConduct": true, +} + const Hackers = [ TeamHacker0, TeamHacker1, @@ -361,6 +388,7 @@ module.exports = { duplicateAccountLinkHacker0: duplicateAccountLinkHacker0, waitlistedHacker0: waitlistedHacker0, + unconfirmedAccountHacker0: unconfirmedAccountHacker0, Hackers: Hackers, storeAll: storeAll, diff --git a/tests/util/roleBinding.test.util.js b/tests/util/roleBinding.test.util.js index 2d664876..d6311c8b 100644 --- a/tests/util/roleBinding.test.util.js +++ b/tests/util/roleBinding.test.util.js @@ -4,39 +4,60 @@ const Util = { Account: require("./account.test.util"), }; const Constants = { - Role: require("../../constants/role.constant") + Role: require("../../constants/role.constant"), + General: require("../../constants/general.constant") }; const logger = require("../../services/logger.service"); -// function setUpAccountRoleBinding???(???) { -// /* -// TODO -// */ -// } +function createRoleBinding(accountId, accountType, specificRoles = []) { + let roleBinding = { + accountId: accountId, + roles: [Constants.Role.accountRole] + }; + switch (accountType) { + case Constants.General.HACKER: + roleBinding.roles.push(Constants.Role.hackerRole); + break; + case Constants.General.VOLUNTEER: + roleBinding.roles.push(Constants.Role.volunteerRole); + break; + case Constants.General.STAFF: + roleBinding.roles.push(Constants.Role.adminRole); + break; + case Constants.General.SPONSOR_T1: + roleBinding.roles.push(Constants.Role.sponsorT1Role); + break; + case Constants.General.SPONSOR_T2: + roleBinding.roles.push(Constants.Role.sponsorT2Role); + break; + case Constants.General.SPONSOR_T3: + roleBinding.roles.push(Constants.Role.sponsorT3Role); + break; + case Constants.General.SPONSOR_T4: + roleBinding.roles.push(Constants.Role.sponsorT4Role); + break; + case Constants.General.SPONSOR_T5: + roleBinding.roles.push(Constants.Role.sponsorT5Role); + break; + } -const RoleBindings = [ - RoleBindingAdmin0, + for (const role of specificRoles) { + roleBinding.roles.push(role); + } + return roleBinding; +} - RoleBinding1, - RoleBinding2, - RoleBinding3, - RoleBinding4, - RoleBinding5, - RoleBinding6, - RoleBinding7, - RoleBindingHacker1, - RoleBindingHacker2, - RoleBindingNewHacker1, - RoleBindingSponsor1, - RoleBindingSponsor2, - RoleBindingVolunteer1, - RoleBindingNewVolunteer1, +function createRoleBindings(accounts) { + let roleBindings = []; - RoleBindingHacker3, -]; + for (const account of accounts) { + roleBindings.push(createRoleBinding(account._id, account.accountType)); + } + return roleBindings; +} function storeAll(attributes) { const roleBindingDocs = []; @@ -61,16 +82,27 @@ async function dropAll() { } } +const TeamHackerRB = createRoleBindings(Util.Account.hackerAccounts.stored.team); +const NoTeamHackerRB = createRoleBindings(Util.Account.hackerAccounts.stored.noTeam); +const VolunteerRB = createRoleBindings(Util.Account.volunteerAccounts.stored); +const StaffRB = createRoleBindings(Util.Account.staffAccounts.stored); +const SponsorT1RB = createRoleBindings(Util.Account.sponsorT1Accounts.stored); +const SponsorT2RB = createRoleBindings(Util.Account.sponsorT2Accounts.stored); +const SponsorT3RB = createRoleBindings(Util.Account.sponsorT3Accounts.stored); +const SponsorT4RB = createRoleBindings(Util.Account.sponsorT4Accounts.stored); +const SponsorT5RB = createRoleBindings(Util.Account.sponsorT5Accounts.stored); + module.exports = { - RoleBinding1: RoleBinding1, - RoleBinding2: RoleBinding2, - RoleBinding3: RoleBinding3, - RoleBinding4: RoleBinding4, - RoleBinding5: RoleBinding5, - RoleBinding6: RoleBinding6, - RoleBinding7: RoleBinding7, - RoleBindings: RoleBindings, - RoleBindingHacker3: RoleBindingHacker3, + TeamHackerRB: TeamHackerRB, + NoTeamHackerRB: NoTeamHackerRB, + VolunteerRB: VolunteerRB, + StaffRB: StaffRB, + SponsorT1RB: SponsorT1RB, + SponsorT2RB: SponsorT2RB, + SponsorT3RB: SponsorT3RB, + SponsorT4RB: SponsorT4RB, + SponsorT5RB: SponsorT5RB, + storeAll: storeAll, dropAll: dropAll, }; \ No newline at end of file diff --git a/tests/util/sponsor.test.util.js b/tests/util/sponsor.test.util.js index 3623d7cb..220c6504 100644 --- a/tests/util/sponsor.test.util.js +++ b/tests/util/sponsor.test.util.js @@ -13,7 +13,7 @@ const T1Sponsor0 = { "tier": 1, "company": "Best company NA", "contractURL": "https://linkto.con", - "nominees": [Util.Hacker.HackerA._id], + "nominees": [Util.Hacker.TeamHacker0._id], }; const newT2Sponsor0 = { @@ -22,7 +22,7 @@ const newT2Sponsor0 = { "tier": 2, "company": "Best company EU", "contractURL": "https://linktocontract2.con", - "nominees": [Util.Hacker.HackerB._id] + "nominees": [Util.Hacker.NoTeamHacker0._id] }; const duplicateAccountLinkSponsor1 = { @@ -31,11 +31,11 @@ const duplicateAccountLinkSponsor1 = { "tier": 3, "company": "Best company NA1", "contractURL": "https://linkto1.con", - "nominees": [Util.Hacker.HackerA._id], + "nominees": [Util.Hacker.TeamHacker0._id], }; const Sponsors = [ - Sponsor0, + T1Sponsor0, ]; function storeAll(attributes) { @@ -65,11 +65,8 @@ module.exports = { T1Sponsor0: T1Sponsor0, newT2Sponsor0: newT2Sponsor0, - duplicateAccountLinkSponsor1: duplicateAccountLinkSponsor1, - - Sponsors: Sponsors, storeAll: storeAll, dropAll: dropAll, diff --git a/tests/util/staff.test.util.js b/tests/util/staff.test.util.js index 3f7300c9..9bfb4e8e 100644 --- a/tests/util/staff.test.util.js +++ b/tests/util/staff.test.util.js @@ -8,7 +8,7 @@ const logger = require("../../services/logger.service"); const Staff0 = { "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.staffAccounts.stored[0]; + "accountId": Util.Account.staffAccounts.stored[0], }; const Staffs = [ Staff0, @@ -38,7 +38,7 @@ async function dropAll() { } module.exports = { - Staff1: Staff1, + Staff0: Staff0, Staffs: Staffs, storeAll: storeAll, dropAll: dropAll, diff --git a/tests/util/team.test.util.js b/tests/util/team.test.util.js index 45b98b86..2ba49ea1 100644 --- a/tests/util/team.test.util.js +++ b/tests/util/team.test.util.js @@ -22,14 +22,14 @@ const newTeam1 = { const createdNewTeam1 = { "name": "BronzeTeam1", - "members": [Util.Hacker.HackerB._id], + "members": [Util.Hacker.NoTeamHacker0._id], "projectName": "YetAnotherProject" }; const Team1 = { "_id": Constants.MongoId.team1Id, "name": "BronzeTeam", - "members": [Util.Hacker.HackerA._id], + "members": [Util.Hacker.TeamHacker0._id], "devpostURL": "justanother.devpost.com", "projectName": "YetAnotherProject" }; @@ -37,7 +37,7 @@ const Team1 = { const Team2 = { "_id": Constants.MongoId.team2Id, "name": "SilverTeam", - "members": [Util.Hacker.HackerC._id], + "members": [Util.Hacker.waitlistedHacker0._id], "devpostURL": "watwatwat.devpost.com", "projectName": "WatWatWat", }; @@ -45,7 +45,7 @@ const Team2 = { const Team3 = { "_id": Constants.MongoId.team3Id, "name": "FullTeam", - "members": [Util.Hacker.HackerD._id, Util.Hacker.HackerE._id, Util.Hacker.HackerF._id, Util.Hacker.HackerG._id] + "members": [Util.Hacker.TeamHacker1._id, Util.Hacker.TeamHacker2._id, Util.Hacker.TeamHacker3._id, Util.Hacker.TeamHacker4._id] }; const Teams = [ diff --git a/tests/util/volunteer.test.util.js b/tests/util/volunteer.test.util.js index 74e706ae..3b334605 100644 --- a/tests/util/volunteer.test.util.js +++ b/tests/util/volunteer.test.util.js @@ -6,22 +6,24 @@ const mongoose = require("mongoose"); const Volunteer = require("../../models/volunteer.model"); const logger = require("../../services/logger.service"); -const newVolunteer1 = { - "accountId": Util.Account.generatedAccounts[15]._id +const newVolunteer0 = { + "accountId": Util.Account.volunteerAccounts.new[0]._id }; const duplicateVolunteer1 = { "accountId": Util.Account.volunteerAccounts.stored[0]._id }; -const adminVolunteer1 = { - "accountId": Util.Account.Admin1._id -}; - const Volunteer0 = { "_id": mongoose.Types.ObjectId(), "accountId": Util.Account.volunteerAccounts.stored[0]._id }; + +const invalidVolunteer0 = { + "_id": mongoose.Types.ObjectId(), + "accountId": Util.Account.staffAccounts.stored[0]._id +}; + const Volunteers = [ Volunteer0, ]; @@ -51,9 +53,10 @@ async function dropAll() { module.exports = { duplicateVolunteer1: duplicateVolunteer1, - adminVolunteer1: adminVolunteer1, - newVolunteer1: newVolunteer1, - Volunteer1: Volunteer1, + newVolunteer0: newVolunteer0, + Volunteer0: Volunteer0, + invalidVolunteer0: invalidVolunteer0, + Volunteers: Volunteers, storeAll: storeAll, dropAll: dropAll, diff --git a/tests/volunteer.test.js b/tests/volunteer.test.js index d3fc5b70..7f3574dc 100644 --- a/tests/volunteer.test.js +++ b/tests/volunteer.test.js @@ -19,19 +19,22 @@ const util = { const Admin0 = util.account.Admin0; const HackerAccount0 = util.account.hackerAccounts.stored.team[0]; + const VolunteerAccount0 = util.account.volunteerAccounts.stored[0]; +const Volunteer0 = util.volunteer.Volunteer0; +const newVolunteerAccount0 = util.account.volunteerAccounts.new[0]; +const newVolunteer0 = util.volunteer.newVolunteer0; +const duplicateVolunteer = util.volunteer.duplicateVolunteer1; + +const invalidVolunteer0 = util.volunteer.invalidVolunteer0; -const adminVolunteer = util.volunteer.adminVolunteer1; -const newVolunteerAccount = util.account.generatedAccounts[15]; -const newVolunteer = util.volunteer.newVolunteer1; -const duplicateVolunteer = util.volunteer.duplicateVolunteer1; describe("GET volunteer", function () { it("should FAIL to get volunteer due to lack of authentication", function (done) { chai.request(server.app) - .get(`/api/volunteer/${util.volunteer.Volunteer0._id}`) + .get(`/api/volunteer/${Volunteer0._id}`) .end(function (err, res) { res.should.have.status(401); res.should.be.json; @@ -44,7 +47,7 @@ describe("GET volunteer", function () { }); it("should Fail to GET volunteer due inappropriate authorization", function (done) { - util.auth.login(agent, util.account.hackerAccounts.stored.team[2], (error) => { + util.auth.login(agent, HackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -91,7 +94,7 @@ describe("GET volunteer", function () { return done(error); } return agent - .get(`/api/volunteer/${util.volunteer.Volunteer0._id}`) + .get(`/api/volunteer/${Volunteer0._id}`) .end(function (err, res) { if (err) { return done(err); @@ -142,7 +145,7 @@ describe("POST create volunteer", function () { chai.request(server.app) .post(`/api/volunteer`) .type("application/json") - .send(util.volunteer.newVolunteer1) + .send(util.volunteer.newVolunteer0) .end(function (err, res) { res.should.have.status(401); res.should.be.json; @@ -163,7 +166,7 @@ describe("POST create volunteer", function () { return agent .post(`/api/volunteer`) .type("application/json") - .send(adminVolunteer) + .send(invalidVolunteer0) .end(function (err, res) { res.should.have.status(409); res.should.be.json; @@ -178,7 +181,7 @@ describe("POST create volunteer", function () { // succeed on user case it("should create a volunteer for the user /api/volunteer POST", function (done) { - util.auth.login(agent, newVolunteerAccount, (error) => { + util.auth.login(agent, newVolunteerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -186,7 +189,7 @@ describe("POST create volunteer", function () { return agent .post(`/api/volunteer`) .type("application/json") - .send(newVolunteer) + .send(newVolunteer0) .end(function (err, res) { res.should.have.status(200); res.should.be.json; @@ -196,7 +199,7 @@ describe("POST create volunteer", function () { // deleting id because that was generated, and not part of original data delete res.body.data.id; - chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(util.volunteer.newVolunteer1)); + chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(newVolunteer0)); done(); }); }); @@ -204,7 +207,7 @@ describe("POST create volunteer", function () { // fail due to duplicate accountId it("should create a volunteer for the user /api/volunteer POST", function (done) { - util.auth.login(agent, newVolunteerAccount, (error) => { + util.auth.login(agent, newVolunteerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -227,7 +230,7 @@ describe("POST create volunteer", function () { // fail on non-volunteer type account trying to create volunteer it("should fail to create a volunteer due to authorization /api/volunteer POST", function (done) { - util.auth.login(agent, Hacker0, (error) => { + util.auth.login(agent, HackerAccount0, (error) => { if (error) { agent.close(); return done(error); From f74f6c389185752aa0a922cc149f2bb03b98baad Mon Sep 17 00:00:00 2001 From: Richard Zhang Date: Mon, 14 Jan 2019 17:32:59 -0500 Subject: [PATCH 06/11] fix account tests --- constants/general.constant.js | 2 +- tests/account.test.js | 18 +++++++++--------- tests/util/account.test.util.js | 17 ++++++++++------- tests/util/accountConfirmation.test.util.js | 4 ++-- tests/util/resetPassword.test.util.js | 2 +- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/constants/general.constant.js b/constants/general.constant.js index c2294dfc..77af8a5e 100644 --- a/constants/general.constant.js +++ b/constants/general.constant.js @@ -141,5 +141,5 @@ module.exports = { CACHE_TIMEOUT_STATS: CACHE_TIMEOUT_STATS, CACHE_KEY_STATS: CACHE_KEY_STATS, MAX_TEAM_SIZE: MAX_TEAM_SIZE, - SAMPLE_DIET_RESTRICTIONS, + SAMPLE_DIET_RESTRICTIONS: SAMPLE_DIET_RESTRICTIONS, }; \ No newline at end of file diff --git a/tests/account.test.js b/tests/account.test.js index 9de890c5..f718405f 100644 --- a/tests/account.test.js +++ b/tests/account.test.js @@ -28,13 +28,13 @@ const resetToken = util.reset.ResetToken; const Admin0 = util.account.staffAccounts.stored[0]; const teamHackerAccount0 = util.account.hackerAccounts.stored.team[0]; - - - //This account has a confirmation token in the db -const storedAccount2 = util.account.NonConfirmedAccount1; +const storedAccount1 = util.account.NonConfirmedAccount1; +const storedAccount2 = util.account.NonConfirmedAccount2; + //This account does not have a confirmation token in the DB -const storedAccount3 = util.account.NonConfirmedAccount2; +const storedAccount3 = util.account.NonConfirmedAccount3; + // admin role binding const newAccount0 = util.account.unlinkedAccounts.new[0]; @@ -183,7 +183,7 @@ describe("POST create account", function () { chai.request(server.app) .post(`/api/account/`) .type("application/json") - .send(teamHackerAccount0) + .send(newAccount0) .end(function (err, res) { res.should.have.status(200); res.should.be.json; @@ -191,7 +191,7 @@ describe("POST create account", function () { res.body.message.should.equal(Constants.Success.ACCOUNT_CREATE); // use acc.toStrippedJSON to deal with hidden passwords and convert _id to id - const acc = (new Account(teamHackerAccount0)).toStrippedJSON(); + const acc = (new Account(newAccount0)).toStrippedJSON(); // delete id as those are generated delete acc.id; delete res.body.data.id; @@ -470,7 +470,7 @@ describe("GET retrieve permissions", function () { describe("GET resend confirmation email", function () { it("should SUCCEED and resend the confirmation email", function (done) { - util.auth.login(agent, storedAccount3, (error) => { + util.auth.login(agent, storedAccount1, (error) => { if (error) { agent.close(); return done(error); @@ -506,7 +506,7 @@ describe("GET resend confirmation email", function () { }); }); it("should FAIL as account confirmation token does not exist", function (done) { - util.auth.login(agent, storedAccount2, (error) => { + util.auth.login(agent, storedAccount3, (error) => { if (error) { agent.close(); return done(error); diff --git a/tests/util/account.test.util.js b/tests/util/account.test.util.js index e8a5fede..a552f72e 100644 --- a/tests/util/account.test.util.js +++ b/tests/util/account.test.util.js @@ -48,7 +48,7 @@ function generateRandomValue(atr) { case "password": return Math.random().toString(36).substr(0, 10); case "dietaryRestrictions": - return Constants.SAMPLE_DIET_RESTRICTIONS[Math.floor(Math.random() * Constants.SAMPLE_DIET_RESTRICTIONS.length)]; + return [Constants.SAMPLE_DIET_RESTRICTIONS[Math.floor(Math.random() * Constants.SAMPLE_DIET_RESTRICTIONS.length)]]; case "shirtSize": return Constants.SHIRT_SIZES[Math.floor(Math.random() * Constants.SHIRT_SIZES.length)]; case "confirmed": @@ -96,11 +96,12 @@ function createNAccounts(n, acc = {}) { let hackerAccounts = { new: createNAccounts(10, { - "accountType": Constants.HACKER + "accountType": Constants.HACKER, }), stored: { team: createNAccounts(10, { - "accountType": Constants.HACKER + "accountType": Constants.HACKER, + "confirmed": true, }), noTeam: createNAccounts(10, { "accountType": Constants.HACKER @@ -131,7 +132,8 @@ let staffAccounts = { let sponsorT1Accounts = { new: createNAccounts(5, { - "accountType": Constants.SPONSOR_T1 + "accountType": Constants.SPONSOR_T1, + "confirmed": false, }), stored: createNAccounts(5, { "accountType": Constants.SPONSOR_T1 @@ -206,7 +208,7 @@ const waitlistedHacker0 = { "firstName": "abcd", "lastName": "defg3", "pronoun": "They/Them", - "email": "abc.def7@blahblah.com", + "email": "waitlisted1@blahblah.com", "password": "probsShouldBeHashed2", "dietaryRestrictions": ["vegetarian"], "shirtSize": "M", @@ -222,7 +224,7 @@ const NonConfirmedAccount1 = { "firstName": "LMAO", "lastName": "ROFL", "pronoun": "Ey/Em", - "email": "abc.def6@blahblah.com", + "email": "notconfirmed1@blahblah.com", "password": "probsShouldBeHashed5", "dietaryRestrictions": ["something1", "something2"], "shirtSize": "XXL", @@ -246,7 +248,8 @@ const NonConfirmedAccount2 = { const NonConfirmedAccount3 = createAccount({ "confirmed": false, - "accountType": Constants.HACKER + "accountType": Constants.HACKER, + "email": "notconfirmed3@blahblah.com" }); const extraAccounts = [waitlistedHacker0, NonConfirmedAccount1, NonConfirmedAccount2, NonConfirmedAccount3]; diff --git a/tests/util/accountConfirmation.test.util.js b/tests/util/accountConfirmation.test.util.js index de005027..acae130f 100644 --- a/tests/util/accountConfirmation.test.util.js +++ b/tests/util/accountConfirmation.test.util.js @@ -16,9 +16,9 @@ const logger = require("../../services/logger.service"); const HackerConfirmation = { "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.hackerAccounts.stored.noTeam[0], + "accountId": Util.Account.NonConfirmedAccount1._id, "accountType": Constants.HACKER, - "email": Util.Account.hackerAccounts.stored.noTeam[0].email + "email": Util.Account.NonConfirmedAccount1.email }; const HackerConfirmation2 = { diff --git a/tests/util/resetPassword.test.util.js b/tests/util/resetPassword.test.util.js index 8a1bc1a4..a5008466 100644 --- a/tests/util/resetPassword.test.util.js +++ b/tests/util/resetPassword.test.util.js @@ -14,7 +14,7 @@ const logger = require("../../services/logger.service"); const ResetPasswordToken1 = { "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.hackerAccounts.stored.team[0] + "accountId": Util.Account.hackerAccounts.stored.team[0]._id }; const ResetToken = Services.resetPassword.generateToken(ResetPasswordToken1._id, ResetPasswordToken1.accountId); From fa51879dec9ed68064e44c6e64ade14fbaa5758c Mon Sep 17 00:00:00 2001 From: Richard Zhang Date: Mon, 14 Jan 2019 18:12:33 -0500 Subject: [PATCH 07/11] Fix hacker --- tests/hacker.test.js | 336 ++++++++++++++-------------- tests/setup.spec.js | 18 ++ tests/util/account.test.util.js | 40 ++-- tests/util/hacker.test.util.js | 19 +- tests/util/roleBinding.test.util.js | 26 ++- 5 files changed, 246 insertions(+), 193 deletions(-) diff --git a/tests/hacker.test.js b/tests/hacker.test.js index 8d1def35..9157b99d 100644 --- a/tests/hacker.test.js +++ b/tests/hacker.test.js @@ -41,171 +41,171 @@ const duplicateAccountLinkHacker0 = util.hacker.duplicateAccountLinkHacker0; const invalidHacker1 = util.hacker.invalidHacker1; -describe("GET hacker", function () { - // fail on authentication - it("should fail to list a hacker's information on /api/hacker/:id GET due to authentication", function (done) { - chai.request(server.app) - .get(`/api/hacker/` + TeamHacker0._id) - .end(function (err, res) { - res.should.have.status(401); - res.should.be.json; - res.body.should.have.property("message"); - res.body.message.should.equal(Constants.Error.AUTH_401_MESSAGE); - done(); - }); - }); - - // success case - it("should list the user's hacker info on /api/hacker/self GET", function (done) { - util.auth.login(agent, teamHackerAccount0, (error) => { - if (error) { - agent.close(); - return done(error); - } - return agent - .get("/api/hacker/self") - .end(function (err, res) { - if (err) { - return done(err); - } - res.should.have.status(200); - res.should.be.json; - res.body.should.have.property("message"); - res.body.message.should.equal(Constants.Success.HACKER_READ); - res.body.should.have.property("data"); - - let hacker = new Hacker(TeamHacker0); - chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); - done(); - }); - }); - }); - - // fail case due to wrong account type - it("should fail to list the hacker info of an admin due to wrong account type /api/account/self GET", function (done) { - util.auth.login(agent, Admin0, (error) => { - if (error) { - agent.close(); - return done(error); - } - return agent - .get("/api/hacker/self") - .end(function (err, res) { - res.should.have.status(409); - res.should.be.json; - res.body.should.have.property("message"); - res.body.message.should.equal(Constants.Error.ACCOUNT_TYPE_409_MESSAGE); - done(); - }); - }); - }); - - // succeed on admin case - it("should list a hacker's information using admin power on /api/hacker/:id GET", function (done) { - util.auth.login(agent, Admin0, (error) => { - if (error) { - agent.close(); - return done(error); - } - return agent - .get(`/api/hacker/${TeamHacker0._id}`) - // does not have password because of to stripped json - .end(function (err, res) { - if (err) { - return done(err); - } - res.should.have.status(200); - res.should.be.json; - res.body.should.have.property("message"); - res.body.message.should.equal(Constants.Success.HACKER_READ); - res.body.should.have.property("data"); - - let hacker = new Hacker(TeamHacker0); - chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); - - done(); - }); - }); - }); - - // succeed on :self case - it("should list the user's hacker information on /api/hacker/:id GET", function (done) { - util.auth.login(agent, teamHackerAccount0, (error) => { - if (error) { - agent.close(); - return done(error); - } - return agent - .get(`/api/hacker/${TeamHacker0._id}`) - // does not have password because of to stripped json - .end(function (err, res) { - if (err) { - return done(err); - } - res.should.have.status(200); - res.should.be.json; - res.body.should.have.property("message"); - res.body.message.should.equal(Constants.Success.HACKER_READ); - res.body.should.have.property("data"); - - let hacker = new Hacker(TeamHacker0); - - chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); - - done(); - }); - }); - }); - - // fail due to lack of authorization - it("should fail to list a hacker information due to lack of authorization on /api/hacker/:id GET", function (done) { - util.auth.login(agent, noTeamHackerAccount0, (error) => { - if (error) { - agent.close(); - return done(error); - } - return agent - .get(`/api/hacker/${TeamHacker0._id}`) - // does not have password because of to stripped json - .end(function (err, res) { - if (err) { - return done(err); - } - res.should.have.status(403); - res.should.be.json; - res.body.should.have.property("message"); - res.body.message.should.equal(Constants.Error.AUTH_403_MESSAGE); - res.body.should.have.property("data"); - - done(); - }); - }); - }); - - // fail due to lack of hacker - it("should fail to list an invalid hacker /api/hacker/:id GET", function (done) { - util.auth.login(agent, Admin0, (error) => { - if (error) { - agent.close(); - return done(error); - } - return agent - .get(`/api/hacker/${invalidHacker1._id}`) - .end(function (err, res) { - if (err) { - return done(err); - } - res.should.have.status(404); - res.should.be.json; - res.body.should.have.property("message"); - res.body.message.should.equal(Constants.Error.HACKER_404_MESSAGE); - res.body.should.have.property("data"); - - done(); - }); - }); - }); -}); +// describe("GET hacker", function () { +// // fail on authentication +// it("should fail to list a hacker's information on /api/hacker/:id GET due to authentication", function (done) { +// chai.request(server.app) +// .get(`/api/hacker/` + TeamHacker0._id) +// .end(function (err, res) { +// res.should.have.status(401); +// res.should.be.json; +// res.body.should.have.property("message"); +// res.body.message.should.equal(Constants.Error.AUTH_401_MESSAGE); +// done(); +// }); +// }); + +// // success case +// it("should list the user's hacker info on /api/hacker/self GET", function (done) { +// util.auth.login(agent, teamHackerAccount0, (error) => { +// if (error) { +// agent.close(); +// return done(error); +// } +// return agent +// .get("/api/hacker/self") +// .end(function (err, res) { +// if (err) { +// return done(err); +// } +// res.should.have.status(200); +// res.should.be.json; +// res.body.should.have.property("message"); +// res.body.message.should.equal(Constants.Success.HACKER_READ); +// res.body.should.have.property("data"); + +// let hacker = new Hacker(TeamHacker0); +// chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); +// done(); +// }); +// }); +// }); + +// // fail case due to wrong account type +// it("should fail to list the hacker info of an admin due to wrong account type /api/account/self GET", function (done) { +// util.auth.login(agent, Admin0, (error) => { +// if (error) { +// agent.close(); +// return done(error); +// } +// return agent +// .get("/api/hacker/self") +// .end(function (err, res) { +// res.should.have.status(409); +// res.should.be.json; +// res.body.should.have.property("message"); +// res.body.message.should.equal(Constants.Error.ACCOUNT_TYPE_409_MESSAGE); +// done(); +// }); +// }); +// }); + +// // succeed on admin case +// it("should list a hacker's information using admin power on /api/hacker/:id GET", function (done) { +// util.auth.login(agent, Admin0, (error) => { +// if (error) { +// agent.close(); +// return done(error); +// } +// return agent +// .get(`/api/hacker/${TeamHacker0._id}`) +// // does not have password because of to stripped json +// .end(function (err, res) { +// if (err) { +// return done(err); +// } +// res.should.have.status(200); +// res.should.be.json; +// res.body.should.have.property("message"); +// res.body.message.should.equal(Constants.Success.HACKER_READ); +// res.body.should.have.property("data"); + +// let hacker = new Hacker(TeamHacker0); +// chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); + +// done(); +// }); +// }); +// }); + +// // succeed on :self case +// it("should list the user's hacker information on /api/hacker/:id GET", function (done) { +// util.auth.login(agent, teamHackerAccount0, (error) => { +// if (error) { +// agent.close(); +// return done(error); +// } +// return agent +// .get(`/api/hacker/${TeamHacker0._id}`) +// // does not have password because of to stripped json +// .end(function (err, res) { +// if (err) { +// return done(err); +// } +// res.should.have.status(200); +// res.should.be.json; +// res.body.should.have.property("message"); +// res.body.message.should.equal(Constants.Success.HACKER_READ); +// res.body.should.have.property("data"); + +// let hacker = new Hacker(TeamHacker0); + +// chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); + +// done(); +// }); +// }); +// }); + +// // fail due to lack of authorization +// it("should fail to list a hacker information due to lack of authorization on /api/hacker/:id GET", function (done) { +// util.auth.login(agent, noTeamHackerAccount0, (error) => { +// if (error) { +// agent.close(); +// return done(error); +// } +// return agent +// .get(`/api/hacker/${TeamHacker0._id}`) +// // does not have password because of to stripped json +// .end(function (err, res) { +// if (err) { +// return done(err); +// } +// res.should.have.status(403); +// res.should.be.json; +// res.body.should.have.property("message"); +// res.body.message.should.equal(Constants.Error.AUTH_403_MESSAGE); +// res.body.should.have.property("data"); + +// done(); +// }); +// }); +// }); + +// // fail due to lack of hacker +// it("should fail to list an invalid hacker /api/hacker/:id GET", function (done) { +// util.auth.login(agent, Admin0, (error) => { +// if (error) { +// agent.close(); +// return done(error); +// } +// return agent +// .get(`/api/hacker/${invalidHacker1._id}`) +// .end(function (err, res) { +// if (err) { +// return done(err); +// } +// res.should.have.status(404); +// res.should.be.json; +// res.body.should.have.property("message"); +// res.body.message.should.equal(Constants.Error.HACKER_404_MESSAGE); +// res.body.should.have.property("data"); + +// done(); +// }); +// }); +// }); +// }); describe("POST create hacker", function () { // fail on authentication @@ -267,7 +267,7 @@ describe("POST create hacker", function () { return agent .post(`/api/hacker/`) .type("application/json") - .send(newHacker1) + .send(newHacker0) .end(function (err, res) { res.should.have.status(200); res.should.be.json; @@ -554,7 +554,7 @@ describe("PATCH update one hacker", function () { return done(error); } return agent - .patch(`/api/hacker/${noTeamHacker0._id}`) + .patch(`/api/hacker/${TeamHacker0._id}`) .type("application/json") .send({ gender: "Other" @@ -665,7 +665,7 @@ describe("PATCH update one hacker", function () { return done(error); } return agent - .patch(`/api/hacker/confirmation/${util.hacker.HackerC._id}`) + .patch(`/api/hacker/confirmation/${util.hacker.waitlistedHacker0._id}`) .type("application/json") .send({ confirm: true diff --git a/tests/setup.spec.js b/tests/setup.spec.js index b747a55d..0a9cff83 100644 --- a/tests/setup.spec.js +++ b/tests/setup.spec.js @@ -64,6 +64,14 @@ async function storeAll() { await Util.Account.storeAll(Util.Account.sponsorT5Accounts.stored); await Util.Account.storeAll(Util.Account.unlinkedAccounts.stored); + await Util.Account.storeAll(Util.Account.hackerAccounts.new); + await Util.Account.storeAll(Util.Account.volunteerAccounts.new); + await Util.Account.storeAll(Util.Account.sponsorT1Accounts.new); + await Util.Account.storeAll(Util.Account.sponsorT2Accounts.new); + await Util.Account.storeAll(Util.Account.sponsorT3Accounts.new); + await Util.Account.storeAll(Util.Account.sponsorT4Accounts.new); + await Util.Account.storeAll(Util.Account.sponsorT5Accounts.new); + await Util.Account.storeAll(Util.Account.extraAccounts); await Util.Hacker.storeAll(Util.Hacker.Hackers); @@ -85,6 +93,16 @@ async function storeAll() { await Util.RoleBinding.storeAll(Util.RoleBinding.SponsorT3RB); await Util.RoleBinding.storeAll(Util.RoleBinding.SponsorT4RB); await Util.RoleBinding.storeAll(Util.RoleBinding.SponsorT5RB); + + await Util.RoleBinding.storeAll(Util.RoleBinding.newHackerRB); + await Util.RoleBinding.storeAll(Util.RoleBinding.newVolunteerRB); + await Util.RoleBinding.storeAll(Util.RoleBinding.newSponsorT1RB); + await Util.RoleBinding.storeAll(Util.RoleBinding.newSponsorT2RB); + await Util.RoleBinding.storeAll(Util.RoleBinding.newSponsorT3RB); + await Util.RoleBinding.storeAll(Util.RoleBinding.newSponsorT4RB); + await Util.RoleBinding.storeAll(Util.RoleBinding.newSponsorT5RB); + + await Util.RoleBinding.storeAll(Util.RoleBinding.unconfirmedAccountRB); } async function dropAll() { diff --git a/tests/util/account.test.util.js b/tests/util/account.test.util.js index a552f72e..4a3c07fd 100644 --- a/tests/util/account.test.util.js +++ b/tests/util/account.test.util.js @@ -97,6 +97,7 @@ function createNAccounts(n, acc = {}) { let hackerAccounts = { new: createNAccounts(10, { "accountType": Constants.HACKER, + "confirmed": true, }), stored: { team: createNAccounts(10, { @@ -104,7 +105,8 @@ let hackerAccounts = { "confirmed": true, }), noTeam: createNAccounts(10, { - "accountType": Constants.HACKER + "accountType": Constants.HACKER, + "confirmed": true, }), }, invalid: createNAccounts(10, { @@ -114,10 +116,12 @@ let hackerAccounts = { let volunteerAccounts = { new: createNAccounts(5, { - "accountType": Constants.VOLUNTEER + "accountType": Constants.VOLUNTEER, + "confirmed": true, }), stored: createNAccounts(5, { - "accountType": Constants.VOLUNTEER + "accountType": Constants.VOLUNTEER, + "confirmed": true, }), invalid: createNAccounts(5, { "accountType": Constants.VOLUNTEER @@ -126,7 +130,8 @@ let volunteerAccounts = { let staffAccounts = { stored: createNAccounts(5, { - "accountType": Constants.STAFF + "accountType": Constants.STAFF, + "confirmed": true, }) }; @@ -136,7 +141,8 @@ let sponsorT1Accounts = { "confirmed": false, }), stored: createNAccounts(5, { - "accountType": Constants.SPONSOR_T1 + "accountType": Constants.SPONSOR_T1, + "confirmed": true, }), invalid: createNAccounts(5, { "accountType": Constants.SPONSOR_T1 @@ -145,10 +151,12 @@ let sponsorT1Accounts = { let sponsorT2Accounts = { new: createNAccounts(5, { - "accountType": Constants.SPONSOR_T2 + "accountType": Constants.SPONSOR_T2, + "confirmed": true, }), stored: createNAccounts(5, { - "accountType": Constants.SPONSOR_T2 + "accountType": Constants.SPONSOR_T2, + "confirmed": true, }), invalid: createNAccounts(5, { "accountType": Constants.SPONSOR_T2 @@ -157,10 +165,12 @@ let sponsorT2Accounts = { let sponsorT3Accounts = { new: createNAccounts(5, { - "accountType": Constants.SPONSOR_T3 + "accountType": Constants.SPONSOR_T3, + "confirmed": true, }), stored: createNAccounts(5, { - "accountType": Constants.SPONSOR_T3 + "accountType": Constants.SPONSOR_T3, + "confirmed": true, }), invalid: createNAccounts(5, { "accountType": Constants.SPONSOR_T3 @@ -169,10 +179,12 @@ let sponsorT3Accounts = { let sponsorT4Accounts = { new: createNAccounts(5, { - "accountType": Constants.SPONSOR_T4 + "accountType": Constants.SPONSOR_T4, + "confirmed": true, }), stored: createNAccounts(5, { - "accountType": Constants.SPONSOR_T4 + "accountType": Constants.SPONSOR_T4, + "confirmed": true, }), invalid: createNAccounts(5, { "accountType": Constants.SPONSOR_T4 @@ -181,10 +193,12 @@ let sponsorT4Accounts = { let sponsorT5Accounts = { new: createNAccounts(5, { - "accountType": Constants.SPONSOR_T5 + "accountType": Constants.SPONSOR_T5, + "confirmed": true, }), stored: createNAccounts(5, { - "accountType": Constants.SPONSOR_T5 + "accountType": Constants.SPONSOR_T5, + "confirmed": true, }), invalid: createNAccounts(5, { "accountType": Constants.SPONSOR_T5 diff --git a/tests/util/hacker.test.util.js b/tests/util/hacker.test.util.js index 8485427d..85fb2d78 100644 --- a/tests/util/hacker.test.util.js +++ b/tests/util/hacker.test.util.js @@ -12,7 +12,7 @@ const logger = require("../../services/logger.service"); const TeamHacker0 = { "_id": Constants.MongoId.hackerAId, - "accountId": Util.Account.hackerAccounts.stored.team[0], + "accountId": Util.Account.hackerAccounts.stored.team[0]._id, "status": "Confirmed", "school": "University of Blah", "degree": "Masters", @@ -59,7 +59,7 @@ const TeamHacker1 = { "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": "European", + "ethnicity": ["European"], "major": "EE", "graduationYear": 2019, "codeOfConduct": true, @@ -87,7 +87,7 @@ const TeamHacker2 = { "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": "European", + "ethnicity": ["European"], "major": "EE", "graduationYear": 2019, "codeOfConduct": true, @@ -115,7 +115,7 @@ const TeamHacker3 = { "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": "European", + "ethnicity": ["European"], "major": "EE", "graduationYear": 2019, "codeOfConduct": true, @@ -143,7 +143,7 @@ const TeamHacker4 = { "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": "European", + "ethnicity": ["European"], "major": "EE", "graduationYear": 2019, "codeOfConduct": true, @@ -275,7 +275,7 @@ const invalidHacker1 = { const duplicateAccountLinkHacker0 = { "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.hackerAccounts.stored.team[0], + "accountId": Util.Account.hackerAccounts.stored.team[0]._id, "status": "Applied", "school": "University of Blah", "degree": "Undergraduate", @@ -321,7 +321,7 @@ const waitlistedHacker0 = { "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": "European", + "ethnicity": ["European"], "major": "EE", "graduationYear": 2019, "codeOfConduct": true, @@ -349,7 +349,7 @@ const unconfirmedAccountHacker0 = { "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": "European", + "ethnicity": ["European"], "major": "EE", "graduationYear": 2019, "codeOfConduct": true, @@ -364,9 +364,6 @@ const Hackers = [ NoTeamHacker0, - invalidHacker0, - invalidHacker1, - duplicateAccountLinkHacker0, waitlistedHacker0 ]; diff --git a/tests/util/roleBinding.test.util.js b/tests/util/roleBinding.test.util.js index d6311c8b..b23b17b3 100644 --- a/tests/util/roleBinding.test.util.js +++ b/tests/util/roleBinding.test.util.js @@ -9,7 +9,7 @@ const Constants = { }; const logger = require("../../services/logger.service"); -function createRoleBinding(accountId, accountType, specificRoles = []) { +function createRoleBinding(accountId, accountType = null, specificRoles = []) { let roleBinding = { accountId: accountId, roles: [Constants.Role.accountRole] @@ -92,6 +92,20 @@ const SponsorT3RB = createRoleBindings(Util.Account.sponsorT3Accounts.stored); const SponsorT4RB = createRoleBindings(Util.Account.sponsorT4Accounts.stored); const SponsorT5RB = createRoleBindings(Util.Account.sponsorT5Accounts.stored); +const newHackerRB = createRoleBindings(Util.Account.hackerAccounts.new); +const newVolunteerRB = createRoleBindings(Util.Account.volunteerAccounts.new); +const newSponsorT1RB = createRoleBindings(Util.Account.sponsorT1Accounts.new); +const newSponsorT2RB = createRoleBindings(Util.Account.sponsorT2Accounts.new); +const newSponsorT3RB = createRoleBindings(Util.Account.sponsorT3Accounts.new); +const newSponsorT4RB = createRoleBindings(Util.Account.sponsorT4Accounts.new); +const newSponsorT5RB = createRoleBindings(Util.Account.sponsorT5Accounts.new); + +const unconfirmedAccountRB = [ + createRoleBinding(Util.Account.NonConfirmedAccount1._id), + createRoleBinding(Util.Account.NonConfirmedAccount2._id), + createRoleBinding(Util.Account.NonConfirmedAccount3._id), +]; + module.exports = { TeamHackerRB: TeamHackerRB, NoTeamHackerRB: NoTeamHackerRB, @@ -103,6 +117,16 @@ module.exports = { SponsorT4RB: SponsorT4RB, SponsorT5RB: SponsorT5RB, + newHackerRB: newHackerRB, + newVolunteerRB: newVolunteerRB, + newSponsorT1RB: newSponsorT1RB, + newSponsorT2RB: newSponsorT2RB, + newSponsorT3RB: newSponsorT3RB, + newSponsorT4RB: newSponsorT4RB, + newSponsorT5RB: newSponsorT5RB, + + unconfirmedAccountRB: unconfirmedAccountRB, + storeAll: storeAll, dropAll: dropAll, }; \ No newline at end of file From 16a962abe6a6b587e41328619d82b097f0f8ff15 Mon Sep 17 00:00:00 2001 From: Richard Zhang Date: Mon, 14 Jan 2019 18:31:04 -0500 Subject: [PATCH 08/11] Fix sponsor and volunteer --- tests/hacker.test.js | 330 ++++++++++++++++++++-------------------- tests/sponsor.test.js | 23 +-- tests/volunteer.test.js | 2 +- 3 files changed, 178 insertions(+), 177 deletions(-) diff --git a/tests/hacker.test.js b/tests/hacker.test.js index 9157b99d..7a01957e 100644 --- a/tests/hacker.test.js +++ b/tests/hacker.test.js @@ -41,171 +41,171 @@ const duplicateAccountLinkHacker0 = util.hacker.duplicateAccountLinkHacker0; const invalidHacker1 = util.hacker.invalidHacker1; -// describe("GET hacker", function () { -// // fail on authentication -// it("should fail to list a hacker's information on /api/hacker/:id GET due to authentication", function (done) { -// chai.request(server.app) -// .get(`/api/hacker/` + TeamHacker0._id) -// .end(function (err, res) { -// res.should.have.status(401); -// res.should.be.json; -// res.body.should.have.property("message"); -// res.body.message.should.equal(Constants.Error.AUTH_401_MESSAGE); -// done(); -// }); -// }); - -// // success case -// it("should list the user's hacker info on /api/hacker/self GET", function (done) { -// util.auth.login(agent, teamHackerAccount0, (error) => { -// if (error) { -// agent.close(); -// return done(error); -// } -// return agent -// .get("/api/hacker/self") -// .end(function (err, res) { -// if (err) { -// return done(err); -// } -// res.should.have.status(200); -// res.should.be.json; -// res.body.should.have.property("message"); -// res.body.message.should.equal(Constants.Success.HACKER_READ); -// res.body.should.have.property("data"); - -// let hacker = new Hacker(TeamHacker0); -// chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); -// done(); -// }); -// }); -// }); - -// // fail case due to wrong account type -// it("should fail to list the hacker info of an admin due to wrong account type /api/account/self GET", function (done) { -// util.auth.login(agent, Admin0, (error) => { -// if (error) { -// agent.close(); -// return done(error); -// } -// return agent -// .get("/api/hacker/self") -// .end(function (err, res) { -// res.should.have.status(409); -// res.should.be.json; -// res.body.should.have.property("message"); -// res.body.message.should.equal(Constants.Error.ACCOUNT_TYPE_409_MESSAGE); -// done(); -// }); -// }); -// }); - -// // succeed on admin case -// it("should list a hacker's information using admin power on /api/hacker/:id GET", function (done) { -// util.auth.login(agent, Admin0, (error) => { -// if (error) { -// agent.close(); -// return done(error); -// } -// return agent -// .get(`/api/hacker/${TeamHacker0._id}`) -// // does not have password because of to stripped json -// .end(function (err, res) { -// if (err) { -// return done(err); -// } -// res.should.have.status(200); -// res.should.be.json; -// res.body.should.have.property("message"); -// res.body.message.should.equal(Constants.Success.HACKER_READ); -// res.body.should.have.property("data"); - -// let hacker = new Hacker(TeamHacker0); -// chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); - -// done(); -// }); -// }); -// }); - -// // succeed on :self case -// it("should list the user's hacker information on /api/hacker/:id GET", function (done) { -// util.auth.login(agent, teamHackerAccount0, (error) => { -// if (error) { -// agent.close(); -// return done(error); -// } -// return agent -// .get(`/api/hacker/${TeamHacker0._id}`) -// // does not have password because of to stripped json -// .end(function (err, res) { -// if (err) { -// return done(err); -// } -// res.should.have.status(200); -// res.should.be.json; -// res.body.should.have.property("message"); -// res.body.message.should.equal(Constants.Success.HACKER_READ); -// res.body.should.have.property("data"); - -// let hacker = new Hacker(TeamHacker0); - -// chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); - -// done(); -// }); -// }); -// }); - -// // fail due to lack of authorization -// it("should fail to list a hacker information due to lack of authorization on /api/hacker/:id GET", function (done) { -// util.auth.login(agent, noTeamHackerAccount0, (error) => { -// if (error) { -// agent.close(); -// return done(error); -// } -// return agent -// .get(`/api/hacker/${TeamHacker0._id}`) -// // does not have password because of to stripped json -// .end(function (err, res) { -// if (err) { -// return done(err); -// } -// res.should.have.status(403); -// res.should.be.json; -// res.body.should.have.property("message"); -// res.body.message.should.equal(Constants.Error.AUTH_403_MESSAGE); -// res.body.should.have.property("data"); - -// done(); -// }); -// }); -// }); - -// // fail due to lack of hacker -// it("should fail to list an invalid hacker /api/hacker/:id GET", function (done) { -// util.auth.login(agent, Admin0, (error) => { -// if (error) { -// agent.close(); -// return done(error); -// } -// return agent -// .get(`/api/hacker/${invalidHacker1._id}`) -// .end(function (err, res) { -// if (err) { -// return done(err); -// } -// res.should.have.status(404); -// res.should.be.json; -// res.body.should.have.property("message"); -// res.body.message.should.equal(Constants.Error.HACKER_404_MESSAGE); -// res.body.should.have.property("data"); - -// done(); -// }); -// }); -// }); -// }); +describe("GET hacker", function () { + // fail on authentication + it("should fail to list a hacker's information on /api/hacker/:id GET due to authentication", function (done) { + chai.request(server.app) + .get(`/api/hacker/` + TeamHacker0._id) + .end(function (err, res) { + res.should.have.status(401); + res.should.be.json; + res.body.should.have.property("message"); + res.body.message.should.equal(Constants.Error.AUTH_401_MESSAGE); + done(); + }); + }); + + // success case + it("should list the user's hacker info on /api/hacker/self GET", function (done) { + util.auth.login(agent, teamHackerAccount0, (error) => { + if (error) { + agent.close(); + return done(error); + } + return agent + .get("/api/hacker/self") + .end(function (err, res) { + if (err) { + return done(err); + } + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property("message"); + res.body.message.should.equal(Constants.Success.HACKER_READ); + res.body.should.have.property("data"); + + let hacker = new Hacker(TeamHacker0); + chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); + done(); + }); + }); + }); + + // fail case due to wrong account type + it("should fail to list the hacker info of an admin due to wrong account type /api/account/self GET", function (done) { + util.auth.login(agent, Admin0, (error) => { + if (error) { + agent.close(); + return done(error); + } + return agent + .get("/api/hacker/self") + .end(function (err, res) { + res.should.have.status(409); + res.should.be.json; + res.body.should.have.property("message"); + res.body.message.should.equal(Constants.Error.ACCOUNT_TYPE_409_MESSAGE); + done(); + }); + }); + }); + + // succeed on admin case + it("should list a hacker's information using admin power on /api/hacker/:id GET", function (done) { + util.auth.login(agent, Admin0, (error) => { + if (error) { + agent.close(); + return done(error); + } + return agent + .get(`/api/hacker/${TeamHacker0._id}`) + // does not have password because of to stripped json + .end(function (err, res) { + if (err) { + return done(err); + } + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property("message"); + res.body.message.should.equal(Constants.Success.HACKER_READ); + res.body.should.have.property("data"); + + let hacker = new Hacker(TeamHacker0); + chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); + + done(); + }); + }); + }); + + // succeed on :self case + it("should list the user's hacker information on /api/hacker/:id GET", function (done) { + util.auth.login(agent, teamHackerAccount0, (error) => { + if (error) { + agent.close(); + return done(error); + } + return agent + .get(`/api/hacker/${TeamHacker0._id}`) + // does not have password because of to stripped json + .end(function (err, res) { + if (err) { + return done(err); + } + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property("message"); + res.body.message.should.equal(Constants.Success.HACKER_READ); + res.body.should.have.property("data"); + + let hacker = new Hacker(TeamHacker0); + + chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); + + done(); + }); + }); + }); + + // fail due to lack of authorization + it("should fail to list a hacker information due to lack of authorization on /api/hacker/:id GET", function (done) { + util.auth.login(agent, noTeamHackerAccount0, (error) => { + if (error) { + agent.close(); + return done(error); + } + return agent + .get(`/api/hacker/${TeamHacker0._id}`) + // does not have password because of to stripped json + .end(function (err, res) { + if (err) { + return done(err); + } + res.should.have.status(403); + res.should.be.json; + res.body.should.have.property("message"); + res.body.message.should.equal(Constants.Error.AUTH_403_MESSAGE); + res.body.should.have.property("data"); + + done(); + }); + }); + }); + + // fail due to lack of hacker + it("should fail to list an invalid hacker /api/hacker/:id GET", function (done) { + util.auth.login(agent, Admin0, (error) => { + if (error) { + agent.close(); + return done(error); + } + return agent + .get(`/api/hacker/${invalidHacker1._id}`) + .end(function (err, res) { + if (err) { + return done(err); + } + res.should.have.status(404); + res.should.be.json; + res.body.should.have.property("message"); + res.body.message.should.equal(Constants.Error.HACKER_404_MESSAGE); + res.body.should.have.property("data"); + + done(); + }); + }); + }); +}); describe("POST create hacker", function () { // fail on authentication diff --git a/tests/sponsor.test.js b/tests/sponsor.test.js index cc4a2e54..dab90b9d 100644 --- a/tests/sponsor.test.js +++ b/tests/sponsor.test.js @@ -18,14 +18,13 @@ const util = { account: require("./util/account.test.util"), }; -const Admin0 = util.account.staffAccounts[0]; +const Admin0 = util.account.staffAccounts.stored[0]; const HackerAccount0 = util.account.hackerAccounts.stored.team[0]; const T1SponsorAccount0 = util.account.sponsorT1Accounts.stored[0]; const newT2SponsorAccount0 = util.account.sponsorT2Accounts.new[0]; const T1Sponsor0 = util.sponsor.T1Sponsor0; const newT2Sponsor0 = util.sponsor.newT2Sponsor0; - let duplicateSponsor = util.sponsor.duplicateAccountLinkSponsor1; @@ -64,7 +63,8 @@ describe("GET user sponsor", function () { res.body.should.have.property("data"); res.body.data.should.be.a("object"); - chai.expect(res.body.data).to.deep.equal(sponsorT1Account0); + let sponsor = new Sponsor(T1Sponsor0); + chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(sponsor.toJSON())); done(); }); }); @@ -72,7 +72,7 @@ describe("GET user sponsor", function () { // regular user access success it("should succeed to list a user's sponsor info on /api/sponsor/:id GET", function (done) { - util.auth.login(agent, sponsorT1Account0, (error) => { + util.auth.login(agent, T1SponsorAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -90,7 +90,8 @@ describe("GET user sponsor", function () { res.body.should.have.property("data"); res.body.data.should.be.a("object"); - chai.expect(res.body.data).to.deep.equal(T1SponsorAccount0); + let sponsor = new Sponsor(T1Sponsor0); + chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(sponsor.toJSON())); done(); }); }); @@ -104,7 +105,7 @@ describe("GET user sponsor", function () { return done(error); } return agent - .get(`/api/sponsor/${util.sponsor.Sponsor1._id}`) + .get(`/api/sponsor/${util.sponsor.T1Sponsor0._id}`) .end(function (err, res) { if (err) { return done(err); @@ -163,7 +164,7 @@ describe("POST create sponsor", function () { // success case with self caes - there is no admin case it("should SUCCEED and create a new sponsor", function (done) { - util.auth.login(agent, newSponsorAccount, (error) => { + util.auth.login(agent, newT2SponsorAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -171,7 +172,7 @@ describe("POST create sponsor", function () { return agent .post(`/api/sponsor/`) .type("application/json") - .send(newSponsor) + .send(newT2Sponsor0) .end(function (err, res) { res.should.have.status(200); res.should.be.json; @@ -180,7 +181,7 @@ describe("POST create sponsor", function () { res.body.should.have.property("data"); // deleting id because that was generated, and not part of original data - const sponsor = (new Sponsor(newSponsor)).toJSON(); + const sponsor = (new Sponsor(newT2Sponsor0)).toJSON(); delete res.body.data.id; delete sponsor.id; chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(sponsor)); @@ -191,7 +192,7 @@ describe("POST create sponsor", function () { // fail case - duplicate accountId it("should fail to create a sponsor due to duplicate accountId", function (done) { - util.auth.login(agent, sponsorT1Account0, (error) => { + util.auth.login(agent, T1SponsorAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -220,7 +221,7 @@ describe("POST create sponsor", function () { return agent .post(`/api/sponsor/`) .type("application/json") - .send(newSponsor) + .send(newT2Sponsor0) .end(function (err, res) { res.should.have.status(403); res.should.be.json; diff --git a/tests/volunteer.test.js b/tests/volunteer.test.js index 7f3574dc..bde8924c 100644 --- a/tests/volunteer.test.js +++ b/tests/volunteer.test.js @@ -17,7 +17,7 @@ const util = { auth: require("./util/auth.test.util") }; -const Admin0 = util.account.Admin0; +const Admin0 = util.account.staffAccounts.stored[0]; const HackerAccount0 = util.account.hackerAccounts.stored.team[0]; const VolunteerAccount0 = util.account.volunteerAccounts.stored[0]; From 3b8532f5c9c5a4e09b4a4edc54205912d6422907 Mon Sep 17 00:00:00 2001 From: Richard Zhang Date: Mon, 14 Jan 2019 23:12:47 -0500 Subject: [PATCH 09/11] Make final changes --- tests/hacker.test.js | 2 +- tests/role.test.js | 2 +- tests/search.service.spec.js | 4 +- tests/setup.spec.js | 64 ++++-------------- tests/team.test.js | 28 ++++---- tests/util/account.test.util.js | 28 +++++++- tests/util/accountConfirmation.test.util.js | 6 +- tests/util/bus.test.util.js | 6 +- tests/util/hacker.test.util.js | 8 ++- tests/util/resetPassword.test.util.js | 6 +- tests/util/role.test.util.js | 11 +++- tests/util/roleBinding.test.util.js | 73 ++++++++++++++------- tests/util/sponsor.test.util.js | 6 +- tests/util/staff.test.util.js | 6 +- tests/util/team.test.util.js | 6 +- tests/util/volunteer.test.util.js | 6 +- 16 files changed, 153 insertions(+), 109 deletions(-) diff --git a/tests/hacker.test.js b/tests/hacker.test.js index 7a01957e..45bb1511 100644 --- a/tests/hacker.test.js +++ b/tests/hacker.test.js @@ -634,7 +634,7 @@ describe("PATCH update one hacker", function () { return done(error); } return agent - .patch(`/api/hacker/confirmation/${noTeamHacker0._id}`) + .patch(`/api/hacker/confirmation/${TeamHacker0._id}`) .type("application/json") .send({ confirm: false diff --git a/tests/role.test.js b/tests/role.test.js index 34796c8e..b5444098 100644 --- a/tests/role.test.js +++ b/tests/role.test.js @@ -18,7 +18,7 @@ const Constants = { Success: require("../constants/success.constant"), }; -const Admin0 = util.account.staffAccounts[0]; +const Admin0 = util.account.staffAccounts.stored[0]; const Hacker0 = util.account.hackerAccounts.stored.team[0]; describe("POST create role", function () { diff --git a/tests/search.service.spec.js b/tests/search.service.spec.js index 356a0b32..78bf1d59 100644 --- a/tests/search.service.spec.js +++ b/tests/search.service.spec.js @@ -38,7 +38,7 @@ const badQuery = [{ value: "passowrd" }]; -const Admin0 = util.account.staffAccounts[0]; +const Admin0 = util.account.staffAccounts.stored[0]; const noTeamHackerAccount0 = util.account.hackerAccounts.stored.noTeam[0]; @@ -164,7 +164,7 @@ describe("Searching for hackers", function () { }); }); }); - it("Should throw an error because out of a fake model", function (done) { + it("Should throw an error because of a fake model", function (done) { util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); diff --git a/tests/setup.spec.js b/tests/setup.spec.js index 0a9cff83..a369e2e1 100644 --- a/tests/setup.spec.js +++ b/tests/setup.spec.js @@ -15,9 +15,6 @@ const Util = { AccountConfirmation: require("./util/accountConfirmation.test.util"), ResetPassword: require("./util/resetPassword.test.util.js") }; -const Constants = { - Role: require("../constants/role.constant"), -}; const logger = require("../services/logger.service"); //make sure that we are connected to the database @@ -53,56 +50,17 @@ afterEach(function (done) { }); }); async function storeAll() { - await Util.Account.storeAll(Util.Account.hackerAccounts.stored.team); - await Util.Account.storeAll(Util.Account.hackerAccounts.stored.noTeam); - await Util.Account.storeAll(Util.Account.volunteerAccounts.stored); - await Util.Account.storeAll(Util.Account.staffAccounts.stored); - await Util.Account.storeAll(Util.Account.sponsorT1Accounts.stored); - await Util.Account.storeAll(Util.Account.sponsorT2Accounts.stored); - await Util.Account.storeAll(Util.Account.sponsorT3Accounts.stored); - await Util.Account.storeAll(Util.Account.sponsorT4Accounts.stored); - await Util.Account.storeAll(Util.Account.sponsorT5Accounts.stored); - await Util.Account.storeAll(Util.Account.unlinkedAccounts.stored); - - await Util.Account.storeAll(Util.Account.hackerAccounts.new); - await Util.Account.storeAll(Util.Account.volunteerAccounts.new); - await Util.Account.storeAll(Util.Account.sponsorT1Accounts.new); - await Util.Account.storeAll(Util.Account.sponsorT2Accounts.new); - await Util.Account.storeAll(Util.Account.sponsorT3Accounts.new); - await Util.Account.storeAll(Util.Account.sponsorT4Accounts.new); - await Util.Account.storeAll(Util.Account.sponsorT5Accounts.new); - - await Util.Account.storeAll(Util.Account.extraAccounts); - - await Util.Hacker.storeAll(Util.Hacker.Hackers); - await Util.Sponsor.storeAll(Util.Sponsor.Sponsors); - await Util.Team.storeAll(Util.Team.Teams); - await Util.Staff.storeAll(Util.Staff.Staffs); - await Util.AccountConfirmation.storeAll(Util.AccountConfirmation.AccountConfirmationTokens); - await Util.ResetPassword.storeAll(Util.ResetPassword.ResetPasswords); - await Util.Bus.storeAll(Util.Bus.Busses); - await Util.Volunteer.storeAll(Util.Volunteer.Volunteers); - await Util.Role.storeAll(Constants.Role.allRolesArray); - - await Util.RoleBinding.storeAll(Util.RoleBinding.TeamHackerRB); - await Util.RoleBinding.storeAll(Util.RoleBinding.NoTeamHackerRB); - await Util.RoleBinding.storeAll(Util.RoleBinding.VolunteerRB); - await Util.RoleBinding.storeAll(Util.RoleBinding.StaffRB); - await Util.RoleBinding.storeAll(Util.RoleBinding.SponsorT1RB); - await Util.RoleBinding.storeAll(Util.RoleBinding.SponsorT2RB); - await Util.RoleBinding.storeAll(Util.RoleBinding.SponsorT3RB); - await Util.RoleBinding.storeAll(Util.RoleBinding.SponsorT4RB); - await Util.RoleBinding.storeAll(Util.RoleBinding.SponsorT5RB); - - await Util.RoleBinding.storeAll(Util.RoleBinding.newHackerRB); - await Util.RoleBinding.storeAll(Util.RoleBinding.newVolunteerRB); - await Util.RoleBinding.storeAll(Util.RoleBinding.newSponsorT1RB); - await Util.RoleBinding.storeAll(Util.RoleBinding.newSponsorT2RB); - await Util.RoleBinding.storeAll(Util.RoleBinding.newSponsorT3RB); - await Util.RoleBinding.storeAll(Util.RoleBinding.newSponsorT4RB); - await Util.RoleBinding.storeAll(Util.RoleBinding.newSponsorT5RB); - - await Util.RoleBinding.storeAll(Util.RoleBinding.unconfirmedAccountRB); + await Util.Account.storeAll(); + await Util.Hacker.storeAll(); + await Util.Sponsor.storeAll(); + await Util.Team.storeAll(); + await Util.Staff.storeAll(); + await Util.AccountConfirmation.storeAll(); + await Util.ResetPassword.storeAll(); + await Util.Bus.storeAll(); + await Util.Volunteer.storeAll(); + await Util.Role.storeAll(); + await Util.RoleBinding.storeAll(); } async function dropAll() { diff --git a/tests/team.test.js b/tests/team.test.js index 95462362..3c37cb95 100644 --- a/tests/team.test.js +++ b/tests/team.test.js @@ -20,7 +20,7 @@ const Constants = { const agent = chai.request.agent(server.app); -const Admin0 = util.account.staffAccounts[0]; +const Admin0 = util.account.staffAccounts.stored[0]; const teamHackerAccount0 = util.account.hackerAccounts.stored.team[0]; const noTeamHackerAccount0 = util.account.hackerAccounts.stored.noTeam[0]; const sponsorT1Account0 = util.account.sponsorT1Accounts.stored[0]; @@ -77,14 +77,14 @@ describe("GET team", function () { res.body.data.should.have.property("team"); res.body.data.team.name.should.equal("FullTeam"); res.body.data.should.have.property("members"); - res.body.data.members[0].firstName.should.equal("abcd"); - res.body.data.members[0].lastName.should.equal("defg4"); - res.body.data.members[1].firstName.should.equal("abcd"); - res.body.data.members[1].lastName.should.equal("defg5"); - res.body.data.members[2].firstName.should.equal("abcd"); - res.body.data.members[2].lastName.should.equal("defg6"); - res.body.data.members[3].firstName.should.equal("abcd"); - res.body.data.members[3].lastName.should.equal("defg7"); + res.body.data.members[0].firstName.should.equal(util.account.hackerAccounts.stored.team[1].firstName); + res.body.data.members[0].lastName.should.equal(util.account.hackerAccounts.stored.team[1].lastName); + res.body.data.members[1].firstName.should.equal(util.account.hackerAccounts.stored.team[2].firstName); + res.body.data.members[1].lastName.should.equal(util.account.hackerAccounts.stored.team[2].lastName); + res.body.data.members[2].firstName.should.equal(util.account.hackerAccounts.stored.team[3].firstName); + res.body.data.members[2].lastName.should.equal(util.account.hackerAccounts.stored.team[3].lastName); + res.body.data.members[3].firstName.should.equal(util.account.hackerAccounts.stored.team[4].firstName); + res.body.data.members[3].lastName.should.equal(util.account.hackerAccounts.stored.team[4].lastName); done(); }); @@ -368,7 +368,7 @@ describe("PATCH join team", function () { describe("PATCH change team info", function () { it("should FAIL to change a hacker's team information due to invalid authentication", function (done) { chai.request(server.app) - .patch(`/api/team/${util.hacker.HackerF._id}`) + .patch(`/api/team/${util.hacker.TeamHacker4._id}`) .type("application/json") .send({ name: "BronzeTeamASDF", @@ -391,7 +391,7 @@ describe("PATCH change team info", function () { return done(error); } return agent - .patch(`/api/team/${util.hacker.HackerA._id}`) + .patch(`/api/team/${util.hacker.TeamHacker0._id}`) .type("application/json") .send({ name: "SuccessTeamASDF", @@ -408,14 +408,14 @@ describe("PATCH change team info", function () { }); }); - it("should SUCCEED to change the hacker's team information (even though the hackers are different, the team is the same)", function (done) { + it("should SUCCEED to change the hacker's team information", function (done) { util.auth.login(agent, util.account.hackerAccounts.stored.team[1], (error) => { if (error) { agent.close(); return done(error); } return agent - .patch(`/api/team/${util.hacker.HackerD._id}`) + .patch(`/api/team/${util.hacker.TeamHacker1._id}`) .type("application/json") .send({ name: "SuccessTeamASDF", @@ -460,7 +460,7 @@ describe("PATCH change team info", function () { return done(error); } return agent - .patch(`/api/team/${util.hacker.HackerD._id}`) + .patch(`/api/team/${util.hacker.TeamHacker3._id}`) .type("application/json") .send({ name: "SuccessTeamASDF", diff --git a/tests/util/account.test.util.js b/tests/util/account.test.util.js index 4a3c07fd..87b5decd 100644 --- a/tests/util/account.test.util.js +++ b/tests/util/account.test.util.js @@ -207,7 +207,8 @@ let sponsorT5Accounts = { let unlinkedAccounts = { new: [createAccount({ - "accountType": Constants.HACKER + "accountType": Constants.HACKER, + "confirmed": false, })], invalid: [createAccount()], stored: [createAccount({ @@ -297,7 +298,7 @@ function encryptPassword(user) { return encryptedUser; } -function storeAll(attributes) { +function store(attributes) { const acctDocs = []; const acctNames = []; for (var i = 0; i < attributes.length; i++) { @@ -309,6 +310,29 @@ function storeAll(attributes) { return Account.collection.insertMany(acctDocs); } +async function storeAll() { + await store(hackerAccounts.stored.team); + await store(hackerAccounts.stored.noTeam); + await store(volunteerAccounts.stored); + await store(staffAccounts.stored); + await store(sponsorT1Accounts.stored); + await store(sponsorT2Accounts.stored); + await store(sponsorT3Accounts.stored); + await store(sponsorT4Accounts.stored); + await store(sponsorT5Accounts.stored); + await store(unlinkedAccounts.stored); + + await store(hackerAccounts.new); + await store(volunteerAccounts.new); + await store(sponsorT1Accounts.new); + await store(sponsorT2Accounts.new); + await store(sponsorT3Accounts.new); + await store(sponsorT4Accounts.new); + await store(sponsorT5Accounts.new); + + await store(extraAccounts); +} + async function dropAll() { try { await Account.collection.drop(); diff --git a/tests/util/accountConfirmation.test.util.js b/tests/util/accountConfirmation.test.util.js index acae130f..88280d1a 100644 --- a/tests/util/accountConfirmation.test.util.js +++ b/tests/util/accountConfirmation.test.util.js @@ -56,7 +56,7 @@ const AccountConfirmationTokens = [ HackerConfirmation4 ]; -function storeAll(attributes) { +function store(attributes) { const accountConfirmationDocs = []; const accountConfirmationIds = []; for (var i = 0; i < attributes.length; i++) { @@ -66,6 +66,10 @@ function storeAll(attributes) { return AccountConfirmationToken.collection.insertMany(accountConfirmationDocs); } +async function storeAll() { + await store(AccountConfirmationTokens); +} + async function dropAll() { try { await AccountConfirmationToken.collection.drop(); diff --git a/tests/util/bus.test.util.js b/tests/util/bus.test.util.js index 0603ecf1..2e1aa1ab 100644 --- a/tests/util/bus.test.util.js +++ b/tests/util/bus.test.util.js @@ -28,7 +28,7 @@ module.exports = { dropAll: dropAll }; -function storeAll(attributes) { +function store(attributes) { const busDocs = []; const busZips = []; for (var i = 0; i < attributes.length; i++) { @@ -39,6 +39,10 @@ function storeAll(attributes) { return Bus.collection.insertMany(busDocs); } +async function storeAll() { + await store(Busses); +} + async function dropAll() { try { await Bus.collection.drop(); diff --git a/tests/util/hacker.test.util.js b/tests/util/hacker.test.util.js index 85fb2d78..598a8e69 100644 --- a/tests/util/hacker.test.util.js +++ b/tests/util/hacker.test.util.js @@ -353,7 +353,7 @@ const unconfirmedAccountHacker0 = { "major": "EE", "graduationYear": 2019, "codeOfConduct": true, -} +}; const Hackers = [ TeamHacker0, @@ -392,7 +392,7 @@ module.exports = { dropAll: dropAll }; -function storeAll(attributes) { +function store(attributes) { const hackerDocs = []; const hackerIds = []; for (var i = 0; i < attributes.length; i++) { @@ -403,6 +403,10 @@ function storeAll(attributes) { return Hacker.collection.insertMany(hackerDocs); } +async function storeAll() { + await store(Hackers); +} + async function dropAll() { try { await Hacker.collection.drop(); diff --git a/tests/util/resetPassword.test.util.js b/tests/util/resetPassword.test.util.js index a5008466..8a45abb5 100644 --- a/tests/util/resetPassword.test.util.js +++ b/tests/util/resetPassword.test.util.js @@ -23,7 +23,7 @@ const ResetPasswords = [ ResetPasswordToken1 ]; -function storeAll(attributes) { +function store(attributes) { const resetPasswordDocs = []; const resetPasswordIds = []; for (var i = 0; i < attributes.length; i++) { @@ -33,6 +33,10 @@ function storeAll(attributes) { return ResetPassword.collection.insertMany(resetPasswordDocs); } +async function storeAll() { + await store(ResetPasswords); +} + async function dropAll() { try { await ResetPassword.collection.drop(); diff --git a/tests/util/role.test.util.js b/tests/util/role.test.util.js index 142b47c5..c9e1da5c 100644 --- a/tests/util/role.test.util.js +++ b/tests/util/role.test.util.js @@ -1,6 +1,9 @@ "use strict"; const Role = require("../../models/role.model"); -const Constants = require("../../constants/general.constant"); +const Constants = { + General: require("../../constants/general.constant"), + Role: require("../../constants/role.constant"), +} const Routes = require("../../constants/routes.constant"); const mongoose = require("mongoose"); const logger = require("../../services/logger.service"); @@ -20,7 +23,7 @@ const duplicateRole1 = { ] }; -function storeAll(attributes) { +function store(attributes) { const roleDocs = []; const roleNames = []; attributes.forEach((attribute) => { @@ -31,6 +34,10 @@ function storeAll(attributes) { return Role.collection.insertMany(roleDocs); } +async function storeAll() { + await store(Constants.Role.allRolesArray); +} + async function dropAll() { try { await Role.collection.drop(); diff --git a/tests/util/roleBinding.test.util.js b/tests/util/roleBinding.test.util.js index b23b17b3..8d3b42b8 100644 --- a/tests/util/roleBinding.test.util.js +++ b/tests/util/roleBinding.test.util.js @@ -59,29 +59,6 @@ function createRoleBindings(accounts) { return roleBindings; } -function storeAll(attributes) { - const roleBindingDocs = []; - const roleBindingNames = []; - attributes.forEach((attribute) => { - roleBindingDocs.push(new RoleBinding(attribute)); - roleBindingNames.push(attribute.name); - }); - - return RoleBinding.collection.insertMany(roleBindingDocs); -} - -async function dropAll() { - try { - await RoleBinding.collection.drop(); - } catch (e) { - if (e.code === 26) { - logger.info("namespace %s not found", RoleBinding.collection.name); - } else { - throw e; - } - } -} - const TeamHackerRB = createRoleBindings(Util.Account.hackerAccounts.stored.team); const NoTeamHackerRB = createRoleBindings(Util.Account.hackerAccounts.stored.noTeam); const VolunteerRB = createRoleBindings(Util.Account.volunteerAccounts.stored); @@ -100,12 +77,58 @@ const newSponsorT3RB = createRoleBindings(Util.Account.sponsorT3Accounts.new); const newSponsorT4RB = createRoleBindings(Util.Account.sponsorT4Accounts.new); const newSponsorT5RB = createRoleBindings(Util.Account.sponsorT5Accounts.new); -const unconfirmedAccountRB = [ +const extraAccounts = [ createRoleBinding(Util.Account.NonConfirmedAccount1._id), createRoleBinding(Util.Account.NonConfirmedAccount2._id), createRoleBinding(Util.Account.NonConfirmedAccount3._id), + createRoleBinding(Util.Account.waitlistedHacker0._id, Constants.General.HACKER), ]; +function store(attributes) { + const roleBindingDocs = []; + const roleBindingNames = []; + attributes.forEach((attribute) => { + roleBindingDocs.push(new RoleBinding(attribute)); + roleBindingNames.push(attribute.name); + }); + + return RoleBinding.collection.insertMany(roleBindingDocs); +} + +async function storeAll() { + await store(TeamHackerRB); + await store(NoTeamHackerRB); + await store(VolunteerRB); + await store(StaffRB); + await store(SponsorT1RB); + await store(SponsorT2RB); + await store(SponsorT3RB); + await store(SponsorT4RB); + await store(SponsorT5RB); + + await store(newHackerRB); + await store(newVolunteerRB); + await store(newSponsorT1RB); + await store(newSponsorT2RB); + await store(newSponsorT3RB); + await store(newSponsorT4RB); + await store(newSponsorT5RB); + + await store(extraAccounts); +} + +async function dropAll() { + try { + await RoleBinding.collection.drop(); + } catch (e) { + if (e.code === 26) { + logger.info("namespace %s not found", RoleBinding.collection.name); + } else { + throw e; + } + } +} + module.exports = { TeamHackerRB: TeamHackerRB, NoTeamHackerRB: NoTeamHackerRB, @@ -125,7 +148,7 @@ module.exports = { newSponsorT4RB: newSponsorT4RB, newSponsorT5RB: newSponsorT5RB, - unconfirmedAccountRB: unconfirmedAccountRB, + extraAccounts: extraAccounts, storeAll: storeAll, dropAll: dropAll, diff --git a/tests/util/sponsor.test.util.js b/tests/util/sponsor.test.util.js index 220c6504..8eb7ec1c 100644 --- a/tests/util/sponsor.test.util.js +++ b/tests/util/sponsor.test.util.js @@ -38,7 +38,7 @@ const Sponsors = [ T1Sponsor0, ]; -function storeAll(attributes) { +function store(attributes) { const sponsorDocs = []; const sponsorComps = []; attributes.forEach((attribute) => { @@ -49,6 +49,10 @@ function storeAll(attributes) { return Sponsor.collection.insertMany(sponsorDocs); } +async function storeAll() { + await store(Sponsors); +} + async function dropAll() { try { await Sponsor.collection.drop(); diff --git a/tests/util/staff.test.util.js b/tests/util/staff.test.util.js index 9bfb4e8e..e43da75e 100644 --- a/tests/util/staff.test.util.js +++ b/tests/util/staff.test.util.js @@ -14,7 +14,7 @@ const Staffs = [ Staff0, ]; -function storeAll(attributes) { +function store(attributes) { const staffDocs = []; const staffIds = []; attributes.forEach((attribute) => { @@ -25,6 +25,10 @@ function storeAll(attributes) { return Staff.collection.insertMany(staffDocs); } +async function storeAll() { + await store(Staffs); +} + async function dropAll() { try { await Staff.collection.drop(); diff --git a/tests/util/team.test.util.js b/tests/util/team.test.util.js index 2ba49ea1..e04f6f34 100644 --- a/tests/util/team.test.util.js +++ b/tests/util/team.test.util.js @@ -54,7 +54,7 @@ const Teams = [ Team3 ]; -function storeAll(attributes) { +function store(attributes) { const teamDocs = []; const names = []; attributes.forEach((attribute) => { @@ -65,6 +65,10 @@ function storeAll(attributes) { return Team.collection.insertMany(teamDocs); } +async function storeAll() { + await store(Teams); +} + async function dropAll() { try { await Team.collection.drop(); diff --git a/tests/util/volunteer.test.util.js b/tests/util/volunteer.test.util.js index 3b334605..c104f905 100644 --- a/tests/util/volunteer.test.util.js +++ b/tests/util/volunteer.test.util.js @@ -28,7 +28,7 @@ const Volunteers = [ Volunteer0, ]; -function storeAll(attributes) { +function store(attributes) { const volunteerDocs = []; const volunteerIds = []; attributes.forEach((attribute) => { @@ -39,6 +39,10 @@ function storeAll(attributes) { return Volunteer.collection.insertMany(volunteerDocs); } +async function storeAll() { + await store(Volunteers); +} + async function dropAll() { try { await Volunteer.collection.drop(); From a25edbc642339ca66682fa062d48f410e3d4c03d Mon Sep 17 00:00:00 2001 From: Richard Zhang Date: Tue, 15 Jan 2019 09:19:16 -0500 Subject: [PATCH 10/11] Fix tests from develop --- tests/hacker.test.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/hacker.test.js b/tests/hacker.test.js index 562cdcac..ed281b25 100644 --- a/tests/hacker.test.js +++ b/tests/hacker.test.js @@ -208,13 +208,13 @@ describe("GET hacker", function () { // succeed on admin case it("should list a hacker's information using admin power on /api/hacker/email/:email GET", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/hacker/email/${storedAccount1.email}`) + .get(`/api/hacker/email/${teamHackerAccount0.email}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -226,7 +226,7 @@ describe("GET hacker", function () { res.body.message.should.equal(Constants.Success.HACKER_READ); res.body.should.have.property("data"); - let hacker = new Hacker(storedHacker1); + let hacker = new Hacker(TeamHacker0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); done(); @@ -236,13 +236,13 @@ describe("GET hacker", function () { // succeed on :self case it("should list the user's hacker information on /api/hacker/email/:email GET", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/hacker/email/${storedAccount1.email}`) + .get(`/api/hacker/email/${teamHackerAccount0.email}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -254,7 +254,7 @@ describe("GET hacker", function () { res.body.message.should.equal(Constants.Success.HACKER_READ); res.body.should.have.property("data"); - let hacker = new Hacker(storedHacker1); + let hacker = new Hacker(TeamHacker0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); @@ -265,13 +265,13 @@ describe("GET hacker", function () { // fail due to lack of authorization it("should fail to list a hacker information due to lack of authorization on /api/hacker/email/:id GET", function (done) { - util.auth.login(agent, storedAccount2, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/hacker/email/${storedAccount1.email}`) + .get(`/api/hacker/email/${teamHackerAccount0.email}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { From c95abf0a8cd86300ca7461cd8e06468a7a8a704d Mon Sep 17 00:00:00 2001 From: Richard Zhang Date: Tue, 15 Jan 2019 20:03:43 -0500 Subject: [PATCH 11/11] fix typo with test --- tests/hacker.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/hacker.test.js b/tests/hacker.test.js index ed281b25..01e9d9c6 100644 --- a/tests/hacker.test.js +++ b/tests/hacker.test.js @@ -265,7 +265,7 @@ describe("GET hacker", function () { // fail due to lack of authorization it("should fail to list a hacker information due to lack of authorization on /api/hacker/email/:id GET", function (done) { - util.auth.login(agent, teamHackerAccount0, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error);