Skip to content

Commit

Permalink
Unit & integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyakaspeter committed Dec 3, 2021
1 parent 79564a7 commit ec97c3a
Show file tree
Hide file tree
Showing 16 changed files with 948 additions and 140 deletions.
6 changes: 6 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,11 @@
},
"problemMatcher": []
},
{
"label": "Run tests (server)",
"type": "shell",
"command": "yarn workspace server test",
"problemMatcher": []
},
]
}
3 changes: 3 additions & 0 deletions server/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
"rules": {
"node/no-unsupported-features/es-syntax": 0
},
"ignorePatterns": [
"test/*"
],
"settings": {}
}
2 changes: 1 addition & 1 deletion server/config/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import multer from 'multer';
import { GridFsStorage } from 'multer-gridfs-storage';
import User from '../models/user.js';

dotenv.config();
if (process.env.NODE_ENV !== 'test') dotenv.config();

const MONGODB_URL = process.env.MONGODB_URL;
const UPLOAD_BUCKET_NAME = process.env.MONGODB_UPLOAD_BUCKET_NAME || 'uploads';
Expand Down
5 changes: 1 addition & 4 deletions server/config/openvidu.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import axios from 'axios';
import dotenv from 'dotenv';
import { OpenVidu } from 'openvidu-node-client';

dotenv.config();

const OPENVIDU_URL = process.env.OPENVIDU_URL;
const OPENVIDU_URL = process.env.OPENVIDU_URL || 'https://localhost:443';
const OPENVIDU_SECRET = process.env.OPENVIDU_SECRET;

export const ovClient = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET);
Expand Down
81 changes: 54 additions & 27 deletions server/config/passport.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import dotenv from 'dotenv';
import passport from 'passport';
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
// eslint-disable-next-line node/no-unpublished-import
import MockStrategy from 'passport-mock-strategy';
import User from '../models/user.js';

dotenv.config();

const GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID;
const GOOGLE_CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET;

Expand All @@ -18,30 +17,58 @@ passport.deserializeUser((id, done) => {
}).lean();
});

