Skip to content
This repository has been archived by the owner on Aug 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #168 from AntoineDao/tests
Browse files Browse the repository at this point in the history
Tests FTW! 🎊💥🎉 thanks @AntoineDao!
  • Loading branch information
didimitrie authored Dec 12, 2019
2 parents 1a32e37 + 5e26ad0 commit 3fc9ce8
Show file tree
Hide file tree
Showing 16 changed files with 3,447 additions and 55 deletions.
27 changes: 22 additions & 5 deletions app/api/projects/ProjectDelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,44 @@ module.exports = async ( req, res ) => {

let streams = await DataStream.find( { streamId: { $in: project.streams } }, 'canWrite canRead streamId owner' )
let allOtherProjects = await Project.find( { 'streams': { $in: project.streams }, _id: { $ne: project._id } } )

let modifiedStreams = [ ]

for ( let stream of streams ) {
let otherProjects = allOtherProjects.filter( p => p.streams.indexOf( stream.streamId ) > -1 )
let otherCW = Array.prototype.concat( ...otherProjects.map( p => p.permissions.canWrite ) )
let otherCR = Array.prototype.concat( ...otherProjects.map( p => p.permissions.canRead ) )

// Replaced these two with a gross forEach method below because casting Mongoose Arrays to
// normal Javascript Arrays removes certain comparative properties when it comes to bson _id
// objects. Check this link for vague explanations: https://stackoverflow.com/questions/41063587/mongoose-indexof-in-an-objectid-array

// let otherCW = Array.prototype.concat( ...otherProjects.map( p => p.permissions.canWrite ) )
// let otherCR = Array.prototype.concat( ...otherProjects.map( p => p.permissions.canRead ) )

let modified = false


project.permissions.canRead.forEach( id => {
let index = stream.canRead.indexOf( id )
if ( otherCR.indexOf( id ) === -1 && index > -1 ) {
let canReadOther = false;
otherProjects.forEach( p => {
if ( p.permissions.canRead.indexOf( id ) > -1 ) {
canReadOther = true;
}
} );
if ( !canReadOther && index > -1 ) {
stream.canRead.splice( index, 1 )
modified = true
}
} )

project.permissions.canWrite.forEach( id => {
let index = stream.canWrite.indexOf( id )
if ( otherCW.indexOf( id ) === -1 && index > -1 ) {
let canWriteOther = false;
otherProjects.forEach( p => {
if ( p.permissions.canWrite.indexOf( id ) > -1 ) {
canWriteOther = true;
}
} );
if ( !canWriteOther && index > -1 ) {
stream.canWrite.splice( index, 1 )
modified = true
}
Expand Down
25 changes: 21 additions & 4 deletions app/api/projects/ProjectDeleteStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,39 @@ module.exports = async ( req, res ) => {

let otherProjects = await Project.find( { 'streams': stream.streamId, _id: { $ne: project._id } } )

let otherCW = Array.prototype.concat( ...otherProjects.map( p => p.permissions.canWrite ) )
let otherCR = Array.prototype.concat( ...otherProjects.map( p => p.permissions.canRead ) )
// Replaced these two with a gross forEach method below because casting Mongoose Arrays to
// normal Javascript Arrays removes certain comparative properties when it comes to bson _id
// objects. Check this link for vague explanations: https://stackoverflow.com/questions/41063587/mongoose-indexof-in-an-objectid-array

// let otherCW = Array.prototype.concat( ...otherProjects.map( p => p.permissions.canWrite ) )
// let otherCR = Array.prototype.concat( ...otherProjects.map( p => p.permissions.canRead ) )

project.permissions.canRead.forEach( id => {
let index = stream.canRead.indexOf( id )
if ( otherCR.indexOf( id ) === -1 && index > -1 ) {
let canReadOther = false;
otherProjects.forEach( p => {
if ( p.permissions.canRead.indexOf( id ) > -1 ) {
canReadOther = true;
}
} );
if ( !canReadOther && index > -1 ) {
stream.canRead.splice( index, 1 )
}
} )

project.permissions.canWrite.forEach( id => {
let index = stream.canWrite.indexOf( id )
if ( otherCW.indexOf( id ) === -1 && index > -1 ) {
let canWriteOther = false;
otherProjects.forEach( p => {
if ( p.permissions.canWrite.indexOf( id ) > -1 ) {
canWriteOther = true;
}
} );
if ( !canWriteOther && index > -1 ) {
stream.canWrite.splice( index, 1 )
}
} )

await Promise.all( [ stream.save( ), project.save( ) ] )

return res.send( { success: true, project: project, stream: stream } )
Expand Down
1 change: 0 additions & 1 deletion app/api/projects/ProjectDeleteUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ module.exports = async ( req, res ) => {

let pullWrite = otherCW.indexOf( req.params.userId ) === -1 && stream.canWrite.indexOf( req.params.userId ) > -1,
pullRead = otherCR.indexOf( req.params.userId ) === -1 && stream.canRead.indexOf( req.params.userId ) > -1

if ( pullWrite && pullRead )
streamsToPullBothFrom.push( streamId )
else if ( pullWrite )
Expand Down
12 changes: 11 additions & 1 deletion app/api/projects/ProjectGetAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ const Project = require( '../../../models/Project' )

module.exports = ( req, res ) => {
let query = q2m( req.query )
Project.find( { sort: query.options.sort, skip: query.options.skip, limit: query.options.limit } )
let finalCriteria = {}

let andCrit = Object.keys( query.criteria ).map( key => {
let crit = {}
crit[key] = query.criteria[key]
return crit
} )

if ( andCrit.length !== 0 ) finalCriteria.$and = andCrit

Project.find( finalCriteria, query.options.fields, { sort: query.options.sort, skip: query.options.skip, limit: query.options.limit } )
.then( resources => {
res.send( { success: true, resources: resources } )
} )
Expand Down
5 changes: 4 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ version: '{build}'

image: Ubuntu

stack: node 9
stack: node 10

install:
- sh: npm install

test_script:
- sh: npm test

after_test:
- sh: npm run lint


Expand Down
18 changes: 18 additions & 0 deletions config/loadEnv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const dotenv = require( 'dotenv' );

const NODE_ENV = process.env.NODE_ENV || 'dev';

module.exports = () => {
let configPath = './.env'

if ( NODE_ENV == 'test' ) {
configPath = './test/.env.test'
}

const configResult = dotenv.config( { path: configPath } )

if ( configResult.error ) {
throw configResult.error
// throw new Error( 'There is an error in the .env configuration file. Will use the default provided ones (if any).' )
}
}
5 changes: 3 additions & 2 deletions config/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { createLogger, format, transports } = require( 'winston' )
require( 'winston-daily-rotate-file' )

const logDir = 'logs'
const LOG_LEVEL = process.env.NODE_ENV === 'test' ? [] : process.env.LOG_LEVEL || 'debug'

if ( !fs.existsSync( logDir ) ) {
fs.mkdirSync( logDir )
Expand All @@ -15,15 +16,15 @@ const drfTransport = new transports.DailyRotateFile( {
} )

const logger = createLogger( {
level: 'debug',
level: LOG_LEVEL,
format: format.combine(
format.timestamp( { format: 'YYYY-MM-DD HH:mm:ss' } ),
format.errors( { stack: true } ),
format.json( )
),
transports: [
new transports.Console( {
level: 'debug',
level: LOG_LEVEL,
format: format.combine( format.colorize( ), format.timestamp( { format: 'YYYY-MM-DD HH:mm:ss' } ), format.printf( info => `${info.timestamp} ${info.level}: ${info.message}` ) )
} ),
drfTransport
Expand Down
Loading

0 comments on commit 3fc9ce8

Please sign in to comment.