update deps, node >= 14, add mariadb and postgres to tests

This commit is contained in:
lesion
2022-07-27 11:23:57 +02:00
parent b54fca0ce8
commit 4ac78db475
10 changed files with 251 additions and 136 deletions

View File

@@ -1,21 +1,38 @@
const request = require('supertest')
const fs = require('fs')
const dayjs = require('dayjs')
const path = require('path')
const admin = { username: 'admin', password: 'test', grant_type: 'password', client_id: 'self' }
const admin = { username: 'admin', password: 'JqFuXEnkTyOR', grant_type: 'password', client_id: 'self' }
let token
// - event list should be empty
// - try to write without auth
// - registration should be not allowed when disabled
// - registration should create a new user (not active) when enabled
// - unconfirmed user cannot login
// - should not login without auth data
// - should login with correct authentication
let app
let places = []
beforeAll( async () => {
fs.copyFileSync('./starter.sqlite', './testdb.sqlite')
await require('../server/initialize.server.js').start()
app = require('../server/routes.js').handler
switch (process.env.DB) {
case 'mariadb':
process.env.config_path = path.resolve(__dirname, './seeds/config.mariadb.json')
break
case 'postgresql':
process.env.config_path = path.resolve(__dirname, './seeds/config.postgres.json')
break
case 'sqlite':
default:
process.env.config_path = path.resolve(__dirname, './seeds/config.sqlite.json')
}
app = await require('../server/routes.js').main()
const { sequelize } = require('../server/api/models/index')
await sequelize.query('DELETE FROM settings')
await sequelize.query('DELETE FROM events')
await sequelize.query('DELETE FROM users')
await sequelize.query('DELETE FROM ap_users')
await sequelize.query('DELETE FROM tags')
await sequelize.query('DELETE FROM places')
await sequelize.query('DELETE FROM collections')
})
afterAll( async () => {
await require('../server/initialize.server.js').shutdown(false)
})
describe('Basic', () => {
@@ -35,9 +52,18 @@ describe('Authentication / Authorization', () => {
test('should not authenticate with wrong user/password', () => {
return request(app).post('/oauth/login')
.set('Content-Type', 'application/x-www-form-urlencoded')
.expect(500)
})
test('shoud register an admin as first user', async () => {
const response = await request(app)
.post('/api/user/register')
.send({ email: 'admin', password: 'test' })
.expect(200)
expect(response.body.id).toBeDefined()
})
test('should authenticate with correct user/password', async () => {
const response = await request(app)
.post('/oauth/login')
@@ -72,7 +98,7 @@ describe('Events', () => {
const required_fields = {
'title': {},
'start_datetime': { title: 'test title' },
'place_id or place_name and place_address': { title: 'test title', start_datetime: new Date().getTime() * 1000, place_name: 'test place name'},
'place_id or place_name and place_address': { title: 'test title', start_datetime: dayjs().unix()+1000, place_name: 'test place name'},
}
const promises = Object.keys(required_fields).map(async field => {
@@ -80,47 +106,51 @@ describe('Events', () => {
.expect(400)
expect(response.text).toBe(`${field} required`)
})
return Promise.all(promises)
})
test('should create anon event only when allowed', async () => {
await request(app).post('/api/settings')
.send({ key: 'allow_anon_event', value: false })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
await request(app).post('/api/event')
.expect(403)
await request(app).post('/api/event')
.send({ title: 'test title', place_name: 'place name', place_address: 'address', tags: ['test'], start_datetime: new Date().getTime() * 1000 })
let response = await request(app).post('/api/event')
.send({ title: 'test title 2', place_name: 'place name', place_address: 'address', tags: ['test'], start_datetime: dayjs().unix()+1000 })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(response.body.place.id).toBeDefined()
places.push(response.body.place.id)
await request(app).post('/api/settings')
.send({ key: 'allow_anon_event', value: true })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
return request(app).post('/api/event')
.send({ title: 'test title', place_name: 'place name 2', place_address: 'address 2', tags: ['test'], start_datetime: new Date().getTime() * 1000 })
response = await request(app).post('/api/event')
.send({ title: 'test title 3', place_name: 'place name 2', place_address: 'address 2', tags: ['test'], start_datetime: dayjs().unix()+1000 })
.expect(200)
expect(response.body.place.id).toBeDefined()
places.push(response.body.place.id)
})
test('should trim tags', async () => {
const event = {
title: 'test title',
place_id: 1,
start_datetime: dayjs().unix(),
title: 'test title 4',
place_id: places[0],
start_datetime: dayjs().unix()+1000,
tags: [' test tag ']
}
const response = await request(app).post('/api/event')
.send(event)
.expect(200)
@@ -133,7 +163,7 @@ describe('Events', () => {
describe('Tags', () => {
test('should create event with tags', async () => {
const event = await request(app).post('/api/event')
.send({ title: 'test tags', place_id: 2, start_datetime: new Date().getTime() * 1000, tags: ['tag1', 'Tag2', 'tAg3'] })
.send({ title: 'test tags', place_id: places[1], start_datetime: dayjs().unix()+1000 , tags: ['tag1', 'Tag2', 'tAg3'] })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
@@ -142,7 +172,7 @@ describe('Tags', () => {
test('should create event trimming tags / ignore sensitiviness', async () => {
const event = await request(app).post('/api/event')
.send({ title: 'test trimming tags', place_id: 2, start_datetime: new Date().getTime() * 1000, tags: ['Tag1', 'taG2 '] })
.send({ title: 'test trimming tags', place_id: places[1], start_datetime: dayjs().unix()+1000, tags: ['Tag1', 'taG2 '] })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
@@ -155,8 +185,6 @@ describe('Tags', () => {
const response = await request(app).get('/api/events?tags=tAg3')
.expect(200)
// console.error(response.body)
// console.error(response.body[0].tags)
expect(response.body.length).toBe(1)
// expect(response.body[0].title).toBe('test tags')
expect(response.body[0].tags.length).toBe(3)
@@ -194,6 +222,8 @@ describe('Place', () => {
})
let collections = []
let filters = []
describe ('Collection', () => {
test('should not create a new collection if not allowed', () => {
return request(app).post('/api/collections')
@@ -206,7 +236,8 @@ describe ('Collection', () => {
.send({ name: 'test collection' })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(response.body.id).toBe(1)
expect(response.body.id).toBeDefined()
collections.push(response.body.id)
})
test('should do not have any event when no filters', async () => {
@@ -220,14 +251,16 @@ describe ('Collection', () => {
test('should add a new filter', async () => {
await request(app)
.post('/api/filter')
.send({ collectionId: collections[0], tags: ['test'] })
.expect(403)
const response = await request(app).post('/api/filter')
.send({ collectionId: 1, tags: ['test'] })
.send({ collectionId: collections[0], tags: ['test'] })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(response.body.id).toBe(1)
expect(response.body.id).toBeDefined()
filters.push(response.body.id)
})
@@ -241,36 +274,36 @@ describe ('Collection', () => {
test('should remove filter', async () => {
await request(app)
.delete('/api/filter/1')
.delete(`/api/filter/${filters[0]}`)
.expect(403)
await request(app)
.delete('/api/filter/1')
.delete(`/api/filter/${filters[0]}`)
.auth(token.access_token, { type: 'bearer' })
.expect(200)
const response = await request(app)
.get('/api/filter/1')
.get(`/api/filter/${filters[0]}`)
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(response.body.length).toBe(0)
})
test('shoud filter for tags', async () => {
await request(app)
let response = await request(app)
.post('/api/filter')
.send({ collectionId: 1, tags: ['test'] })
.send({ collectionId: collections[0], tags: ['test'] })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
let response = await request(app)
.get('/api/filter/1')
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(response.body.length).toBe(1)
// response = await request(app)
// .get(`/api/filter/${response.body.id}`)
// .auth(token.access_token, { type: 'bearer' })
// .expect(200)
// expect(response.body.length).toBe(1)
response = await request(app)
.get(`/api/collections/test collection`)
.expect(200)
@@ -279,4 +312,4 @@ describe ('Collection', () => {
})
})
})