dimanche 30 août 2015

How do I test this API endpoint with mongoose and node

I am writing a server API and I'm curious on how to test functions like this that rely on database responses.

router.post('/auth', koaBody, function*(next) {
  var person;
  if (!this.request.body.username || !this.request.body.password) {
    this["throw"]('Missing Username or Password in request', 401);
  } else {
    person = (yield People.findOne({
      username: this.request.body.username
    }).exec());
    if (!person) {
      this["throw"]('Incorrect Username/Password', 401);
    } else {
      if (((yield bcrypt.compare(this.request.body.password, person.password))) === true) {
        this.status = 200;
        this.body = jwtHelper.sign(_.omit(person, '_id', 'region', 'password', 'cell', 'lastLogin'), config.sessionSecret, {
          expiresInMinutes: 365 * 24 * 60
        });
      } else {
        this["throw"]('Incorrect Username/Password', 401);
      }
    }
  }
  (yield next);
});

Aucun commentaire:

Enregistrer un commentaire