diff --git a/common/models/end_user.json b/common/models/end_user.json index b936c03a4f4c2c1cf1a172b2c9f06edb52fdf040..439e03c38cce59646e5eb5698076451fe7bdf65e 100644 --- a/common/models/end_user.json +++ b/common/models/end_user.json @@ -21,6 +21,10 @@ }, "registration": { "type": "string" + }, + "name": { + "type": "string", + "required": true } }, "validations": [], diff --git a/enviroment.js b/enviroment.js index df6fd95d1ecf84f24ab32477f0948244dd405b90..b340ec6026b371201b25a78c70e8f5ac706217f2 100644 --- a/enviroment.js +++ b/enviroment.js @@ -1,5 +1,41 @@ -const URL = "http://localhost:3000/api/"; +const URL = 'http://localhost:3000/api/'; + +const randomLowerCase = function() { + let possible = 'abcdefghijklmnopqrstuvwxyz'; + return (possible.charAt(Math.floor(Math.random() * possible.length))); +}; + +const randomUpperCase = function() { + let possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + return (possible.charAt(Math.floor(Math.random() * possible.length))); +}; + +const randomNumber = function(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return (Math.floor(Math.random() * (max - min + 1)) + min); +}; + +const randomWord = function(num, allUpper = false, firstUpper = false) { + let word = ''; + for (let i = 0; i < num; i++) { + if (!allUpper) { + if (i == 0 && firstUpper) { + word = word + randomUpperCase(); + } else { + word = word + randomLowerCase(); + } + } else { + word = word + randomUpperCase(); + } + } + return word; +}; module.exports = { - URL -} \ No newline at end of file + URL, + randomLowerCase, + randomUpperCase, + randomNumber, + randomWord, +}; diff --git a/package.json b/package.json index 95800652fa400d0978384b130e8b3c200a458f2a..d9f33890d94895737ff3b63c8f3b0e3a5310f09c 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "lint": "eslint .", "start": "node .", "posttest": "npm run lint && nsp check", - "tests":"mocha ./test" + "tests": "mocha ./test" }, "dependencies": { "compression": "^1.0.3", diff --git a/test/account.js b/test/account.js index 5ddb917a48a31812d1b42b906ac48e9a3018f2be..e70a592ec11542d1ece61b40d65628dd4cbc1e8f 100644 --- a/test/account.js +++ b/test/account.js @@ -1,32 +1,67 @@ var chakram = require('chakram'), expect = chakram.expect; -var url = require('../enviroment') -const URL = url.URL; +var env = require('../enviroment'); +const URL = env.URL; -describe("Account", function() { - it("should create an admin account", function (){ - let testObject = { - "email": "admin@admin.com", - "permission": "admin", - "registration": "NMBR#123456789", - "username": "Administrador Atencioso", - "password":"admin" - } - return chakram.post(`${URL}end_users`,testObject).then( function (myResponse){ - expect(myResponse).to.have.status(200) - expect(myResponse.body.result.permission).to.contain('admin') - }); +describe('Account', function() { + it('should create an admin account', function() { + let name = env.randomWord(env.randomNumber(1, 25)); + let testObject = { + 'email': `${name}@admin.com`, + 'permission': 'admin', + 'registration': 'NMBR#123456789', + 'username': `${name}`, + 'name': `${name.replace(/\b\w/g, l => l.toUpperCase())}`, + 'password': 'admin', + }; + return chakram.post(`${URL}end_users`, testObject).then(function(myResponse) { + expect(myResponse).to.have.status(200); + expect(myResponse.body.result.permission).to.contain('admin'); }); - it("should create an user account", function (){ - let testObject = { - "email": "enduser@enduser.com", - "permission": "user", - "registration": "NMBR#321654987", - "username": "Usuário Feliz", - "password":"end_user" - } - return chakram.post(`${URL}end_users`,testObject).then( function (myResponse){ - expect(myResponse).to.have.status(200) - expect(myResponse.body.result.permission).to.contain('user') - }); + }); + it('should create an user account', function() { + let name = env.randomWord(env.randomNumber(1, 25)); + let testObject = { + 'email': `${name}@enduser.com`, + 'permission': 'user', + 'registration': 'NMBR#321654987', + 'username': `${name}`, + 'name': `${name.replace(/\b\w/g, l => l.toUpperCase())}`, + 'password': 'end_user', + }; + return chakram.post(`${URL}end_users`, testObject).then(function(myResponse) { + expect(myResponse).to.have.status(200); + expect(myResponse.body.result.permission).to.contain('user'); }); -}) \ No newline at end of file + }); + it('should login to an admin account', function() { + let testObject = { + 'email': 'abc@admin.com', + 'password': 'admin', + }; + return chakram.post(`${URL}end_users/login`, testObject).then(function(myResponse) { + expect(myResponse).to.have.status(200); + expect(myResponse.body.result).to.be.a('object'); + }); + }); + it('should login to an user account', function() { + let testObject = { + 'email': 'user@user.com', + 'password': 'user', + }; + return chakram.post(`${URL}end_users/login`, testObject).then(function(myResponse) { + expect(myResponse).to.have.status(200); + expect(myResponse.body.result).to.be.a('object'); + }); + }); + it('should logout', function() { + let testObject = { + 'email': 'user@user.com', + 'password': 'user', + }; + return chakram.post(`${URL}end_users/login`, testObject).then(function(myResponse) { + return chakram.post(`${URL}end_users/logout?access_token=${myResponse.body.result.id}`).then(function(myResponse2) { + expect(myResponse2).to.have.status(200); + }); + }); + }); +}); diff --git a/test/category.js b/test/category.js index 16fe3db0b3465f69d9ee3de417616bf8c30aa11c..d8fe8062211981ae3d00d260ab84478a65a670e7 100644 --- a/test/category.js +++ b/test/category.js @@ -1,41 +1,65 @@ var chakram = require('chakram'), expect = chakram.expect; -var url = require('../enviroment') -const URL = url.URL; +var env = require('../enviroment'); +const URL = env.URL; -describe("Categories", function() { - it("should return all categories", function(){ - return chakram.get(`${URL}categories`).then(function (myResponse){ - expect(myResponse).to.have.status(200); - expect(myResponse.body.result).to.be.a('array') - }); +describe('Categories', function() { + it('should return all categories', function() { + return chakram.get(`${URL}categories`).then(function(myResponse) { + expect(myResponse).to.have.status(200); + expect(myResponse.body.result).to.be.a('array'); }); - it("should return one category", function(){ - return chakram.get(`${URL}categories`).then( function (myResponse){ - let category = myResponse.body.result[0] - return chakram.get(`${URL}categories/${myResponse.body.result[0].id}`).then( function (myResponse2){ - expect(myResponse2).to.have.status(200); - expect(myResponse2.body.result.category_name).to.contain(category.category_name) - }); - }); + }); + it('should return one category', function() { + return chakram.get(`${URL}categories`).then(function(myResponse) { + let category = myResponse.body.result[0]; + return chakram.get(`${URL}categories/${myResponse.body.result[0].id}`).then(function(myResponse2) { + expect(myResponse2).to.have.status(200); + expect(myResponse2.body.result.category_name).to.contain(category.category_name); + }); }); - it("should try to create a new category", function(){ - let testObject = { - "category_name":"Terreiro", - "category_description":"Lugar que mantém as tradições dos afrodescendentes" - } - return chakram.post(`${URL}categories`,testObject).then( function (myResponse){ - expect(myResponse).to.have.status(401) - expect(myResponse.body.error.message).to.contain("Authorization Required") - }); + }); + it('should try to create a new category', function(){ + let testObject = { + 'category_name': 'Terreiro', + 'category_description': 'Lugar que mantém as tradições dos afrodescendentes', + }; + return chakram.post(`${URL}categories`, testObject).then(function(myResponse) { + expect(myResponse).to.have.status(401); + expect(myResponse.body.error.code).to.contain('AUTHORIZATION_REQUIRED'); }); - // it("should create a new category as admin", function(){ - // let testObject = { - // "category_name":"Terreiro", - // "category_description":"Lugar que mantém as tradições dos afrodescendentes" - // } - // return chakram.post(`${URL}categories`,testObject).then( function (myResponse){ - // expect(myResponse).to.have.status(401) - // expect(myResponse.body.error.message).to.contain("Authorization Required") - // }); - // }); -}) \ No newline at end of file + }); + it('should try to create a new category as user', function(){ + let testObject = { + 'email': 'user@user.com', + 'password': 'user', + }; + + let testObject2 = { + 'category_name': 'Terreiro', + 'category_description': 'Lugar que mantém as tradições dos afrodescendentes', + }; + return chakram.post(`${URL}end_users/login`, testObject).then(function(myResponse) { + return chakram.post(`${URL}categories?access_token=${myResponse.body.result.id}`, testObject2).then(function(myResponse2) { + expect(myResponse2).to.have.status(401); + expect(myResponse2.body.error.code).to.contain('AUTHORIZATION_REQUIRED'); + }); + }); + }); + it('should create a new category as admin', function(){ + let testObject = { + 'email': 'abc@admin.com', + 'password': 'admin', + }; + + let testObject2 = { + 'category_name': 'Terreiro' + env.randomWord(15, true), + 'category_description': 'Lugar que mantém as tradições dos afrodescendentes', + }; + return chakram.post(`${URL}end_users/login`, testObject).then(function(myResponse) { + return chakram.post(`${URL}categories?access_token=${myResponse.body.result.id}`, testObject2).then(function(myResponse2) { + expect(myResponse2).to.have.status(200); + expect(myResponse2.body.result).to.be.a('object'); + }); + }); + }); +});