passport.use(
new GoogleStrategy(
{
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: '/auth/google/callback',
proxy: true,
},
(accessToken, refreshToken, profile, done) => {
User.findOneAndUpdate(
{
googleId: profile.id,
name: profile.displayName,
email: profile.emails[0]?.value,
picture: profile.photos[0]?.value,
if (process.env.NODE_ENV !== 'test') {
passport.use(
new GoogleStrategy(
{
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: '/auth/google/callback',
proxy: true,
},
(accessToken, refreshToken, profile, done) => {
User.findOneAndUpdate(
{
googleId: profile.id,
name: profile.displayName,
email: profile.emails[0]?.value,
picture: profile.photos[0]?.value,
},
{},
{ new: true, upsert: true },
(err, user) => {
return done(err, user);
}
);
}
)
);
} else {
const getMockStrategy = name =>
new MockStrategy(
{
name: name,
user: {
googleId: (+new Date()).toString(),
name: name,
email: `${name}@example.com`,
picture: '',
},
{},
{ new: true, upsert: true },
(err, user) => {
return done(err, user);
}
);
}
)
);
},
(user, done) => {
User.findOneAndUpdate(
user,
{},
{ new: true, upsert: true },
(err, user) => {
return done(err, user);
}
);
}
);

passport.use(getMockStrategy('mock1'));
passport.use(getMockStrategy('mock2'));
}

export default passport;
3 changes: 0 additions & 3 deletions server/controllers/room/createRoom.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import dotenv from 'dotenv';
import Room from '../../models/room.js';

dotenv.config();

export const createRoom = async (req, res) => {
const date = new Date();
const room = { ...req.body, image: req.file?.id, lastActivity: date };
Expand Down
3 changes: 0 additions & 3 deletions server/controllers/room/deleteRoom.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import dotenv from 'dotenv';
import Room from '../../models/room.js';
import { signalRoomDeleted } from '../../signals/roomDeleted.js';

dotenv.config();

export const deleteRoom = async (req, res) => {
const roomId = req.params.roomId;

Expand Down
3 changes: 0 additions & 3 deletions server/controllers/room/getRoomImage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import dotenv from 'dotenv';
import { gfs } from '../../config/mongoose.js';
import Room from '../../models/room.js';

dotenv.config();

export const getRoomImage = async (req, res) => {
const roomId = req.params.roomId;

Expand Down
3 changes: 0 additions & 3 deletions server/controllers/room/getRooms.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import dotenv from 'dotenv';
import Room from '../../models/room.js';

dotenv.config();

export const getRooms = async (req, res) => {
const userId = req.user._id;

Expand Down
3 changes: 0 additions & 3 deletions server/controllers/room/updateRoom.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import dotenv from 'dotenv';
import { gfs } from '../../config/mongoose.js';
import Room from '../../models/room.js';
import { signalRoomUpdated } from '../../signals/roomUpdated.js';

dotenv.config();

export const updateRoom = async (req, res) => {
const deleteImage = req.body.image === 'null';
const room = { ...req.body, image: deleteImage ? null : req.file?.id };
Expand Down
18 changes: 9 additions & 9 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import MongoStore from 'connect-mongo';
import cors from 'cors';
import dotenv from 'dotenv';
import express from 'express';
import session from 'express-session';
import http from 'http';
Expand All @@ -9,16 +8,13 @@ import passport from './config/passport.js';
import socketio from './config/socketio.js';
import routes from './routes/index.js';

dotenv.config();

const PORT = process.env.PORT || 5000;
const SESSION_SECRET = process.env.SESSION_SECRET;

mongoose.connection.once('open', () => {
const app = express();
const SESSION_SECRET = process.env.SESSION_SECRET || 'secret';

const server = http.createServer(app);
export const app = express();
export const server = http.createServer(app);

mongoose.connection.once('open', () => {
const sessionStore = MongoStore.create({
client: mongoose.connection.getClient(),
});
Expand All @@ -43,5 +39,9 @@ mongoose.connection.once('open', () => {

socketio.init(server, expressSession, passportMw, passportSession);

server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
if (process.env.NODE_ENV !== 'test') {
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
}

app.emit('initialized');
});
12 changes: 9 additions & 3 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
},
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
"dev": "nodemon index.js",
"test": "mocha --recursive --exit"
},
"dependencies": {
"axios": "^0.23.0",
Expand All @@ -27,6 +28,11 @@
"socket.io": "^4.3.1"
},
"devDependencies": {
"nodemon": "^2.0.14"
"chai": "^4.3.4",
"chai-http": "^4.3.0",
"mocha": "^9.1.3",
"mongodb-memory-server": "^8.0.4",
"nodemon": "^2.0.14",
"passport-mock-strategy": "^2.0.0"
}
}
}
38 changes: 26 additions & 12 deletions server/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,35 @@ import passport from '../config/passport.js';

const router = express.Router();

router.get(
'/google',
passport.authenticate('google', {
scope: ['profile', 'email'],
prompt: 'select_account',
})
);

router.get('/google/callback', passport.authenticate('google'), (req, res) => {
res.redirect('/');
});

router.get('/logout', (req, res) => {
req.logout();
res.redirect('/');
});

if (process.env.NODE_ENV !== 'test') {
router.get(
'/google',
passport.authenticate('google', {
scope: ['profile', 'email'],
prompt: 'select_account',
})
);

router.get(
'/google/callback',
passport.authenticate('google'),
(req, res) => {
res.redirect('/');
}
);
} else {
router.get('/mock1', passport.authenticate('mock1'), (req, res) =>
res.status(200).end()
);

router.get('/mock2', passport.authenticate('mock2'), (req, res) =>
res.status(200).end()
);
}

export default router;
Loading

0 comments on commit ec97c3a

Please sign in to comment.