Skip to content

Commit 2b96e04

Browse files
committed
Refactor login logic.
Signed-off-by: Eric Wang <[email protected]>
1 parent 1d04736 commit 2b96e04

File tree

4 files changed

+23
-28
lines changed

4 files changed

+23
-28
lines changed

Diff for: lib/plugins/cache.js

+1-13
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,7 @@ plugin.login = function(user, cb) {
7171
plugin.next.login(user, function(e, user) {
7272
if (e) return cb(e);
7373
session.saveUser(user);
74-
75-
plugin.next.getFavorites(function(e, favorites) {
76-
if (e) return cb(e);
77-
78-
// TODO: pick other useful values from favorites
79-
var favorite = _.find(favorites.favorites.private_favorites, function(f) {
80-
return f.name === 'Favorite';
81-
});
82-
user.hash = favorite.id_hash;
83-
84-
session.saveUser(user);
85-
return cb(null, user);
86-
});
74+
return cb(null, user);
8775
});
8876
};
8977

Diff for: lib/plugins/leetcode.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,19 @@ plugin.login = function(user, cb) {
360360
user.sessionId = h.getSetCookieValue(resp, 'LEETCODE_SESSION');
361361
user.name = h.getSetCookieValue(resp, 'messages')
362362
.match('Successfully signed in as ([^.]*)')[1];
363+
session.saveUser(user);
363364

364-
return cb(null, user);
365+
plugin.getFavorites(function(e, favorites) {
366+
if (e) return cb(e);
367+
368+
// TODO: pick other useful values from favorites
369+
var favorite = _.find(favorites.favorites.private_favorites, function(f) {
370+
return f.name === 'Favorite';
371+
});
372+
user.hash = favorite.id_hash;
373+
374+
return cb(null, user);
375+
});
365376
});
366377
});
367378
};

Diff for: test/plugins/test_cache.js

+5-14
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ describe('plugin:cache', function() {
2121
{id: 1, name: 'name1', slug: 'slug1', starred: true, category: 'algorithms'}
2222
];
2323
var PROBLEM = {id: 0, slug: 'slug0', category: 'algorithms'};
24-
var FAVORITES = {
25-
favorites: {
26-
private_favorites: [{id_hash: 'abcdef', name: 'Favorite'}]
27-
}
28-
};
2924

3025
var NEXT = {};
3126

@@ -179,8 +174,7 @@ describe('plugin:cache', function() {
179174

180175
describe('#user', function() {
181176
var USER = {name: 'test-user', pass: 'password'};
182-
var USER_AFTER = {name: 'test-user', pass: 'password', hash: 'abcdef'};
183-
var USER_AFTER_SAFE = {name: 'test-user', hash: 'abcdef'};
177+
var USER_SAFE = {name: 'test-user'};
184178

185179
it('should login ok', function(done) {
186180
config.AUTO_LOGIN = true;
@@ -192,16 +186,13 @@ describe('plugin:cache', function() {
192186
NEXT.login = function(user, cb) {
193187
return cb(null, user);
194188
};
195-
NEXT.getFavorites = function(cb) {
196-
return cb(null, FAVORITES);
197-
};
198189

199190
plugin.login(USER, function(e, user) {
200191
assert.equal(e, null);
201-
assert.deepEqual(user, USER_AFTER);
192+
assert.deepEqual(user, USER);
202193

203194
// after login
204-
assert.deepEqual(session.getUser(), USER_AFTER);
195+
assert.deepEqual(session.getUser(), USER);
205196
assert.equal(session.isLogin(), true);
206197
done();
207198
});
@@ -217,8 +208,8 @@ describe('plugin:cache', function() {
217208

218209
plugin.login(USER, function(e, user) {
219210
assert.equal(e, null);
220-
assert.deepEqual(user, USER_AFTER);
221-
assert.deepEqual(session.getUser(), USER_AFTER_SAFE);
211+
assert.deepEqual(user, USER);
212+
assert.deepEqual(session.getUser(), USER_SAFE);
222213
assert.equal(session.isLogin(), true);
223214
done();
224215
});

Diff for: test/plugins/test_leetcode.js

+5
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,18 @@ describe('plugin:leetcode', function() {
5757
"messages='Successfully signed in as Eric.'; Max-Age=31449600; Path=/; secure"
5858
]});
5959

60+
nock('https://leetcode.com')
61+
.get('/list/api/questions')
62+
.reply(200, JSON.stringify({favorites: {private_favorites: [{id_hash: 'abcdef', name: 'Favorite'}]}}));
63+
6064
plugin.login({}, function(e, user) {
6165
assert.equal(e, null);
6266

6367
assert.equal(user.loginCSRF, 'LOGIN_CSRF_TOKEN');
6468
assert.equal(user.sessionCSRF, 'SESSION_CSRF_TOKEN');
6569
assert.equal(user.sessionId, 'SESSION_ID');
6670
assert.equal(user.name, 'Eric');
71+
assert.equal(user.hash, 'abcdef');
6772
done();
6873
});
6974
});

0 commit comments

Comments
 (0)