forked from cnodejs/nodeclub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.test.js
More file actions
156 lines (146 loc) · 4.7 KB
/
github.test.js
File metadata and controls
156 lines (146 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var app = require('../../app');
var request = require('supertest')(app);
var mm = require('mm');
var github = require('../../controllers/github');
var Models = require('../../models');
var User = Models.User;
var config = require('../../config');
var support = require('../support/support');
describe('test/controllers/github.test.js', function () {
before(function (done) {
support.ready(done);
});
afterEach(function () {
mm.restore();
});
it('should 302 when get /auth/github', function (done) {
var _clientID = config.GITHUB_OAUTH.clientID;
config.GITHUB_OAUTH.clientID = 'aldskfjo2i34j2o3';
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?');
config.GITHUB_OAUTH.clientID = _clientID;
done();
});
});
describe('get /auth/github/callback', function () {
before(function () {
app.get('/auth/github/test_callback',
function (req, res, next) {
req.user = {
id: 'notexists',
emails: [
{value: 'notexists@gmail.com'}
],
_json: {avatar_url: 'http://avatar_url'}
};
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', {save: function (callback) {
process.nextTick(callback);
}});
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.containEql('/auth/github/create');
done();
});
});
});
describe('post /auth/github/create', function () {
before(function () {
var displayName = 'alsotang' + +new Date();
var username = 'alsotang' + +new Date();
var email = 'alsotang@gmail.com' + +new Date();
app.post('/auth/github/test_create', function (req, res, next) {
req.session.profile = {
displayName: displayName,
username: req.body.githubName || username,
accessToken: 'a3l24j23lk5jtl35tkjglfdsf',
emails: [
{value: email}
],
_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.countDocuments(function (err, count) {
userCount = count;
request.post('/auth/github/test_create')
.send({isnew: '1'})
.expect(302, function (err, res) {
if (err) {
return done(err);
}
res.headers.should.have.property('location')
.with.endWith('/');
User.countDocuments(function (err, count) {
count.should.equal(userCount + 1);
done();
});
});
});
});
it('should not create a new user when loginname or email conflict', function (done) {
request.post('/auth/github/test_create')
.send({isnew: '1'})
.expect(500, function (err, res) {
if (err) {
return done(err);
}
res.text.should.match(/您 GitHub 账号的.*与之前在 CNodejs 注册的.*重复了/);
done();
});
});
it('should link a old user', function (done) {
var username = 'alsotang' + +new Date();
var pass = 'hehe';
support.createUserByNameAndPwd(username, pass, function (user) {
request.post('/auth/github/test_create')
.send({name: username, pass: pass, githubName: username})
.end(function (err, res) {
res.status.should.equal(302);
res.headers.location.should.equal('/');
done(err);
});
});
});
});
});