Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: can not star bug also add test #49

Merged
merged 1 commit into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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