Skip to content
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
"jsonwebtoken": "^9.0.2",
"moment": "^2.30.1",
"mongodb": "^6.5.0",
"mongoose": "^8.3.0",
"mongoose": "^8.3.2",
"pino": "^8.20.0",
"pino-pretty": "^11.0.0",
"swagger-ui-express": "^5.0.0",
"switcher-client": "^3.2.1",
"switcher-client": "^4.0.2",
"validator": "^13.11.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/client/relay/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { StrategiesToRelayDataType, RelayMethods } from '../../models/config.js'
import { checkHttpsAgent } from '../../external/switcher-api-facade.js';

const agent = async (url) => {
const rejectUnauthorized = !(await checkHttpsAgent(url));
return new https.Agent({ rejectUnauthorized });
const response = await checkHttpsAgent(url);
return new https.Agent({ rejectUnauthorized: !(response?.result) });
};

export async function resolveNotification(relay, entry, environment) {
Expand Down
34 changes: 20 additions & 14 deletions src/external/switcher-api-facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ export const SwitcherKeys = Object.freeze({
ACCOUNT_OUT_NOTIFY: 'ACCOUNT_OUT_NOTIFY',
SLACK_INTEGRATION: 'SLACK_INTEGRATION',
RATE_LIMIT: 'RATE_LIMIT',
HTTPS_AGENT: 'HTTPS_AGENT'
HTTPS_AGENT: 'HTTPS_AGENT',
SWITCHER_AC_METADATA: 'SWITCHER_AC_METADATA'
});

function switcherFlagResult(flag, message) {
if (!flag) {
function switcherFlagResult(response, message) {
if (!response.result) {
throw new FeatureUnavailableError(message);
}
}

async function checkFeature(feature, params) {
const switcher = Switcher.factory();
return switcher.isItOn(feature, params, true);
return switcher.detail().isItOn(feature, params);
}

export async function checkDomain(req) {
Expand Down Expand Up @@ -133,14 +134,14 @@ export async function checkMetrics(config) {
return true;

const { owner } = await getDomainById(config.domain);
const flag = await checkFeature(SwitcherKeys.ELEMENT_CREATION, [
const response = await checkFeature(SwitcherKeys.ELEMENT_CREATION, [
checkPayload(JSON.stringify({
feature: 'metrics',
owner
}))
]);

if (!flag) {
if (!response.result) {
if (!config.disable_metrics) {
config.disable_metrics = new Map();
config.disable_metrics.set(EnvType.DEFAULT, true);
Expand Down Expand Up @@ -180,9 +181,9 @@ export async function checkSlackIntegration(value) {
return;

switcherFlagResult(
await checkFeature(SwitcherKeys.SLACK_INTEGRATION, [
checkValue(value)
]), 'Slack Integration is not available.');
await checkFeature(SwitcherKeys.SLACK_INTEGRATION, [
checkValue(value)
]), 'Slack Integration is not available.');
}

export function notifyAcCreation(adminid) {
Expand All @@ -205,16 +206,21 @@ export function notifyAcDeletion(adminid) {

export async function getRateLimit(key, component) {
if (process.env.SWITCHER_API_ENABLE === 'true' && key !== process.env.SWITCHER_API_KEY) {
const fromMetadata = await checkFeature(SwitcherKeys.SWITCHER_AC_METADATA);
const domain = await getDomainById(component.domain);
const result = await checkFeature(SwitcherKeys.RATE_LIMIT, [
const response = await checkFeature(SwitcherKeys.RATE_LIMIT, [
checkValue(String(domain.owner))
]);

if (result) {
const log = Switcher.getLogger(SwitcherKeys.RATE_LIMIT)
.find(log => log.input[0][1] === String(domain.owner));
if (response.result) {
if (!fromMetadata.result) {
const log = Switcher.getLogger(SwitcherKeys.RATE_LIMIT)
.find(log => log.input[0][1] === String(domain.owner));

return JSON.parse(log.response.message).rate_limit;
return JSON.parse(log.response.message).rate_limit;
}

return response.metadata.rate_limit;
}
}

Expand Down
29 changes: 20 additions & 9 deletions tests/unit-test/switcher-api-facade.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,19 @@ describe('Testing Switcher API Facade', () => {
test('UNIT_API_FACADE - Should enable feature - History', async () => {
//given
Switcher.assume(SwitcherKeys.ELEMENT_CREATION).true();
const resultFeature = await checkHistory(domainId);
const response = await checkHistory(domainId);

//test
expect(resultFeature).toBe(true);
expect(response.result).toBe(true);
});

test('UNIT_API_FACADE - Should NOT enable feature - History', async () => {
//given
Switcher.assume(SwitcherKeys.ELEMENT_CREATION).false();
const resultFeature = await checkHistory(domainId);
const response = await checkHistory(domainId);

//test
expect(resultFeature).toBe(false);
expect(response.result).toBe(false);
});

test('UNIT_API_FACADE - Should enable feature - Sign up new Account', async () => {
Expand Down Expand Up @@ -243,12 +243,22 @@ describe('Testing Switcher API Facade', () => {

test('UNIT_API_FACADE - Should read rate limit - 100 Request Per Minute', async () => {
const call = async () => {
Switcher.assume(SwitcherKeys.SWITCHER_AC_METADATA).true();
Switcher.assume(SwitcherKeys.RATE_LIMIT).true().withMetadata({ rate_limit: 100 });
return getRateLimit(component1Key, component1);
};

await expect(call()).resolves.toBe(100);
});

test('UNIT_API_FACADE - Should read rate limit - 100 Request Per Minute - using messgae (depracated)', async () => {
const call = async () => {
Switcher.assume(SwitcherKeys.SWITCHER_AC_METADATA).false();
Switcher.assume(SwitcherKeys.RATE_LIMIT).true();
ExecutionLogger.add(
{ message: JSON.stringify({ rate_limit: 100 }) },
SwitcherKeys.RATE_LIMIT,
[checkValue(domainDocument.owner.toString())]
);

ExecutionLogger.add({ message: JSON.stringify({ rate_limit: 100 }) }, SwitcherKeys.RATE_LIMIT, [
checkValue(domainDocument.owner.toString())
]);

return getRateLimit(component1Key, component1);
};
Expand All @@ -258,6 +268,7 @@ describe('Testing Switcher API Facade', () => {

test('UNIT_API_FACADE - Should NOT read rate limit - Default Request Per Minute', async () => {
const call = async () => {
Switcher.assume(SwitcherKeys.SWITCHER_AC_METADATA).true();
Switcher.assume(SwitcherKeys.RATE_LIMIT).false();
return getRateLimit(component1Key, component1);
};
Expand Down