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
1 change: 1 addition & 0 deletions .env-cmdrc-template
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

"SWITCHER_API_LOGGER": false,
"SWITCHER_API_ENABLE": false,
"SWITCHER_API_THROTTLE": 5000,
"SWITCHER_API_URL": "http://localhost:3000",
"SWITCHER_API_KEY": "MOCK_SWITCHER_API_KEY",
"SWITCHER_API_DOMAIN": "MOCK_SWITCHER_API_DOMAIN",
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ services:
- SWITCHER_API_KEY=${SWITCHER_API_KEY}
- SWITCHER_API_DOMAIN=${SWITCHER_API_DOMAIN}
- SWITCHER_API_ENVIRONMENT=${SWITCHER_API_ENVIRONMENT}
- SWITCHER_API_THROTTLE=${SWITCHER_API_THROTTLE}

- SWITCHER_SLACK_JWT_SECRET=${SWITCHER_SLACK_JWT_SECRET}
- SWITCHER_GITOPS_JWT_SECRET=${SWITCHER_GITOPS_JWT_SECRET}
Expand Down
17 changes: 7 additions & 10 deletions src/external/switcher-api-facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const environment = process.env.SWITCHER_API_ENVIRONMENT;
const domainName = process.env.SWITCHER_API_DOMAIN;
const url = process.env.SWITCHER_API_URL;
const logger = process.env.SWITCHER_API_LOGGER == 'true';
const throttle = process.env.SWITCHER_API_THROTTLE;
const certPath = process.env.SSL_CERT;
const component = 'switcherapi';

Expand All @@ -26,8 +27,7 @@ export const SwitcherKeys = Object.freeze({
ACCOUNT_OUT_NOTIFY: 'ACCOUNT_OUT_NOTIFY',
SLACK_INTEGRATION: 'SLACK_INTEGRATION',
RATE_LIMIT: 'RATE_LIMIT',
HTTPS_AGENT: 'HTTPS_AGENT',
SWITCHER_AC_METADATA: 'SWITCHER_AC_METADATA'
HTTPS_AGENT: 'HTTPS_AGENT'
});

function switcherFlagResult(response, message) {
Expand All @@ -38,6 +38,11 @@ function switcherFlagResult(response, message) {

async function checkFeature(feature, params) {
const switcher = Switcher.factory();

if (throttle) {
switcher.throttle(throttle);
}

return switcher.detail().isItOn(feature, params);
}

Expand Down Expand Up @@ -206,20 +211,12 @@ 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 response = await checkFeature(SwitcherKeys.RATE_LIMIT, [
checkValue(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 response.metadata.rate_limit;
}
}
Expand Down
20 changes: 1 addition & 19 deletions tests/unit-test/switcher-api-facade.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import {
component1,
component1Key
} from '../fixtures/db_api';
import { Switcher, checkValue } from 'switcher-client';
import ExecutionLogger from 'switcher-client/src/lib/utils/executionLogger';
import { Switcher } from 'switcher-client';

afterAll(async () => {
await new Promise(resolve => setTimeout(resolve, 1000));
Expand Down Expand Up @@ -243,32 +242,15 @@ 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())
]);

return getRateLimit(component1Key, component1);
};

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

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