Back rate limit by redia and upgrade node-redis

This commit is contained in:
Ajay
2022-04-13 17:36:07 -04:00
parent 41c92da37e
commit 8dc87da462
14 changed files with 292 additions and 158 deletions

View File

@@ -12,21 +12,20 @@ const randKey2 = genRandom(16);
describe("redis test", function() {
before(async function() {
if (!config.redis) this.skip();
await redis.setAsync(randKey1, randValue1);
await redis.set(randKey1, randValue1);
});
it("Should get stored value", (done) => {
redis.getAsync(randKey1)
redis.get(randKey1)
.then(res => {
if (res.err) assert.fail(res.err);
assert.strictEqual(res.reply, randValue1);
assert.strictEqual(res, randValue1);
done();
});
}).catch(err => assert.fail(err));
});
it("Should not be able to get not stored value", (done) => {
redis.getAsync(randKey2)
redis.get(randKey2)
.then(res => {
if (res.reply || res.err ) assert.fail("Value should not be found");
if (res) assert.fail("Value should not be found");
done();
});
}).catch(err => assert.fail(err));
});
});

View File

@@ -6,6 +6,7 @@ import { client } from "../utils/httpClient";
import { db, privateDB } from "../../src/databases/databases";
import redis from "../../src/utils/redis";
import assert from "assert";
import { Logger } from "../../src/utils/logger";
// helpers
const getSegment = (UUID: string) => db.prepare("get", `SELECT "votes", "locked", "category" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
@@ -51,8 +52,13 @@ const postVoteCategory = (userID: string, UUID: string, category: string) => cli
}
});
const checkUserVIP = async (publicID: HashedUserID) => {
const { reply } = await redis.getAsync(tempVIPKey(publicID));
return reply;
try {
const reply = await redis.get(tempVIPKey(publicID));
return reply;
} catch (e) {
Logger.error(e as string);
return false;
}
};
describe("tempVIP test", function() {
@@ -67,7 +73,7 @@ describe("tempVIP test", function() {
await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [publicPermVIP1]);
await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [publicPermVIP2]);
// clear redis if running consecutive tests
await redis.delAsync(tempVIPKey(publicTempVIPOne));
await redis.del(tempVIPKey(publicTempVIPOne));
});
it("Should update db version when starting the application", () => {

View File

@@ -57,7 +57,7 @@ async function init() {
mocha.run((failures) => {
mockServer.close();
server.close();
redis.close(true);
redis.quit();
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
});
});