Skip to content

Commit

Permalink
Adopt JavaScript Standard Style also for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steunix committed Oct 3, 2024
1 parent 8f1d495 commit 127df16
Show file tree
Hide file tree
Showing 15 changed files with 730 additions and 717 deletions.
26 changes: 14 additions & 12 deletions test/common.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global before */

global.agent = require('superagent')
global.assert = require('assert')
global.fs = require('fs')
Expand Down Expand Up @@ -42,13 +44,13 @@ before((done) => {
console.log('Passweaver API test before hook')
// Read listen port from config
console.log('Reading port from config')
var port = JSON.parse(
fs.readFileSync(
const port = JSON.parse(
global.fs.readFileSync(
'config.json'
)
).listen.port
var ip = JSON.parse(
fs.readFileSync(
const ip = JSON.parse(
global.fs.readFileSync(
'config.json'
)
).listen.host
Expand All @@ -57,22 +59,22 @@ before((done) => {
console.log(`Running tests on ${global.host}`)

// Get both admin jwt and user jwt
agent
global.agent
.post(`${global.host}/api/v1/login`)
.send({'username':'ADMIN', 'password': '0'})
.then(res=>{
.send({ username: 'ADMIN', password: '0' })
.then(res => {
global.adminJWT = res.body.data.jwt
agent
global.agent
.post(`${global.host}/api/v1/login`)
.send({'username':'USER1', 'password': '0'})
.then(res=>{
.send({ username: 'USER1', password: '0' })
.then(res => {
global.userJWT = res.body.data.jwt
done()
})
})
})

function rnd (prefix) {
const rnd = (new Date%9e6).toString(36)
global.rnd = (prefix) => {
const rnd = (new Date() % 9e6).toString(36)
return `${prefix}_${rnd}`
}
16 changes: 8 additions & 8 deletions test/events.spec.cjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
require('./common.cjs')
/* global describe, it, agent, assert */

describe('Events', function() {
require('./common.cjs')

it('Add events', async()=> {
describe('Events', function () {
it('Add events', async () => {
const res1 = await agent
.post(`${host}/api/v1/events`)
.set('Authorization',`Bearer ${global.userJWT}`)
.send({event: 1, entity: 1, entityid: '1'})
.catch(v=>v)
.post(`${global.host}/api/v1/events`)
.set('Authorization', `Bearer ${global.userJWT}`)
.send({ event: 1, entity: 1, entityid: '1' })
.catch(v => v)

assert.strictEqual(res1.status, 201)
})

})
122 changes: 62 additions & 60 deletions test/folders.spec.cjs
Original file line number Diff line number Diff line change
@@ -1,142 +1,144 @@
/* global describe, it, agent, assert */

require('./common.cjs')

describe('Folders', function() {
it('Create, update and delete folder', async()=>{
describe('Folders', function () {
it('Create, update and delete folder', async () => {
const res1 = await agent
.post(`${host}/api/v1/folders/sample1/folders`)
.set('Authorization',`Bearer ${global.userJWT}`)
.post(`${global.host}/api/v1/folders/sample1/folders`)
.set('Authorization', `Bearer ${global.userJWT}`)
.send(global.folderCreateData)
.catch(v=>v)
.catch(v => v)

assert.strictEqual(res1.status, 201)
const folder = res1.body.data.id

const res2 = await agent
.patch(`${host}/api/v1/folders/${folder}`)
.set('Authorization',`Bearer ${global.userJWT}`)
.patch(`${global.host}/api/v1/folders/${folder}`)
.set('Authorization', `Bearer ${global.userJWT}`)
.send({ description: 'updated' })
.catch(v=>v)
.catch(v => v)

assert.strictEqual(res2.status, 200)

const res3 = await agent
.delete(`${host}/api/v1/folders/${folder}`)
.set('Authorization',`Bearer ${global.userJWT}`)
.catch(v=>v)
.delete(`${global.host}/api/v1/folders/${folder}`)
.set('Authorization', `Bearer ${global.userJWT}`)
.catch(v => v)

assert.strictEqual(res3.status, 200)
})

it('Create, unauthorized', async()=>{
it('Create, unauthorized', async () => {
const res1 = await agent
.post(`${host}/api/v1/folders/0/folders`)
.set('Authorization',`Bearer ${global.userJWT}`)
.post(`${global.host}/api/v1/folders/0/folders`)
.set('Authorization', `Bearer ${global.userJWT}`)
.send(global.folderCreateData)
.catch(v=>v)
.catch(v => v)

assert.strictEqual(res1.status, 403)
})

it('Create, bad data', async()=>{
it('Create, bad data', async () => {
const res1 = await agent
.post(`${host}/api/v1/folders/0/folders`)
.set('Authorization',`Bearer ${global.adminJWT}`)
.post(`${global.host}/api/v1/folders/0/folders`)
.set('Authorization', `Bearer ${global.adminJWT}`)
.send({})
.catch(v=>v)
.catch(v => v)

assert.strictEqual(res1.status, 400)
})

it('Update, bad data', async()=>{
it('Update, bad data', async () => {
const res1 = await agent
.patch(`${host}/api/v1/folders/sample1`)
.set('Authorization',`Bearer ${global.adminJWT}`)
.send({'description': ''})
.catch(v=>v)
.patch(`${global.host}/api/v1/folders/sample1`)
.set('Authorization', `Bearer ${global.adminJWT}`)
.send({ description: '' })
.catch(v => v)

assert.strictEqual(res1.status, 400)
})

it('Get folder', async()=>{
it('Get folder', async () => {
const res1 = await agent
.get(`${host}/api/v1/folders/sample1`)
.set('Authorization',`Bearer ${global.userJWT}`)
.catch(v=>v)
.get(`${global.host}/api/v1/folders/sample1`)
.set('Authorization', `Bearer ${global.userJWT}`)
.catch(v => v)

assert.strictEqual(res1.status, 200)
})

it('Get folder, unexistent', async()=>{
it('Get folder, unexistent', async () => {
const res1 = await agent
.get(`${host}/api/v1/folders/000`)
.set('Authorization',`Bearer ${global.userJWT}`)
.catch(v=>v)
.get(`${global.host}/api/v1/folders/000`)
.set('Authorization', `Bearer ${global.userJWT}`)
.catch(v => v)

assert.strictEqual(res1.status, 404)
})

it('Delete non empty folder', async()=>{
it('Delete non empty folder', async () => {
const res1 = await agent
.post(`${host}/api/v1/folders/sample1/folders`)
.set('Authorization',`Bearer ${global.userJWT}`)
.post(`${global.host}/api/v1/folders/sample1/folders`)
.set('Authorization', `Bearer ${global.userJWT}`)
.send(global.folderCreateData)
.catch(v=>v)
.catch(v => v)

assert.strictEqual(res1.status, 201)
const folder = res1.body.data.id

const res2 = await agent
.delete(`${host}/api/v1/folders/sample1`)
.set('Authorization',`Bearer ${global.userJWT}`)
.catch(v=>v)
.delete(`${global.host}/api/v1/folders/sample1`)
.set('Authorization', `Bearer ${global.userJWT}`)
.catch(v => v)

assert.strictEqual(res2.status, 422)

const res3 = await agent
.delete(`${host}/api/v1/folders/${folder}`)
.set('Authorization',`Bearer ${global.userJWT}`)
.catch(v=>v)
.delete(`${global.host}/api/v1/folders/${folder}`)
.set('Authorization', `Bearer ${global.userJWT}`)
.catch(v => v)

assert.strictEqual(res3.status, 200)
})

it('Delete folder, unauthorized', async()=>{
it('Delete folder, unauthorized', async () => {
const res1 = await agent
.delete(`${host}/api/v1/folders/sample2`)
.set('Authorization',`Bearer ${global.userJWT}`)
.catch(v=>v)
.delete(`${global.host}/api/v1/folders/sample2`)
.set('Authorization', `Bearer ${global.userJWT}`)
.catch(v => v)

assert.strictEqual(res1.status, 403)
})

it('Delete system folders, unprocessable', async()=>{
it('Delete system folders, unprocessable', async () => {
const res1 = await agent
.delete(`${host}/api/v1/folders/0`)
.set('Authorization',`Bearer ${global.userJWT}`)
.catch(v=>v)
.delete(`${global.host}/api/v1/folders/0`)
.set('Authorization', `Bearer ${global.userJWT}`)
.catch(v => v)

assert.strictEqual(res1.status, 422)

const res2 = await agent
.delete(`${host}/api/v1/folders/P`)
.set('Authorization',`Bearer ${global.userJWT}`)
.catch(v=>v)
.delete(`${global.host}/api/v1/folders/P`)
.set('Authorization', `Bearer ${global.userJWT}`)
.catch(v => v)

assert.strictEqual(res2.status, 422)
})

it('Update system folders, unprocessable', async()=>{
it('Update system folders, unprocessable', async () => {
const res1 = await agent
.delete(`${host}/api/v1/folders/0`)
.set('Authorization',`Bearer ${global.userJWT}`)
.catch(v=>v)
.delete(`${global.host}/api/v1/folders/0`)
.set('Authorization', `Bearer ${global.userJWT}`)
.catch(v => v)

assert.strictEqual(res1.status, 422)

const res2 = await agent
.delete(`${host}/api/v1/folders/P`)
.set('Authorization',`Bearer ${global.userJWT}`)
.catch(v=>v)
.delete(`${global.host}/api/v1/folders/P`)
.set('Authorization', `Bearer ${global.userJWT}`)
.catch(v => v)

assert.strictEqual(res2.status, 422)
})
Expand Down
Loading

0 comments on commit 127df16

Please sign in to comment.