Merge pull request #2802 from Human-Connection/better_cypress_import

refactor(modules): Various import fixes
This commit is contained in:
mattwr18 2020-01-20 10:01:26 +01:00 committed by GitHub
commit 978c04178c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 72 additions and 94 deletions

View File

@ -7,7 +7,7 @@
"build": "babel src/ -d dist/ --copy-files", "build": "babel src/ -d dist/ --copy-files",
"start": "node dist/", "start": "node dist/",
"dev": "nodemon --exec babel-node src/ -e js,gql", "dev": "nodemon --exec babel-node src/ -e js,gql",
"dev:debug": "nodemon --exec babel-node --inspect=0.0.0.0:9229 src/index.js -e js,gql", "dev:debug": "nodemon --exec babel-node --inspect=0.0.0.0:9229 src/ -e js,gql",
"lint": "eslint src --config .eslintrc.js", "lint": "eslint src --config .eslintrc.js",
"test": "jest --forceExit --detectOpenHandles --runInBand", "test": "jest --forceExit --detectOpenHandles --runInBand",
"db:reset": "babel-node src/seed/reset-db.js", "db:reset": "babel-node src/seed/reset-db.js",

View File

@ -1,7 +1,8 @@
import dotenv from 'dotenv' import dotenv from 'dotenv'
import path from 'path' if (require.resolve) {
// are we in a nodejs environment?
dotenv.config({ path: path.resolve(__dirname, '../../.env') }) dotenv.config({ path: require.resolve('../../.env') })
}
const { const {
MAPBOX_TOKEN, MAPBOX_TOKEN,
@ -27,6 +28,15 @@ export const requiredConfigs = {
PRIVATE_KEY_PASSPHRASE, PRIVATE_KEY_PASSPHRASE,
} }
if (require.resolve) {
// are we in a nodejs environment?
Object.entries(requiredConfigs).map(entry => {
if (!entry[1]) {
throw new Error(`ERROR: "${entry[0]}" env variable is missing.`)
}
})
}
export const smtpConfigs = { export const smtpConfigs = {
SMTP_HOST, SMTP_HOST,
SMTP_PORT, SMTP_PORT,

View File

@ -1,4 +1,4 @@
module.exports = { export default {
id: { type: 'string', primary: true, lowercase: true }, id: { type: 'string', primary: true, lowercase: true },
status: { type: 'string', valid: ['permanent', 'temporary'] }, status: { type: 'string', valid: ['permanent', 'temporary'] },
type: { type: 'string', valid: ['role', 'crowdfunding'] }, type: { type: 'string', valid: ['role', 'crowdfunding'] },

View File

@ -1,6 +1,6 @@
import uuid from 'uuid/v4' import uuid from 'uuid/v4'
module.exports = { export default {
id: { type: 'string', primary: true, default: uuid }, id: { type: 'string', primary: true, default: uuid },
name: { type: 'string', required: true, default: false }, name: { type: 'string', required: true, default: false },
slug: { type: 'string' }, slug: { type: 'string' },

View File

@ -1,6 +1,6 @@
import uuid from 'uuid/v4' import uuid from 'uuid/v4'
module.exports = { export default {
id: { type: 'string', primary: true, default: uuid }, id: { type: 'string', primary: true, default: uuid },
createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() }, createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() },
updatedAt: { updatedAt: {

View File

@ -1,6 +1,6 @@
import uuid from 'uuid/v4' import uuid from 'uuid/v4'
module.exports = { export default {
id: { type: 'string', primary: true, default: uuid }, id: { type: 'string', primary: true, default: uuid },
goal: { type: 'number' }, goal: { type: 'number' },
progress: { type: 'number' }, progress: { type: 'number' },

View File

@ -1,4 +1,4 @@
module.exports = { export default {
email: { type: 'string', primary: true, lowercase: true, email: true }, email: { type: 'string', primary: true, lowercase: true, email: true },
createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() }, createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() },
verifiedAt: { type: 'string', isoDate: true }, verifiedAt: { type: 'string', isoDate: true },

View File

@ -1,4 +1,4 @@
module.exports = { export default {
createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() }, createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() },
token: { type: 'string', primary: true, token: true }, token: { type: 'string', primary: true, token: true },
generatedBy: { generatedBy: {

View File

@ -1,4 +1,4 @@
module.exports = { export default {
id: { type: 'string', primary: true }, id: { type: 'string', primary: true },
lat: { type: 'number' }, lat: { type: 'number' },
lng: { type: 'number' }, lng: { type: 'number' },

View File

@ -1,6 +1,6 @@
import uuid from 'uuid/v4' import uuid from 'uuid/v4'
module.exports = { export default {
id: { type: 'string', primary: true, default: uuid }, id: { type: 'string', primary: true, default: uuid },
activityId: { type: 'string', allow: [null] }, activityId: { type: 'string', allow: [null] },
objectId: { type: 'string', allow: [null] }, objectId: { type: 'string', allow: [null] },

View File

@ -1,6 +1,6 @@
import uuid from 'uuid/v4' import uuid from 'uuid/v4'
module.exports = { export default {
id: { type: 'string', primary: true, default: uuid }, id: { type: 'string', primary: true, default: uuid },
createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() }, createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() },
updatedAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() }, updatedAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() },

View File

@ -1,6 +1,6 @@
import uuid from 'uuid/v4' import uuid from 'uuid/v4'
module.exports = { export default {
id: { type: 'string', primary: true, default: uuid }, id: { type: 'string', primary: true, default: uuid },
url: { type: 'string', uri: true, required: true }, url: { type: 'string', uri: true, required: true },
createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() }, createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() },

View File

@ -1,4 +1,4 @@
module.exports = { export default {
id: { type: 'string', primary: true }, id: { type: 'string', primary: true },
deleted: { type: 'boolean', default: false }, deleted: { type: 'boolean', default: false },
disabled: { type: 'boolean', default: false }, disabled: { type: 'boolean', default: false },

View File

@ -1,4 +1,4 @@
module.exports = { export default {
email: { type: 'string', primary: true, lowercase: true, email: true }, email: { type: 'string', primary: true, lowercase: true, email: true },
createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() }, createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() },
nonce: { type: 'string', token: true }, nonce: { type: 'string', token: true },

View File

@ -1,6 +1,6 @@
import uuid from 'uuid/v4' import uuid from 'uuid/v4'
module.exports = { export default {
id: { type: 'string', primary: true, default: uuid }, // TODO: should be type: 'uuid' but simplified for our tests id: { type: 'string', primary: true, default: uuid }, // TODO: should be type: 'uuid' but simplified for our tests
actorId: { type: 'string', allow: [null] }, actorId: { type: 'string', allow: [null] },
name: { type: 'string', disallow: [null], min: 3 }, name: { type: 'string', disallow: [null], min: 3 },

View File

@ -1,16 +1,16 @@
// NOTE: We cannot use `fs` here to clean up the code. Cypress breaks on any npm // NOTE: We cannot use `fs` here to clean up the code. Cypress breaks on any npm
// module that is not browser-compatible. Node's `fs` module is server-side only // module that is not browser-compatible. Node's `fs` module is server-side only
export default { export default {
Badge: require('./Badge.js'), Badge: require('./Badge.js').default,
User: require('./User.js'), User: require('./User.js').default,
EmailAddress: require('./EmailAddress.js'), EmailAddress: require('./EmailAddress.js').default,
UnverifiedEmailAddress: require('./UnverifiedEmailAddress.js'), UnverifiedEmailAddress: require('./UnverifiedEmailAddress.js').default,
SocialMedia: require('./SocialMedia.js'), SocialMedia: require('./SocialMedia.js').default,
Post: require('./Post.js'), Post: require('./Post.js').default,
Comment: require('./Comment.js'), Comment: require('./Comment.js').default,
Category: require('./Category.js'), Category: require('./Category.js').default,
Tag: require('./Tag.js'), Tag: require('./Tag.js').default,
Location: require('./Location.js'), Location: require('./Location.js').default,
Donations: require('./Donations.js'), Donations: require('./Donations.js').default,
Report: require('./Report.js'), Report: require('./Report.js').default,
} }

View File

@ -1,30 +1,5 @@
import fs from 'fs'
import path from 'path' import path from 'path'
import { mergeTypes } from 'merge-graphql-schemas' import { mergeTypes, fileLoader } from 'merge-graphql-schemas'
const findGqlFiles = dir => {
var results = []
var list = fs.readdirSync(dir)
list.forEach(file => {
file = path.join(dir, file).toString('utf-8')
var stat = fs.statSync(file)
if (stat && stat.isDirectory()) {
// Recurse into a subdirectory
results = results.concat(findGqlFiles(file))
} else {
if (path.extname(file) === '.gql') {
// Is a gql file
results.push(file)
}
}
})
return results
}
const typeDefs = []
findGqlFiles(__dirname).forEach(file => {
typeDefs.push(fs.readFileSync(file).toString('utf-8'))
})
const typeDefs = fileLoader(path.join(__dirname, './**/*.gql'))
export default mergeTypes(typeDefs, { all: true }) export default mergeTypes(typeDefs, { all: true })

View File

@ -1,30 +1,18 @@
import { getDriver, getNeode } from '../../bootstrap/neo4j' import { getDriver, getNeode } from '../../bootstrap/neo4j'
import createBadge from './badges.js'
import createUser from './users.js'
import createPost from './posts.js'
import createComment from './comments.js'
import createCategory from './categories.js'
import createTag from './tags.js'
import createSocialMedia from './socialMedia.js'
import createLocation from './locations.js'
import createEmailAddress from './emailAddresses.js'
import createDonations from './donations.js'
import createUnverifiedEmailAddresss from './unverifiedEmailAddresses.js'
import createReport from './reports.js'
const factories = { const factories = {
Badge: createBadge, Badge: require('./badges.js').default,
User: createUser, User: require('./users.js').default,
Post: createPost, Post: require('./posts.js').default,
Comment: createComment, Comment: require('./comments.js').default,
Category: createCategory, Category: require('./categories.js').default,
Tag: createTag, Tag: require('./tags.js').default,
SocialMedia: createSocialMedia, SocialMedia: require('./socialMedia.js').default,
Location: createLocation, Location: require('./locations.js').default,
EmailAddress: createEmailAddress, EmailAddress: require('./emailAddresses.js').default,
UnverifiedEmailAddress: createUnverifiedEmailAddresss, UnverifiedEmailAddress: require('./unverifiedEmailAddresses.js').default,
Donations: createDonations, Donations: require('./donations.js').default,
Report: createReport, Report: require('./reports.js').default,
} }
export const cleanDatabase = async (options = {}) => { export const cleanDatabase = async (options = {}) => {
@ -34,7 +22,7 @@ export const cleanDatabase = async (options = {}) => {
await session.writeTransaction(transaction => { await session.writeTransaction(transaction => {
return transaction.run( return transaction.run(
` `
MATCH (everything) MATCH (everything)
DETACH DELETE everything DETACH DELETE everything
`, `,
) )

View File

@ -1,21 +1,13 @@
import express from 'express' import express from 'express'
import helmet from 'helmet' import helmet from 'helmet'
import { ApolloServer } from 'apollo-server-express' import { ApolloServer } from 'apollo-server-express'
import CONFIG, { requiredConfigs } from './config' import CONFIG from './config'
import middleware from './middleware' import middleware from './middleware'
import { getNeode, getDriver } from './bootstrap/neo4j' import { getNeode, getDriver } from './bootstrap/neo4j'
import decode from './jwt/decode' import decode from './jwt/decode'
import schema from './schema' import schema from './schema'
import webfinger from './activitypub/routes/webfinger' import webfinger from './activitypub/routes/webfinger'
// check required configs and throw error
// TODO check this directly in config file - currently not possible due to testsetup
Object.entries(requiredConfigs).map(entry => {
if (!entry[1]) {
throw new Error(`ERROR: "${entry[0]}" env variable is missing.`)
}
})
const driver = getDriver() const driver = getDriver()
const neode = getNeode() const neode = getNeode()

View File

@ -11,10 +11,19 @@
// This function is called when a project is opened or re-opened (e.g. due to // This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing) // the project's config changing)
const cucumber = require('cypress-cucumber-preprocessor').default const cucumber = require('cypress-cucumber-preprocessor').default
module.exports = on => { const dotenv = require('dotenv')
module.exports = (on, config) => {
// (on, config) => { // (on, config) => {
// `on` is used to hook into various events Cypress emits // `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config // `config` is the resolved Cypress config
const { parsed } = dotenv.config({ path: require.resolve('../../backend/.env') })
config.env.NEO4J_URI = parsed.NEO4J_URI
config.env.NEO4J_USERNAME = parsed.NEO4J_USERNAME
config.env.NEO4J_PASSWORD = parsed.NEO4J_PASSWORD
on('file:preprocessor', cucumber()) on('file:preprocessor', cucumber())
return config
} }

View File

@ -1,9 +1,13 @@
import Factory from '../../backend/src/seed/factories' import Factory from '../../backend/src/seed/factories'
import { getDriver, getNeode } from '../../backend/src/bootstrap/neo4j' import { getDriver, getNeode } from '../../backend/src/bootstrap/neo4j'
import neode from 'neode'
const neo4jDriver = getDriver() const neo4jConfigs = {
const neodeInstance = getNeode() uri: Cypress.env('NEO4J_URI'),
username: Cypress.env('NEO4J_USERNAME'),
password: Cypress.env('NEO4J_PASSWORD')
}
const neo4jDriver = getDriver(neo4jConfigs)
const neodeInstance = getNeode(neo4jConfigs)
const factoryOptions = { neo4jDriver, neodeInstance } const factoryOptions = { neo4jDriver, neodeInstance }
const factory = Factory(factoryOptions) const factory = Factory(factoryOptions)