UNPKG

decentralized-internet

Version:

An NPM library of programs to create decentralized web and distributed computing projects

1 lines 5.08 kB
const Joi=require("@hapi/joi"),Lab=require("lab"),lab=exports.lab=Lab.script(),request=require("request"),Promise=require("bluebird"),_=require("underscore");var agentOptions={},getClusterPostServer=function(){return"http://localhost:8180"},joiokres=Joi.object().keys({ok:Joi.boolean().valid(!0),id:Joi.string(),rev:Joi.string()}),token="",createUser=function(e){return new Promise(function(t,r){var n={url:getClusterPostServer()+"/auth/user",method:"POST",json:e,agentOptions:agentOptions};request(n,function(e,n,s){e?r(e):t(s)})})},userLogin=function(e){return new Promise(function(t,r){var n={url:getClusterPostServer()+"/auth/login",method:"POST",json:e,agentOptions:agentOptions};request(n,function(e,n,s){e?r(e):t(s)})})},resetPassword=function(e){return new Promise(function(t,r){var n={url:getClusterPostServer()+"/auth/reset",method:"POST",json:e,agentOptions:agentOptions};request(n,function(e,n,s){e?r(e):t(s)})})},getUser=function(e){return new Promise(function(t,r){var n={url:getClusterPostServer()+"/auth/user",method:"GET",headers:{authorization:e}};request(n,function(e,n,s){e?r(e):t(JSON.parse(s))})})},getUsers=function(e){return new Promise(function(t,r){var n={url:getClusterPostServer()+"/auth/users",method:"GET",headers:{authorization:e}};request(n,function(e,n,s){e?r(e):t(s)})})},updateUser=function(e,t){return new Promise(function(r,n){var s={url:getClusterPostServer()+"/auth/user",method:"PUT",json:e,headers:{authorization:t}};request(s,function(e,t,s){e?n(e):r(s)})})},deleteUser=function(e){return new Promise(function(t,r){var n={url:getClusterPostServer()+"/auth/user",method:"DELETE",agentOptions:agentOptions,headers:{authorization:e}};request(n,function(e,n,s){e?r(e):t(s)})})},deleteUsers=function(e,t){return new Promise(function(r,n){var s={url:getClusterPostServer()+"/auth/users",method:"DELETE",agentOptions:agentOptions,json:t,headers:{authorization:e}};request(s,function(e,t,s){e?n(e):r(s)})})};lab.experiment("Test clusterpost auth jwt",function(){var e={email:"hapi.jwt.couch@gmail.com",name:"Hapi jwt",password:"SomePassword90!"};lab.test("returns true when new user is created.",function(){return createUser(e).then(function(e){Joi.assert(e.token,Joi.string().required())})}),lab.test("returns true if same user fails to be created.",function(){return createUser(e).then(function(e){Joi.assert(e.token,Joi.object().keys({statusCode:Joi.number().valid(409),error:Joi.string(),message:Joi.string()}))})}),lab.test("returns true when recovery email is sent.",function(){return resetPassword({email:e.email}).then(function(e){Joi.assert(e,Joi.string().valid("An email has been sent to recover your password."))})});var t="";lab.test("returns true when user is logged in.",function(){return userLogin({email:"hapi.jwt.couch@gmail.com",password:"SomePassword90!"}).then(function(e){Joi.assert(e.token,Joi.string().required()),t="Bearer "+e.token})}),lab.test("returns true when unauthorized user access api.",function(){return getUsers(t).then(function(e){Joi.assert(e,Joi.object().keys({statusCode:Joi.number().valid(403),error:Joi.string(),message:Joi.string()}))})}),lab.test("returns true when user scope is updated manually to admin",function(){return getUser(t).then(function(e){return console.log(e,"dddddddd"),new Promise(function(t,r){var n={uri:"http://localhost:5984/clusterjobstest/"+e._id,method:"GET"};request(n,function(e,n,s){var o=JSON.parse(s);console.log(s,"dddd"),o.scope.push("admin"),request({uri:"http://localhost:5984/hapijwtcouch/_bulk_docs",method:"POST",json:{docs:[o]}},function(e,n,s){e?r(e):s.error?r(s.error):t(s)})})})})});var r="",n={email:"someemail@gmail.com",name:"Test user",password:"Some88Password!"};lab.test("returns true when a user is created, then all users are fetched by the admin, the scope of the new user is updated by the admin",function(){return createUser(n).then(function(e){return userLogin({email:"someemail@gmail.com",password:"Some88Password!"})}).then(function(e){return r="Bearer "+e.token,getUsers(t)}).then(function(e){var r=JSON.parse(e);Joi.assert(r,Joi.array().items(Joi.object()));var s=_.find(r,function(e){return e.email===n.email});return s.scope.push("clusterpost"),updateUser(s,t)}).then(function(e){Joi.assert(e,Joi.object().keys({ok:Joi.boolean(),id:Joi.string(),rev:Joi.string()}))})}),lab.test('Returns true if the "default" user fails to modify the admin user',function(){return getUser(t).then(function(e){var t=e;return t.scope=["default"],updateUser(t,r)}).then(function(e){Joi.assert(e.statusCode,401)})}),lab.test("Returns true if admin user deletes an user",function(){return getUsers(t).then(function(e){var r=JSON.parse(e);Joi.assert(r,Joi.array().items(Joi.object()));var s=_.find(r,function(e){return e.email===n.email});return deleteUsers(t,s)}).then(function(e){Joi.assert(e,Joi.object().keys({ok:Joi.boolean(),id:Joi.string(),rev:Joi.string()}))})}),lab.test("returns true when valid user deletes itself.",function(){return deleteUser(t).then(function(e){Joi.assert(e,Joi.object().keys({ok:Joi.boolean(),id:Joi.string(),rev:Joi.string()}))})})});