mirror of
https://github.com/bellingcat/datasheet-server.git
synced 2026-06-11 21:08:33 +03:00
new server test with precommit hook to secure .env files, set up CI environment (#5)
* new server test with precommit hook to secure .env files, set up CI environment (#3) * [TESTS] added all registered routes to api server test * [WIP] abstracting config to env where it makes sense, refactoring elsewhere, adding more tests * [WIP] fixed tests so they fail as expected * [WIP] * [DEBUG] fixed issues with the env configuration, added correct tests * [MISC] env didn't get readded on last precommit * [TESTS] added longer wait time for server as sometimtimes tests fail arbitrarily
This commit is contained in:
@@ -15,6 +15,20 @@ export default ({ config, controller }) => {
|
||||
res.json(controller.blueprints())
|
||||
})
|
||||
|
||||
api.get('/update', (req, res) => {
|
||||
controller
|
||||
.update()
|
||||
.then(msg =>
|
||||
res.json({
|
||||
success: msg
|
||||
})
|
||||
)
|
||||
.catch(err =>
|
||||
res.status(404)
|
||||
.send({ error: err.message, err })
|
||||
)
|
||||
})
|
||||
|
||||
api.get('/:sheet/:tab/:resource/:frag', (req, res) => {
|
||||
const { sheet, tab, resource, frag } = req.params
|
||||
controller
|
||||
@@ -27,8 +41,9 @@ export default ({ config, controller }) => {
|
||||
})
|
||||
|
||||
api.get('/:sheet/:tab/:resource', (req, res) => {
|
||||
const { sheet, tab, resource } = req.params
|
||||
controller
|
||||
.retrieve(req.params.sheet, req.params.tab, req.params.resource)
|
||||
.retrieve(sheet, tab, resource)
|
||||
.then(data => res.json(data))
|
||||
.catch(err =>
|
||||
res.status(err.status || 404)
|
||||
@@ -36,30 +51,16 @@ export default ({ config, controller }) => {
|
||||
)
|
||||
})
|
||||
|
||||
api.get('/update', (req, res) => {
|
||||
controller
|
||||
.update()
|
||||
.then(msg =>
|
||||
res.json({
|
||||
success: msg
|
||||
})
|
||||
)
|
||||
.catch(err =>
|
||||
res.status(404)
|
||||
.send({ error: err.message })
|
||||
)
|
||||
})
|
||||
|
||||
// ERROR routes. Note that it is important that these come AFTER routes
|
||||
// like /update, so that the regex does not greedily match these routes.
|
||||
|
||||
api.get('/:sheet', (req, res) => {
|
||||
res.status(404)
|
||||
res.status(400)
|
||||
.send({ error: copy.errors.onlysheet })
|
||||
})
|
||||
|
||||
api.get('/:sheet/:tab', (req, res) => {
|
||||
res.status(404)
|
||||
res.status(400)
|
||||
.send({ error: copy.errors.onlyTab })
|
||||
})
|
||||
|
||||
|
||||
@@ -3,12 +3,10 @@ import BP from './lib/blueprinters'
|
||||
export default {
|
||||
port: 4040,
|
||||
googleSheets: {
|
||||
email: 'SOME_SERVICE_ACCOUNT_EMAIL',
|
||||
privateKey: 'SOME_SERVICE_ACCOUNT_PRIVATE_KEY',
|
||||
sheets: [
|
||||
{
|
||||
name: 'example',
|
||||
id: '1UC7DkCFeUXHfpUxUGruExwFbP4pqVBdJLOKfo6wDDGk',
|
||||
id: '15gb_aYJw7WSVZmtS0FZvcpGx1-kwKX3VHH8YV6La4hY',
|
||||
tabs: {
|
||||
export_events: [BP.deeprows, BP.rows],
|
||||
export_categories: [BP.groups, BP.rows],
|
||||
|
||||
@@ -3,7 +3,10 @@ import express from 'express'
|
||||
import initialize from './initialize'
|
||||
import middleware from './middleware'
|
||||
import api from './api'
|
||||
import config from './config'
|
||||
// import config from './sheets_config'
|
||||
import dotenv from 'dotenv'
|
||||
|
||||
dotenv.config()
|
||||
|
||||
let app = express()
|
||||
app.server = http.createServer(app)
|
||||
@@ -15,6 +18,8 @@ if (process.env.NODE_ENV === 'development') {
|
||||
app.use(cors())
|
||||
}
|
||||
|
||||
const config = process.env
|
||||
|
||||
initialize(controller => {
|
||||
app.use(
|
||||
middleware({
|
||||
@@ -30,7 +35,7 @@ initialize(controller => {
|
||||
})
|
||||
)
|
||||
|
||||
app.server.listen(process.env.PORT || config.port, () => {
|
||||
app.server.listen(process.env.PORT || 4040, () => {
|
||||
console.log(`Started on port ${app.server.address().port}`)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import StoreJson from './models/StoreJson'
|
||||
import Fetcher from './lib/Fetcher'
|
||||
import Controller from './lib/Controller'
|
||||
import config from './config'
|
||||
import sheetsConfig from './sheets_config'
|
||||
|
||||
const { googleSheets } = config
|
||||
const { sheets, privateKey, email } = googleSheets
|
||||
const { googleSheets } = sheetsConfig
|
||||
const { sheets } = googleSheets
|
||||
|
||||
function authenticate (_fetcher) {
|
||||
return _fetcher.fetcher.authenticate(email, privateKey).then(msg => {
|
||||
return _fetcher.fetcher.authenticate(process.env.SERVICE_ACCOUNT_EMAIL, process.env.SERVICE_ACCOUNT_PRIVATE_KEY).then(msg => {
|
||||
console.log(msg)
|
||||
return true
|
||||
})
|
||||
@@ -24,7 +24,7 @@ export default callback => {
|
||||
Promise.all(fetchers.map(authenticate))
|
||||
.then(() => {
|
||||
console.log(`===================`)
|
||||
console.log(`grant access to: ${email}`)
|
||||
console.log(`grant access to: ${process.env.SERVICE_ACCOUNT_EMAIL}`)
|
||||
console.log(`===================`)
|
||||
|
||||
// NB: reformat fetchers as config for controller
|
||||
@@ -39,7 +39,7 @@ export default callback => {
|
||||
console.log(err)
|
||||
console.log(
|
||||
`ERROR: the server couldn't connect to all of the sheets you provided. Ensure you have granted access to ${
|
||||
email
|
||||
process.env.SERVICE_ACCOUNT_EMAIL
|
||||
} on ALL listed sheets.`
|
||||
)
|
||||
})
|
||||
|
||||
@@ -36,6 +36,7 @@ class Controller {
|
||||
retrieve (sheet, tab, resource) {
|
||||
if (this._sheetExists(sheet)) {
|
||||
const fetcher = this.fetchers[sheet]
|
||||
// console.log(fetcher);
|
||||
return fetcher.retrieve(tab, resource)
|
||||
} else {
|
||||
return Promise.reject(new Error(copy.errors.noResource(sheet)))
|
||||
|
||||
@@ -189,7 +189,7 @@ class Fetcher {
|
||||
|
||||
retrieveFrag (tab, resource, frag) {
|
||||
const title = fmtName(tab)
|
||||
const url = `${this.id}/${tab}/${resource}/${frag}`
|
||||
const url = `${this.sheetName}/${tab}/${resource}/${frag || ''}`
|
||||
return this.db.load(url, this.blueprints[title])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Router } from 'express'
|
||||
import { mapboxAccessToken } from '../config'
|
||||
import morgan from 'morgan'
|
||||
import mapbox from './mapbox'
|
||||
|
||||
@@ -10,8 +9,8 @@ export default ({ config, db }) => {
|
||||
/* logging middleware */
|
||||
routes.use(morgan('dev'))
|
||||
|
||||
if (mapboxAccessToken) {
|
||||
routes.get('/mapbox/:z/:y/:x', mapbox(mapboxAccessToken))
|
||||
if (process.env.MAPBOX_TOKEN) {
|
||||
routes.get('/mapbox/:z/:y/:x', mapbox(process.env.MAPBOX_TOKEN))
|
||||
}
|
||||
|
||||
return routes
|
||||
|
||||
19
src/sheets_config.js
Normal file
19
src/sheets_config.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import BP from './lib/blueprinters'
|
||||
|
||||
export default {
|
||||
googleSheets: {
|
||||
sheets: [
|
||||
{
|
||||
name: 'example',
|
||||
id: '1UC7DkCFeUXHfpUxUGruExwFbP4pqVBdJLOKfo6wDDGk',
|
||||
tabs: {
|
||||
export_events: [BP.deeprows, BP.rows],
|
||||
export_categories: [BP.groups, BP.rows],
|
||||
export_sources: BP.ids,
|
||||
export_sites: BP.rows,
|
||||
export_tags: BP.tree
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user