mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Export a function in server.js and test teardown
This commit is contained in:
parent
6b216be05c
commit
ec9c205bee
@ -1,5 +1,20 @@
|
||||
import { request } from 'graphql-request'
|
||||
import server from './server'
|
||||
import createServer from './server'
|
||||
|
||||
let getHost
|
||||
let app
|
||||
let port
|
||||
|
||||
beforeEach(async () => {
|
||||
const server = createServer()
|
||||
app = await server.start({ port: 0 })
|
||||
port = app.address().port
|
||||
getHost = () => `http://127.0.0.1:${port}`
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await app.close()
|
||||
})
|
||||
|
||||
describe('login', () => {
|
||||
describe('asking for a `token`', () => {
|
||||
@ -15,14 +30,6 @@ describe('login', () => {
|
||||
})
|
||||
|
||||
describe('with a non-existing email', () => {
|
||||
let getHost
|
||||
|
||||
beforeEach(async () => {
|
||||
const app = await server.start({ port: 0 })
|
||||
const { port } = app.address()
|
||||
getHost = () => `http://127.0.0.1:${port}`
|
||||
})
|
||||
|
||||
const mutation = `
|
||||
mutation {
|
||||
login(email:"user@example.com", password:"asdfasd"){
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import server from './server'
|
||||
import createServer from './server'
|
||||
|
||||
const serverConfig = {
|
||||
port: process.env.GRAPHQL_PORT || 4000
|
||||
@ -8,6 +8,7 @@ const serverConfig = {
|
||||
// }
|
||||
}
|
||||
|
||||
const server = createServer()
|
||||
server.start(serverConfig, options => {
|
||||
/* eslint-disable-next-line no-console */
|
||||
console.log(`Server ready at ${process.env.GRAPHQL_URI} 🚀`)
|
||||
|
||||
@ -22,10 +22,6 @@ let schema = makeExecutableSchema({
|
||||
|
||||
const driver = neo4j().getDriver()
|
||||
|
||||
const MOCK = (process.env.MOCK === 'true')
|
||||
/* eslint-disable-next-line no-console */
|
||||
console.log('MOCK:', MOCK)
|
||||
|
||||
schema = augmentSchema(schema, {
|
||||
query: {
|
||||
exclude: ['Statistics', 'LoggedInUser']
|
||||
@ -36,31 +32,35 @@ schema = augmentSchema(schema, {
|
||||
})
|
||||
schema = applyScalars(applyDirectives(schema))
|
||||
|
||||
const server = new GraphQLServer({
|
||||
context: async (req) => {
|
||||
const payload = {
|
||||
driver,
|
||||
user: null,
|
||||
req: req.request
|
||||
}
|
||||
try {
|
||||
const token = payload.req.headers.authorization.replace('Bearer ', '')
|
||||
payload.user = await jwt.verify(token, process.env.JWT_SECRET)
|
||||
} catch (err) {
|
||||
// nothing
|
||||
}
|
||||
const createServer = (options) => {
|
||||
const defaults = {
|
||||
context: async (req) => {
|
||||
const payload = {
|
||||
driver,
|
||||
user: null,
|
||||
req: req.request
|
||||
}
|
||||
try {
|
||||
const token = payload.req.headers.authorization.replace('Bearer ', '')
|
||||
payload.user = await jwt.verify(token, process.env.JWT_SECRET)
|
||||
} catch (err) {
|
||||
// nothing
|
||||
}
|
||||
|
||||
return payload
|
||||
},
|
||||
schema: schema,
|
||||
tracing: true,
|
||||
middlewares: middleware(schema),
|
||||
mocks: MOCK ? mocks : false
|
||||
})
|
||||
return payload
|
||||
},
|
||||
schema: schema,
|
||||
tracing: true,
|
||||
middlewares: middleware(schema),
|
||||
mocks: (process.env.MOCK === 'true') ? mocks : false
|
||||
}
|
||||
const server = new GraphQLServer(Object.assign({}, defaults, options))
|
||||
|
||||
passport.use('jwt', jwtStrategy())
|
||||
server.express.use(passport.initialize())
|
||||
passport.use('jwt', jwtStrategy())
|
||||
server.express.use(passport.initialize())
|
||||
|
||||
server.express.post('/graphql', passport.authenticate(['jwt'], { session: false }))
|
||||
server.express.post('/graphql', passport.authenticate(['jwt'], { session: false }))
|
||||
return server
|
||||
}
|
||||
|
||||
export default server
|
||||
export default createServer
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user