0

I've been working a while on getting a test to work with Mocha and Passport. I tried a couple articles on here, but I can't get them to work. Currently, I have installed supertest and I'm trying that.

process.env.NODE_ENV = 'test';


var chai = require('chai');
var chaiHttp = require('chai-http');
var app = require('../app');
//var request = require('supertest')//(app);
//var server = request.agent(app);
//var Strategy = require('passport-strategy');
var Strategy = require('passport-local').Strategy;
var m_ticket_data = require('../model/ticket');
var m_Kbase_data = require('../model/Kbase');
var m_KbaseScript_schema = require('../model/KbaseScript');


var should = chai.should();
var expect = chai.expect;
chai.use(chaiHttp);
chai.use(require('chai-passport-strategy'));



describe('Ticket', function() {


    var user, info;

    before(function(done) {
      console.log("hello from strategy");
          chai.passport.use( new Strategy(function(username, password, done){
              console.log("hello from strategy2");
              done(null, { id: '1234' }, { scope: 'read' });
          }

          ))
            .success(function(u, i) {
              user = u;
              info = i;
              done();
            })
            .req(function(req) {
              //req.headers.authorization = 'Bearer vF9dft4qmT';
            })
            .authenticate();                

        done();
          });

    it ('blankout the database', function(done){
            m_ticket_data.remove({}, function(){
                  done();

            });

      });



      it('looks for a blank from /ticket/all', function (done) {
          chai.request('http://127.0.0.1:5000')
          .get('/ticket/all')
          .end(function (err, res) {
              res.status.should.equal(200);
              console.log(res.body);
              //expect(res.body).to.deep.equal({});
              done();
          });
      });

});

I can't create a temp user for testing, so I was thinking I was going to have to overwrite the authentication. However, I'm having a problem doing that. I found this npm (https://github.com/jaredhanson/chai-passport-strategy), and I'm trying this as the latest.

This is a test I created that works fine. I don't need to authenticate for it.

process.env.NODE_ENV = 'test';


var chai = require('chai');
var chaiHttp = require('chai-http');
var server = require('../app');

var m_Kbase_data = require('../model/Kbase');
var m_KbaseScript_schema = require('../model/KbaseScript');


var should = chai.should();
var expect = chai.expect;
chai.use(chaiHttp);

describe('KBasefull', function() {

    m_Kbase_data.collection.drop();


    it('need to add a kbase article for testing /KBase', function (done) {
          chai.request('http://127.0.0.1:5000')
          .post('/kbase')
          .send({Problem: "Problem", description: "Description", resolution: "Something to fix"})
          .end(function(err, res){
              res.should.have.status(200);
              done();
          });
    });
   }  
user269964
  • 159
  • 11

0 Answers0