Alsotang 75d2c51b5c 写了 OAuth 部分的测试并小重构了一下
重构:
1. 重构 app.js 里面混乱的 app.use
2. 将 midderwares 纠正成了 middlewares
2013-12-26 12:30:00 +08:00

123 lines
3.6 KiB
JavaScript

var app = require('../../app');
var request = require('supertest')(app);
var mm = require('mm');
var passport = require('passport');
var path = require('path');
var github = require('../../controllers/github');
var Models = require('../../models');
var User = Models.User;
describe('controllers/github.js', function () {
afterEach(function () {
mm.restore();
});
it('should 302 when get /auth/github', function (done) {
request.get('/auth/github')
.expect(302, function (err, res) {
if (err) {
return done(err);
}
res.headers.should.have.property('location')
.with.startWith('https://github.com/login/oauth/authorize?');
done();
});
});
describe('get /auth/github/callback', function () {
before(function () {
app.get('/auth/github/test_callback',
function (req, res, next) {
req.user = {id: 'notexists'};
next();
},
github.callback);
});
it('should redirect to /auth/github/new when the github id not in database', function (done) {
request.get('/auth/github/test_callback?code=123456')
.expect(302, function (err, res) {
if (err) {
return done(err);
}
res.headers.should.have.property('location')
.with.endWith('/auth/github/new');
done();
});
});
it('should redirect to / when the user is registed', function (done) {
mm.data(User, 'findOne', {});
request.get('/auth/github/test_callback?code=123456')
.expect(302, function (err, res) {
if (err) {
return done(err);
}
res.headers.should.have.property('location')
.with.endWith('/');
done();
});
});
});
describe('get /auth/github/new', function () {
it('should 200', function (done) {
request.get('/auth/github/new')
.expect(200, function (err, res) {
if (err) {
return done(err);
}
res.text.should.include('/auth/github/create');
done();
});
});
});
describe('post /auth/github/create', function () {
before(function () {
app.post('/auth/github/test_create', function (req, res, next) {
req.session.profile = {
displayName: 'alsotang' + new Date(),
username: 'alsotang' + new Date(),
accessToken: 'a3l24j23lk5jtl35tkjglfdsf',
emails: [{value: 'alsotang@gmail.com' + new Date()}],
_json: {avatar_url: 'http://avatar_url.com/1.jpg'},
id: 22,
};
next();
}, github.create);
});
it('should create a new user', function (done) {
var userCount;
User.count(function (err, count) {
userCount = count;
request.post('/auth/github/test_create')
.expect(302, function (err, res) {
if (err) {
return done(err);
}
res.headers.should.have.property('location')
.with.endWith('/');
User.count(function (err, count) {
count.should.equal(userCount + 1);
done();
});
});
});
});
it('should link a old user', function (done) {
var username = 'Alsotang';
var pass = 'hehe';
mm(User, 'findOne', function (loginInfo, callback) {
loginInfo.loginname.should.equal(username.toLowerCase());
callback(null, {save: function () {
done();
}});
});
request.post('/auth/github/test_create')
.send({name: username, pass: pass})
.end();
});
});
});