-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Server tests. #288 #289
Merged
Caleb-Cohen
merged 45 commits into
Together-100Devs:development
from
romanstetsyk:server-tests
Feb 17, 2023
Merged
Server tests. #288 #289
Changes from 23 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
4514ac2
install jest and supertest
romanstetsyk 355fb8d
separate app and server into different files
romanstetsyk a129715
add jest coverage folder to gitignore
romanstetsyk 68418f5
allow the use of mock user in test environment
romanstetsyk 49bc2a8
change validateBody to be a sync function
romanstetsyk ead33ee
add 'use strict', fix variable declarations
romanstetsyk f46d3da
extract constants
romanstetsyk b1ce0ec
remove console logs
romanstetsyk ae0a56d
add mock user to db
romanstetsyk 7a1da5a
refactor, fix validation bugs in createEventSchema
romanstetsyk 955feb6
test httpError
romanstetsyk 07c1606
test validateObjectId
romanstetsyk 96a7d68
test validateBody
romanstetsyk b66295a
refactor unit tests
romanstetsyk 54f4ce4
export STRING_MAX_LENGTH to be used in tests
romanstetsyk 97fe831
fix dotenv path to work in tests
romanstetsyk ab1da48
modify mock request
romanstetsyk 48bb000
test routes
romanstetsyk 41e2826
refactor maxEvents middleware
romanstetsyk 62a9794
add tests for maxEvents
romanstetsyk 2487a42
allow event end time (not date) to be before start time
romanstetsyk 16b21ea
test create event with valid data
romanstetsyk fe7e81b
Merge branch 'development' into server-tests
romanstetsyk a362786
timezone bug fix: add offset to days
romanstetsyk be3122c
test createEventArray
romanstetsyk 36e9965
create date range with Date instead of datefns lib
romanstetsyk 4209cd4
test createEventsArray in JST timezone
romanstetsyk 2714185
refactor createEventsArray and tests
romanstetsyk 718db49
Merge branch 'development' into server-tests
romanstetsyk e03b97b
fix test JST time
romanstetsyk 4c0dece
use Temporal to work with dates
romanstetsyk faaa8aa
fix event duration using Temporal
romanstetsyk eebae6e
test cases for createEventsArray
romanstetsyk 6ae950b
test validateBody
romanstetsyk 70fa9a6
fix acceptance tests
romanstetsyk c91f50e
Merge branch 'development' into server-tests
romanstetsyk aea5b3e
change hardcoded timezone to machine timezone
romanstetsyk 8cbd957
Merge branch 'development' into server-tests
romanstetsyk 099302d
update test script
romanstetsyk a9c9ee1
Merge branch 'development' into server-tests
romanstetsyk cea7784
remove irrelevant test (maxEvents)
romanstetsyk 99359df
update front-end test
romanstetsyk af596a6
add script to run all tests together
romanstetsyk ce73c30
add route tests
romanstetsyk 1eb314e
added formidable, jsbi, moved jest and supertest to dev dependencies
Caleb-Cohen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ build | |
.DS_Store | ||
.mongo | ||
formLogic.txt | ||
/coverage/ |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
const express = require("express"); | ||
const path = require("path"); | ||
const app = express(); | ||
const mongoose = require("mongoose"); | ||
const passport = require("passport"); | ||
const session = require("express-session"); | ||
const MongoStore = require("connect-mongo")(session); | ||
const flash = require("express-flash"); | ||
const logger = require("morgan"); | ||
//Use .env file in config folder | ||
require("dotenv").config({ path: path.resolve(__dirname, "config", ".env") }); | ||
const mainRoutes = require("./routes/main"); | ||
const eventsRoutes = require("./routes/events"); | ||
const mockUser = require("./config/mockUser.json"); | ||
const User = require("./models/User"); | ||
|
||
// Passport config | ||
require("./config/passport")(passport); | ||
|
||
//Body Parsing | ||
app.use(express.urlencoded({ extended: true })); | ||
app.use(express.json()); | ||
|
||
//Logging | ||
app.use(logger("dev")); | ||
|
||
// Setup Sessions - stored in MongoDB | ||
app.use( | ||
session({ | ||
secret: process.env.SESSION_SECRET || "keyboard cat", | ||
resave: false, | ||
saveUninitialized: false, | ||
store: new MongoStore({ mongooseConnection: mongoose.connection }), | ||
}) | ||
); | ||
|
||
// Render React as View | ||
app.use(express.static(path.join(__dirname, "..", "client", "build"))); | ||
|
||
// Passport middleware | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
|
||
// Allows to use a mock user in development and testing environments | ||
if ( | ||
["development", "test"].includes(process.env.NODE_ENV) && | ||
process.env.MOCK_USER === "true" | ||
) { | ||
console.log("In development - using mocked user"); | ||
app.use(async (req, res, next) => { | ||
req.user = mockUser; | ||
let user = await User.findOne({ _id: mockUser._id }).exec(); | ||
if (!user) { | ||
await User.create(mockUser); | ||
} | ||
next(); | ||
}); | ||
} | ||
|
||
//Use flash messages for errors, info, ect... | ||
app.use(flash()); | ||
|
||
//Setup Routes For Which The Server Is Listening | ||
app.use("/", mainRoutes); | ||
app.use("/events", eventsRoutes); | ||
app.get("'", (req, res) => { | ||
res.sendFile(path.join(__dirname, "..", "client", "build", "index.html")); | ||
}); | ||
|
||
// 404 handler | ||
app.use((req, res) => { | ||
res.status(404).json({ message: "Not found" }); | ||
}); | ||
|
||
// error handler | ||
app.use((err, req, res, next) => { | ||
const { status = 500, message = "Server error", stack } = err; | ||
// console.log(stack); | ||
res.status(status).json({ message }); | ||
}); | ||
|
||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,36 @@ | ||
"use strict"; | ||
|
||
require("express-async-errors"); | ||
const httpError = require("../utilities/httpError"); | ||
const { Event } = require("../models/Event"); | ||
|
||
const MAX_EVENTS_PER_USER = 5; | ||
|
||
/** | ||
* Checks if the number of different future recurring events | ||
* (with distinct groupId) and nonrecurring events exceeds 5. | ||
* (with distinct groupId) and nonrecurring events exceeds MAX_EVENTS_PER_USER. | ||
*/ | ||
const maxEvents = async (req, _, next) => { | ||
try { | ||
const existingRecurringEvents = await Event.distinct("groupId", { | ||
user: req.user._id, | ||
groupId: { $ne: null }, | ||
startAt: { $gte: Date.now() }, | ||
}).exec(); | ||
const existingRecurringEvents = await Event.distinct("groupId", { | ||
user: req.user._id, | ||
groupId: { $ne: null }, | ||
startAt: { $gte: Date.now() }, | ||
}).exec(); | ||
|
||
const existingNonrecurringEvents = await Event.countDocuments({ | ||
user: req.user._id, | ||
groupId: null, | ||
startAt: { $gte: Date.now() }, | ||
}).exec(); | ||
const existingNonrecurringEvents = await Event.countDocuments({ | ||
user: req.user._id, | ||
groupId: null, | ||
startAt: { $gte: Date.now() }, | ||
}).exec(); | ||
|
||
if (existingRecurringEvents.length + existingNonrecurringEvents >= 5) { | ||
throw httpError(403, "Exceeded maximum allowed events per user."); | ||
} | ||
|
||
next(); | ||
} catch (err) { | ||
next(err); | ||
if ( | ||
existingRecurringEvents.length + existingNonrecurringEvents >= | ||
MAX_EVENTS_PER_USER | ||
) { | ||
throw httpError(403, "Exceeded maximum allowed events per user."); | ||
} | ||
|
||
next(); | ||
}; | ||
|
||
module.exports = maxEvents; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Express
app
andserver
needed to be separated into separate files so thatapp
could be imported into the tests without starting the server