Skip to content

Commit

Permalink
Add unit test for validating triggers (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
Siegrift authored Oct 1, 2023
1 parent f5c9257 commit c767c77
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
44 changes: 41 additions & 3 deletions packages/data-pusher/src/validation/schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { ZodError } from 'zod';
import { configSchema, signedApisSchema } from './schema';
import dotenv from 'dotenv';
import { Config, configSchema, signedApisSchema } from './schema';
import { interpolateSecrets } from './utils';
import { config } from '../../test/fixtures';

it('validates example config', async () => {
const config = JSON.parse(readFileSync(join(__dirname, '../../config/pusher.example.json'), 'utf8'));
const exampleConfig = JSON.parse(readFileSync(join(__dirname, '../../config/pusher.example.json'), 'utf8'));

await expect(configSchema.parseAsync(config)).resolves.toEqual(expect.any(Object));
// The mnemonic is not interpolated (and thus invalid).
await expect(configSchema.parseAsync(exampleConfig)).rejects.toEqual(
new ZodError([
{
code: 'custom',
message: 'Invalid mnemonic',
path: ['airnodeWalletMnemonic'],
},
])
);

const exampleSecrets = dotenv.parse(readFileSync(join(__dirname, '../../config/secrets.example.env'), 'utf8'));
await expect(configSchema.parseAsync(interpolateSecrets(exampleConfig, exampleSecrets))).resolves.toEqual(
expect.any(Object)
);
});

it('ensures signed API names are unique', () => {
Expand All @@ -32,3 +49,24 @@ it('ensures signed API names are unique', () => {
},
]);
});

it('validates trigger references', async () => {
const invalidConfig: Config = {
...config,
ois: [
// By removing the pre-processing the triggers will end up with different operation effects.
{ ...config.ois[0]!, endpoints: [{ ...config.ois[0]!.endpoints[0]!, preProcessingSpecifications: undefined }] },
],
};

await expect(configSchema.parseAsync(invalidConfig)).rejects.toEqual(
new ZodError([
{
code: 'custom',
message:
'If beaconIds contains more than 1 beacon, the endpoint utilized by each beacons must have same operation effect',
path: ['triggers', 'signedApiUpdates', 0],
},
])
);
});
2 changes: 1 addition & 1 deletion packages/data-pusher/src/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export const apisCredentialsSchema = z.array(config.apiCredentialsSchema);

export const configSchema = z
.object({
airnodeWalletMnemonic: z.string(),
airnodeWalletMnemonic: z.string().refine((mnemonic) => ethers.utils.isValidMnemonic(mnemonic), 'Invalid mnemonic'),
beaconSets: z.any(),
chains: z.any(),
gateways: z.any(),
Expand Down

0 comments on commit c767c77

Please sign in to comment.