Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
fix: use new api for voting again
Browse files Browse the repository at this point in the history
  • Loading branch information
KillWolfVlad committed Dec 13, 2023
1 parent 1575276 commit 4fac116
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
File renamed without changes.
69 changes: 42 additions & 27 deletions upvote.test.js
Original file line number Diff line number Diff line change
@@ -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(
/<input type="hidden" name="csrf_token" value="(.*)">/
)[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`
);
});
}
Expand Down

0 comments on commit 4fac116

Please sign in to comment.