CLIを使用した認証で新しいfeathersアプリを作成します。
# use the default value for each prompt
feathers g app
feathers g authentication
supertest
を開発依存関係としてインストールします。
npm i -D supertest
次のファイルをtest/services/authentication.test.js
追加します。
const supertest = require('supertest');
const app = require('../../src/app');
const config = require('../../config/default.json');
const testUser = {
email: '[email protected]',
password: 'password1234',
};
describe('POST /authentication', () => {
beforeEach(() => {
// delete all users then create the test user
return app.service('users').remove(null)
.then(() => app.service('users').create(Object.assign({}, testUser)));
});
afterEach(() => {
// delete all users
return app.service('users').remove(null);
});
it.only('creates a valid JWT', () => {
return supertest(app)
.post('/authentication')
.type('json')
.send(Object.assign({strategy: 'local'}, testUser))
.expect(201)
.then(res => {
return app.passport.verifyJWT(res.body.accessToken, {
secret: config.authentication.secret,
});
});
});
});
テストを実行します。
npm test
テストに合格する必要があります。
テストは次の出力で失敗します。
POST /authentication
info: after: users - Method: remove
info: after: users - Method: create
info: error: authentication - Method: create: Authentication strategy 'local' is not registered.
error: BadRequest: Authentication strategy 'local' is not registered.
at Error.BadRequest (D:\Users\Jordan\Desktop\feathers-test-test2\node_modules\feathers-errors\lib\index.js:93:17)
at Object.<anonymous> (D:\Users\Jordan\Desktop\feathers-test-test2\node_modules\feathers-authentication\lib\hooks\authenticate.js:62:29)
at process._tickCallback (internal/process/next_tick.js:109:7)
1) creates a valid JWT
info: after: users - Method: remove
0 passing (381ms)
1 failing
1) POST /authentication creates a valid JWT:
Error: expected 201 "Created", got 400 "Bad Request"
at Test._assertStatus (node_modules\supertest\lib\test.js:266:12)
at Test._assertFunction (node_modules\supertest\lib\test.js:281:11)
at Test.assert (node_modules\supertest\lib\test.js:171:18)
at Server.assert (node_modules\supertest\lib\test.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)
モジュールバージョン:
{
"dependencies": {
"body-parser": "^1.17.2",
"compression": "^1.7.0",
"cors": "^2.8.4",
"feathers": "^2.2.0",
"feathers-authentication": "^1.2.7",
"feathers-authentication-hooks": "^0.1.4",
"feathers-authentication-jwt": "^0.3.2",
"feathers-authentication-local": "^0.4.4",
"feathers-configuration": "^0.4.1",
"feathers-errors": "^2.9.1",
"feathers-hooks": "^2.0.2",
"feathers-hooks-common": "^3.7.2",
"feathers-nedb": "^2.7.0",
"feathers-rest": "^1.8.0",
"feathers-socketio": "^2.0.0",
"helmet": "^3.8.1",
"nedb": "^1.8.0",
"serve-favicon": "^2.4.3",
"winston": "^2.3.1"
},
"devDependencies": {
"eslint": "^4.6.1",
"mocha": "^3.5.0",
"request": "^2.81.0",
"request-promise": "^4.2.1",
"supertest": "^3.0.0"
}
}
NodeJSバージョン:v6.11.2
この問題はfeathers-authenticationに属している
app.listen
を呼び出さない場合は、 app.setup(server)を手動で呼び出す必要があります。 多くのプラグイン(認証を含む)は、他のすべて(ミドルウェアやサービスなど)が登録された後、 setup
メソッドを使用してセットアップを行っています。
ありがとうございました。 それが問題でした。 私はその仮定supertest
呼び出しapp.listen
しかし、それを呼び出し、 http.createServer(app).listen
。 これが私が思いついた回避策です:
const supertest = require('supertest');
const app = require('../../src/app');
const config = require('../../config/default.json');
const testUser = {
email: '[email protected]',
password: 'password1234',
};
describe('POST /authentication', () => {
let server;
beforeEach(done => {
// start the app
server = app.listen(0, done);
});
beforeEach(() => {
// delete all users then create the test user
return app.service('users').remove(null)
.then(() => app.service('users').create(Object.assign({}, testUser)));
});
afterEach(() => {
// delete all users
return app.service('users').remove(null);
});
afterEach(done => {
// stop the app
server.close(done);
});
it('creates a valid JWT', () => {
return supertest(server)
.post('/authentication')
.type('json')
.send(Object.assign({strategy: 'local'}, testUser))
.expect(201)
.then(res => {
return app.passport.verifyJWT(res.body.accessToken, {
secret: config.authentication.secret,
});
});
});
});
この問題は、クローズされた後、最近のアクティビティがないため、自動的にロックされています。 関連するバグについては、この問題へのリンクを含む新しい問題を開いてください。
最も参考になるコメント
app.listen
を呼び出さない場合は、 app.setup(server)を手動で呼び出す必要があります。 多くのプラグイン(認証を含む)は、他のすべて(ミドルウェアやサービスなど)が登録された後、setup
メソッドを使用してセットアップを行っています。