Tuesday, 2 May 2017

node mongoose mocha how to debug a failing mongoose test?

Similar to what I did previously with Permissions , I am trying to test adding Privileges into my User... the parameters are OK but the findOneAndUpdate push does not insert it into the array...

MODEL

    const Schema = mongoose.Schema;

    const Privilege = new Schema({
      privilege: {
        table_id: {
          type: String,
          required: false
        },
        canWrite: {
          type: Boolean,
          required: true,
          default: false
        }
      }
    });

    const UserSchema = new mongoose.Schema({
      username: {
          type: String,
          required: true
      },
      ...
      privileges: [Privilege]
      ...

TEST

      const newWritePrivilege = { privilege: { user_id: '56z787zzz67fc', canWrite: true }};

     .post(`/api/v1/users/${user._id}/privileges`)
             .send(newWritePrivilege)
             .expect(httpStatus.OK)

CONTROLLER

    function grantPrivilege(req, res, next) {
      console.log('GRANT PRIVILEGE: %j TO USER %j', req.body, req.params.userId);
      User.findOneAndUpdate({ _id: req.params.userId }, { $push: { privilege: req.body } }, { new: true })
        .then(savedUser => res.json(savedUser))
        .catch((e) => {
          console.log('ERROR: ', e);
          next(e);
        });
    }

In the console.log, I can check that the user id is correct, and the user document exist, but nothing is inserted ... How can I debug it ?

CONSOLE

    GRANT PRIVILEGE: {"privilege":{"table_id":"56z787zzz67fc","canWrite":true}} TO USER "5908fe55aec3be291bc317b5"
    user: 
    {"__v":0,"username":"KK123","password":"KKpassword","email":"kk123@example.com","mobileNumber":"9999999999",
    "_id":"5908fe55aec3be291bc317b5","createdAt":"2017-05-02T21:47:01.795Z","privileges":[]}

thank for feedback

ADDITIONAL INFO ..

The test return 200 but nothing into the array

    res:     
    {"req":

{"method":"POST","url":"http://127.0.0.1:65296/api/v1/users/5909015a9be435292a992bfc/privileges", "data":{"privilege": {"table_id":"56z787zzz67fc","canWrite":true}},"headers":{"user-agent":"node-superagent/3.5.2", "content-type":"application/json"}},"header":{"vary":"X-HTTP-Method-Override, Accept-Encoding","x-dns-prefetch-control":"off","x-frame-options":"SAMEORIGIN","x-download-options":"noopen","x-content-type-options":"nosniff","x-xss-protection":"1; mode=block","access-control-allow-origin":"*", "content-type":"application/json; charset=utf-8","content-length":"4","etag":"W/\"4-K+iMpCQsduglOsYkdIUQZQMtaDM\"", "date":"Tue, 02 May 2017 21:59:54 GMT","connection":"close"}, "status":200,"text":"null"}



via erwin

No comments:

Post a Comment