如何使用 Passport Google OAuth 身份验证和 mocha 测试 Express REST API?
How to test an Express REST API with Passport Google OAuth authentication with mocha?
我正在构建一个应用程序,作为它的一部分,我在 Express 上有一个 REST API,我想为其编写集成测试。我正在使用 "mocha" 和 "chai-http" 来执行此操作,除了添加身份验证之外,它都运行良好。所以在这样的测试中:
process.env.NODE_ENV = 'development'
let chai = require('chai')
let chaiHttp = require('chai-http')
let should = chai.should()
var server = require('../app.js')
var schemas = require('../schemas')
var Snippet = schemas.Snippet
chai.use(require('chai-passport-strategy'))
chai.use(chaiHttp)
describe('Snippet', () => {
beforeEach((done) => {
Snippet.destroy({
where: {}
}).then(function () {
done()
}).catch(function (e) {
done(e)
})
})
describe('snippet create', () => {
it('it should store a snippet ', (done) => {
let snippet = {
title: 'Test',
description: 'Mock description',
snippet: 'Mock body',
keywords: 'Test key, words;input',
type: 'sql',
rating: 1,
approved: false
}
chai.request(server)
.post('/api/submitSnippet/')
.send(snippet)
.end((err, res) => {
if (err) console.log(err)
res.should.have.status(200)
res.body.should.be.a('object')
res.body.should.have.property('title')
res.body.should.have.property('description')
res.body.should.have.property('snippet')
res.body.should.have.property('keywords')
res.body.should.have.property('type')
res.body.should.have.property('rating')
res.body.should.have.property('approved')
res.body.title.should.equal('Test')
res.body.description.should.equal('Mock description')
res.body.snippet.should.equal('Mock body')
res.body.keywords.should.equal(['Test', 'key', 'words', 'input'])
res.body.type.should.equal('sql')
res.body.rating.should.equal(1)
res.body.approved.should.equal(false)
done()
})
})
})
})
我会收到错误消息,因为我的请求不是来自经过身份验证的会话。我正在使用 "passport" 和 Google OAuth 策略,所以我无法向登录页面发送 post 请求,而且我想不出登录或验证我的请求的方法。 (数据库操作是Sequelize)。如何在应用程序中测试 API 操作?有标准方法吗?
我发现的问题的唯一解决方案是使用模拟护照身份验证策略进行测试,如下所示:
https://www.npmjs.com/package/passport-mocked
当你在测试环境中时,使用它而不是你的策略。
我正在构建一个应用程序,作为它的一部分,我在 Express 上有一个 REST API,我想为其编写集成测试。我正在使用 "mocha" 和 "chai-http" 来执行此操作,除了添加身份验证之外,它都运行良好。所以在这样的测试中:
process.env.NODE_ENV = 'development'
let chai = require('chai')
let chaiHttp = require('chai-http')
let should = chai.should()
var server = require('../app.js')
var schemas = require('../schemas')
var Snippet = schemas.Snippet
chai.use(require('chai-passport-strategy'))
chai.use(chaiHttp)
describe('Snippet', () => {
beforeEach((done) => {
Snippet.destroy({
where: {}
}).then(function () {
done()
}).catch(function (e) {
done(e)
})
})
describe('snippet create', () => {
it('it should store a snippet ', (done) => {
let snippet = {
title: 'Test',
description: 'Mock description',
snippet: 'Mock body',
keywords: 'Test key, words;input',
type: 'sql',
rating: 1,
approved: false
}
chai.request(server)
.post('/api/submitSnippet/')
.send(snippet)
.end((err, res) => {
if (err) console.log(err)
res.should.have.status(200)
res.body.should.be.a('object')
res.body.should.have.property('title')
res.body.should.have.property('description')
res.body.should.have.property('snippet')
res.body.should.have.property('keywords')
res.body.should.have.property('type')
res.body.should.have.property('rating')
res.body.should.have.property('approved')
res.body.title.should.equal('Test')
res.body.description.should.equal('Mock description')
res.body.snippet.should.equal('Mock body')
res.body.keywords.should.equal(['Test', 'key', 'words', 'input'])
res.body.type.should.equal('sql')
res.body.rating.should.equal(1)
res.body.approved.should.equal(false)
done()
})
})
})
})
我会收到错误消息,因为我的请求不是来自经过身份验证的会话。我正在使用 "passport" 和 Google OAuth 策略,所以我无法向登录页面发送 post 请求,而且我想不出登录或验证我的请求的方法。 (数据库操作是Sequelize)。如何在应用程序中测试 API 操作?有标准方法吗?
我发现的问题的唯一解决方案是使用模拟护照身份验证策略进行测试,如下所示: https://www.npmjs.com/package/passport-mocked 当你在测试环境中时,使用它而不是你的策略。