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/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 467b1ee5..eb41bcab 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(); } 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..83f0dd9b --- /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(new Settings(setting)); +} + +module.exports = { + store: store, + drop: drop +}; diff --git a/tests/settings.test.js b/tests/settings.test.js index 12c6f55e..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,8 +85,7 @@ describe("PATCH settings", function() { agent .patch(`/api/settings/`) .type("application/json") - .send(util.settings.settingConfirmClosed) - // does not have password because of to stripped json + .send(Constants.Settings.CONFIRM_CLOSED) .end(function(err, res) { res.should.have.status(200); res.should.be.json; @@ -109,8 +108,7 @@ describe("PATCH settings", function() { agent .patch(`/api/settings/`) .type("application/json") - .send(util.settings.settingRemoteHackathon) - // does not have password because of to stripped json + .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 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 };