Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- signup(req, res) {
- const username = req.body.username;
- const email = req.body.email;
- const password = req.body.password;
- if(!username ) {
- return res.status(400).send({error: "You need to fill in your username"})
- }else if(!email) {
- return res.status(400).send({error:"You need to fill in your email"})
- }else if(!password) {
- return res.status(400).send({error: "You need to fill in your password"})
- }
- return User.findOne({
- where: {
- username,
- }
- })
- .then (user => {
- if(user) {
- return res.status(400).send({message: 'Username already taken'})
- }
- return User.create({
- username,
- email,
- password,
- isAdmin: req.body.isAdmin,
- })
- .then((user) =>
- {
- const token = user.generateAuthToken();
- return res.header('x-auth', token).status(201)
- .send({
- message: `Welcome ${user.username}`,
- user
- });
- })
- .catch((error) => { return res.status(400).send(`${error.errors[0].message}`)})
- })
- describe('Users API test', function () {
- var user1 = {
- username: 'chair',
- email: "chair@example.com",
- password: "chairman",
- isAdmin: true
- }
- describe("It should create a user", function() {
- it('POST /api/users/signup', function(done) {
- request(app)
- .post('/api/users/signup')
- .send(user1)
- .end(function(err, res) {
- expect(res.statusCode).to.equal(201);
- expect(res.body.username).to.equal('chair')
- done()
- })
- })
- })
- })
- POST /api/users/signup 201 1694.701 ms - 183
- 1) POST /api/users/signup
- Executing (default): DROP TABLE IF EXISTS "Books" CASCADE;
- 0 passing (2s)
- 1 failing
- 1) Users API test It should create a user POST /api/users/signup:
- Uncaught AssertionError: expected undefined to equal 'chair'
- at Test.<anonymous> (servertestsapp.test.js:39:36)
- at Test.assert (node_modulessupertestlibtest.js:179:6)
- at Server.assert (node_modulessupertestlibtest.js:131:12)
- at emitCloseNT (net.js:1552:8)
- at _combinedTickCallback (internal/process/next_tick.js:77:11)
- at process._tickCallback (internal/process/next_tick.js:104:9)
- npm ERR! Test failed. See above for more details.
- [nodemon] app crashed - waiting for file changes before starting...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement