2014-02-26 40 views
2

我有一个SailsJS设置,并且正在使用Mocha/Supertest/Superagent组合来运行单元测试。我搜索了四周并阅读了supertest,以及它如何扩展superagent.agent('url')以存储会话和cookie。我知道我的/运动员/登录和/运动员/当前路线正在工作,因为我可以使用邮差测试他们,并且他们返回正确的值。不过,测试时,我得到一个登录200个状态,但上/运动员404 /电流使用SailsJS/Node,Mocha,Supertest进行身份验证的会话存储

这里是我目前的工作:

1.摩卡测试登录和验证登录的用户会话在

var should = require('should'), 
    assert = require('assert'), 
    request = require('supertest'); 

it('/athlete/login should return 200 and athlete on success', function (done){ 
       var athleteStub = AthleteStub(), 
        setPassword = athleteStub.password; 

       Athlete.create(athleteStub, function(err, newAthlete) { 
        var user = request.agent(sails.express.app); 

        user 
          .post('/athlete/login') 
          .send({ 'email': athleteStub.email, 'password': setPassword }) 
          .expect('Content-Type', /json/) 
          .end(function (err, res) { 
           if(err) return done(err); 

           console.log('test', user); 

           // res.should.have.status(200); 
           // res.body.email.should.equal(athleteStub.email); 

           user 
            .get('/athlete/current') 
            .set('Accept', 'application/json') 
            .expect('Content-Type', /json/) 
            .expect(200) 
            .end(function (err, res) { 
             if(err) return done(err); 
             done(); 
            }); 


          }); 
       }); 
      }); 

2 /登录和/当前的行动

login: function (req, res) { 
    var bcrypt = require('bcrypt'); 

    Athlete.findOneByEmail(req.body.email).done(function (err, athlete) { 
     if (err) { 
      res.json({ error: 'DB error' }, 500); 
     } 

     if (athlete) { 

      if (!athlete.isActive){ 
       res.json({ error: 'Your account is not active.' }, 500); 
      } 

      bcrypt.compare(req.body.password, athlete.password, function (err, match) { 
       if (err){ 
        res.json({ error: 'Server error' }, 500); 
       } 

       if (match) { 
        // password match 
        req.session.athlete = athlete.id; 
        res.json(athlete); 
       } else { 
        // invalid password 
        if (req.session.athlete){ 
         req.session.athlete = null; 
        } 
        res.json({ error: 'Invalid password' }, 403); 
       } 
      }); 
     } else { 
      res.json({ error: 'User not found' }, 404); 
     } 
    }); 
}, 

current: function (req, res){ 

    if(req.session.athlete){ 
     Athlete.findOneById(req.session.athlete) 
      .where({ isActive: true }) 
      .done(function(err, athlete) { 
      if (err) { 
       res.json({ error: 'No active athlete found with ID of '+req.params.id }, 404); 
      } else { 
       res.json(athlete); 
      } 
     }); 
    }else{ 
     res.json({ error: 'No active athlete currently logged in.' }, 404); 
    } 

}, 

SOLUTION

我已经改变了一些路由所以基本上从上面把 '/运动员/电流/' 的现在是 '/运动员/ ME /'

  it("/athlete/me should return user data if athlete is logged in", function(done){ 
      var agent = request.agent(sails.hooks.http.app), 
       athleteStub = AthleteStub(), 
       setPassword = athleteStub.password; 

      agent 
       .post('/athlete') 
       .send(athleteStub) 
       .expect('Content-Type', /json/) 
       .end(function (err, res) { 
        should.not.exist(err); 
        res.should.have.status(200); 
        res.body.email.should.equal(athleteStub.email); 
        res.body.firstName.should.equal(athleteStub.firstName); 
        res.body.lastName.should.equal(athleteStub.lastName); 

        agent 
         .post('/athlete/login') 
         .send({ 'email': athleteStub.email, 'password': setPassword }) 
         .expect('Content-Type', /json/) 
         .end(function (err, res) { 
          should.not.exist(err); 
          res.should.have.status(200); 

          agent 
           .get('/athlete/me') 
           .expect(200) 
           .end(function(err, res) { 
             should.not.exist(err); 

             res.body.email.should.equal(athleteStub.email); 
             res.should.have.status(200); 
             done(); 
            }); 
         }); 
       }); 
     }); 
+0

嗨,有没有找到一个解决您的问题?我面临同样的问题。提前致谢。 – jmcollin92

+0

@ jmcollin92 - 没有,仍然有同样的问题。现在,我刚刚离开测试,以确保我的构建通过。我已经开始使用Sails的最新测试版了,并且最终会再次实现它(或者至少试图) – cgaubuchon

回答

1

不知道,如果你找到了一个答案或不,但我能得到的cookie保存以下代码:

var agent = request.agent(sails.hooks.http.app); 

describe('Session', function(done) { 
it("should be able to create", function(done) { 


agent 
    .post("/session/create") 
    .send({email: "[email protected]",password: "test", confirmation: "test"}) 
    .expect('set-cookie', 'cookie=hey; Path=/', done) 
    .end(function(err, res) { 
    console.log('got a response!! '+ JSON.stringify(res.body)); 
    done(); 
    }); 
}); 
it("should be logged in", function(done) { 


agent 
    .get("/user/index") 
    .end(function(err, res) { 
    console.log('got a response from user/index!! '+ JSON.stringify(res.body)); 
    done(); 
    }); 
    }); 
}); 
+0

需要将代理变量声明放入它的声明中,但最终让它工作在非常粗糙的状态现在。不知道这是0.10版的帆/水线固定的东西。现在用我的解决方案更新OP。 – cgaubuchon