Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(redis): on connection error return it through context fail #170

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/oauth/test/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Then print data
},
request: {
//&scope=Auth1&resource=http%3A%2F%2Fissuer1.zenswarm.forkbomb.eu%3A3100%2Fcredential_issuer%2F
body: 'response_type=code&client_id=did:dyne:sandbox.genericissuer:6Cp8mPUvJmQaMxQPSnNyhb74f9Ga4WqfXCkBneFgikm5&state=xyz&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&code_challenge_method=S256&redirect_uri=https%3A%2F%2FWallet.example.org%2Fcb&authorization_details=%5B%7B%22type%22%3A+%22openid_credential%22%2C+%22credential_configuration_id%22%3A+%22Auth1%22%2C%22locations%22%3A+%5B%22http%3A%2F%2Fissuer1.zenswarm.forkbomb.eu%3A3100%2Fcredential_issuer%2F%22%5D%7D%5D',
body: 'response_type=code&client_id=did:dyne:sandbox.genericissuer:6Cp8mPUvJmQaMxQPSnNyhb74f9Ga4WqfXCkBneFgikm5&state=xyz&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&code_challenge_method=S256&redirect_uri=https%3A%2F%2FWallet.example.org%2Fcb&authorization_details=%5B%7B%22type%22%3A+%22openid_credential%22%2C+%22credential_configuration_id%22%3A+%22Auth1%22%2C%22locations%22%3A+%5B%22https%3A%2F%2Fissuer1.zenswarm.forkbomb.eu%2Fcredential_issuer%2F%22%5D%7D%5D',
headers: {
Authorization: '',
},
Expand All @@ -88,7 +88,7 @@ Then print data
grants: ['authorization_code'],
redirectUris: ['https://Wallet.example.org/cb'],
scope: ['Auth1'],
resource: "http://issuer1.zenswarm.forkbomb.eu/credential_issuer/"
resource: "https://issuer1.zenswarm.forkbomb.eu/credential_issuer/"
},
},
});
Expand Down
18 changes: 15 additions & 3 deletions pkg/redis/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export const write = p.new('connect',
async (ctx: PluginContext) => {
const redisUrl = ctx.fetchConnect()[0];
const client = redisClient.createClient({ url: redisUrl });
await client.connect();
try {
await client.connect();
} catch (e) {
return ctx.fail(new RedisError(e.message));
}
const key = ctx.fetch("key") as string;
if (typeof key !== 'string') return ctx.fail(new RedisError('key must be string'));
const data = ctx.fetch("object") as JsonableObject;
Expand All @@ -43,7 +47,11 @@ export const read = p.new('connect',
const client = redisClient.createClient({ url: redisUrl });
const key = ctx.fetch("key") as string;
if (typeof key !== 'string') return ctx.fail(new RedisError('key must be string'));
await client.connect();
try {
await client.connect();
} catch (e) {
return ctx.fail(new RedisError(e.message));
}
await client.sendCommand(["SETNX", key, "{}"]);

return ctx.pass(JSON.parse((await client.get(key)) || "{}"));
Expand All @@ -57,7 +65,11 @@ export const deleteRedis = p.new('connect',
const client = redisClient.createClient({ url: redisUrl });
const key = ctx.fetch("key") as string;
if (typeof key !== 'string') return ctx.fail(new RedisError('key must be string'));
await client.connect();
try {
await client.connect();
} catch (e) {
return ctx.fail(new RedisError(e.message));
}

return ctx.pass(await client.del(key))
},
Expand Down
50 changes: 50 additions & 0 deletions pkg/redis/test/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import test, { registerCompletionHandler } from 'ava';
import { Slangroom } from '@slangroom/core';
import { redis } from '@slangroom/redis';
import process from 'node:process';
// read the version from the package.json
import packageJson from '@slangroom/redis/package.json' with { type: 'json' };

// https://github.com/avajs/ava/blob/main/docs/08-common-pitfalls.md#timeouts-because-a-file-failed-to-exit
registerCompletionHandler(() => {
process.exit();
});

const stripAnsiCodes = (str: string) => str.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');

test('Redis write and read back', async (t) => {
const obj = {
name: 'test person',
Expand All @@ -38,3 +42,49 @@ Then I connect to 'redis' and send key 'key1' and read key from redis and output
t.deepEqual(res['result']['read1'], obj);
t.deepEqual(res['result']['read2'], {});
});

test('Redis wrong url', async (t) => {
const readRedis = `Rule unknown ignore
Given I connect to 'redis' and send key 'key' and read key from redis and output into 'read'
Given I have a 'string dictionary' named 'read'
Then print data
`;
const slangroom = new Slangroom(redis);
const fn = slangroom.execute(readRedis, {
keys: {
redis: 'redis://wrong_url:6379',
key: 'some_random_key',
},
});
const error = await t.throwsAsync(fn);
t.true(stripAnsiCodes((error as Error).message).startsWith(
`0 | Rule unknown ignore
1 | Given I connect to 'redis' and send key 'key' and read key from redis and output into 'read'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 | Given I have a 'string dictionary' named 'read'
3 | Then print data

Error colors:
- error
- suggested words
- missing words
- extra words

Slangroom @slangroom/redis@${packageJson.version} Error:`));
});

test('Redis read not set key', async (t) => {
const readRedis = `Rule unknown ignore
Given I connect to 'redis' and send key 'key' and read key from redis and output into 'read'
Given I have a 'string dictionary' named 'read'
Then print data
`;
const slangroom = new Slangroom(redis);
const res = await slangroom.execute(readRedis, {
keys: {
redis: 'redis://localhost:6379',
key: 'some_random_key',
},
});
t.deepEqual(res['result']['read'], []);
});
Loading