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
11 changes: 9 additions & 2 deletions src/client/relay/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import axios from 'axios';
import https from 'https';
import { StrategiesToRelayDataType, RelayMethods } from '../../models/config';
import { checkHttpsAgent } from '../../external/switcher-api-facade';

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

export function resolveNotification(relay, entry, environment) {
const url = relay.endpoint[environment];
Expand Down Expand Up @@ -41,15 +48,15 @@ export async function resolveVerification(relay, environment) {

async function post(url, data, headers) {
try {
return await axios.post(url, data, headers);
return await axios.post(url, data, { httpsAgent: agent(url) }, headers);
} catch (error) {
throw new Error(`Failed to reach ${url} via POST`);
}
}

async function get(url, data, headers) {
try {
return await axios.get(`${url}${data}`, headers);
return await axios.get(`${url}${data}`, { httpsAgent: agent(url) }, headers);
} catch (error) {
throw new Error(`Failed to reach ${url} via GET`);
}
Expand Down
10 changes: 9 additions & 1 deletion src/external/switcher-api-facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export const SwitcherKeys = Object.freeze({
ACCOUNT_OUT_NOTIFY: 'ACCOUNT_OUT_NOTIFY',
SLACK_INTEGRATION: 'SLACK_INTEGRATION',
SLACK_UPDATE: 'SLACK_UPDATE',
RATE_LIMIT: 'RATE_LIMIT'
RATE_LIMIT: 'RATE_LIMIT',
HTTPS_AGENT: 'HTTPS_AGENT'
});

function switcherFlagResult(flag, message) {
Expand Down Expand Up @@ -220,4 +221,11 @@ export async function getRateLimit(key, component) {
}

return parseInt(process.env.MAX_REQUEST_PER_MINUTE || DEFAULT_RATE_LIMIT);
}

export async function checkHttpsAgent(value) {
if (process.env.SWITCHER_API_ENABLE != 'true')
return;

return await checkFeature(SwitcherKeys.HTTPS_AGENT, [checkValue(value)]);
}
2 changes: 1 addition & 1 deletion tests/relay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { adminMasterAccountId } from './fixtures/db_api';
import { StrategiesType, ConfigStrategy } from '../src/models/config-strategy';

const changeStrategy = async (strategyId, newOperation, status, environment) => {
const strategy = await ConfigStrategy.findById(strategyId).exec()
const strategy = await ConfigStrategy.findById(strategyId).exec();
strategy.operation = newOperation ? newOperation : strategy.operation;
strategy.activated.set(environment, status !== undefined ? status : strategy.activated.get(environment));
strategy.updatedBy = adminMasterAccountId;
Expand Down
6 changes: 3 additions & 3 deletions tests/slack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { TicketValidationType } from '../src/models/slack_ticket';

afterAll(async () => {
await Slack.deleteMany();
await Slack.deleteMany().exec();
await new Promise(resolve => setTimeout(resolve, 1000));
await mongoose.disconnect();
});
Expand Down Expand Up @@ -840,7 +840,7 @@ describe('Slack Route - Process Ticket', () => {

test('SLACK_SUITE - Should reset installation tickets', async () => {
//verify that
let slackInstallation = await Slack.findOne({ team_id: slack.team_id });
let slackInstallation = await Slack.findOne({ team_id: slack.team_id }).exec();
expect(slackInstallation.tickets.length).toBeGreaterThan(0);

//test
Expand All @@ -851,7 +851,7 @@ describe('Slack Route - Process Ticket', () => {
team_id: slack.team_id
}).expect(200);

slackInstallation = await Slack.findOne({ team_id: slack.team_id });
slackInstallation = await Slack.findOne({ team_id: slack.team_id }).exec();
expect(slackInstallation.tickets.length).toEqual(0);
});

Expand Down
46 changes: 46 additions & 0 deletions tests/unit-test/client/relay.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { resolveValidation } from '../../../src/client/relay';
import sinon from 'sinon';
import axios from 'axios';
import { RelayMethods } from '../../../src/models/config';
import { StrategiesType } from '../../../src/models/config-strategy';
import { EnvType } from '../../../src/models/environment';
import { Switcher } from 'switcher-client';

describe('Testing Client Relay', () => {

let axiosStub;

beforeAll(() => {
process.env.SWITCHER_API_ENABLE = true;
});

test('CLIENT_RELAY_SUITE - Should resolve validation', async () => {
// Mock
axiosStub = sinon.stub(axios, 'get');

// Given
const mockRelayService = { data: { result: true, reason: 'Success' } };
axiosStub.returns(Promise.resolve(mockRelayService));
Switcher.assume('HTTPS_AGENT').true();

const relay = {
endpoint: {
default: 'https://localhost'
},
method: RelayMethods.GET,
auth_prefix: 'Bearer',
auth_token: {
default: 'token'
}
};

const entry = [{
strategy: StrategiesType.VALUE,
input: 'test'
}];

const res = await resolveValidation(relay, entry, EnvType.DEFAULT);
expect(res.result).toBe(true);
});

});