From c95f53211834d474550cb343ec02e58079539f98 Mon Sep 17 00:00:00 2001 From: Pierre Theo Klein Date: Tue, 10 Nov 2020 19:24:02 -0500 Subject: [PATCH 1/5] refactor: move const settings objects into constants file --- constants/settings.constant.js | 39 +++++++++++++++++++++++++++ tests/util/settings.test.util.js | 45 +++++--------------------------- 2 files changed, 45 insertions(+), 39 deletions(-) create mode 100644 constants/settings.constant.js diff --git a/constants/settings.constant.js b/constants/settings.constant.js new file mode 100644 index 00000000..ee0df49b --- /dev/null +++ b/constants/settings.constant.js @@ -0,0 +1,39 @@ +const APP_NOT_YET_OPEN = { + openTime: new Date(Date.now() + 100000000000), + closeTime: new Date(Date.now() + 10000000000000000), + confirmTime: new Date(Date.now() + 100000000000000000) +}; + +const APP_OPEN = { + openTime: new Date(Date.now() - 100), + closeTime: new Date(Date.now() + 10000000000), + confirmTime: new Date(Date.now() + 100000000000000) +}; + +const APP_CLOSED = { + openTime: new Date(Date.now() - 100), + closeTime: new Date(Date.now() - 1000), + confirmTime: new Date(Date.now() + 100000000000000) +}; + +const CONFIRM_CLOSED = { + openTime: new Date(Date.now() - 10000), + closeTime: new Date(Date.now() - 1000), + confirmTime: new Date(Date.now() - 100) +}; + +const REMOTE_HACKATHON = { + openTime: new Date(Date.now() - 100), + closeTime: new Date(Date.now() + 10000000000), + confirmTime: new Date(Date.now() + 100000000000000), + isRemote: true +}; + +// Some utility dates that are used in tests and seed script +module.exports = { + APP_NOT_YET_OPEN: APP_NOT_YET_OPEN, + APP_OPEN: APP_OPEN, + APP_CLOSED: APP_CLOSED, + CONFIRM_CLOSED: CONFIRM_CLOSED, + REMOTE_HACKATHON: REMOTE_HACKATHON +}; diff --git a/tests/util/settings.test.util.js b/tests/util/settings.test.util.js index e1a2e91b..bb6961e2 100644 --- a/tests/util/settings.test.util.js +++ b/tests/util/settings.test.util.js @@ -1,51 +1,23 @@ const Settings = require("../../models/settings.model"); const logger = require("../../services/logger.service"); - -const settingApplicationNotYetOpen = { - openTime: new Date(Date.now() + 100000000000), - closeTime: new Date(Date.now() + 10000000000000000), - confirmTime: new Date(Date.now() + 100000000000000000) -}; - -const settingApplicationOpen = { - openTime: new Date(Date.now() - 100), - closeTime: new Date(Date.now() + 10000000000), - confirmTime: new Date(Date.now() + 100000000000000) -}; - -const settingApplicationClosed = { - openTime: new Date(Date.now() - 100), - closeTime: new Date(Date.now() - 1000), - confirmTime: new Date(Date.now() + 100000000000000) -}; - -const settingConfirmClosed = { - openTime: new Date(Date.now() - 10000), - closeTime: new Date(Date.now() - 1000), - confirmTime: new Date(Date.now() - 100) -}; - -const settingRemoteHackathon = { - openTime: new Date(Date.now() - 100), - closeTime: new Date(Date.now() + 10000000000), - confirmTime: new Date(Date.now() + 100000000000000), - isRemote: true +const Constants = { + Settings: require("../../constants/settings.constant") }; async function storeAll() { - const toStore = new Settings(settingApplicationOpen); + const toStore = new Settings(Constants.Settings.APP_OPEN); Settings.collection.insertOne(toStore); } async function setApplicationClosed() { await dropAll(); - const toStore = new Settings(settingApplicationClosed); + const toStore = new Settings(Constants.Settings.APP_CLOSED); Settings.collection.insertOne(toStore); } async function setApplicationNotYetOpen() { await dropAll(); - const toStore = new Settings(settingApplicationNotYetOpen); + const toStore = new Settings(Constants.Settings.APP_NOT_YET_OPEN); Settings.collection.insertOne(toStore); } @@ -64,10 +36,5 @@ module.exports = { storeAll: storeAll, dropAll: dropAll, setApplicationClosed: setApplicationClosed, - setApplicationNotYetOpen: setApplicationNotYetOpen, - settingApplicationNotYetOpen: settingApplicationNotYetOpen, - settingApplicationOpen: settingApplicationOpen, - settingApplicationClosed: settingApplicationClosed, - settingConfirmClosed: settingConfirmClosed, - settingRemoteHackathon: settingRemoteHackathon + setApplicationNotYetOpen: setApplicationNotYetOpen }; From 0e093c991a16320cbf3991b5d07a9f30e93f9df4 Mon Sep 17 00:00:00 2001 From: Pierre Theo Klein Date: Tue, 10 Nov 2020 19:24:17 -0500 Subject: [PATCH 2/5] feat: add settings to seed script --- seed/index.js | 18 ++++++++++++++++-- seed/roles.seed.js | 9 --------- seed/settings.seed.js | 22 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 seed/settings.seed.js diff --git a/seed/index.js b/seed/index.js index 467b1ee5..a8c833d8 100644 --- a/seed/index.js +++ b/seed/index.js @@ -1,12 +1,24 @@ "use strict"; const Constants = { - Role: require("../constants/role.constant") + Role: require("../constants/role.constant"), + Settings: require("../constants/settings.constant") }; const Seed = { - Roles: require("./roles.seed") + Roles: require("./roles.seed"), + Settings: require("./settings.seed") }; +const Services = { + env: require("../services/env.service") +}; +const path = require("path"); + +const envLoadResult = Services.env.load(path.join(__dirname, "../.env")); +if (envLoadResult.error) { + Services.log.error(envLoadResult.error); +} + const db = require("../services/database.service"); //connect to db db.connect(undefined, () => { @@ -30,8 +42,10 @@ async function onConnected() { async function dropAll() { await Seed.Roles.dropAll(); + await Seed.Settings.drop(); } async function storeAll() { await Seed.Roles.storeAll(Constants.Role.allRolesArray); + await Seed.Settings.store(Constants.Settings.APP_NOT_YET_OPEN); } diff --git a/seed/roles.seed.js b/seed/roles.seed.js index b60c762c..2e5eb80e 100644 --- a/seed/roles.seed.js +++ b/seed/roles.seed.js @@ -1,14 +1,5 @@ "use strict"; const Role = require("../models/role.model"); -const Services = { - env: require("../services/env.service") -}; -const path = require("path"); - -const envLoadResult = Services.env.load(path.join(__dirname, "../.env")); -if (envLoadResult.error) { - Services.log.error(envLoadResult.error); -} /** * Drops all elements in Role diff --git a/seed/settings.seed.js b/seed/settings.seed.js new file mode 100644 index 00000000..01f39725 --- /dev/null +++ b/seed/settings.seed.js @@ -0,0 +1,22 @@ +"use strict"; +const Settings = require("../models/settings.model"); + +/** + * Drops all elements in Role + */ +function drop() { + return Settings.deleteMany({}); +} + +/** + * Stores all of the roles in the db + * @param {Settings} setting the setting that we want to seed + */ +function store(setting) { + return Settings.collection.insertOne(setting); +} + +module.exports = { + store: store, + drop: drop +}; From 1f110a2706c31a4aeb836e80909dbf70f2247257 Mon Sep 17 00:00:00 2001 From: Pierre Theo Klein Date: Tue, 10 Nov 2020 19:40:10 -0500 Subject: [PATCH 3/5] fix: update settings schema defaults so that they are more sensible, and use that in seed --- models/settings.model.js | 6 +++--- seed/index.js | 2 +- seed/settings.seed.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/models/settings.model.js b/models/settings.model.js index 93a6d20c..ab704535 100644 --- a/models/settings.model.js +++ b/models/settings.model.js @@ -4,15 +4,15 @@ const mongoose = require("mongoose"); const settings = new mongoose.Schema({ openTime: { type: Date, - default: 0 + default: Date.now() + 2628000000 // One month from now. }, closeTime: { type: Date, - default: Date.now() + 31104000000 // Add a year from now. + default: Date.now() + 31540000000 + 2628000000 // One year and 1 month from now. }, confirmTime: { type: Date, - default: Date.now() + 31104000000 + 2628000000 // 1 year and 1 month from now. + default: Date.now() + 31540000000 + 2628000000 + 2628000000 // 1 year and 2 months from now. }, isRemote: { type: Boolean, diff --git a/seed/index.js b/seed/index.js index a8c833d8..eb41bcab 100644 --- a/seed/index.js +++ b/seed/index.js @@ -47,5 +47,5 @@ async function dropAll() { async function storeAll() { await Seed.Roles.storeAll(Constants.Role.allRolesArray); - await Seed.Settings.store(Constants.Settings.APP_NOT_YET_OPEN); + await Seed.Settings.store(); } diff --git a/seed/settings.seed.js b/seed/settings.seed.js index 01f39725..83f0dd9b 100644 --- a/seed/settings.seed.js +++ b/seed/settings.seed.js @@ -13,7 +13,7 @@ function drop() { * @param {Settings} setting the setting that we want to seed */ function store(setting) { - return Settings.collection.insertOne(setting); + return Settings.collection.insertOne(new Settings(setting)); } module.exports = { From c7a580dc0d6c0724adb57487c3b1f512c03a0df7 Mon Sep 17 00:00:00 2001 From: chenxuan-zhou <46543122+chenxuan-zhou@users.noreply.github.com> Date: Fri, 18 Jun 2021 15:43:39 -0400 Subject: [PATCH 4/5] fix: export and use settings constants --- tests/settings.test.js | 2 -- tests/util/settings.test.util.js | 4 +++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/settings.test.js b/tests/settings.test.js index 12c6f55e..e3a258bb 100644 --- a/tests/settings.test.js +++ b/tests/settings.test.js @@ -86,7 +86,6 @@ describe("PATCH settings", function() { .patch(`/api/settings/`) .type("application/json") .send(util.settings.settingConfirmClosed) - // does not have password because of to stripped json .end(function(err, res) { res.should.have.status(200); res.should.be.json; @@ -110,7 +109,6 @@ describe("PATCH settings", function() { .patch(`/api/settings/`) .type("application/json") .send(util.settings.settingRemoteHackathon) - // does not have password because of to stripped json .end(function(err, res) { res.should.have.status(200); res.should.be.json; diff --git a/tests/util/settings.test.util.js b/tests/util/settings.test.util.js index bb6961e2..156acc7c 100644 --- a/tests/util/settings.test.util.js +++ b/tests/util/settings.test.util.js @@ -36,5 +36,7 @@ module.exports = { storeAll: storeAll, dropAll: dropAll, setApplicationClosed: setApplicationClosed, - setApplicationNotYetOpen: setApplicationNotYetOpen + setApplicationNotYetOpen: setApplicationNotYetOpen, + settingConfirmClosed: Constants.Settings.CONFIRM_CLOSED, + settingRemoteHackathon: Constants.Settings.REMOTE_HACKATHON }; From 90406f422f06217641ed1658a7590b0bcfc2dd42 Mon Sep 17 00:00:00 2001 From: chenxuan-zhou <46543122+chenxuan-zhou@users.noreply.github.com> Date: Mon, 21 Jun 2021 17:31:19 -0400 Subject: [PATCH 5/5] fix: import setting constants directly in test --- tests/settings.test.js | 8 ++++---- tests/util/settings.test.util.js | 4 +--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/settings.test.js b/tests/settings.test.js index e3a258bb..6cff748a 100644 --- a/tests/settings.test.js +++ b/tests/settings.test.js @@ -8,12 +8,12 @@ chai.should(); const util = { account: require("./util/account.test.util"), auth: require("./util/auth.test.util"), - settings: require("./util/settings.test.util") }; const Constants = { Success: require("../constants/success.constant"), - Error: require("../constants/error.constant") + Error: require("../constants/error.constant"), + Settings: require("../constants/settings.constant") }; const invalidAccount = util.account.hackerAccounts.stored.noTeam[0]; @@ -85,7 +85,7 @@ describe("PATCH settings", function() { agent .patch(`/api/settings/`) .type("application/json") - .send(util.settings.settingConfirmClosed) + .send(Constants.Settings.CONFIRM_CLOSED) .end(function(err, res) { res.should.have.status(200); res.should.be.json; @@ -108,7 +108,7 @@ describe("PATCH settings", function() { agent .patch(`/api/settings/`) .type("application/json") - .send(util.settings.settingRemoteHackathon) + .send(Constants.Settings.REMOTE_HACKATHON) .end(function(err, res) { res.should.have.status(200); res.should.be.json; diff --git a/tests/util/settings.test.util.js b/tests/util/settings.test.util.js index 156acc7c..bb6961e2 100644 --- a/tests/util/settings.test.util.js +++ b/tests/util/settings.test.util.js @@ -36,7 +36,5 @@ module.exports = { storeAll: storeAll, dropAll: dropAll, setApplicationClosed: setApplicationClosed, - setApplicationNotYetOpen: setApplicationNotYetOpen, - settingConfirmClosed: Constants.Settings.CONFIRM_CLOSED, - settingRemoteHackathon: Constants.Settings.REMOTE_HACKATHON + setApplicationNotYetOpen: setApplicationNotYetOpen };