mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-17 13:08:49 +03:00
Add endpoints for rating endpoint (dislikes)
https://github.com/ajayyy/SponsorBlock/issues/1039
This commit is contained in:
49
test/cases/ratings/getRating.ts
Normal file
49
test/cases/ratings/getRating.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { db } from "../../../src/databases/databases";
|
||||
import { getHash } from "../../../src/utils/getHash";
|
||||
import assert from "assert";
|
||||
import { client } from "../../utils/httpClient";
|
||||
import { AxiosResponse } from "axios";
|
||||
import { partialDeepEquals } from "../../utils/partialDeepEquals";
|
||||
|
||||
const endpoint = "/api/ratings/rate/";
|
||||
const getRating = (hash: string, params?: unknown): Promise<AxiosResponse> => client.get(endpoint + hash, { params });
|
||||
|
||||
describe("getRating", () => {
|
||||
before(async () => {
|
||||
const insertUserNameQuery = 'INSERT INTO "ratings" ("videoID", "service", "type", "count", "hashedVideoID") VALUES (?, ?, ?, ?, ?)';
|
||||
await db.prepare("run", insertUserNameQuery, ["some-likes-and-dislikes", "YouTube", 0, 5, getHash("some-likes-and-dislikes", 1)]); //b3f0
|
||||
await db.prepare("run", insertUserNameQuery, ["some-likes-and-dislikes", "YouTube", 1, 10, getHash("some-likes-and-dislikes", 1)]);
|
||||
});
|
||||
|
||||
it("Should be able to get dislikes and likes by default", (done) => {
|
||||
getRating("b3f0")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
type: 0,
|
||||
count: 5,
|
||||
}, {
|
||||
type: 1,
|
||||
count: 10,
|
||||
}];
|
||||
assert.ok(partialDeepEquals(res.data, expected));
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to filter for only dislikes", (done) => {
|
||||
getRating("b3f0", { type: 0 })
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
type: 0,
|
||||
count: 5,
|
||||
}];
|
||||
assert.ok(partialDeepEquals(res.data, expected));
|
||||
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
});
|
||||
96
test/cases/ratings/postRating.ts
Normal file
96
test/cases/ratings/postRating.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { db } from "../../../src/databases/databases";
|
||||
import { getHash } from "../../../src/utils/getHash";
|
||||
import assert from "assert";
|
||||
import { client } from "../../utils/httpClient";
|
||||
import { AxiosResponse } from "axios";
|
||||
import { partialDeepEquals } from "../../utils/partialDeepEquals";
|
||||
|
||||
const endpoint = "/api/ratings/rate/";
|
||||
const postRating = (body: unknown): Promise<AxiosResponse> => client.post(endpoint, body);
|
||||
const queryDatabase = (videoID: string) => db.prepare("all", `SELECT * FROM "ratings" WHERE "videoID" = ?`, [videoID]);
|
||||
|
||||
describe("postRating", () => {
|
||||
before(async () => {
|
||||
const insertUserNameQuery = 'INSERT INTO "ratings" ("videoID", "service", "type", "count", "hashedVideoID") VALUES (?, ?, ?, ?, ?)';
|
||||
await db.prepare("run", insertUserNameQuery, ["multiple-rates", "YouTube", 0, 3, getHash("multiple-rates", 1)]);
|
||||
});
|
||||
|
||||
it("Should be able to vote on a video", (done) => {
|
||||
postRating({
|
||||
userID: "rating-testman",
|
||||
videoID: "normal-video",
|
||||
type: 0
|
||||
})
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
hashedVideoID: getHash("normal-video", 1),
|
||||
videoID: "normal-video",
|
||||
type: 0,
|
||||
count: 1,
|
||||
service: "YouTube"
|
||||
}];
|
||||
assert.ok(partialDeepEquals(await queryDatabase("normal-video"), expected));
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to undo a vote on a video", (done) => {
|
||||
postRating({
|
||||
userID: "rating-testman",
|
||||
videoID: "normal-video",
|
||||
type: 0,
|
||||
enabled: false
|
||||
})
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
type: 0,
|
||||
count: 0
|
||||
}];
|
||||
assert.ok(partialDeepEquals(await queryDatabase("normal-video"), expected));
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to vote after someone else on a video", (done) => {
|
||||
postRating({
|
||||
userID: "rating-testman",
|
||||
videoID: "multiple-rates",
|
||||
type: 0
|
||||
})
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
type: 0,
|
||||
count: 4
|
||||
}];
|
||||
assert.ok(partialDeepEquals(await queryDatabase("multiple-rates"), expected));
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to vote a different type than existing votes on a video", (done) => {
|
||||
postRating({
|
||||
userID: "rating-testman",
|
||||
videoID: "multiple-rates",
|
||||
type: 1
|
||||
})
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
type: 0,
|
||||
count: 4
|
||||
}, {
|
||||
type: 1,
|
||||
count: 1
|
||||
}];
|
||||
assert.ok(partialDeepEquals(await queryDatabase("multiple-rates"), expected));
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
});
|
||||
24
test/test.ts
24
test/test.ts
@@ -32,19 +32,21 @@ async function init() {
|
||||
// Instantiate a Mocha instance.
|
||||
const mocha = new Mocha();
|
||||
|
||||
const testDir = "./test/cases";
|
||||
const testDirs = ["./test/cases", "./test/cases/ratings"];
|
||||
|
||||
// Add each .ts file to the mocha instance
|
||||
fs.readdirSync(testDir)
|
||||
.filter((file) =>
|
||||
// Only keep the .ts files
|
||||
file.substr(-3) === ".ts"
|
||||
)
|
||||
.forEach(function(file) {
|
||||
mocha.addFile(
|
||||
path.join(testDir, file)
|
||||
);
|
||||
});
|
||||
testDirs.forEach(testDir => {
|
||||
fs.readdirSync(testDir)
|
||||
.filter((file) =>
|
||||
// Only keep the .ts files
|
||||
file.substr(-3) === ".ts"
|
||||
)
|
||||
.forEach(function(file) {
|
||||
mocha.addFile(
|
||||
path.join(testDir, file)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
const mockServer = createMockServer(() => {
|
||||
Logger.info("Started mock HTTP Server");
|
||||
|
||||
Reference in New Issue
Block a user