add etag tests

- add shadowban self test
- add init and -it to docker runs
This commit is contained in:
Michael C 2023-02-20 15:56:08 -05:00
parent 81b0c27180
commit c586c9a7e7
No known key found for this signature in database
GPG key ID: FFB04FB3B878B7B4
3 changed files with 64 additions and 2 deletions

View file

@ -9,8 +9,8 @@
"cover:report": "nyc report",
"dev": "nodemon",
"dev:bash": "nodemon -x 'npm test ; npm start'",
"postgres:docker": "docker run --rm -p 5432:5432 -e POSTGRES_USER=ci_db_user -e POSTGRES_PASSWORD=ci_db_pass postgres:14-alpine",
"redis:docker": "docker run --rm -p 6379:6379 redis:7-alpine --save '' --appendonly no",
"postgres:docker": "docker run --init -it --rm -p 5432:5432 -e POSTGRES_USER=ci_db_user -e POSTGRES_PASSWORD=ci_db_pass postgres:14-alpine",
"redis:docker": "docker run --init -it --rm -p 6379:6379 redis:7-alpine --save '' --appendonly no",
"start": "ts-node src/index.ts",
"tsc": "tsc -p tsconfig.json",
"lint": "eslint src test",

41
test/cases/eTag.ts Normal file
View file

@ -0,0 +1,41 @@
import assert from "assert";
import { client } from "../utils/httpClient";
import redis from "../../src/utils/redis";
import crypto from "crypto";
const genRandom = (bytes=8) => crypto.pseudoRandomBytes(bytes).toString("hex");
const validateEtag = (expected: string, actual: string): boolean => {
const [actualHashType, actualHashKey, actualService] = actual.split(";");
const [expectedHashType, expectedHashKey, expectedService] = expected.split(";");
return (actualHashType === expectedHashType) && (actualHashKey === expectedHashKey) && (actualService === expectedService);
};
describe("eTag", () => {
const endpoint = "/etag";
it("Should reject weak etag", (done) => {
const etagKey = `W/test-etag-${genRandom()}`;
client.get(endpoint, { headers: { "If-None-Match": etagKey } })
.then(res => {
assert.strictEqual(res.status, 404);
done();
})
.catch(err => done(err));
});
});
describe("304 etag validation", () => {
const endpoint = "/api/skipSegments";
for (const hashType of ["skipSegments", "skipSegmentsHash", "videoLabel", "videoLabelHash"]) {
it(`${hashType} etag should return 304`, (done) => {
const etagKey = `${hashType};${genRandom};YouTube;${Date.now()}`;
redis.setEx(etagKey, 8400, "test").then(() =>
client.get(endpoint, { headers: { "If-None-Match": etagKey } }).then(res => {
assert.strictEqual(res.status, 304);
const etag = res.headers?.etag ?? "";
assert.ok(validateEtag(etagKey, etag));
done();
}).catch(err => done(err))
);
});
}
});

View file

@ -410,6 +410,27 @@ describe("shadowBanUser", () => {
.catch(err => done(err));
});
it("Should be possible to ban self", (done) => {
const userID = VIPuserID;
const hashUserID = getHash(userID);
client({
method: "POST",
url: endpoint,
params: {
enabled: true,
userID: hashUserID,
categories: `["sponsor"]`,
unHideOldSubmissions: true,
adminUserID: userID,
}
})
.then(res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
});
it("Should be able to ban user by userID and other users who used that IP and hide specific category", (done) => {
const hashedIP = "shadowBannedIP8";
const userID = "shadowBanned8";