Skip to content

Commit

Permalink
fix: add test for rule download fail graceful hndling
Browse files Browse the repository at this point in the history
  • Loading branch information
aarlaud committed Nov 11, 2024
1 parent 2c53470 commit 34cd4b5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/common/filter/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export const retrieveFilters = async (locations: Map<string, string>) => {
method: 'GET',
};
const filter = await makeSingleRawRequestToDownstream(req);
if (filter.statusCode && filter.statusCode > 299) {
throw new Error(
`Error downloading filter ${key}. Url ${location} returned ${filter.statusCode}`,
);
}
retrievedFiltersMap.set(key, filter.body);
} else {
retrievedFiltersMap.set(
Expand Down
74 changes: 74 additions & 0 deletions test/unit/filter-loading.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { LOADEDFILTERSET } from '../../lib/common/types/filter';
const nock = require('nock');
import { retrieveAndLoadFilters } from '../../lib/client/utils/filterLoading';
import { ClientOpts } from '../../lib/common/types/options';
import {
getFilterConfig,
setFilterConfig,
} from '../../lib/client/config/filters';

const scmRulesToTest = [
'azure-repos',
'bitbucket-server',
'bitbucket-server-bearer-auth',
'github',
'github-enterprise',
'gitlab',
];

const rulesetSourceInvalidHostname = 'http://invalid.broker-rules.snyk.io';

describe('filter Rules Loading', () => {
beforeAll(() => {
nock(`${rulesetSourceInvalidHostname}`)
.persist()
.get(/./)
.reply(() => {
return [404];
});
});
beforeEach(() => {
jest.resetModules();
process.env.ACCEPT = 'accept.json';
});

afterEach(() => {
delete process.env.ACCEPT;
});

test.each(scmRulesToTest)(
'Handle gracefully failure to download ruleset - Testing %s',
async (folder) => {
const loadedFilters: Map<string, LOADEDFILTERSET> = new Map();
const dummyFilter: LOADEDFILTERSET = {
private: () => {
return false;
},
public: () => {
return false;
},
};
loadedFilters.set(folder, dummyFilter);
setFilterConfig({ loadedFilters });
expect(getFilterConfig()).toStrictEqual({ loadedFilters });
const filterRulePath = {};
filterRulePath[folder] = `${rulesetSourceInvalidHostname}/${folder}.json`;

const cfg: ClientOpts = {
port: 0,
config: {
brokerType: 'client',
supportedBrokerTypes: [`${folder}`],
filterRulesPaths: filterRulePath,
universalBrokerEnabled: true,
},
filters: new Map(),
};
await expect(retrieveAndLoadFilters(cfg)).rejects.toThrowError(
`Error downloading filter ${folder}. Url http://invalid.broker-rules.snyk.io/${folder}.json returned 404`,
);
// Loaded Filters remain unchanged
expect(getFilterConfig()).toStrictEqual({ loadedFilters });
},
);
});

0 comments on commit 34cd4b5

Please sign in to comment.