2017-03-02 6 views
0

어떻게 작동하는지 이해하기 힘듭니다. 사용자 일반 텍스트 비밀번호를 확인하고 일치시키는 방법을 알고 있습니다. 그러나 bcrypt 암호화를 사용하면 암호가 실제로 암호화되었는지 확인하기 위해 데이터베이스 호출을해야합니다. 할 수있다, 여기 Chai를 사용하여 POST/사용자 비밀번호가 암호화되었는지 테스트하십시오.

describe('create (POST /users)', function() { 
    it('succeeds, with encrypted password', function (done) { 
     chai.request(expressApp) 
     .post('/users/') 
     .send({ 
      email: '[email protected]', 
      username: 'johndoe', 
      first_name: 'John', 
      last_name: 'Doe', 
      password: '123456789', 
      phone_number: '+1.888.12456' 
     }) 
     .end((err, res) => { 
      res.should.have.status(201) 
      res.should.be.json 
      res.body.should.have.property('id', 2) 
      res.body.should.not.have.property('password') 
      User.forge({ id: res.body.id }) 
      .fetch() 
      .then((user) => { 
       console.log(user.attributes.password) // prints 123456789  
       return bcrypt.compare(user.attributes.password, res.body.password).then (function (res) { 
       res.should.equal(true) 
      }) 
      .fetch() 
      .catch(err) 
     done(err) 
     }) 
    }) 

이 시험은 현재 통과 Bookshelf.js

initialize() { 
    this.on('saving', this.encryptPassword) 
    }, 

    encryptPassword (model, attrs, options) { 
    if (attrs.password) { 
     return bcrypt.hash(model.attributes.password, 10).then((hash) => { 
     model.set('password', hash) 
     }) 
    } 
    }, 

를 사용하여 사용자 모델에 대한 코드입니다하지만 난 그게 정확하지 알고 여기에 내가 함께 일하고 있어요 코드입니다 누군가 내가 이것을 이해하도록 도와 주시겠습니까?

답변

0

bcrypt는 해시에 대해 일반 텍스트 암호를 테스트하여 일치하는지 확인하고 오류 및 부울 true/false를 호출하여 다시 호출하는 compare 함수가 있습니다. db에서 꺼내는 User 객체를 사용하여 결과를 테스트해야합니다.

+0

감사합니다. 확실히 워드 프로세서를 먼저 확인해야합니다. 그러나, 나는 여전히해야 할 일을 여전히 고민하고있다. 나는 코드를 업데이트했지만 거기에서 어디로 갈지 아직 확신하지 못했습니다. –

+0

그게 전부입니다. bcrypt.compare의 결과가 true이면 사용자의 암호화 된 비밀번호가 테스트중인 일반 텍스트 비밀번호와 일치합니다. – dmfay