简体   繁体   English

如何使用 mocha 测试带有 Passport Google OAuth 身份验证的 Express REST API?

[英]How to test an Express REST API with Passport Google OAuth authentication with mocha?

I'm building an app and as part of it, I have a REST API on Express that I would like to write integration tests for.我正在构建一个应用程序,作为其中的一部分,我在 Express 上有一个 REST API,我想为其编写集成测试。 I'm using "mocha" and "chai-http" to do this and it all works well aside from when I add authentication.我正在使用“mocha”和“chai-http”来执行此操作,除了添加身份验证外,其他一切都运行良好。 So in a test like:所以在像这样的测试中:

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()
        })
    })
  })
})

I would get an error because my request won't come from an authenticated session.我会收到错误消息,因为我的请求不是来自经过身份验证的会话。 I am using "passport" with Google OAuth strategy so I can't send a post request to a login page and I can't think of a way to log in or authenticate my requests.我在 Google OAuth 策略中使用“passport”,所以我无法向登录页面发送发布请求,也想不出登录或验证我的请求的方法。 (The database operations are Sequelize). (数据库操作是Sequelize)。 How can I test the API operations in the app?如何在应用中测试 API 操作? Is there a standard approach?有标准的方法吗?

我发现的问题的唯一解决方案是使用模拟护照身份验证策略进行如下测试: https : //www.npmjs.com/package/passport-mocked并在您进行测试时使用该策略而不是您的策略环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM