more test

This commit is contained in:
lesion
2022-03-10 13:51:24 +01:00
parent 9a1f650a3b
commit 2fa24b9478
6 changed files with 61 additions and 18 deletions

9
.gitignore vendored
View File

@@ -1,13 +1,14 @@
# Created by .ignore support plugin (hsz.mobi) # Created by .ignore support plugin (hsz.mobi)
### Gancio dev configuration ### Gancio dev configuration
*.sqlite gancio.sqlite
db.sqlite
releases releases
wp-plugin/wpgancio wp-plugin/wpgancio
config/development.json config/development.json
gancio_config.json /gancio_config.json
config.json /config.json
db.sqlite /assets/config.json
thumb thumb
docs/_site docs/_site
.vscode .vscode

View File

@@ -290,6 +290,13 @@ const eventController = {
res.sendStatus(200) res.sendStatus(200)
}, },
async isAnonEventAllowed (req, res, next) {
if (!res.locals.settings.allow_anon_event) {
return res.sendStatus(403)
}
next()
},
async add (req, res) { async add (req, res) {
// req.err comes from multer streaming error // req.err comes from multer streaming error
if (req.err) { if (req.err) {

View File

@@ -101,7 +101,7 @@ if (config.status !== 'READY') {
*/ */
// allow anyone to add an event (anon event has to be confirmed, TODO: flood protection) // allow anyone to add an event (anon event has to be confirmed, TODO: flood protection)
api.post('/event', upload.single('image'), eventController.add) api.post('/event', eventController.isAnonEventAllowed, upload.single('image'), eventController.add)
api.put('/event', isAuth, upload.single('image'), eventController.update) api.put('/event', isAuth, upload.single('image'), eventController.update)
api.get('/event/import', isAuth, helpers.importURL) api.get('/event/import', isAuth, helpers.importURL)

View File

@@ -1,6 +1,7 @@
const request = require('supertest') const request = require('supertest')
const fs = require('fs')
const admin = { username: 'admin', password: 'SsJOn5l0JpBE', grant_type: 'password', client_id: 'self' } const admin = { username: 'admin', password: 'JqFuXEnkTyOR', grant_type: 'password', client_id: 'self' }
let token let token
// - event list should be empty // - event list should be empty
// - try to write without auth // - try to write without auth
@@ -11,6 +12,7 @@ let token
// - should login with correct authentication // - should login with correct authentication
let app let app
beforeAll( async () => { beforeAll( async () => {
fs.copyFileSync('./starter.sqlite', './testdb.sqlite')
await require('../server/initialize.server.js')() await require('../server/initialize.server.js')()
app = require('../server/routes.js') app = require('../server/routes.js')
}) })
@@ -61,17 +63,29 @@ describe('Authentication / Authorization', () => {
.expect(403) .expect(403)
}) })
// test('should create anon event only when allowed', async () => { test('should create anon event only when allowed', async () => {
// let response let response
// response = await request(app) response = await request(app).post('/api/settings')
// .post('/api/settings') // auth._token.local .send({ key: 'allow_anon_event', value: false })
// .send({ key: 'allow_anon_event', value: false }) .auth(token.access_token, { type: 'bearer' })
// .auth(token.access_token, { type: 'bearer' }) .expect(200)
// .expect(200)
// // expect(response.statusCode).toBe(200) response = await request(app).post('/api/event')
// // response = await request(app).post('/api/settings') .expect(403)
// // .send({ key: 'allow_anon_event', value: false })
// }) response = await request(app).post('/api/settings')
.send({ key: 'allow_anon_event', value: true })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
response = await request(app).post('/api/event')
.send({ title: 'test title', place_name: 'place name', start_datetime: new Date().getTime() * 1000 })
.expect(200)
// expect(response.statusCode).toBe(200)
// response = await request(app).post('/api/settings')
// .send({ key: 'allow_anon_event', value: false })
})
}) })
@@ -87,7 +101,7 @@ describe('Events', () => {
const promises = Object.keys(required_fields).map(async field => { const promises = Object.keys(required_fields).map(async field => {
const response = await request(app).post('/api/event').send(required_fields[field]) const response = await request(app).post('/api/event').send(required_fields[field])
expect(response.statusCode).toBe(400) .expect(400)
expect(response.text).toBe(`${field} is required`) expect(response.text).toBe(`${field} is required`)
return return
}) })

21
tests/seeds/config.json Normal file
View File

@@ -0,0 +1,21 @@
{
"baseurl": "http://localhost:13120",
"hostname": "127.0.0.1",
"server": {
"host": "0.0.0.0",
"port": 13120
},
"log_level": "error",
"log_path": "./logs",
"db": {
"dialect": "sqlite",
"storage": "./testdb.sqlite",
"host": "localhost",
"database": "gancio",
"logging": false,
"dialectOptions": {
"autoJsonMap": false
}
},
"upload_path": "./uploads"
}

BIN
tests/seeds/starter.sqlite Normal file

Binary file not shown.