From 4fac116a5a58a72cdccddf373b4d0db45b5a8a8a Mon Sep 17 00:00:00 2001 From: KillWolfVlad Date: Wed, 13 Dec 2023 18:42:58 +0500 Subject: [PATCH] fix: use new api for voting again --- suggestions.json => suggestionUrls.json | 0 upvote.test.js | 69 +++++++++++++++---------- 2 files changed, 42 insertions(+), 27 deletions(-) rename suggestions.json => suggestionUrls.json (100%) diff --git a/suggestions.json b/suggestionUrls.json similarity index 100% rename from suggestions.json rename to suggestionUrls.json diff --git a/upvote.test.js b/upvote.test.js index 99ca509..4d6e64a 100644 --- a/upvote.test.js +++ b/upvote.test.js @@ -1,53 +1,68 @@ import assert from "node:assert"; import { describe, it } from "node:test"; -import suggestions from "./suggestions.json" assert { type: "json" }; +import suggestionUrls from "./suggestionUrls.json" assert { type: "json" }; describe("Upvote", () => { - for (const suggestion of suggestions) { - it(suggestion, async () => { - const feedbackId = suggestion.match(/\/suggestions\/(\d+)\//)[1]; - - const htmlResponse = await fetch( - `https://feedback.gitkraken.com/suggestions/${encodeURIComponent( - feedbackId - )}`, - { - signal: AbortSignal.timeout(60_000), - } - ); + for (const suggestionUrl of suggestionUrls) { + it(suggestionUrl, async () => { + const suggestionId = suggestionUrl.match(/\/suggestions\/(\d+)\//)[1]; - assert.strictEqual(htmlResponse.status, 200, "site must be available"); + const getSuggestionHtmlResponse = await fetch(suggestionUrl, { + signal: AbortSignal.timeout(60_000), + }); - const html = await htmlResponse.text(); + assert.strictEqual( + getSuggestionHtmlResponse.status, + 200, + `${suggestionUrl} must be available` + ); + + const suggestionHtml = await getSuggestionHtmlResponse.text(); - const csrfToken = html.match( + const csrfToken = suggestionHtml.match( // )[1]; - const formData = new FormData(); + const voteFormData = new FormData(); + + voteFormData.append("csrf_token", csrfToken); + voteFormData.append("showVotingOptions", "true"); + + const voteCookies = getSuggestionHtmlResponse.headers + .getSetCookie() + .map((setCookie) => { + const [cookie] = setCookie + .split(";") + .map((x) => x.trim()) + .filter((x) => !!x); + + return cookie; + }) + .join("; "); - formData.append("csrf_token", csrfToken); - formData.append("showVotingOptions", "true"); + const voteHeaders = { + cookie: voteCookies, + "hx-request": "true", + referer: suggestionUrl, + }; - const toggleUpvoteResponse = await fetch( + const voteResponse = await fetch( `https://feedback.gitkraken.com/s/${encodeURIComponent( - feedbackId + suggestionId )}/vote`, { method: "POST", - body: formData, - headers: { - cookie: htmlResponse.headers.getSetCookie().join(";"), - }, + body: voteFormData, + headers: voteHeaders, signal: AbortSignal.timeout(60_000), } ); assert.strictEqual( - toggleUpvoteResponse.status, + voteResponse.status, 200, - "voting must be successfully" + `voting for ${suggestionUrl} must be successfully` ); }); }