2017-02-15 8 views
0

ヘルパーファイルが正しく動作していることを確認するテストを実行しようとしています。期限切れのトークンがあるとエラーリバースを取得できません。進む。useFakeTimers mocha chai sinon - テストで正しい結果ではない

私はテストで直接に時間を偽ることができ、その外ではないと感じています。実際のヘルパーファイルのコードを変更すると目的を破るため、テストでjwt.verify関数をコピーしたくないということです。この仕事をするためにこの上の任意のヘルプ?

私はsinonと時間を偽っています。私が今何時に時計のチックの後になったかを調べるためにテストすれば、正しい結果が得られます。しかし何らかの理由で、これは別のファイルの関数には適用されません。

local.jsファイル

const moment = require('moment'); 
const jwt = require('jsonwebtoken'); 

const secret = process.env.TOKEN_SECRET; 

function encodeToken(user) { 
    const playload = { 
    exp: moment().add(1, 'hours').unix(), // expires the token in an hour 
    iat: moment().unix(), 
    sub: user.id 
    }; 

    return jwt.sign(playload, secret); 
} 

function decodeToken(token, callback) { 
    const payload = jwt.verify(token, secret, function (err, decoded) { 
    const now = moment().unix(); 
    console.log('tim: ' + decoded.exp); //just to see 
    console.log('now: ' + now); // just to see 
    if (now > decoded.exp) { 
     callback('Token has expired.'); 
    } 
    callback(null, decoded); 
    }); 
} 

module.exports = { 
    encodeToken, 
    decodeToken 
}; 

と私のテストファイル

process.env.NODE_ENV = 'test'; 

const chai = require('chai'); 
const should = chai.should(); 
const sinon = require('sinon'); 

const localAuth = require('../../src/server/auth/local'); 

describe('decodeToken()', function() { 
    var clock; 
    beforeEach(function() { 
     clock = sinon.useFakeTimers(); 
    }); 

    afterEach(function() { 
     clock.restore(); 
    }); 
    it('should return a decoded payload', function (done) { 
     const token = localAuth.encodeToken({ 
     id: 1 
     }); 
     should.exist(token); 
     token.should.be.a('string'); 
     clock.tick(36001000000); 
     localAuth.decodeToken(token, (err, res) => { 
     should.exist(err); 
     res.should.eql('Token has expired.'); 
     done(); 
     }); 
    }); 
    }); 
+0

はあなたのために、以下の回答作業をしましたか? – anoop

答えて

1

JWTは、有効期限をチェックし、それ自体でエラーがスローされます。だから我々はエラーメッセージから断言しなければならない。コードにいくつかの変更を加えて動作させました。

Iは以下のようにこれをテスト

、(コードスニペット)

const moment = require('moment'); 
const jwt = require('jsonwebtoken'); 

const secret = 'abczzxczxczxc'; 

function encodeToken(user) { 
    const payload = { 
    exp: moment().add(1, 'hours').unix(), // expires the token in an hour 
    iat: moment().unix(), 
    sub: user.id 
    }; 

    const token = jwt.sign(payload, secret); 
    return token; 
} 

function decodeToken(token, callback) { 
    jwt.verify(token, secret, function(err, decoded) { 
    callback(err, decoded); 
    }); 
} 

module.exports = { 
    encodeToken, 
    decodeToken 
}; 
以下のようにテストさ

process.env.NODE_ENV = 'test'; 

const chai = require('chai'); 
const should = chai.should(); 
const sinon = require('sinon'); 

const localAuth = require('./'); 

describe('decodeToken()', function() { 
    var clock; 
    beforeEach(function() { 
    clock = sinon.useFakeTimers(); 
    }); 

    afterEach(function() { 
    clock.restore(); 
    }); 
    it('should return a decoded payload', function (done) { 
    const token = localAuth.encodeToken({ 
     id: 1 
    }); 
    token.should.exist; 
    token.should.be.a('string'); 
    clock.tick(36001000000); 
    localAuth.decodeToken(token, (err, res) => { 
     should.exist(err); 
     err.message.should.eql('jwt expired'); 
     done(); 
    }); 
    }); 
}); 

出力

➜faketimer ./node_modules/mocha/bin/mocha index_test.js

decodeToken() ✓は、デコードされたペイロード(17ms)を通過

1を返す必要があります

+0

それはまさに私が必要としていたものでした。私はJWTが期限切れになっているかどうか分かりませんでしたので、本当に説明に感謝します:)そして遅れのための謝罪...数日間病気になりました。もう一度ありがとう! –

+0

@ThomasGorczynski心配はいりません。喜んで.. – anoop

関連する問題