sum7/warehost-frontend
sum7
/
warehost-frontend
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost-frontend/tests/index.coffee

658 lines
21 KiB
CoffeeScript
Raw Normal View History

2016-08-14 14:22:11 +02:00
expect = require("chai").expect
request = require('request')
lib = require('./lib')
config = require('./config')
describe('API',->
it('status',(done)->
request({method: 'GET',uri:config.ADDRESS+"/status"},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
2016-08-17 22:35:25 +02:00
if(typeof body == "string")
body = JSON.parse(body)
expect(body.data).to.be.true
2016-08-20 01:17:28 +02:00
expect(body.error).to.be.undefined
2016-08-14 14:22:11 +02:00
done()
)
)
2016-08-20 01:17:28 +02:00
describe('invite',->
2016-08-23 22:57:35 +02:00
describe('add',->
2016-08-24 23:11:57 +02:00
it('-> without login',(done)->
invite = {
username: 'test',
password:'test'
}
request({method: 'POST',uri:config.ADDRESS+"/invite",json:invite},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.data).to.be.false
expect(body.error.msg).to.not.be.null
expect(body.error.fields).to.include('session')
done()
)
)
2016-08-23 22:57:35 +02:00
it('-> validate',(done)->
lib.login((j)->
invite = {
username: 'test',
password:'test'
}
request({method: 'POST',uri:config.ADDRESS+"/invite",jar:j,json:invite},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.be.true
expect(body.session.login).to.not.be.null
done()
)
)
)
)
2016-08-20 01:17:28 +02:00
describe('list',->
2016-08-24 23:11:57 +02:00
it('-> without login',(done)->
request({method: 'GET',uri:config.ADDRESS+"/invite"},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.data).to.be.false
expect(body.error.msg).to.not.be.null
expect(body.error.fields).to.include('session')
done()
)
)
2016-08-20 01:17:28 +02:00
it('-> validate',(done)->
lib.login((j)->
request({method: 'GET',uri:config.ADDRESS+"/invite",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
2016-08-23 22:57:35 +02:00
expect(body.error).to.be.undefined
expect(body.data).to.be.a("array")
2016-08-24 23:11:57 +02:00
expect(body.data).to.have.length(1)
expect(body.session.login).to.not.be.null
done()
)
)
)
)
describe('admin/toggle',->
before((done)->
lib.login({
username:'test',
password:'test'
},(j)->
invite = {
username:'test_admin',
password:'test'
}
request({method: 'POST',uri:config.ADDRESS+"/invite",jar:j,json:invite},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.be.true
2016-08-23 22:57:35 +02:00
expect(body.session.login).to.not.be.null
2016-08-20 01:17:28 +02:00
done()
)
)
)
2016-08-24 23:11:57 +02:00
it('-> without login (get)',(done)->
request({method: 'GET',uri:config.ADDRESS+"/invitor"},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.data).to.be.false
expect(body.error.msg).to.not.be.null
expect(body.error.fields).to.include('session')
done()
)
)
it('-> without login (put)',(done)->
request({method: 'PUT',uri:config.ADDRESS+"/invitor"},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.data).to.be.false
expect(body.error.msg).to.not.be.null
expect(body.error.fields).to.include('session')
done()
)
)
it('-> validate on',(done)->
lib.login({username:'test_admin',password:'test'},(j)->
request({method: 'GET',uri:config.ADDRESS+"/invitor",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data.admin).to.be.false
expect(body.session.login).to.not.be.null
request({method: 'PUT',uri:config.ADDRESS+"/invitor",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.be.true
expect(body.session.login).to.not.be.null
request({method: 'GET',uri:config.ADDRESS+"/invitor",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data.admin).to.be.true
expect(body.session.login).to.not.be.null
done()
)
)
)
)
)
it('-> validate off',(done)->
lib.login({username:'test_admin',password:'test'},(j)->
request({method: 'GET',uri:config.ADDRESS+"/invitor",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data.admin).to.be.true
expect(body.session.login).to.not.be.null
request({method: 'PUT',uri:config.ADDRESS+"/invitor",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.be.true
expect(body.session.login).to.not.be.null
request({method: 'GET',uri:config.ADDRESS+"/invitor",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data.admin).to.be.false
expect(body.session.login).to.not.be.null
done()
)
)
)
)
)
it('-> validate on',(done)->
lib.login({username:'test_admin',password:'test'},(j)->
request({method: 'GET',uri:config.ADDRESS+"/invitor",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data.admin).to.be.false
expect(body.session.login).to.not.be.null
request({method: 'PUT',uri:config.ADDRESS+"/invitor",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.be.true
expect(body.session.login).to.not.be.null
request({method: 'GET',uri:config.ADDRESS+"/invitor",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data.admin).to.be.true
expect(body.session.login).to.not.be.null
done()
)
)
)
)
)
)
describe('edit',->
it('-> not invited/exists',(done)->
lib.login({username:'test',password:'test'},(j)->
request({method: 'PUT',uri:config.ADDRESS+"/invite/"+1,jar:j,json:{
username: 'test_not_exits',
password:'test'
}},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.not.undefined
expect(body.data).to.be.false
expect(body.session.login).to.not.be.null
done()
)
)
)
it('-> invited, not used',(done)->
lib.login({username:'test',password:'test'},(j)->
invite = {
username: 'test_not_used',
password:'test'
}
request({method: 'POST',uri:config.ADDRESS+"/invite",jar:j,json:invite},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.be.true
expect(body.session.login).to.not.be.null
request({method: 'GET',uri:config.ADDRESS+"/invite",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.have.length(1)
expect(body.session.login).to.not.be.null
request({method: 'PUT',uri:config.ADDRESS+"/invite/"+body.data[0].invited.ID,jar:j,json:{
username:'test2_not_used',
password:'test2'
}},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.be.true
expect(body.session.login).to.not.be.null
done()
)
)
)
)
)
it('-> invited, used',(done)->
lib.login({username:'test',password:'test'},(j)->
invite = {
username: 'test_used',
password:'test'
}
request({method: 'POST',uri:config.ADDRESS+"/invite",jar:j,json:invite},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.be.true
expect(body.session.login).to.not.be.null
lib.login({username:'test_used',password:'test'},()->
request({method: 'GET',uri:config.ADDRESS+"/invite",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.have.length(2)
expect(body.session.login).to.not.be.null
i = 0
if(body.data[0].invited.username!="test_used")
i = 1
request({method: 'PUT',uri:config.ADDRESS+"/invite/"+body.data[i].invited.ID,jar:j,json:{
username:'test2_not_used',
password:'test2'
}},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.not.undefined
expect(body.data).to.be.false
expect(body.session.login).to.not.be.null
done()
)
)
)
)
)
)
it('-> superadmin',(done)->
lib.login((j)->
request({method: 'GET',uri:config.ADDRESS+"/invite",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.have.length(1)
expect(body.session.login).to.not.be.null
request({method: 'PUT',uri:config.ADDRESS+"/invite/"+body.data[0].invited.ID,jar:j,json:{
username:'test2',
password:'test2'
}},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.be.true
expect(body.session.login).to.not.be.null
done()
)
)
)
)
)
describe('delete',->
it('-> not invited/exists',(done)->
lib.login({username:'test2',password:'test2'},(j)->
request({method: 'DELETE',uri:config.ADDRESS+"/invite/"+1,jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.not.undefined
expect(body.data).to.be.false
expect(body.session.login).to.not.be.null
done()
)
)
)
it('-> invited, admin',(done)->
lib.login({username:'test2',password:'test2'},(j)->
request({method: 'GET',uri:config.ADDRESS+"/invite",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.have.length(2)
expect(body.session.login).to.not.be.null
i = 0
if(body.data[i].invited.username!="test_admin")
i = 1
if(body.data[i].invited.username!="test_admin")
i = 2
request({method: 'DELETE',uri:config.ADDRESS+"/invite/"+body.data[i].invited.ID,jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.be.true
expect(body.session.login).to.not.be.null
done()
)
)
)
)
it('-> invited, not used',(done)->
lib.login({username:'test2',password:'test2'},(j)->
request({method: 'GET',uri:config.ADDRESS+"/invite",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.have.length(2)
expect(body.session.login).to.not.be.null
i = 0
if(body.data[0].invited.username!="test_not_used")
i = 1
request({method: 'DELETE',uri:config.ADDRESS+"/invite/"+body.data[i].invited.ID,jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.be.true
expect(body.session.login).to.not.be.null
done()
)
)
)
)
it('-> invited, used',(done)->
lib.login({username:'test2',password:'test2'},(j)->
request({method: 'GET',uri:config.ADDRESS+"/invite",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.have.length(1)
expect(body.session.login).to.not.be.null
i = 0
if(body.data[0].invited.username!="test_used")
i = 1
request({method: 'DELETE',uri:config.ADDRESS+"/invite/"+body.data[i].invited.ID,jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.not.undefined
expect(body.data).to.be.false
expect(body.session.login).to.not.be.null
done()
)
)
)
)
it('-> superadmin',(done)->
lib.login((j)->
request({method: 'GET',uri:config.ADDRESS+"/invite",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.have.length(1)
expect(body.session.login).to.not.be.null
request({method: 'DELETE',uri:config.ADDRESS+"/invite/"+body.data[0].invited.ID,jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
if(typeof body == "string")
body = JSON.parse(body)
expect(body.error).to.be.undefined
expect(body.data).to.be.true
expect(body.session.login).to.not.be.null
done()
)
)
)
)
2016-08-20 01:17:28 +02:00
)
)
2016-08-14 14:22:11 +02:00
describe('login',->
it('-> wrong username',(done)->
request({method: 'POST',uri: config.ADDRESS+"/login",json:{
username: config.testdata.username+"f",
password: config.testdata.password
}},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
2016-08-17 22:35:25 +02:00
if(typeof body == "string")
body = JSON.parse(body)
2016-08-14 14:22:11 +02:00
expect(body.data).to.be.false
2016-08-17 22:35:25 +02:00
expect(body.error.msg).to.not.be.null
expect(body.error.fields).to.include('username')
2016-08-14 14:22:11 +02:00
done()
)
)
it('-> wrong password',(done)->
request({method: 'POST',uri: config.ADDRESS+"/login",json:{
username: config.testdata.username,
password: config.testdata.password+"f"
}},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
2016-08-17 22:35:25 +02:00
if(typeof body == "string")
body = JSON.parse(body)
2016-08-14 14:22:11 +02:00
expect(body.data).to.be.false
2016-08-17 22:35:25 +02:00
expect(body.error.msg).to.not.be.null
expect(body.error.fields).to.include('password')
2016-08-14 14:22:11 +02:00
done()
)
)
it('-> validate',(done)->
request({method: 'POST',uri: config.ADDRESS+"/login",json:{
username: config.testdata.username,
password: config.testdata.password
}},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
2016-08-17 22:35:25 +02:00
if(typeof body == "string")
body = JSON.parse(body)
2016-08-23 22:57:35 +02:00
expect(body.error).to.be.undefined
2016-08-24 23:11:57 +02:00
expect(body.data).to.be.true
2016-08-14 14:22:11 +02:00
expect(body.session.login.active).to.be.true
done()
)
)
)
describe('logout',->
it('-> without login',(done)->
request({method: 'GET',uri:config.ADDRESS+"/logout"},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
2016-08-17 22:35:25 +02:00
if(typeof body == "string")
body = JSON.parse(body)
expect(body.data).to.be.false
expect(body.error.msg).to.not.be.null
expect(body.error.fields).to.include('session')
2016-08-14 14:22:11 +02:00
done()
)
)
it('-> validate',(done)->
lib.login((j)->
request({method: 'GET',uri:config.ADDRESS+"/logout",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
2016-08-17 22:35:25 +02:00
if(typeof body == "string")
body = JSON.parse(body)
expect(body.data).to.be.true
2016-08-23 22:57:35 +02:00
expect(body.error).to.be.undefined
expect(body.session.login).to.be.undefined
2016-08-14 14:22:11 +02:00
done()
)
)
)
)
describe('password',->
it('-> without login',(done)->
request({method: 'POST',uri: config.ADDRESS+"/password",json:{
username: config.testdata.username,
password: config.testdata.password
}},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
2016-08-17 22:35:25 +02:00
if(typeof body == "string")
body = JSON.parse(body)
expect(body.data).to.be.false
expect(body.error.msg).to.not.be.null
expect(body.error.fields).to.include('session')
2016-08-23 22:57:35 +02:00
expect(body.session.login).to.be.undefined
2016-08-14 14:22:11 +02:00
done()
)
)
it('-> wrong current password',(done)->
lib.login((j)->
request({method: 'POST',uri: config.ADDRESS+"/password",json:{
currentpassword: config.testdata.password+"f",
newpassword: config.testdata.password+"ff",
},jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
2016-08-17 22:35:25 +02:00
if(typeof body == "string")
body = JSON.parse(body)
expect(body.data).to.be.false
expect(body.error.msg).to.not.be.null
expect(body.error.fields).to.include('currentpassword')
expect(body.session.login).to.not.be.null
2016-08-14 14:22:11 +02:00
done()
)
)
)
it('-> wrong new password',(done)->
lib.login((j)->
request({method: 'POST',uri: config.ADDRESS+"/password",json:{
currentpassword: config.testdata.password,
newpassword: "",
},jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
2016-08-17 22:35:25 +02:00
if(typeof body == "string")
body = JSON.parse(body)
expect(body.data).to.be.false
expect(body.error.msg).to.not.be.null
expect(body.error.fields).to.include('newpassword')
2016-08-23 22:57:35 +02:00
expect(body.session.login).to.not.be.undefined
2016-08-14 14:22:11 +02:00
done()
)
)
)
it('-> validate',(done)->
lib.login((j)->
request({method: 'POST',uri: config.ADDRESS+"/password",json:{
currentpassword: config.testdata.password,
newpassword: config.testdata.password+"f",
},jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
2016-08-17 22:35:25 +02:00
if(typeof body == "string")
body = JSON.parse(body)
2016-08-23 22:57:35 +02:00
expect(body.error).to.be.undefined
2016-08-24 23:11:57 +02:00
expect(body.data).to.be.true
2016-08-14 14:22:11 +02:00
request({method: 'POST',uri: config.ADDRESS+"/password",json:{
currentpassword: config.testdata.password+"f",
newpassword: config.testdata.password,
},jar:j},(err,res,body)->
2016-08-17 22:35:25 +02:00
expect(body.data).to.be.true
2016-08-14 14:22:11 +02:00
done()
)
)
)
)
)
describe('delete',->
it('-> without login',(done)->
request({method: 'GET',uri:config.ADDRESS+"/delete"},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
2016-08-17 22:35:25 +02:00
if(typeof body == "string")
body = JSON.parse(body)
expect(body.data).to.be.false
expect(body.error.msg).to.not.be.null
expect(body.error.fields).to.include('session')
2016-08-23 22:57:35 +02:00
expect(body.session.login).to.be.undefined
2016-08-14 14:22:11 +02:00
done()
)
)
it('-> validate',(done)->
2016-08-24 23:11:57 +02:00
lib.login({username:'test_used',password:'test'},(j)->
2016-08-14 14:22:11 +02:00
request({method: 'GET',uri:config.ADDRESS+"/delete",jar:j},(err,res,body)->
expect(err).to.be.null
expect(res.statusCode).to.be.equal(200)
2016-08-17 22:35:25 +02:00
if(typeof body == "string")
body = JSON.parse(body)
2016-08-23 22:57:35 +02:00
expect(body.error).to.be.undefined
2016-08-24 23:11:57 +02:00
expect(body.data).to.be.true
2016-08-23 22:57:35 +02:00
expect(body.session.login).to.be.undefined
2016-08-14 14:22:11 +02:00
done()
)
)
)
)
)