Skip to content

Commit

Permalink
fix: update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
aarlaud committed Nov 7, 2024
1 parent 54a0956 commit 8784de7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
12 changes: 8 additions & 4 deletions lib/common/filter/filter-rules-loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ function injectRulesAtRuntime(
config.ACCEPT_GIT;
if (ACCEPT_CODE) {
logger.info({ accept: ACCEPT_CODE }, 'Injecting Accept rules for Code/Git');

const templateGET = nestedCopy(
filters.private.filter(
(entry) =>
Expand Down Expand Up @@ -242,6 +241,7 @@ function injectRulesAtRuntime(
}
}
}

const ACCEPT_APPRISK = process.env.ACCEPT_APPRISK || config.ACCEPT_APPRISK;
if (ACCEPT_APPRISK) {
logger.debug(
Expand Down Expand Up @@ -337,17 +337,21 @@ export default async (
}
retrievedFilters = await retrieveFilters(rulesUrisMap);
}

let filters;
if (config.universalBrokerEnabled) {
filters = new Map();
const supportedBrokerTypes = config.supportedBrokerTypes;
supportedBrokerTypes.forEach((type) => {
filters[type] = yaml.safeLoad(retrievedFilters[type]);
if (!retrievedFilters.get(type)) {
throw new Error(`Missing filter for ${type}.`);
}
filters[type] = yaml.safeLoad(retrievedFilters.get(type));
filters[type] = injectRulesAtRuntime(filters[type], config, type);
});
} else {
if (acceptFilename && retrievedFilters) {
if (!acceptFilename) {
return filters;
} else if (acceptFilename && retrievedFilters) {
filters = yaml.safeLoad(retrievedFilters.get('current'));
} else {
const acceptLocation = path.resolve(
Expand Down
12 changes: 8 additions & 4 deletions lib/common/filter/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'node:path';
import fs from 'fs';
import { makeSingleRawRequestToDownstream } from '../http/request';
import { PostFilterPreparedRequest } from '../relay/prepareRequest';
import version from '../utils/version';
Expand Down Expand Up @@ -49,10 +50,13 @@ export const retrieveFilters = async (locations: Map<string, string>) => {
} else {
retrievedFiltersMap.set(
key,
`${path.resolve(
findProjectRoot(__dirname) ?? process.cwd(),
location, // this should handle the override for custom filters
)}`,
fs.readFileSync(
`${path.resolve(
findProjectRoot(__dirname) ?? process.cwd(),
location, // this should handle the override for custom filters
)}`,
'utf-8',
),
);
}
}
Expand Down
53 changes: 27 additions & 26 deletions test/unit/runtime-rules-hotloading.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ describe('filter Rules Loading', () => {

test.each(scmRulesToTest)(
'Loads normal accept file - Testing %s',
(folder) => {
const loadedRules = loadFilterRules(
async (folder) => {
const loadedRules = await loadFilterRules(
{
brokerType: 'client',
supportedBrokerTypes: [],
Expand All @@ -51,9 +51,9 @@ describe('filter Rules Loading', () => {

test.each(scmRulesToTest)(
'Skip injection if no or invalid IAC extensions - Testing %s',
(folder) => {
async (folder) => {
process.env.ACCEPT_IAC = 'rf';
const loadedRules = loadFilterRules(
const loadedRules = await loadFilterRules(
{
brokerType: 'client',
supportedBrokerTypes: [],
Expand All @@ -70,9 +70,9 @@ describe('filter Rules Loading', () => {

test.each(scmRulesToTest)(
'Injection of valid IAC extensions - Testing %s',
(folder) => {
async (folder) => {
process.env.ACCEPT_IAC = 'tf,yaml, json,yml,tpl';
const loadedRules = loadFilterRules(
const loadedRules = await loadFilterRules(
{
brokerType: 'client',
supportedBrokerTypes: [],
Expand All @@ -89,9 +89,9 @@ describe('filter Rules Loading', () => {

test.each(scmRulesToTest)(
'Injection of valid IAC extensions - Testing %s',
(folder) => {
async (folder) => {
process.env.ACCEPT_IAC = 'tf,json';
const loadedRules = loadFilterRules(
const loadedRules = await loadFilterRules(
{
brokerType: 'client',
supportedBrokerTypes: [],
Expand All @@ -108,9 +108,9 @@ describe('filter Rules Loading', () => {

test.each(scmRulesToTest)(
'Injection of valid CODE rules - Testing %s',
(folder) => {
async (folder) => {
process.env.ACCEPT_CODE = 'true';
const loadedRules = loadFilterRules(
const loadedRules = await loadFilterRules(
{
brokerType: 'client',
supportedBrokerTypes: [],
Expand All @@ -127,9 +127,9 @@ describe('filter Rules Loading', () => {

test.each(scmRulesToTest)(
'Injection of valid Git rules - Testing %s',
(folder) => {
async (folder) => {
process.env.ACCEPT_GIT = 'true';
const loadedRules = loadFilterRules(
const loadedRules = await loadFilterRules(
{
brokerType: 'client',
supportedBrokerTypes: [],
Expand All @@ -146,10 +146,10 @@ describe('filter Rules Loading', () => {

test.each(scmRulesToTest)(
'Injection of valid Git rules without snippets - Testing %s',
(folder) => {
async (folder) => {
process.env.ACCEPT_GIT = 'true';
process.env.DISABLE_SNIPPETS = 'true';
const loadedRules = loadFilterRules(
const loadedRules = await loadFilterRules(
{
brokerType: 'client',
supportedBrokerTypes: scmRulesToTest,
Expand Down Expand Up @@ -185,10 +185,10 @@ describe('filter Rules Loading', () => {

test.each(scmRulesToTest)(
'Injection of valid CODE GH rules - Testing %s',
(folder) => {
async (folder) => {
process.env.ACCEPT_LARGE_MANIFESTS = 'true';

const loadedRules = loadFilterRules(
const loadedRules = await loadFilterRules(
{
brokerType: 'client',
supportedBrokerTypes: [],
Expand All @@ -205,10 +205,10 @@ describe('filter Rules Loading', () => {

test.each(scmRulesToTest)(
'Injection of valid CODE rules and IAC extensions (yaml only) - Testing %s',
(folder) => {
async (folder) => {
process.env.ACCEPT_CODE = 'true';
process.env.ACCEPT_IAC = 'yaml';
const loadedRules = loadFilterRules(
const loadedRules = await loadFilterRules(
{
brokerType: 'client',
supportedBrokerTypes: [],
Expand All @@ -226,7 +226,7 @@ describe('filter Rules Loading', () => {

test.each(scmRulesToTest)(
'Injection of valid AppRisk rules - Testing %s',
(folder) => {
async (folder) => {
process.env.ACCEPT_APPRISK = 'true';
process.env[`BROKER_DOWNSTREAM_TYPE_${folder}`] = 'true';
const config: CONFIGURATION = {
Expand All @@ -236,7 +236,7 @@ describe('filter Rules Loading', () => {
filterRulesPaths: {},
};
config[camelcase(`BROKER_DOWNSTREAM_TYPE_${folder}`)] = 'true';
const loadedRules = loadFilterRules(
const loadedRules = await loadFilterRules(
config,
path.join(__dirname, '../..', `client-templates/${folder}`),
);
Expand All @@ -249,7 +249,7 @@ describe('filter Rules Loading', () => {

test.each(scmRulesToTest)(
'Injection of valid ACCEPT_CUSTOM_PR_TEMPLATES rules - Testing %s',
(folder) => {
async (folder) => {
process.env.ACCEPT_CUSTOM_PR_TEMPLATES = 'true';
process.env[`BROKER_DOWNSTREAM_TYPE_${folder}`] = 'true';
const config: CONFIGURATION = {
Expand All @@ -259,7 +259,7 @@ describe('filter Rules Loading', () => {
filterRulesPaths: {},
};
config[camelcase(`BROKER_DOWNSTREAM_TYPE_${folder}`)] = 'true';
const loadedRules = loadFilterRules(
const loadedRules = await loadFilterRules(
config,
path.join(__dirname, '../..', `client-templates/${folder}`),
);
Expand All @@ -272,18 +272,19 @@ describe('filter Rules Loading', () => {

test.each(scmUniversalRulesToTest)(
'Injection of valid Git rules - Universal Broker - Testing %s',
(folder) => {
async (folder) => {
process.env.ACCEPT_GIT = 'true';
const filterRulesPath = {};
for (const type of scmUniversalRulesToTest) {
filterRulesPath[`${type}`] = `defaultFilters/${type}.json`;
}
const loadedRules = loadFilterRules({
const loadedRules = await loadFilterRules({
brokerType: 'client',
supportedBrokerTypes: scmUniversalRulesToTest,
filterRulesPaths: filterRulesPath,
universalBrokerEnabled: true,
});

expect(
loadedRules[folder].private.filter((x) =>
x.path.includes('*/git-upload-pack'),
Expand Down Expand Up @@ -315,7 +316,7 @@ describe('filter Rules Loading', () => {

test.each(scmRulesToTest)(
'Injection of valid Git rules with AppRisk enabled - Testing %s',
(folder) => {
async (folder) => {
process.env.ACCEPT_GIT = 'true';
process.env.ACCEPT_APPRISK = 'true';
const config: CONFIGURATION = {
Expand All @@ -325,7 +326,7 @@ describe('filter Rules Loading', () => {
filterRulesPaths: {},
};
config[camelcase(`BROKER_DOWNSTREAM_TYPE_${folder}`)] = 'true';
const loadedRules = loadFilterRules(
const loadedRules = await loadFilterRules(
config,
path.join(__dirname, '../..', `client-templates/${folder}`),
);
Expand Down

0 comments on commit 8784de7

Please sign in to comment.