From cd5d4ef16105533fe29eef840fde14fd4958984f Mon Sep 17 00:00:00 2001 From: yihong Date: Fri, 21 Aug 2020 14:29:19 +0800 Subject: [PATCH] fix: can not star bug also add test (#49) --- lib/plugins/leetcode.js | 34 ++++++++----------- .../find-the-difference-star.json.20200821 | 1 + .../find-the-difference-unstar.json.20200821 | 1 + test/plugins/test_leetcode.js | 10 +++--- 4 files changed, 21 insertions(+), 25 deletions(-) create mode 100644 test/mock/find-the-difference-star.json.20200821 create mode 100644 test/mock/find-the-difference-unstar.json.20200821 diff --git a/lib/plugins/leetcode.js b/lib/plugins/leetcode.js index 7790bb97..c403582d 100644 --- a/lib/plugins/leetcode.js +++ b/lib/plugins/leetcode.js @@ -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); }); }; diff --git a/test/mock/find-the-difference-star.json.20200821 b/test/mock/find-the-difference-star.json.20200821 new file mode 100644 index 00000000..d07567d3 --- /dev/null +++ b/test/mock/find-the-difference-star.json.20200821 @@ -0,0 +1 @@ +{"data":{"addQuestionToFavorite":{"ok":true,"error":null,"favoriteIdHash":"","questionId":"389","__typename":"AddQuestionToFavorite"}}} \ No newline at end of file diff --git a/test/mock/find-the-difference-unstar.json.20200821 b/test/mock/find-the-difference-unstar.json.20200821 new file mode 100644 index 00000000..9d2c26a8 --- /dev/null +++ b/test/mock/find-the-difference-unstar.json.20200821 @@ -0,0 +1 @@ +{"data":{"removeQuestionFromFavorite":{"ok":true,"error":null,"favoriteIdHash":"","questionId":"389","__typename":"RemoveQuestionFromFavorite"}}} \ No newline at end of file diff --git a/test/plugins/test_leetcode.js b/test/plugins/test_leetcode.js index 7fa2363c..39a1431e 100644 --- a/test/plugins/test_leetcode.js +++ b/test/plugins/test_leetcode.js @@ -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); @@ -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); @@ -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) {