Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I'm using "mocha" and "chai-http" to do this and it all works well aside from when I add authentication. 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. (The database operations are Sequelize). How can I test the API operations in the app? Is there a standard approach?

like image 331
Augustin Grigorov Avatar asked Oct 21 '25 03:10

Augustin Grigorov


1 Answers

The only solution to the problem I found is to use a mock passport authentication strategy for testing like this one: https://www.npmjs.com/package/passport-mocked And use that instead of your strategy when you are in a testing environment.

like image 142
Augustin Grigorov Avatar answered Oct 23 '25 18:10

Augustin Grigorov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!