lundi 6 juillet 2015

SailsJS Passport Session Persistance Issue: Mocha Test Failing

I'm trying to write some basic authentication tests using Mocha, Chai, and Superagent with SailsJS and Passport as the authentication framework. The following is my test scenario and for some reason I'm unable to keep a persistent session it seems as my last test to see if the user has access to /userplansetting/edit fails. Note: I have confirmed that this works when I login via the web interface.

Update: Here's a repo that contains similar code and results http://ift.tt/1KLHu6e

request = require("superagent")
crypto = require("crypto")
async = require("async")
chai = require("chai")
expect = chai.expect
should = chai.should()
assert = chai.assert

userStub = ->
  randString = crypto.randomBytes(20).toString("hex")
  username: randString.slice(0, 15)
  biography: randString + " is a auto generated user!"
  email: randString + "@gmail.com"
  password: "123123123123"
  displayName: "John Doe"
  language: "en-us"

describe "Auth", ->
  appURL = "http://localhost:1335"
  user = undefined
  agent1 = request.agent() # sails.hooks.http.app

  loginUser = (agent, userObj) ->
    (done) ->
      onResponse = (err, res) ->
        should.not.exist(err)
        res.status.should.eql 200
        res.text.should.include "Your Campaigns"
        done()
      agent.post(appURL + "/login")
        .send(userObj)
        .end onResponse

  registerUser = (agent, userObj) ->
    (done) ->
      onResponse = (err, res) ->
        should.not.exist(err)
        res.status.should.eql 200
        res.text.should.include "Your Campaigns"
        done()
      agent.post(appURL + "/auth/local/register")
        .send(userObj)
        .end onResponse

  describe "Register User", ->
    describe "JSON Requests", ->
      describe "POST", ->
        it "/auth/local/register should register a user", (done) ->
          uStub = userStub()
          password = uStub.password
          userObj =
            email: uStub.email
            username: uStub.username
            biography: uStub.biography
            displayName: uStub.displayName
            language: uStub.language
            password: password
          registerUser(agent1, userObj)
          done()

  describe "Sign Out Registered User", ->
    describe "JSON Requests", ->
      describe "GET", ->
        agent = request.agent()
        it "should start with signin", (done) ->
          userObj =
            email: global.fixtures.user[0].email
            password: global.fixtures.passport[0].password
          loginUser(agent, userObj)
          done()
        it "should sign the user out", (done) ->
          agent.get(appURL + "/auth/local/logout")
            .end (err, res) ->
              if err then done(err)
              res.status.should.eql 200
              res.redirects.should.eql [ appURL + "/login" ]
              done()
        it "should destroy the user session", (done) ->
          agent.get(appURL + "/plan")
            .end (err, res) ->
              should.exist(err)
              expect(res).to.have.property('error')
              res.status.should.eql 403
              res.text.should.include 'You are not permitted to perform this action.'
              done()

  describe "UnAuthenticated", ->
    describe "JSON Requests", ->
      describe "POST", ->
        agent2 = request.agent()
        it "/auth/local should login user", (done) ->
          userObj =
            email: global.fixtures.user[1].email
            password: global.fixtures.passport[1].password
          loginUser(agent2, userObj)
          done()
        it "/userplansetting/edit should allow access", (done) ->
            # do a seccond request to ensures how user is logged in
            agent2.get(appURL + "/userplansetting/edit")
              .end (err, res) ->
                should.not.exist(err)
                sails.log res
                res.status.should.eql 200
                done()

Test results

+------------------------------------+
| Running mocha tests                |
+------------------------------------+
Debugger listening on port 5858
warn: Lifting sails...
debug: Loading models from /Users/robsawyer/Sites/specs/test/fixtures/models
  i18n:debug will write to /Users/robsawyer/Sites/specs/config/locales/en.json +0ms
  i18n:debug read /Users/robsawyer/Sites/specs/config/locales/en.json for locale: en +1ms
  i18n:debug will write to /Users/robsawyer/Sites/specs/config/locales/es.json +1ms
  i18n:debug read /Users/robsawyer/Sites/specs/config/locales/es.json for locale: es +0ms
  i18n:debug will write to /Users/robsawyer/Sites/specs/config/locales/fr.json +0ms
  i18n:debug read /Users/robsawyer/Sites/specs/config/locales/fr.json for locale: fr +0ms
  i18n:debug will write to /Users/robsawyer/Sites/specs/config/locales/de.json +1ms
  i18n:debug read /Users/robsawyer/Sites/specs/config/locales/de.json for locale: de +0ms
debug: --------------------------------------------------------
debug: :: Mon Jul 06 2015 10:34:11 GMT-0700 (PDT)
debug: Environment : test/bootstrap.test.*,test
debug: Port        : 1335
debug: --------------------------------------------------------
debug: --- Populated the database. ---
  Barrels
    constructor
      ✓ should load all the json files from default folder
      ✓ should set generate lowercase property names for models
    populate()
      populate(cb)
        ✓ should populate the DB with users

  Auth
    Register User
      JSON Requests
        POST
          ✓ /auth/local/register should register a user
    Sign Out Registered User
      JSON Requests
        GET
          ✓ should start with signin
          ✓ should sign the user out (57ms)
          ✓ should destroy the user session
    UnAuthenticated
      JSON Requests
        POST
          ✓ /auth/local should login user
debug: { jar:
   { setCookie: [Function: setCookie],
     getCookie: [Function: getCookie],
     getCookies: [Function: getCookies] } }
          1) /userplansetting/edit should allow access

  actions
    login
      ✓ should assume auth method if only one is required

  actions
    logout
      ✓ should trigger default logout if params.type is undefined

  UserModel
    to have
      ✓ attributes


warn: Lowering sails...

  11 passing (4s)
  1 failing

  1) Auth UnAuthenticated JSON Requests POST /userplansetting/edit should allow access:
     Uncaught AssertionError: expected [Error: Forbidden] to not exist

Aucun commentaire:

Enregistrer un commentaire