expect = require("chai").expect request = require('request') lib = require('./lib') config = require('./config') modules = require('./modul') getIndexOfInvitedUsername = (list,username) -> for i in [0..list.length] if(list[i].invited.username==username) return list[i].invited return null 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) if(typeof body == "string") body = JSON.parse(body) expect(body.data).to.be.true expect(body.error).to.be.undefined done() ) ) it('modules',(done)-> request({method: 'GET',uri:config.ADDRESS+"/modules"},(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.a("array") expect(body.error).to.be.undefined done() ) ) describe('invite',-> describe('add',-> 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() ) ) 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() ) ) ) ) describe('list',-> 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() ) ) 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) expect(body.error).to.be.undefined expect(body.data).to.be.a("array") 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 expect(body.session.login).to.not.be.null done() ) ) ) 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, admin',(done)-> lib.login({username:'test',password:'test'},(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.be.a("array") expect(body.session.login).to.not.be.null invited = getIndexOfInvitedUsername(body.data,"test_admin") request({method: 'PUT',uri:config.ADDRESS+"/invite/"+invited.ID,jar:j,json:{ username:'test2_admin', password:'test2_admin' }},(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:'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.be.a("array") expect(body.session.login).to.not.be.null invited = getIndexOfInvitedUsername(body.data,"test_not_used") request({method: 'PUT',uri:config.ADDRESS+"/invite/"+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.be.a("array") expect(body.session.login).to.not.be.null invited = getIndexOfInvitedUsername(body.data,"test_used") request({method: 'PUT',uri:config.ADDRESS+"/invite/"+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.be.a("array") expect(body.session.login).to.not.be.null invited = getIndexOfInvitedUsername(body.data,"test") request({method: 'PUT',uri:config.ADDRESS+"/invite/"+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.be.a("array") expect(body.session.login).to.not.be.null invited = getIndexOfInvitedUsername(body.data,"test_admin") request({method: 'DELETE',uri:config.ADDRESS+"/invite/"+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.be.a("array") expect(body.session.login).to.not.be.null invited = getIndexOfInvitedUsername(body.data,"test_not_used") request({method: 'DELETE',uri:config.ADDRESS+"/invite/"+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.be.a("array") expect(body.session.login).to.not.be.null invited = getIndexOfInvitedUsername(body.data,"test_used") request({method: 'DELETE',uri:config.ADDRESS+"/invite/"+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.be.a("array") expect(body.session.login).to.not.be.null invited = getIndexOfInvitedUsername(body.data,"test2") request({method: 'DELETE',uri:config.ADDRESS+"/invite/"+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() ) ) ) ) ) ) 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) 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('username') 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) 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('password') 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) if(typeof body == "string") body = JSON.parse(body) expect(body.error).to.be.undefined expect(body.data).to.be.true 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) 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',(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) if(typeof body == "string") body = JSON.parse(body) expect(body.data).to.be.true expect(body.error).to.be.undefined expect(body.session.login).to.be.undefined 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) 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') expect(body.session.login).to.be.undefined 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) 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 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) 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') expect(body.session.login).to.not.be.undefined 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) if(typeof body == "string") body = JSON.parse(body) expect(body.error).to.be.undefined expect(body.data).to.be.true request({method: 'POST',uri: config.ADDRESS+"/password",json:{ currentpassword: config.testdata.password+"f", newpassword: config.testdata.password, },jar:j},(err,res,body)-> expect(body.data).to.be.true done() ) ) ) ) ) modules() 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) 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') expect(body.session.login).to.be.undefined done() ) ) it('-> validate',(done)-> lib.login({username:'test_used',password:'test'},(j)-> request({method: 'GET',uri:config.ADDRESS+"/delete",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.be.undefined done() ) ) ) ) )