fix: improve unit testing

This commit is contained in:
lesion 2024-01-07 23:58:37 +01:00
parent f32fc53982
commit 9482ba9e39
No known key found for this signature in database
GPG key ID: 352918250B012177

View file

@ -7,6 +7,7 @@ const admin = { username: 'admin', password: 'test', grant_type: 'password', cli
let token
let app
let places = []
let event_id
beforeAll(async () => {
@ -26,7 +27,6 @@ beforeAll(async () => {
const { sequelize } = require('../server/api/models/index')
// sequelize.sync({ force: true })
await sequelize.query('PRAGMA foreign_keys = OFF')
await sequelize.query('DELETE FROM settings')
await sequelize.query('DELETE FROM user_followers')
await sequelize.query('DELETE FROM events where parentId IS NOT NULL')
await sequelize.query('DELETE FROM ap_users')
@ -34,6 +34,7 @@ beforeAll(async () => {
await sequelize.query('DELETE FROM event_tags')
await sequelize.query('DELETE FROM resources')
await sequelize.query('DELETE FROM instances')
await sequelize.query('DELETE FROM settings')
await sequelize.query('DELETE FROM announcements')
await sequelize.query('DELETE FROM users')
await sequelize.query('DELETE FROM tags')
@ -215,7 +216,34 @@ describe('Events', () => {
})
test('should not confirm anon events', async () => {
const response = await request(app).post('/api/event')
.send({ title: 'test title 6', place_id: places[0], start_datetime: dayjs().unix() + 1000 })
.expect(200)
expect(response.body.is_visible).toBe(false)
event_id = response.body.id
})
test('should not get unconfirmed events', async () => {
let response = await request(app).get(`/api/event/detail/${event_id}`)
.expect(404)
response = await request(app).get(`/api/event/detail/${event_id}`)
.auth(token.access_token, { type: 'bearer' })
.expect(200)
})
test('should confirm event if allowed', async () => {
let response = await request(app).put(`/api/event/confirm/${event_id}`)
.send()
.expect(403)
response = await request(app).put(`/api/event/confirm/${event_id}`)
.auth(token.access_token, { type: 'bearer' })
.send()
.expect(200)
})
test('should not allow start_datetime greater than end_datetime', async () => {
@ -294,10 +322,43 @@ describe('Events', () => {
.expect(200)
.expect('Content-Type', /json/)
event_id = response.body.id
expect(response.body.description).toBe(`<p>inside paragraph</p><a href="https://test.com/?query=true">link with fb reference</a>`)
})
test('should not update event with invalid start_datetime', async () => {
const event = {
id: event_id,
place_id: places[0],
start_datetime: "antani"
}
const response = await request(app).put('/api/event')
.auth(token.access_token, { type: 'bearer' })
.send(event)
.expect(400)
expect(response.text).toBe('Wrong format for start datetime')
})
test('should not update event with invalid end_datetime', async () => {
const event = {
id: event_id,
place_id: places[0],
end_datetime: 2
}
const response = await request(app).put('/api/event')
.auth(token.access_token, { type: 'bearer' })
.send(event)
.expect(400)
expect(response.text).toBe('start datetime is greater than end datetime')
})
})
let event = {}
@ -313,6 +374,17 @@ describe('Tags', () => {
expect(event.body.tags).toStrictEqual(['tag1', 'Tag2', 'tAg3'])
})
test('should dedup tags', async () => {
event = await request(app).post('/api/event')
.send({ title: 'test tags', place_id: places[0], start_datetime: dayjs().unix() + 1000, tags: ['ciao', ' Ciao '] })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(event.body.tags.length).toBe(1)
expect(event.body.tags).toStrictEqual(['ciao'])
})
test('should create event trimming tags / ignore sensitiviness', async () => {
const ret = await request(app).post('/api/event')
.send({ title: 'test trimming tags', place_id: places[1], start_datetime: dayjs().unix() + 1000, tags: ['Tag1', 'taG2 '] })
@ -326,7 +398,6 @@ describe('Tags', () => {
})
test('should modify event tags', async () => {
const ret = await request(app).put('/api/event')
.send({ id: event.body.id, tags: ['tag1', 'tag3', 'tag4'], place_id: places[1] })
.auth(token.access_token, { type: 'bearer' })
@ -340,7 +411,7 @@ describe('Tags', () => {
const response = await request(app).get('/api/events?tags=tAg3')
.expect(200)
expect(response.body.length).toBe(1)
expect(response.body.length).toBe(2)
// expect(response.body[0].title).toBe('test tags')
expect(response.body[0].tags.length).toBe(3)
})
@ -367,7 +438,7 @@ describe('Place', () => {
.expect(200)
expect(response.body.place.name).toBe('place name 2')
expect(response.body.events.length).toBe(2)
expect(response.body.events.length).toBe(3)
expect(response.body.events[0].place.name).toBe('place name 2')
})
@ -546,6 +617,13 @@ describe('Export', () => {
describe('Geocoding', () => {
test('should not be enabled by default', async () => {
await request(app)
.post('/api/settings')
.send({ key: 'allow_geolocation', value: false })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
const response = await request(app).get('/api/placeOSM/Nominatim/test')
.expect(403)