From 66b2b0523e5270f21bf7194d3c79fab2c24d22de Mon Sep 17 00:00:00 2001 From: lesion Date: Thu, 10 Mar 2022 12:34:12 +0100 Subject: [PATCH] start unit testing #142 --- package.json | 3 ++ tests/.setup.js | 61 +++++++++++++++++++++++++++++ tests/app.test.js | 97 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 tests/.setup.js create mode 100644 tests/app.test.js diff --git a/package.json b/package.json index bd89f673..573e0b12 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "build": "nuxt build --modern", "start:inspect": "NODE_ENV=production node --inspect node_modules/.bin/nuxt start --modern", "dev": "nuxt dev", + "test": "cd tests/seeds; jest; cd ../..", "start": "nuxt start --modern", "doc": "cd docs && bundle exec jekyll b", "doc:dev": "cd docs && bundle exec jekyll s --drafts", @@ -73,6 +74,7 @@ }, "devDependencies": { "@nuxtjs/vuetify": "^1.12.3", + "jest": "^27.5.1", "less": "^4.1.1", "less-loader": "^7", "prettier": "^2.3.0", @@ -80,6 +82,7 @@ "pug-plain-loader": "^1.1.0", "sass": "^1.49.4", "sequelize-cli": "^6.3.0", + "supertest": "^6.2.2", "webpack": "4", "webpack-cli": "^4.7.2" }, diff --git a/tests/.setup.js b/tests/.setup.js new file mode 100644 index 00000000..9349d2f2 --- /dev/null +++ b/tests/.setup.js @@ -0,0 +1,61 @@ +// const request = require('supertest') + + +// // - setup .... +// // - 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 admin = {} +// describe('Setup', () => { +// let app +// beforeAll( async () => { +// await require('../server/initialize.server.js')() +// app = require('../server/routes.js') +// return +// }) + +// test('should setup', async () => { +// let response = await request(app).post('/api/setup/db').send({ db: { dialect: 'sqlite', storage: './gancio.sqlite' } }) +// expect(response.statusCode).toBe(200) +// response = await request(app).post('/api/setup/restart') +// expect(response.statusCode).toBe(200) +// expect(response.body.password).toBeDefined() +// expect(response.body.email).toBeDefined() +// admin.password = response.body.password +// admin.email = response.body.email +// }) + +// }) + + + +// // describe('POST /api/event', () => { +// // let app +// // beforeAll( async () => { +// // console.error('dentro il secondo describe di setup beforeAll') +// // app = requireUncached('../server/routes.js') +// // return +// // }) +// // test('should not allow event creation without required fields', async () => { +// // const required_fields = { +// // 'title': {}, +// // 'place_name': { title: 'test title' }, +// // 'start_datetime': { title: 'test title', 'place_name': 'test place name'} +// // } + +// // const promises = Object.keys(required_fields).map(async field => { +// // const response = await request(app).post('/api/event').send(required_fields[field]) +// // expect(response.statusCode).toBe(400) +// // expect(response.text).toBe(`${field} is required`) +// // return +// // }) + +// // return Promise.all(promises) +// // }) +// // }) \ No newline at end of file diff --git a/tests/app.test.js b/tests/app.test.js new file mode 100644 index 00000000..cdb2fca6 --- /dev/null +++ b/tests/app.test.js @@ -0,0 +1,97 @@ +const request = require('supertest') + +const admin = { username: 'admin', password: 'SsJOn5l0JpBE', 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 +beforeAll( async () => { + await require('../server/initialize.server.js')() + app = require('../server/routes.js') +}) + +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') + .expect(500) + }) + + test('should authenticate with correct user/password', async () => { + const response = await request(app).post('/oauth/login') + .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) + }) + + test('should not change settings when not allowed', async () => { + let response + response = await request(app).post('/api/settings') + .send({ key: 'allow_anon_event', value: false }) + .expect(403) + }) + + // test('should create anon event only when allowed', async () => { + // let response + // response = await request(app) + // .post('/api/settings') // auth._token.local + // .send({ key: 'allow_anon_event', value: false }) + // .auth(token.access_token, { type: 'bearer' }) + // .expect(200) + // // expect(response.statusCode).toBe(200) + // // response = await request(app).post('/api/settings') + // // .send({ key: 'allow_anon_event', value: false }) + // }) + +}) + +describe('Events', () => { + + + test('should not allow event creation without required fields', async () => { + const required_fields = { + 'title': {}, + 'place_name': { title: 'test title' }, + 'start_datetime': { title: 'test title', 'place_name': 'test place name'} + } + + const promises = Object.keys(required_fields).map(async field => { + const response = await request(app).post('/api/event').send(required_fields[field]) + expect(response.statusCode).toBe(400) + expect(response.text).toBe(`${field} is required`) + return + }) + + return Promise.all(promises) + }) +})