mirror of
https://github.com/Permissionless-Software-Foundation/x402-base-facilitator.git
synced 2026-09-21 16:52:01 -07:00
Merge pull request #1 from christroutner/controlled-start
Enabling Cors + Controlled Start
This commit is contained in:
+17
@@ -0,0 +1,17 @@
|
||||
# This is a node.js v8+ JavaScript project
|
||||
language: node_js
|
||||
node_js:
|
||||
- "8"
|
||||
|
||||
# Build on Ubuntu Trusty (14.04)
|
||||
# https://docs.travis-ci.com/user/reference/trusty/#javascript-and-nodejs-images
|
||||
dist: trusty
|
||||
sudo: required
|
||||
|
||||
# Use Docker
|
||||
services:
|
||||
- docker
|
||||
|
||||
before_install:
|
||||
- ./install-mongo
|
||||
#- npm install -g mocha
|
||||
+38
-24
@@ -7,42 +7,56 @@ const session = require('koa-generic-session')
|
||||
const passport = require('koa-passport')
|
||||
const mount = require('koa-mount')
|
||||
const serve = require('koa-static')
|
||||
const cors = require('kcors')
|
||||
|
||||
const config = require('../config')
|
||||
const errorMiddleware = require('../src/middleware')
|
||||
|
||||
// Create a Koa instance.
|
||||
const app = new Koa()
|
||||
app.keys = [config.session]
|
||||
async function startServer () {
|
||||
// Create a Koa instance.
|
||||
const app = new Koa()
|
||||
app.keys = [config.session]
|
||||
|
||||
// Connect to the Mongo Database.
|
||||
mongoose.Promise = global.Promise
|
||||
mongoose.connect(config.database)
|
||||
// Connect to the Mongo Database.
|
||||
mongoose.Promise = global.Promise
|
||||
await mongoose.connect(config.database)
|
||||
|
||||
// MIDDLEWARE START
|
||||
// MIDDLEWARE START
|
||||
|
||||
app.use(convert(logger()))
|
||||
app.use(bodyParser())
|
||||
app.use(session())
|
||||
app.use(errorMiddleware())
|
||||
app.use(convert(logger()))
|
||||
app.use(bodyParser())
|
||||
app.use(session())
|
||||
app.use(errorMiddleware())
|
||||
|
||||
// Used to generate the docs.
|
||||
app.use(convert(mount('/docs', serve(`${process.cwd()}/docs`))))
|
||||
// Used to generate the docs.
|
||||
app.use(convert(mount('/docs', serve(`${process.cwd()}/docs`))))
|
||||
|
||||
// User Authentication
|
||||
require('../config/passport')
|
||||
app.use(passport.initialize())
|
||||
app.use(passport.session())
|
||||
// User Authentication
|
||||
require('../config/passport')
|
||||
app.use(passport.initialize())
|
||||
app.use(passport.session())
|
||||
|
||||
// Custom Middleware Modules
|
||||
const modules = require('../src/modules')
|
||||
modules(app)
|
||||
// Custom Middleware Modules
|
||||
const modules = require('../src/modules')
|
||||
modules(app)
|
||||
|
||||
// MIDDLEWARE END
|
||||
// Enable CORS for testing
|
||||
//app.use(cors({origin: '*'}))
|
||||
|
||||
app.listen(config.port, () => {
|
||||
// MIDDLEWARE END
|
||||
|
||||
// app.listen(config.port, () => {
|
||||
// console.log(`Server started on ${config.port}`)
|
||||
// })
|
||||
await app.listen(config.port)
|
||||
console.log(`Server started on ${config.port}`)
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
// startServer()
|
||||
|
||||
// export default app
|
||||
module.exports = app
|
||||
// module.exports = app
|
||||
module.exports = {
|
||||
startServer
|
||||
}
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
require('./bin/server.js')
|
||||
const server = require('./bin/server.js')
|
||||
|
||||
server.startServer()
|
||||
|
||||
+4
-1
@@ -36,6 +36,7 @@
|
||||
"bcrypt": "^0.8.5",
|
||||
"glob": "^7.0.0",
|
||||
"jsonwebtoken": "^7.1.9",
|
||||
"kcors": "^2.2.1",
|
||||
"koa": "^2.5.0",
|
||||
"koa-bodyparser": "^4.2.0",
|
||||
"koa-convert": "^1.2.0",
|
||||
@@ -46,7 +47,9 @@
|
||||
"koa-router": "^7.0.1",
|
||||
"koa-static": "^4.0.2",
|
||||
"mongoose": "^5.0.8",
|
||||
"passport-local": "^1.0.0"
|
||||
"passport-local": "^1.0.0",
|
||||
"request": "^2.85.0",
|
||||
"request-promise": "^4.2.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chai": "^4.1.2",
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
const app = require('../bin/server')
|
||||
// const supertest = require('supertest')
|
||||
// const expect = require('chai').expect
|
||||
const should = require('chai').should
|
||||
// const cleanDb = require('./utils').cleanDb
|
||||
// const authUser = require('./utils').authUser
|
||||
const utils = require('./utils')
|
||||
|
||||
const rp = require('request-promise')
|
||||
const assert = require('chai').assert
|
||||
|
||||
should()
|
||||
// const request = supertest.agent(app.listen())
|
||||
const context = {}
|
||||
|
||||
const LOCALHOST = 'http://localhost:5000'
|
||||
|
||||
|
||||
|
||||
describe('Auth', () => {
|
||||
before(async () => {
|
||||
|
||||
await app.startServer()
|
||||
|
||||
utils.cleanDb()
|
||||
|
||||
/*
|
||||
authUser(request, (err, { user, token }) => {
|
||||
if (err) { return done(err) }
|
||||
|
||||
context.user = user
|
||||
context.token = token
|
||||
done()
|
||||
})
|
||||
*/
|
||||
|
||||
const userObj = {
|
||||
username: 'test',
|
||||
password: 'pass'
|
||||
}
|
||||
const testUser = await utils.createUser(userObj)
|
||||
|
||||
context.user = testUser.user
|
||||
context.token = testUser.token
|
||||
})
|
||||
|
||||
describe('POST /auth', () => {
|
||||
it('should throw 401 if credentials are incorrect', async () => {
|
||||
try {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
uri: `${LOCALHOST}/auth`,
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
username: 'test',
|
||||
password: 'wrongpassword'
|
||||
}
|
||||
}
|
||||
|
||||
let result = await rp(options)
|
||||
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
console.log(`result stringified: ${JSON.stringify(result, null, 2)}`)
|
||||
assert(false, 'Unexpected result')
|
||||
} catch (err) {
|
||||
if (err.statusCode === 422) {
|
||||
assert(err.statusCode === 422, 'Error code 422 expected.')
|
||||
} else if (err.statusCode === 401) {
|
||||
assert(err.statusCode === 401, 'Error code 401 expected.')
|
||||
} else {
|
||||
console.error('Error: ', err)
|
||||
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
|
||||
throw err
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it('should auth user', async () => {
|
||||
try {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
uri: `${LOCALHOST}/auth`,
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
username: 'test',
|
||||
password: 'pass'
|
||||
}
|
||||
}
|
||||
|
||||
let result = await rp(options)
|
||||
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert(result.statusCode === 200, 'Status Code 200 expected.')
|
||||
assert(result.body.user.username === 'test', 'Username of test expected')
|
||||
assert(result.body.user.password === undefined, 'Password expected to be omited')
|
||||
} catch (err) {
|
||||
console.log('Error authenticating test user: ' + JSON.stringify(err, null, 2))
|
||||
throw err
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,52 +1,78 @@
|
||||
const app = require('../bin/server')
|
||||
const supertest = require('supertest')
|
||||
// const { expect, should } = require('chai')
|
||||
const expect = require('chai').expect
|
||||
const should = require('chai').should
|
||||
// const { cleanDb, authUser } = require('./utils')
|
||||
const cleanDb = require('./utils').cleanDb
|
||||
//const authUser = require('./utils').authUser
|
||||
const utils = require('./utils')
|
||||
|
||||
const rp = require('request-promise')
|
||||
const assert = require('chai').assert
|
||||
|
||||
const LOCALHOST = 'http://localhost:5000'
|
||||
|
||||
should()
|
||||
const request = supertest.agent(app.listen())
|
||||
const context = {}
|
||||
|
||||
describe('Users', () => {
|
||||
before((done) => {
|
||||
cleanDb()
|
||||
done()
|
||||
before(async () => {
|
||||
utils.cleanDb()
|
||||
})
|
||||
|
||||
describe('POST /users', () => {
|
||||
it('should reject signup when data is incomplete', (done) => {
|
||||
request
|
||||
.post('/users')
|
||||
.set('Accept', 'application/json')
|
||||
.send({ username: 'supercoolname' })
|
||||
.expect(422, done)
|
||||
it('should reject signup when data is incomplete', async () => {
|
||||
try {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
uri: `${LOCALHOST}/users`,
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
username: 'supercoolname'
|
||||
}
|
||||
}
|
||||
|
||||
let result = await rp(options)
|
||||
|
||||
console.log(`result stringified: ${JSON.stringify(result, null, 2)}`)
|
||||
assert(false, 'Unexpected result')
|
||||
} catch (err) {
|
||||
if (err.statusCode === 422) {
|
||||
assert(err.statusCode === 422, 'Error code 422 expected.')
|
||||
} else if (err.statusCode === 401) {
|
||||
assert(err.statusCode === 401, 'Error code 401 expected.')
|
||||
} else {
|
||||
console.error('Error: ', err)
|
||||
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
|
||||
throw err
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it('should sign up', (done) => {
|
||||
request
|
||||
.post('/users')
|
||||
.set('Accept', 'application/json')
|
||||
.send({ user: { username: 'supercoolname', password: 'supersecretpassword' } })
|
||||
.expect(200, (err, res) => {
|
||||
if (err) { return done(err) }
|
||||
it('should sign up', async () => {
|
||||
try {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
uri: `${LOCALHOST}/users`,
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
user: { username: 'supercoolname', password: 'supersecretpassword' }
|
||||
}
|
||||
}
|
||||
|
||||
res.body.user.should.have.property('username')
|
||||
res.body.user.username.should.equal('supercoolname')
|
||||
expect(res.body.user.password).to.not.exist
|
||||
let result = await rp(options)
|
||||
|
||||
context.user = res.body.user
|
||||
context.token = res.body.token
|
||||
// console.log(`token: ${res.body.token}`)
|
||||
result.body.user.should.have.property('username')
|
||||
result.body.user.username.should.equal('supercoolname')
|
||||
expect(result.body.user.password).to.not.exist
|
||||
|
||||
done()
|
||||
})
|
||||
context.user = result.body.user
|
||||
context.token = result.body.token
|
||||
|
||||
} catch (err) {
|
||||
console.log('Error authenticating test user: ' + JSON.stringify(err, null, 2))
|
||||
throw err
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
/*
|
||||
describe('GET /users', () => {
|
||||
it('should not fetch users if the authorization header is missing', (done) => {
|
||||
request
|
||||
@@ -237,5 +263,7 @@ describe('Users', () => {
|
||||
})
|
||||
.expect(200, done)
|
||||
})
|
||||
|
||||
})
|
||||
*/
|
||||
})
|
||||
@@ -1,54 +0,0 @@
|
||||
const app = require('../bin/server')
|
||||
const supertest = require('supertest')
|
||||
//const { expect, should } = require('chai')
|
||||
const expect = require('chai').expect;
|
||||
const should = require('chai').should;
|
||||
//const { cleanDb, authUser } = require('./utils')
|
||||
const cleanDb = require('./utils').cleanDb
|
||||
const authUser = require('./utils').authUser
|
||||
|
||||
should()
|
||||
const request = supertest.agent(app.listen())
|
||||
const context = {}
|
||||
|
||||
describe('Auth', () => {
|
||||
before((done) => {
|
||||
cleanDb()
|
||||
authUser(request, (err, { user, token }) => {
|
||||
if (err) { return done(err) }
|
||||
|
||||
context.user = user
|
||||
context.token = token
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
describe('POST /auth', () => {
|
||||
it('should throw 401 if credentials are incorrect', (done) => {
|
||||
request
|
||||
.post('/auth')
|
||||
.set('Accept', 'application/json')
|
||||
.send({ username: 'supercoolname', password: 'wrongpassword' })
|
||||
.expect(401, done)
|
||||
})
|
||||
|
||||
it('should auth user', (done) => {
|
||||
request
|
||||
.post('/auth')
|
||||
.set('Accept', 'application/json')
|
||||
.send({ username: 'test', password: 'pass' })
|
||||
.expect(200, (err, res) => {
|
||||
if (err) { return done(err) }
|
||||
|
||||
res.body.user.should.have.property('username')
|
||||
res.body.user.username.should.equal('test')
|
||||
expect(res.body.user.password).to.not.exist
|
||||
|
||||
context.user = res.body.user
|
||||
context.token = res.body.token
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
+39
-1
@@ -1,4 +1,7 @@
|
||||
const mongoose = require('mongoose')
|
||||
const rp = require('request-promise')
|
||||
|
||||
const LOCALHOST = 'http://localhost:5000'
|
||||
|
||||
function cleanDb () {
|
||||
for (const collection in mongoose.connection.collections) {
|
||||
@@ -23,7 +26,42 @@ function authUser (agent, callback) {
|
||||
})
|
||||
}
|
||||
|
||||
// This function is used to create new users.
|
||||
// userObj = {
|
||||
// username,
|
||||
// password
|
||||
// }
|
||||
async function createUser (userObj) {
|
||||
try {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
uri: `${LOCALHOST}/users`,
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
user: {
|
||||
username: userObj.username,
|
||||
password: userObj.password
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let result = await rp(options)
|
||||
|
||||
const retObj = {
|
||||
user: result.body.user,
|
||||
token: result.body.token
|
||||
}
|
||||
|
||||
return retObj
|
||||
} catch (err) {
|
||||
console.log('Error in utils.js/createUser(): ' + JSON.stringify(err, null, 2))
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
cleanDb,
|
||||
authUser
|
||||
authUser,
|
||||
createUser
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user