Skip to content

Commit 75d2c51

Browse files
committed
写了 OAuth 部分的测试并小重构了一下
重构: 1. 重构 app.js 里面混乱的 app.use 2. 将 midderwares 纠正成了 middlewares
1 parent 6edb0e9 commit 75d2c51

9 files changed

Lines changed: 169 additions & 33 deletions

File tree

app.js

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ var passport = require('passport');
1616
var Models = require('./models');
1717
var User = Models.User;
1818
var GitHubStrategy = require('passport-github').Strategy;
19-
var githubStrategyMiddleware = require('./midderwares/github_strategy');
19+
var githubStrategyMiddleware = require('./middlewares/github_strategy');
20+
var routes = require('./routes');
21+
22+
var maxAge = 3600000 * 24 * 30;
23+
var staticDir = path.join(__dirname, 'public');
2024

2125
// assets
2226
var assets = {};
@@ -32,7 +36,6 @@ if (config.mini_assets) {
3236
// host: http://127.0.0.1
3337
var urlinfo = require('url').parse(config.host);
3438
config.hostname = urlinfo.hostname || config.host;
35-
var routes = require('./routes');
3639

3740
config.upload_dir = config.upload_dir || path.join(__dirname, 'public', 'user_data', 'images');
3841
// ensure upload dir exists
@@ -59,25 +62,30 @@ app.configure(function () {
5962
app.use(passport.initialize());
6063
// custom middleware
6164
app.use(require('./controllers/sign').auth_user);
65+
app.use('/upload/', express.static(config.upload_dir, { maxAge: maxAge }));
66+
// old image url: http://host/user_data/images/xxxx
67+
app.use('/user_data/', express.static(path.join(__dirname, 'public', 'user_data'), { maxAge: maxAge }));
68+
});
6269

63-
var csrf = express.csrf();
70+
app.configure('development', function () {
71+
app.use('/public', express.static(staticDir));
72+
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
73+
});
74+
75+
app.configure('production', function () {
6476
app.use(function (req, res, next) {
77+
var csrf = express.csrf();
6578
// ignore upload image
6679
if (req.body && req.body.user_action === 'upload_image') {
6780
return next();
6881
}
6982
csrf(req, res, next);
7083
});
84+
app.use('/public', express.static(staticDir, { maxAge: maxAge }));
85+
app.use(express.errorHandler());
86+
app.set('view cache', true);
7187
});
7288

73-
if (process.env.NODE_ENV !== 'test') {
74-
// plugins
75-
var plugins = config.plugins || [];
76-
for (var i = 0, l = plugins.length; i < l; i++) {
77-
var p = plugins[i];
78-
app.use(require('./plugins/' + p.name)(p.options));
79-
}
80-
}
8189

8290
// set static, dynamic helpers
8391
app.helpers({
@@ -87,23 +95,14 @@ app.helpers({
8795
});
8896
app.dynamicHelpers(require('./common/render_helpers'));
8997

90-
var maxAge = 3600000 * 24 * 30;
91-
app.use('/upload/', express.static(config.upload_dir, { maxAge: maxAge }));
92-
// old image url: http://host/user_data/images/xxxx
93-
app.use('/user_data/', express.static(path.join(__dirname, 'public', 'user_data'), { maxAge: maxAge }));
94-
95-
var staticDir = path.join(__dirname, 'public');
96-
app.configure('development', function () {
97-
app.use('/public', express.static(staticDir));
98-
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
99-
});
100-
101-
app.configure('production', function () {
102-
app.use('/public', express.static(staticDir, { maxAge: maxAge }));
103-
app.use(express.errorHandler());
104-
app.set('view cache', true);
105-
});
106-
98+
if (process.env.NODE_ENV !== 'test') {
99+
// plugins
100+
var plugins = config.plugins || [];
101+
for (var i = 0, l = plugins.length; i < l; i++) {
102+
var p = plugins[i];
103+
app.use(require('./plugins/' + p.name)(p.options));
104+
}
105+
}
107106

108107
// github oauth
109108
passport.serializeUser(function (user, done) {
@@ -114,7 +113,6 @@ passport.deserializeUser(function (user, done) {
114113
});
115114
passport.use(new GitHubStrategy(config.GITHUB_OAUTH, githubStrategyMiddleware));
116115

117-
118116
// routes
119117
routes(app);
120118

File renamed without changes.
File renamed without changes.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"mocha": "*",
3131
"rewire": "*",
3232
"jscover": "*",
33-
"pedding": "*"
33+
"pedding": "*",
34+
"mm": "*"
3435
},
3536
"scripts": {
3637
"test": "make test"

routes.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ var rss = require('./controllers/rss');
1919
var upload = require('./controllers/upload');
2020
var assets = require('./controllers/static');
2121
var tools = require('./controllers/tools');
22-
var auth = require('./midderwares/auth');
23-
var limit = require('./midderwares/limit');
22+
var auth = require('./middlewares/auth');
23+
var limit = require('./middlewares/limit');
2424
var status = require('./controllers/status');
2525
var github = require('./controllers/github');
2626
var passport = require('passport');
27-
var configMiddleware = require('./midderwares/conf');
27+
var configMiddleware = require('./middlewares/conf');
2828
var config = require('./config');
2929

3030

test/controllers/github.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
var app = require('../../app');
2+
var request = require('supertest')(app);
3+
var mm = require('mm');
4+
var passport = require('passport');
5+
var path = require('path');
6+
var github = require('../../controllers/github');
7+
var Models = require('../../models');
8+
var User = Models.User;
9+
10+
describe('controllers/github.js', function () {
11+
afterEach(function () {
12+
mm.restore();
13+
});
14+
15+
it('should 302 when get /auth/github', function (done) {
16+
request.get('/auth/github')
17+
.expect(302, function (err, res) {
18+
if (err) {
19+
return done(err);
20+
}
21+
res.headers.should.have.property('location')
22+
.with.startWith('https://github.com/login/oauth/authorize?');
23+
done();
24+
});
25+
});
26+
27+
describe('get /auth/github/callback', function () {
28+
before(function () {
29+
app.get('/auth/github/test_callback',
30+
function (req, res, next) {
31+
req.user = {id: 'notexists'};
32+
next();
33+
},
34+
github.callback);
35+
});
36+
it('should redirect to /auth/github/new when the github id not in database', function (done) {
37+
request.get('/auth/github/test_callback?code=123456')
38+
.expect(302, function (err, res) {
39+
if (err) {
40+
return done(err);
41+
}
42+
res.headers.should.have.property('location')
43+
.with.endWith('/auth/github/new');
44+
done();
45+
});
46+
});
47+
48+
it('should redirect to / when the user is registed', function (done) {
49+
mm.data(User, 'findOne', {});
50+
51+
request.get('/auth/github/test_callback?code=123456')
52+
.expect(302, function (err, res) {
53+
if (err) {
54+
return done(err);
55+
}
56+
res.headers.should.have.property('location')
57+
.with.endWith('/');
58+
done();
59+
});
60+
});
61+
});
62+
63+
describe('get /auth/github/new', function () {
64+
it('should 200', function (done) {
65+
request.get('/auth/github/new')
66+
.expect(200, function (err, res) {
67+
if (err) {
68+
return done(err);
69+
}
70+
res.text.should.include('/auth/github/create');
71+
done();
72+
});
73+
});
74+
});
75+
76+
describe('post /auth/github/create', function () {
77+
before(function () {
78+
app.post('/auth/github/test_create', function (req, res, next) {
79+
req.session.profile = {
80+
displayName: 'alsotang' + new Date(),
81+
username: 'alsotang' + new Date(),
82+
accessToken: 'a3l24j23lk5jtl35tkjglfdsf',
83+
emails: [{value: 'alsotang@gmail.com' + new Date()}],
84+
_json: {avatar_url: 'http://avatar_url.com/1.jpg'},
85+
id: 22,
86+
};
87+
next();
88+
}, github.create);
89+
});
90+
it('should create a new user', function (done) {
91+
var userCount;
92+
User.count(function (err, count) {
93+
userCount = count;
94+
request.post('/auth/github/test_create')
95+
.expect(302, function (err, res) {
96+
if (err) {
97+
return done(err);
98+
}
99+
res.headers.should.have.property('location')
100+
.with.endWith('/');
101+
User.count(function (err, count) {
102+
count.should.equal(userCount + 1);
103+
done();
104+
});
105+
});
106+
});
107+
});
108+
109+
it('should link a old user', function (done) {
110+
var username = 'Alsotang';
111+
var pass = 'hehe';
112+
mm(User, 'findOne', function (loginInfo, callback) {
113+
loginInfo.loginname.should.equal(username.toLowerCase());
114+
callback(null, {save: function () {
115+
done();
116+
}});
117+
});
118+
request.post('/auth/github/test_create')
119+
.send({name: username, pass: pass})
120+
.end();
121+
});
122+
});
123+
});

test/middlewares/conf.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var conf = require('../../middlewares/conf');
2+
var config = require('../../config');
3+
4+
describe('middlewares/conf.js', function () {
5+
it('should alert no github oauth', function (done) {
6+
var _clientId = config.GITHUB_OAUTH.clientID;
7+
config.GITHUB_OAUTH.clientID = 'your GITHUB_CLIENT_ID';
8+
conf.github({}, {send: function (str) {
9+
str.should.equal('call the admin to set github oauth.');
10+
config.GITHUB_OAUTH.clientID = _clientId;
11+
done();
12+
}});
13+
});
14+
});

0 commit comments

Comments
 (0)