Advertisement
Guest User

Untitled

a guest
Sep 4th, 2017
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. signup(req, res) {
  2. const username = req.body.username;
  3. const email = req.body.email;
  4. const password = req.body.password;
  5.  
  6. if(!username ) {
  7. return res.status(400).send({error: "You need to fill in your username"})
  8. }else if(!email) {
  9. return res.status(400).send({error:"You need to fill in your email"})
  10. }else if(!password) {
  11. return res.status(400).send({error: "You need to fill in your password"})
  12. }
  13.  
  14. return User.findOne({
  15. where: {
  16. username,
  17. }
  18. })
  19. .then (user => {
  20. if(user) {
  21. return res.status(400).send({message: 'Username already taken'})
  22. }
  23. return User.create({
  24. username,
  25. email,
  26. password,
  27. isAdmin: req.body.isAdmin,
  28. })
  29. .then((user) =>
  30. {
  31. const token = user.generateAuthToken();
  32. return res.header('x-auth', token).status(201)
  33. .send({
  34. message: `Welcome ${user.username}`,
  35. user
  36. });
  37. })
  38. .catch((error) => { return res.status(400).send(`${error.errors[0].message}`)})
  39. })
  40.  
  41. describe('Users API test', function () {
  42. var user1 = {
  43. username: 'chair',
  44. email: "chair@example.com",
  45. password: "chairman",
  46. isAdmin: true
  47. }
  48. describe("It should create a user", function() {
  49. it('POST /api/users/signup', function(done) {
  50. request(app)
  51. .post('/api/users/signup')
  52. .send(user1)
  53. .end(function(err, res) {
  54. expect(res.statusCode).to.equal(201);
  55. expect(res.body.username).to.equal('chair')
  56. done()
  57. })
  58. })
  59. })
  60. })
  61.  
  62. POST /api/users/signup 201 1694.701 ms - 183
  63. 1) POST /api/users/signup
  64. Executing (default): DROP TABLE IF EXISTS "Books" CASCADE;
  65.  
  66.  
  67. 0 passing (2s)
  68. 1 failing
  69.  
  70. 1) Users API test It should create a user POST /api/users/signup:
  71. Uncaught AssertionError: expected undefined to equal 'chair'
  72. at Test.<anonymous> (servertestsapp.test.js:39:36)
  73. at Test.assert (node_modulessupertestlibtest.js:179:6)
  74. at Server.assert (node_modulessupertestlibtest.js:131:12)
  75. at emitCloseNT (net.js:1552:8)
  76. at _combinedTickCallback (internal/process/next_tick.js:77:11)
  77. at process._tickCallback (internal/process/next_tick.js:104:9)
  78.  
  79.  
  80.  
  81. npm ERR! Test failed. See above for more details.
  82. [nodemon] app crashed - waiting for file changes before starting...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement