Skip to content

Commit 72e0886

Browse files
committed
cacheGet and cacheSet must not accept undefined parameters
wallet.id can be undefined if Wallet.generate_entity() is called without saving the result and then `asyncEvmAddress` property is accessed. This only occurred in tests and those instances have been resolved.
1 parent 71f3eb1 commit 72e0886

4 files changed

Lines changed: 79 additions & 0 deletions

File tree

src/__tests__/contracts.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export const ContractEntityTests = async function() {
5050
async () => {
5151
await Project.loadFixturesFromFile('/app/src/__tests__/meta.json');
5252
const owner = await Wallet.generate_entity();
53+
await owner.save();
5354
const other = await Wallet.generate_entity();
55+
await other.save();
5456

5557
const local = await utils.setupLocalTestNetContracts({ ShipToken: LATEST_SHIPTOKEN, LOAD: LATEST_LOAD, NOTARY: LATEST_NOTARY }, [owner]);
5658
const network: Network = await Network.getLocalTestNet();

src/__tests__/eventsubscriptions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ export const EventSubscriptionEntityTests = async function() {
8282
}
8383
await Project.loadFixturesFromFile('/app/src/__tests__/meta.json');
8484
const owner = await Wallet.generate_entity();
85+
await owner.save();
8586
const other = await Wallet.generate_entity();
87+
await other.save();
8688

8789
const local = await utils.setupLocalTestNetContracts({ShipToken: LATEST_SHIPTOKEN, LOAD: LATEST_LOAD}, [owner]);
8890
const network: Network = await Network.getLocalTestNet();

src/__tests__/redis.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,69 @@ export const RedisTests = async function() {
7575
expect(returnedValue).toEqual(value2);
7676
});
7777

78+
it(`cacheSet should throw on undefined key`, async() => {
79+
let caughtError;
80+
81+
try {
82+
await cacheSet(undefined, field, value);
83+
fail(`Should have thrown`);
84+
} catch (err) {
85+
caughtError = err;
86+
}
87+
88+
expect(caughtError.toString()).toContain(`Invalid parameters for cacheSet: (undefined,${field},${value})`);
89+
});
90+
91+
it(`cacheSet should throw on undefined field`, async() => {
92+
let caughtError;
93+
94+
try {
95+
await cacheSet(key, undefined, value);
96+
fail(`Should have thrown`);
97+
} catch (err) {
98+
caughtError = err;
99+
}
100+
101+
expect(caughtError.toString()).toContain(`Invalid parameters for cacheSet: (${key},undefined,${value})`);
102+
});
103+
104+
it(`cacheSet should throw on undefined value`, async() => {
105+
let caughtError;
106+
107+
try {
108+
await cacheSet(key, field, undefined);
109+
fail(`Should have thrown`);
110+
} catch (err) {
111+
caughtError = err;
112+
}
113+
114+
expect(caughtError.toString()).toContain(`Invalid parameters for cacheSet: (${key},${field},undefined)`);
115+
});
116+
117+
it(`cacheGet should throw on undefined key`, async() => {
118+
let caughtError;
119+
120+
try {
121+
await cacheGet(undefined, field);
122+
fail(`Should have thrown`);
123+
} catch (err) {
124+
caughtError = err;
125+
}
126+
127+
expect(caughtError.toString()).toContain(`Invalid parameters for cacheGet: (undefined,${field})`);
128+
});
129+
130+
it(`cacheGet should throw on undefined field`, async() => {
131+
let caughtError;
132+
133+
try {
134+
await cacheGet(key, undefined);
135+
fail(`Should have thrown`);
136+
} catch (err) {
137+
caughtError = err;
138+
}
139+
140+
expect(caughtError.toString()).toContain(`Invalid parameters for cacheGet: (${key},undefined)`);
141+
});
142+
78143
};

src/redis.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ export function getRedisClient() {
4747
}
4848

4949
export async function cacheGet(key: string, field: string): Promise<any> {
50+
if (!key || !field) {
51+
throw new Error(`Invalid parameters for cacheGet: (${key},${field})`);
52+
}
53+
5054
const client = getRedisClient();
55+
5156
try {
5257
return await client.asyncHashGet(key, field);
5358
} catch (err) {
@@ -57,7 +62,12 @@ export async function cacheGet(key: string, field: string): Promise<any> {
5762
}
5863

5964
export async function cacheSet(key: string, field: string, value: any): Promise<void> {
65+
if (!key || !field || !value) {
66+
throw new Error(`Invalid parameters for cacheSet: (${key},${field},${value})`);
67+
}
68+
6069
const client = getRedisClient();
70+
6171
try {
6272
await client.asyncHashSet(key, field, value);
6373
} catch (err) {

0 commit comments

Comments
 (0)