2022-03-10 12:34:12 +01:00
|
|
|
const request = require('supertest')
|
2022-05-05 11:12:42 +02:00
|
|
|
const dayjs = require('dayjs')
|
2022-07-27 11:23:57 +02:00
|
|
|
const path = require('path')
|
|
|
|
|
|
|
|
const admin = { username: 'admin', password: 'test', grant_type: 'password', client_id: 'self' }
|
2022-03-10 12:34:12 +01:00
|
|
|
|
|
|
|
let token
|
|
|
|
let app
|
2022-07-27 11:23:57 +02:00
|
|
|
let places = []
|
|
|
|
|
2022-03-10 12:34:12 +01:00
|
|
|
beforeAll( async () => {
|
2022-07-27 11:23:57 +02:00
|
|
|
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')
|
2022-07-29 11:29:45 +02:00
|
|
|
await sequelize.query('DELETE FROM filters')
|
2022-07-27 11:23:57 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
afterAll( async () => {
|
|
|
|
await require('../server/initialize.server.js').shutdown(false)
|
2022-03-10 12:34:12 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('Basic', () => {
|
|
|
|
test('shoud return an empty list', async () => {
|
|
|
|
const response = await request(app).get('/api/events')
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
expect(response.body.length).toBe(0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Authentication / Authorization', () => {
|
|
|
|
test('should not return an user when not authenticated', () => {
|
|
|
|
return request(app).get('/api/user')
|
|
|
|
.expect(403)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should not authenticate with wrong user/password', () => {
|
|
|
|
return request(app).post('/oauth/login')
|
2022-07-27 11:23:57 +02:00
|
|
|
.set('Content-Type', 'application/x-www-form-urlencoded')
|
2022-03-10 12:34:12 +01:00
|
|
|
.expect(500)
|
|
|
|
})
|
|
|
|
|
2022-07-29 11:29:45 +02:00
|
|
|
test('should register an admin as first user', async () => {
|
2022-07-27 11:23:57 +02:00
|
|
|
const response = await request(app)
|
|
|
|
.post('/api/user/register')
|
|
|
|
.send({ email: 'admin', password: 'test' })
|
|
|
|
.expect(200)
|
|
|
|
expect(response.body.id).toBeDefined()
|
2022-07-29 11:29:45 +02:00
|
|
|
return response
|
2022-07-27 11:23:57 +02:00
|
|
|
})
|
|
|
|
|
2022-03-10 12:34:12 +01:00
|
|
|
test('should authenticate with correct user/password', async () => {
|
2022-05-20 12:36:06 +02:00
|
|
|
const response = await request(app)
|
|
|
|
.post('/oauth/login')
|
2022-03-10 12:34:12 +01:00
|
|
|
.set('Content-Type', 'application/x-www-form-urlencoded')
|
|
|
|
.send(admin)
|
|
|
|
.expect(200)
|
|
|
|
expect(response.body.refresh_token).toBeDefined()
|
|
|
|
expect(response.body.access_token).toBeDefined()
|
|
|
|
expect(response.body.token_type).toBe('Bearer')
|
|
|
|
token = response.body
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should get user when authenticated', async () => {
|
|
|
|
const response = await request(app).get('/api/user')
|
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
|
|
|
expect(response.body.email).toBe(admin.username)
|
|
|
|
expect(response.body.is_admin).toBe(true)
|
|
|
|
})
|
2022-07-29 11:29:45 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('Settings', () => {
|
2022-03-10 12:34:12 +01:00
|
|
|
|
|
|
|
test('should not change settings when not allowed', async () => {
|
2022-05-05 11:12:42 +02:00
|
|
|
return request(app).post('/api/settings')
|
2022-03-10 12:34:12 +01:00
|
|
|
.send({ key: 'allow_anon_event', value: false })
|
|
|
|
.expect(403)
|
|
|
|
})
|
|
|
|
|
2022-07-29 11:29:45 +02:00
|
|
|
test('should change settings when allowed', () => {
|
|
|
|
return request(app).post('/api/settings')
|
|
|
|
.send({ key: 'allow_anon_event', value: true })
|
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should retrieve stored array settings', async () => {
|
|
|
|
await request(app).post('/api/settings')
|
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.send({ key: 'test', value: [1,2,'test'] })
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
const response = await request(app)
|
|
|
|
.get('/api/settings')
|
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
expect(response.body.test.length).toBe(3)
|
|
|
|
expect(response.body.test).toStrictEqual([1,2,'test'])
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
test('should retrieve stored object settings', async () => {
|
|
|
|
await request(app).post('/api/settings')
|
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.send({ key: 'test', value: { name: 'test object' } })
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
const response = await request(app)
|
|
|
|
.get('/api/settings')
|
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
expect(response.body.test.name).toBe('test object')
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
test('should retrieve stored string settings', async () => {
|
|
|
|
await request(app).post('/api/settings')
|
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.send({ key: 'test', value: 'test string' })
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
const response = await request(app)
|
|
|
|
.get('/api/settings')
|
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
expect(response.body.test).toBe('test string')
|
|
|
|
})
|
|
|
|
|
2022-05-06 23:26:30 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('Events', () => {
|
|
|
|
|
|
|
|
test('should not allow event creation without required fields', async () => {
|
|
|
|
const required_fields = {
|
|
|
|
'title': {},
|
|
|
|
'start_datetime': { title: 'test title' },
|
2022-07-27 11:23:57 +02:00
|
|
|
'place_id or place_name and place_address': { title: 'test title', start_datetime: dayjs().unix()+1000, place_name: 'test place name'},
|
2022-05-06 23:26:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const promises = Object.keys(required_fields).map(async field => {
|
|
|
|
const response = await request(app).post('/api/event').send(required_fields[field])
|
|
|
|
.expect(400)
|
|
|
|
expect(response.text).toBe(`${field} required`)
|
|
|
|
})
|
2022-07-27 11:23:57 +02:00
|
|
|
|
2022-05-06 23:26:30 +02:00
|
|
|
return Promise.all(promises)
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2022-03-10 13:51:24 +01:00
|
|
|
test('should create anon event only when allowed', async () => {
|
2022-07-27 11:23:57 +02:00
|
|
|
|
2022-05-05 11:12:42 +02:00
|
|
|
await request(app).post('/api/settings')
|
2022-03-10 13:51:24 +01:00
|
|
|
.send({ key: 'allow_anon_event', value: false })
|
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
2022-07-27 11:23:57 +02:00
|
|
|
|
2022-05-05 11:12:42 +02:00
|
|
|
await request(app).post('/api/event')
|
2022-03-10 13:51:24 +01:00
|
|
|
.expect(403)
|
|
|
|
|
2022-07-27 11:23:57 +02:00
|
|
|
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 })
|
2022-05-06 23:26:30 +02:00
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
|
|
|
|
2022-07-27 11:23:57 +02:00
|
|
|
expect(response.body.place.id).toBeDefined()
|
|
|
|
places.push(response.body.place.id)
|
|
|
|
|
2022-05-05 11:12:42 +02:00
|
|
|
await request(app).post('/api/settings')
|
2022-03-10 13:51:24 +01:00
|
|
|
.send({ key: 'allow_anon_event', value: true })
|
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
2022-03-10 12:34:12 +01:00
|
|
|
|
2022-07-27 11:23:57 +02:00
|
|
|
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)
|
2022-03-10 12:34:12 +01:00
|
|
|
|
2022-07-27 11:23:57 +02:00
|
|
|
expect(response.body.place.id).toBeDefined()
|
|
|
|
places.push(response.body.place.id)
|
2022-03-10 12:34:12 +01:00
|
|
|
|
2022-07-27 11:23:57 +02:00
|
|
|
})
|
2022-05-05 11:12:42 +02:00
|
|
|
|
|
|
|
test('should trim tags', async () => {
|
|
|
|
const event = {
|
2022-07-27 11:23:57 +02:00
|
|
|
title: 'test title 4',
|
|
|
|
place_id: places[0],
|
|
|
|
start_datetime: dayjs().unix()+1000,
|
2022-05-05 11:12:42 +02:00
|
|
|
tags: [' test tag ']
|
|
|
|
}
|
2022-07-27 11:23:57 +02:00
|
|
|
|
2022-05-05 11:12:42 +02:00
|
|
|
const response = await request(app).post('/api/event')
|
|
|
|
.send(event)
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
2022-06-18 01:14:51 +02:00
|
|
|
|
2022-05-05 11:12:42 +02:00
|
|
|
expect(response.body.tags[0]).toBe('test tag')
|
|
|
|
})
|
2022-03-10 12:34:12 +01:00
|
|
|
})
|
2022-05-20 12:36:06 +02:00
|
|
|
|
|
|
|
describe('Tags', () => {
|
|
|
|
test('should create event with tags', async () => {
|
|
|
|
const event = await request(app).post('/api/event')
|
2022-07-27 11:23:57 +02:00
|
|
|
.send({ title: 'test tags', place_id: places[1], start_datetime: dayjs().unix()+1000 , tags: ['tag1', 'Tag2', 'tAg3'] })
|
2022-05-20 12:36:06 +02:00
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
expect(event.body.tags.length).toBe(3)
|
|
|
|
})
|
|
|
|
|
2022-06-18 01:14:51 +02:00
|
|
|
test('should create event trimming tags / ignore sensitiviness', async () => {
|
|
|
|
const event = await request(app).post('/api/event')
|
2022-07-27 11:23:57 +02:00
|
|
|
.send({ title: 'test trimming tags', place_id: places[1], start_datetime: dayjs().unix()+1000, tags: ['Tag1', 'taG2 '] })
|
2022-06-18 01:14:51 +02:00
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
expect(event.body.tags.length).toBe(2)
|
|
|
|
expect(event.body.tags[0]).toBe('tag1')
|
|
|
|
expect(event.body.tags[1]).toBe('Tag2')
|
|
|
|
})
|
|
|
|
|
2022-05-20 12:36:06 +02:00
|
|
|
test('should return events searching for tags', async () => {
|
2022-06-18 01:14:51 +02:00
|
|
|
const response = await request(app).get('/api/events?tags=tAg3')
|
2022-05-20 12:36:06 +02:00
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
expect(response.body.length).toBe(1)
|
2022-06-18 01:14:51 +02:00
|
|
|
// expect(response.body[0].title).toBe('test tags')
|
2022-05-20 12:36:06 +02:00
|
|
|
expect(response.body[0].tags.length).toBe(3)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-06-18 01:14:51 +02:00
|
|
|
describe('Place', () => {
|
|
|
|
test('should get events by place', async () => {
|
|
|
|
const response = await request(app).get('/api/place/place name 2')
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
expect(response.body.place.name).toBe('place name 2')
|
|
|
|
expect(response.body.events.length).toBe(2)
|
|
|
|
expect(response.body.events[0].place.name).toBe('place name 2')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('admin should get all places', async () => {
|
|
|
|
await request(app).get('/api/place/all')
|
|
|
|
.expect(403)
|
|
|
|
|
|
|
|
const response = await request(app).get('/api/place/all')
|
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
|
|
|
|
expect(response.body.length).toBe(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should search for a place', async () => {
|
|
|
|
const response = await request(app).get('/api/place?search=place')
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
expect(response.body.length).toBe(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2022-07-27 11:23:57 +02:00
|
|
|
let collections = []
|
|
|
|
let filters = []
|
2022-06-18 01:14:51 +02:00
|
|
|
describe ('Collection', () => {
|
|
|
|
test('should not create a new collection if not allowed', () => {
|
|
|
|
return request(app).post('/api/collections')
|
|
|
|
.send({ name: 'test collection' })
|
2022-05-20 12:36:06 +02:00
|
|
|
.expect(403)
|
|
|
|
})
|
|
|
|
|
2022-06-18 01:14:51 +02:00
|
|
|
test('should create a new collection', async () => {
|
|
|
|
const response = await request(app).post('/api/collections')
|
|
|
|
.send({ name: 'test collection' })
|
2022-05-20 12:36:06 +02:00
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
2022-07-27 11:23:57 +02:00
|
|
|
expect(response.body.id).toBeDefined()
|
|
|
|
collections.push(response.body.id)
|
2022-05-20 12:36:06 +02:00
|
|
|
})
|
|
|
|
|
2022-06-18 01:14:51 +02:00
|
|
|
test('should do not have any event when no filters', async () => {
|
|
|
|
const response = await request(app).get('/api/collections/test collection')
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
expect(response.body.length).toBe(0)
|
|
|
|
})
|
|
|
|
|
2022-05-20 12:36:06 +02:00
|
|
|
test('should add a new filter', async () => {
|
|
|
|
await request(app)
|
|
|
|
.post('/api/filter')
|
2022-07-27 11:23:57 +02:00
|
|
|
.send({ collectionId: collections[0], tags: ['test'] })
|
2022-05-20 12:36:06 +02:00
|
|
|
.expect(403)
|
|
|
|
|
|
|
|
const response = await request(app).post('/api/filter')
|
2022-07-27 11:23:57 +02:00
|
|
|
.send({ collectionId: collections[0], tags: ['test'] })
|
2022-05-20 12:36:06 +02:00
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
|
|
|
|
2022-07-27 11:23:57 +02:00
|
|
|
expect(response.body.id).toBeDefined()
|
|
|
|
filters.push(response.body.id)
|
2022-05-20 12:36:06 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2022-07-29 11:29:45 +02:00
|
|
|
test('shoud get collection\'s filters using withFilters parameter', async () => {
|
|
|
|
const response = await request(app)
|
|
|
|
.get('/api/collections?withFilters=true')
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
expect(response.body.length).toBe(1)
|
|
|
|
expect(response.body[0].name).toBe('test collection')
|
|
|
|
expect(response.body[0].filters.length).toBe(1)
|
|
|
|
expect(response.body[0].filters[0].tags.length).toBe(1)
|
|
|
|
expect(response.body[0].filters[0].tags[0]).toBe('test')
|
|
|
|
})
|
|
|
|
|
2022-06-18 01:14:51 +02:00
|
|
|
test('should get collection events', async () => {
|
2022-05-20 12:36:06 +02:00
|
|
|
const response = await request(app)
|
2022-06-18 01:14:51 +02:00
|
|
|
.get(`/api/collections/test collection`)
|
2022-05-20 12:36:06 +02:00
|
|
|
.expect(200)
|
|
|
|
|
2022-06-11 10:34:09 +02:00
|
|
|
expect(response.body.length).toBe(1)
|
2022-05-20 12:36:06 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
test('should remove filter', async () => {
|
|
|
|
await request(app)
|
2022-07-27 11:23:57 +02:00
|
|
|
.delete(`/api/filter/${filters[0]}`)
|
2022-05-20 12:36:06 +02:00
|
|
|
.expect(403)
|
|
|
|
|
|
|
|
await request(app)
|
2022-07-27 11:23:57 +02:00
|
|
|
.delete(`/api/filter/${filters[0]}`)
|
2022-05-20 12:36:06 +02:00
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
const response = await request(app)
|
2022-07-27 11:23:57 +02:00
|
|
|
.get(`/api/filter/${filters[0]}`)
|
2022-05-20 12:36:06 +02:00
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
2022-07-27 11:23:57 +02:00
|
|
|
|
2022-05-20 12:36:06 +02:00
|
|
|
expect(response.body.length).toBe(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('shoud filter for tags', async () => {
|
2022-07-27 11:23:57 +02:00
|
|
|
let response = await request(app)
|
2022-05-20 12:36:06 +02:00
|
|
|
.post('/api/filter')
|
2022-07-27 11:23:57 +02:00
|
|
|
.send({ collectionId: collections[0], tags: ['test'] })
|
2022-05-20 12:36:06 +02:00
|
|
|
.auth(token.access_token, { type: 'bearer' })
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
|
2022-07-27 11:23:57 +02:00
|
|
|
// response = await request(app)
|
|
|
|
// .get(`/api/filter/${response.body.id}`)
|
|
|
|
// .auth(token.access_token, { type: 'bearer' })
|
|
|
|
// .expect(200)
|
|
|
|
// expect(response.body.length).toBe(1)
|
|
|
|
|
2022-06-11 10:34:09 +02:00
|
|
|
response = await request(app)
|
2022-06-18 01:14:51 +02:00
|
|
|
.get(`/api/collections/test collection`)
|
2022-05-20 12:36:06 +02:00
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
expect(response.body.length).toBe(1)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2022-07-27 11:23:57 +02:00
|
|
|
})
|