-
Notifications
You must be signed in to change notification settings - Fork 42
Backend - Users && more stuff #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3a05223
8f66e19
ad80c85
62abd44
c90a084
5938eb9
40dbfb1
f26c75c
1c4da67
21a9df0
ca8a946
9f6f639
c738269
1715f1f
92833ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| exports.up = knex => knex.schema.createTableIfNotExists('users', table => { | ||
| table.uuid('id').primary() | ||
| table.string('username', 20).unique().notNullable() | ||
| table.string('password', 60).notNullable() | ||
| table.timestamps() | ||
| }) | ||
|
|
||
| exports.down = knex => knex.schema.dropTableIfExists('users') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| const db = require('../../lib/db') | ||
| const User = db.model('User') | ||
|
|
||
| exports.create = (req, res, next) => { | ||
| User.create(req.body) | ||
| .then(user => res.send({ success: true })) | ||
| .catch(err => { | ||
| if (err.code === 'ER_DUP_ENTRY') { | ||
| return res.status(400).send({ | ||
| error: 'Este usuário já existe' | ||
| }) | ||
| } | ||
|
|
||
| return next(err) | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module.exports = bookshelf => bookshelf.model('User', { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Se não usar o visibility também vamos responder as requests com a senha do usuário
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. k
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. esse eu já arrumei |
||
| tableName: 'users', | ||
| hidden: [ 'password' ], | ||
| bcrypt: { field: 'password' } | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| const express = require('express') | ||
| const bodyParser = require('body-parser') | ||
|
|
||
| const validator = require('../../lib/validator') | ||
| const schemas = require('./schemas') | ||
| const handlers = require('./handlers') | ||
|
|
||
| const router = express.Router() | ||
|
|
||
| router.post('/usuarios', | ||
| bodyParser.json(), | ||
| validator({ body: schemas.create }), | ||
| handlers.create) | ||
|
|
||
| module.exports = router |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| const Joi = require('joi') | ||
|
|
||
| exports.model = Joi.object({ | ||
| id: Joi.string().uuid(), | ||
| username: Joi.string(), | ||
| password: Joi.string() | ||
| }) | ||
|
|
||
| exports.create = exports.model.concat(Joi.object({ | ||
| id: Joi.forbidden(), | ||
| username: Joi.string().token().min(3).max(20).required(), | ||
| password: Joi.string().min(8).max(120).required() | ||
| })) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| const { knex } = require('../../lib/db') | ||
|
|
||
| /** | ||
| * Insere dois registros na tabela `users` | ||
| * @return {Promise} Uma promise que resolve quando os registros forem inseridos | ||
| */ | ||
| exports.insertMultiple = () => knex('users').insert([ | ||
| { | ||
| id: '212dd279-129f-474a-beb2-a1cac605cf48', | ||
| username: 'alanhoff', | ||
| password: 'awesomePassword', | ||
| created_at: new Date(), | ||
| updated_at: new Date() | ||
| }, | ||
| { | ||
| id: '6af458a8-df22-4da9-a726-89d14076e220', | ||
| username: 'thebergamo', | ||
| password: 'awesomePassword', | ||
| created_at: new Date(), | ||
| updated_at: new Date() | ||
| } | ||
| ]) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| const lab = exports.lab = require('lab').script() | ||
| const expect = require('code').expect | ||
| const co = require('co') | ||
| const request = require('supertest') | ||
|
|
||
| const server = require('../../lib/server') | ||
| const db = require('../../lib/db') | ||
| const Model = db.model('User') | ||
|
|
||
| const ENDPOINT = '/usuarios' | ||
|
|
||
| lab.describe('users', () => { | ||
| lab.beforeEach(() => db.truncate()) | ||
|
|
||
| lab.describe(`POST ${ENDPOINT}`, () => { | ||
| lab.test('não deve aceitar um payload que não esteja de acordo com o schema', co.wrap(function * () { | ||
| const req = yield request(server) | ||
| .post(ENDPOINT) | ||
| .send({ username: 'p' }) | ||
|
|
||
| expect(req.statusCode).to.equal(422) | ||
| expect(req.res.body.error).to.equal('ValidationError') | ||
| })) | ||
|
|
||
| lab.test('deve criar uma nova linha no banco de dados', co.wrap(function * () { | ||
| const req = yield request(server) | ||
| .post(ENDPOINT) | ||
| .send({ | ||
| username: 'mark', | ||
| password: 'awesomePass' | ||
| }) | ||
|
|
||
| expect(req.statusCode).to.equal(200) | ||
| expect(req.res.body.success).to.equal(true) | ||
|
|
||
| const data = yield Model.findAll() | ||
| expect(data).to.have.length(1) | ||
| expect(data.at(0).toJSON()).to.contain({ | ||
| username: 'mark' | ||
| }) | ||
| })) | ||
|
|
||
| lab.test('retorna um erro quando o usuário já existir', co.wrap(function * () { | ||
| // first request | ||
| yield request(server) | ||
| .post(ENDPOINT) | ||
| .send({ | ||
| username: 'foo', | ||
| password: 'foobarbaz' | ||
| }) | ||
|
|
||
| const req = yield request(server) | ||
| .post(ENDPOINT) | ||
| .send({ | ||
| username: 'foo', | ||
| password: 'foobarbaz' | ||
| }) | ||
|
|
||
| expect(req.statusCode).to.equal(400) | ||
| expect(req.res.body.error).to.equal('Este usuário já existe') | ||
|
|
||
| const data = yield Model.findAll() | ||
| expect(data).to.have.length(1) | ||
| expect(data.at(0).toJSON()).to.contain({ | ||
| username: 'foo' | ||
| }) | ||
| })) | ||
| }) | ||
| }) | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remover a linha em branco