2021-12-19 08:03:50 +01:00
|
|
|
import { config } from "../../src/config";
|
|
|
|
import redis from "../../src/utils/redis";
|
|
|
|
import crypto from "crypto";
|
|
|
|
import assert from "assert";
|
|
|
|
|
2021-12-20 01:37:22 +01:00
|
|
|
const genRandom = (bytes=8) => crypto.pseudoRandomBytes(bytes).toString("hex");
|
|
|
|
|
|
|
|
const randKey1 = genRandom();
|
|
|
|
const randValue1 = genRandom();
|
|
|
|
const randKey2 = genRandom(16);
|
2022-09-02 07:22:04 +02:00
|
|
|
const randKey3 = genRandom();
|
|
|
|
const randValue3 = genRandom();
|
2021-12-19 08:03:50 +01:00
|
|
|
|
|
|
|
describe("redis test", function() {
|
|
|
|
before(async function() {
|
2022-05-04 04:08:44 +02:00
|
|
|
if (!config.redis?.enabled) this.skip();
|
2022-04-13 23:36:07 +02:00
|
|
|
await redis.set(randKey1, randValue1);
|
2021-12-19 08:03:50 +01:00
|
|
|
});
|
|
|
|
it("Should get stored value", (done) => {
|
2022-04-13 23:36:07 +02:00
|
|
|
redis.get(randKey1)
|
2021-12-19 08:03:50 +01:00
|
|
|
.then(res => {
|
2022-04-13 23:36:07 +02:00
|
|
|
assert.strictEqual(res, randValue1);
|
2021-12-19 08:03:50 +01:00
|
|
|
done();
|
2022-09-02 07:22:04 +02:00
|
|
|
}).catch(err => done(err));
|
2021-12-19 08:03:50 +01:00
|
|
|
});
|
2021-12-20 01:37:22 +01:00
|
|
|
it("Should not be able to get not stored value", (done) => {
|
2022-04-13 23:36:07 +02:00
|
|
|
redis.get(randKey2)
|
2021-12-20 01:37:22 +01:00
|
|
|
.then(res => {
|
2022-09-02 07:22:04 +02:00
|
|
|
if (res) done("Value should not be found");
|
2021-12-20 01:37:22 +01:00
|
|
|
done();
|
2022-09-02 07:22:04 +02:00
|
|
|
}).catch(err => done(err));
|
|
|
|
});
|
|
|
|
it("Should be able to delete stored value", (done) => {
|
|
|
|
redis.del(randKey1)
|
|
|
|
.then(() => {
|
|
|
|
redis.get(randKey1)
|
|
|
|
.then(res => {
|
|
|
|
assert.strictEqual(res, null);
|
|
|
|
done();
|
|
|
|
}).catch(err => done(err));
|
|
|
|
}).catch(err => done(err));
|
|
|
|
});
|
|
|
|
it("Should be able to set expiring value", (done) => {
|
|
|
|
redis.setEx(randKey3, 8400, randValue3)
|
|
|
|
.then(() => {
|
|
|
|
redis.get(randKey3)
|
|
|
|
.then(res => {
|
|
|
|
assert.strictEqual(res, randValue3);
|
|
|
|
done();
|
|
|
|
}).catch(err => done(err));
|
|
|
|
}).catch(err => done(err));
|
|
|
|
});
|
|
|
|
it("Should continue when undefined value is fetched", (done) => {
|
|
|
|
const undefkey = `undefined.${genRandom()}`;
|
|
|
|
redis.get(undefkey)
|
|
|
|
.then(result => {
|
|
|
|
assert.ok(!result); // result should be falsy
|
|
|
|
done();
|
|
|
|
});
|
2021-12-21 05:04:41 +01:00
|
|
|
});
|
2021-12-19 08:03:50 +01:00
|
|
|
});
|