Repository navigation
Expand file tree
/
Copy pathusers.test.js
More file actions
252 lines (226 loc) · 7.98 KB
/
Copy pathusers.test.js
File metadata and controls
252 lines (226 loc) · 7.98 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
var app = require('../../../app');
var request = require('supertest')(app);
var should = require("should");
var security = require('../../../core/utils/security');
var factory = require('../../../core/utils/factory');
var _ = require('lodash');
describe('api/users/users.test.js', function() {
var accountExist = 'lisong2010@gmail.com';
var account = '522539441@qq.com';
var registerKey = `REGISTER_CODE_${security.md5(account)}`;
var password = '654321';
var newPassword = '123456';
describe('check email does exists', function(done) {
it('should not check email successful when not input email', function(done) {
request.get(`/users/exists`)
.send()
.end(function(err, res) {
should.not.exist(err);
JSON.parse(res.text).should.containEql({status:"ERROR", message: "请您输入邮箱地址"});
done();
});
});
it('should not exists account when sign up', function(done) {
request.get(`/users/exists?email=${account}`)
.send()
.end(function(err, res) {
should.not.exist(err);
JSON.parse(res.text).should.containEql({status:"OK", exists: false});
done();
});
});
});
describe('send register code to email', function(done) {
it('should not send register code successful when not input email', function(done) {
request.post(`/users/registerCode`)
.send({})
.end(function(err, res) {
should.not.exist(err);
JSON.parse(res.text).should.containEql({status:"ERROR", message: "请您输入邮箱地址"});
done();
});
});
it('should not send register code successful when email already exists', function(done) {
request.post(`/users/registerCode`)
.send({email: accountExist})
.end(function(err, res) {
should.not.exist(err);
JSON.parse(res.text).should.containEql({status:"ERROR", message: `"${accountExist}" 已经注册过,请更换邮箱注册`});
done();
});
});
it('should send register successful', function(done) {
request.post(`/users/registerCode`)
.send({email: account})
.end(function(err, res) {
should.not.exist(err);
JSON.parse(res.text).should.containEql({status:"OK"});
done();
});
});
});
describe('check register code', function(done) {
var token = 'invalid token';
var account2 = '522539441@qq.com2';
var storageToken;
before(function(done){
var client = factory.getRedisClient("default");
client.getAsync(registerKey)
.then(function (t) {
storageToken = t;
done();
});
});
it('should not check register code successful when email already exists', function(done) {
request.get(`/users/registerCode/exists?email=${accountExist}`)
.send()
.end(function(err, res) {
should.not.exist(err);
JSON.parse(res.text).should.containEql({status:"ERROR", message: `"${accountExist}" 已经注册过,请更换邮箱注册`});
done();
});
});
it('should not check register code successful when token expired', function(done) {
request.get(`/users/registerCode/exists?email=${account2}`)
.send()
.end(function(err, res) {
should.not.exist(err);
JSON.parse(res.text).should.containEql({status:"ERROR", message: `验证码已经失效,请您重新获取`});
done();
});
});
it('should not check register code successful when token is invalid', function(done) {
request.get(`/users/registerCode/exists?email=${account}&token=${token}`)
.send()
.end(function(err, res) {
should.not.exist(err);
JSON.parse(res.text).should.containEql({status:"ERROR", message: `您输入的验证码不正确,请重新输入`});
done();
});
});
it('should check register code successful', function(done) {
request.get(`/users/registerCode/exists?email=${account}&token=${storageToken}`)
.send()
.end(function(err, res) {
should.not.exist(err);
JSON.parse(res.text).should.containEql({status:"OK"});
done();
});
});
});
describe('sign up', function(done) {
var storageToken;
before(function(done){
var client = factory.getRedisClient("default");
client.getAsync(registerKey)
.then(function (t) {
storageToken = t;
done();
});
});
it('should not sign up successful when password length invalid', function(done) {
request.post(`/users`)
.send({email:account, password: '1234', token: storageToken})
.end(function(err, res) {
should.not.exist(err);
JSON.parse(res.text).should.containEql({status:"ERROR", message: `请您输入6~20位长度的密码`});
done();
});
});
it('should sign up successful', function(done) {
request.post(`/users`)
.send({email:account, password: password, token: storageToken})
.end(function(err, res) {
should.not.exist(err);
JSON.parse(res.text).should.containEql({status:"OK"});
done();
});
});
});
describe('change password', function(done) {
var authToken;
before(function(done){
request.post('/auth/login')
.send({
account: account,
password: password
})
.end(function(err, res) {
should.not.exist(err);
var rs = JSON.parse(res.text);
rs.should.containEql({status:"OK"});
authToken = (new Buffer(`auth:${_.get(rs, 'results.tokens')}`)).toString('base64');
done();
});
});
it('should not change password successful when authToken invalid', function(done) {
request.patch(`/users/password`)
.send({oldPassword: password, newPassword: newPassword})
.end(function(err, res) {
should.not.exist(err);
res.status.should.equal(401);
done();
});
});
it('should not change password successful where password invalid', function(done) {
request.patch(`/users/password`)
.set('Authorization', `Basic ${authToken}`)
.send({oldPassword: '123321', newPassword: newPassword})
.end(function(err, res) {
should.not.exist(err);
res.status.should.equal(200);
JSON.parse(res.text).should.containEql({status:"ERROR", message: `您输入的旧密码不正确,请重新输入`});
done();
});
});
it('should not change password successful where new password invalid', function(done) {
request.patch(`/users/password`)
.set('Authorization', `Basic ${authToken}`)
.send({oldPassword: password, newPassword: '1234'})
.end(function(err, res) {
should.not.exist(err);
res.status.should.equal(200);
JSON.parse(res.text).should.containEql({status:"ERROR", message: `请您输入6~20位长度的新密码`});
done();
});
});
it('should change password successful', function(done) {
request.patch(`/users/password`)
.set('Authorization', `Basic ${authToken}`)
.send({oldPassword:password, newPassword: newPassword})
.end(function(err, res) {
should.not.exist(err);
res.status.should.equal(200);
JSON.parse(res.text).should.containEql({status:"OK"});
done();
});
});
});
describe('user modules', function(done) {
var authToken;
before(function(done){
request.post('/auth/login')
.send({
account: account,
password: newPassword
})
.end(function(err, res) {
should.not.exist(err);
var rs = JSON.parse(res.text);
rs.should.containEql({status:"OK"});
authToken = (new Buffer(`auth:${_.get(rs, 'results.tokens')}`)).toString('base64');
done();
});
});
it('should get userinfo successful', function(done) {
request.get(`/users`)
.set('Authorization', `Basic ${authToken}`)
.send()
.end(function(err, res) {
should.not.exist(err);
res.status.should.equal(200);
done();
});
});
});
});