Skip to content

Commit

Permalink
fix: can not star bug also add test (leetcode-tools#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
yihong0618 authored Aug 21, 2020
1 parent eded574 commit cd5d4ef
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
34 changes: 14 additions & 20 deletions lib/plugins/leetcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,31 +360,25 @@ plugin.getSubmission = function(submission, cb) {

plugin.starProblem = function(problem, starred, cb) {
log.debug('running leetcode.starProblem');
const opts = plugin.makeOpts();
const user = session.getUser();
const operationName = starred ? 'addQuestionToFavorite' : 'removeQuestionFromFavorite';
const opts = plugin.makeOpts(config.sys.urls.graphql);
opts.headers.Origin = config.sys.urls.base;
opts.headers.Referer = problem.link;

const user = session.getUser();
if (starred) {
opts.url = config.sys.urls.favorites;
opts.method = 'POST';
opts.json = true;
opts.body = {
favorite_id_hash: user.hash,
question_id: problem.id
};
} else {
opts.url = config.sys.urls.favorite_delete
.replace('$hash', user.hash)
.replace('$id', problem.id);
opts.method = 'DELETE';
}
opts.json = true;
opts.body = {
query: `mutation ${operationName}($favoriteIdHash: String!, $questionId: String!) {\n ${operationName}(favoriteIdHash: $favoriteIdHash, questionId: $questionId) {\n ok\n error\n favoriteIdHash\n questionId\n __typename\n }\n}\n`,
variables: {favoriteIdHash: user.hash, questionId: '' + problem.id},
operationName: operationName
};

request(opts, function(e, resp, body) {
e = plugin.checkError(e, resp, 204);
const spin = h.spin(starred? 'star': 'unstar' + 'problem');
request.post(opts, function(e, resp, body) {
spin.stop();
e = plugin.checkError(e, resp, 200);
if (e) return cb(e);

cb(null, starred);
return cb(null, starred);
});
};

Expand Down
1 change: 1 addition & 0 deletions test/mock/find-the-difference-star.json.20200821
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":{"addQuestionToFavorite":{"ok":true,"error":null,"favoriteIdHash":"","questionId":"389","__typename":"AddQuestionToFavorite"}}}
1 change: 1 addition & 0 deletions test/mock/find-the-difference-unstar.json.20200821
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":{"removeQuestionFromFavorite":{"ok":true,"error":null,"favoriteIdHash":"","questionId":"389","__typename":"RemoveQuestionFromFavorite"}}}
10 changes: 5 additions & 5 deletions test/plugins/test_leetcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,8 @@ describe('plugin:leetcode', function() {
describe('#starProblem', function() {
it('should star ok', function(done) {
nock('https://leetcode.com')
.post('/list/api/questions')
.reply(204, '');
.post('/graphql')
.replyWithFile(200, './test/mock/find-the-difference-star.json.20200821');

plugin.starProblem(PROBLEM, true, function(e, starred) {
assert.equal(e, null);
Expand All @@ -520,8 +520,8 @@ describe('plugin:leetcode', function() {

it('should unstar ok', function(done) {
nock('https://leetcode.com')
.delete('/list/api/questions/abcdef/389')
.reply(204, '');
.post('/graphql')
.replyWithFile(200, './test/mock/find-the-difference-unstar.json.20200821');

plugin.starProblem(PROBLEM, false, function(e, starred) {
assert.equal(e, null);
Expand All @@ -532,7 +532,7 @@ describe('plugin:leetcode', function() {

it('should star fail if http error', function(done) {
nock('https://leetcode.com')
.post('/list/api/questions')
.post('/graphql')
.replyWithError('unknown error!');

plugin.starProblem(PROBLEM, true, function(e, starred) {
Expand Down

0 comments on commit cd5d4ef

Please sign in to comment.