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
672 changes: 329 additions & 343 deletions npm-shrinkwrap.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@
"jsonwebtoken": "^9.0.2",
"moment": "^2.30.1",
"mongodb": "^6.17.0",
"mongoose": "^8.16.0",
"mongoose": "^8.16.1",
"pino": "^9.7.0",
"pino-pretty": "^13.0.0",
"swagger-ui-express": "^5.0.1",
"switcher-client": "^4.2.0",
"switcher-client": "^4.3.0",
"validator": "^13.15.15"
},
"devDependencies": {
"env-cmd": "^10.1.0",
"eslint": "^9.29.0",
"jest": "^30.0.2",
"eslint": "^9.30.1",
"jest": "^30.0.4",
"jest-sonar-reporter": "^2.0.0",
"node-notifier": "^10.0.1",
"nodemon": "^3.1.10",
Expand Down
43 changes: 26 additions & 17 deletions src/helpers/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,47 @@ import schema from '../../aggregator/schema.js';
import { getAllDomains } from '../../services/domain.js';
import { domainQuery, reduceSnapshot } from './query.js';

class Cache {
export default class Cache {
#instance;

constructor() {
this.cache = new Map();
this.#instance = new Map();
}

static getInstance() {
if (!Cache.instance) {
Cache.instance = new Cache();
}
return Cache.instance;
}

async initializeCache() {
const domains = await getAllDomains();

for (const domain of domains) {
const result = await graphql({
schema,
source: domainQuery(domain._id),
contextValue: { domain: domain._id }
});

this.#set(domain._id, reduceSnapshot(result.data.domain));
await this.#updateCache(domain);
}
}

async #updateCache(domain) {
const result = await graphql({
schema,
source: domainQuery(domain._id),
contextValue: { domain: domain._id }
});

this.#set(domain._id, reduceSnapshot(result.data.domain));
}

#set(key, value) {
this.cache.set(key, value);
this.#instance.set(String(key), value);
}

get(key) {
return this.cache.get(key);
return this.#instance.get(String(key));
}

getAll() {
return this.cache;
return this.#instance;
}
}

const cache = new Cache();

export default cache;
}
8 changes: 4 additions & 4 deletions tests/client-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ describe('Testing criteria [GraphQL]', () => {
});

test('CLIENT_SUITE - Should not add to metrics when Config has disabled metric flag = true', async () => {
//given
// given
await changeConfigStatus(configId, true, EnvType.DEFAULT);

//add one metric data
Expand Down Expand Up @@ -355,7 +355,7 @@ describe('Testing criteria [GraphQL]', () => {
)
.expect(200);

//test
// test
const afterNumMetricData = await Metric.find({ config: configId }).countDocuments().exec();
expect(numMetricData === afterNumMetricData).toBe(true);
});
Expand Down Expand Up @@ -565,7 +565,7 @@ describe('Testing criteria [REST] ', () => {
});

test('CLIENT_SUITE - Should NOT return success on a entry-based CRITERIA response - Component not registered', async () => {
// Given
// given
const component = new Component({
_id: new mongoose.Types.ObjectId(),
name: 'Temp Component',
Expand All @@ -586,7 +586,7 @@ describe('Testing criteria [REST] ', () => {

const tempToken = response.body.token;

// Test
// test
const req = await request(app)
.post(`/criteria?key=${keyConfig}&showReason=true&showStrategy=true`)
.set('Authorization', `Bearer ${tempToken}`)
Expand Down
4 changes: 2 additions & 2 deletions tests/model/component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Testing component authentication', () => {
beforeAll(async () => await setupDatabase());

test('COMPONENT_MODEL - Should authenticate component using new API key format', async () => {
// Given
// given
const componentId = new mongoose.Types.ObjectId();
const component = new Component({
_id: componentId,
Expand All @@ -31,7 +31,7 @@ describe('Testing component authentication', () => {
// That
const generatedApiKey = await component.generateApiKey();

// Test
// test
const result = await Component.findByCredentials(domainDocument.name, component.name, generatedApiKey);
expect(result.component).not.toBe(undefined);
});
Expand Down
58 changes: 29 additions & 29 deletions tests/relay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ describe('Testing Switcher Relay', () => {
afterAll(setupDatabase);

test('RELAY_SUITE - Should return success when validating relay using GET method', async () => {
// Mock
// mock
axiosStub = sinon.stub(axios, 'get');

// Given
// given
const mockRelayService = { data: { result: true, message: 'A message', metadata: { custom: 'VALUE' } } };
axiosStub.returns(Promise.resolve(mockRelayService));

Expand All @@ -103,7 +103,7 @@ describe('Testing Switcher Relay', () => {
config.relay = bodyRelay(RelayMethods.GET, RelayTypes.VALIDATION);
await config.save();

// Test
// test
const req = await request(app)
.post(`/criteria?key=${keyConfig}&showReason=true&showStrategy=true`)
.set('Authorization', `Bearer ${token}`)
Expand All @@ -128,10 +128,10 @@ describe('Testing Switcher Relay', () => {
});

test('RELAY_SUITE - Should return success when validating relay using POST method', async () => {
// Mock
// mock
axiosStub = sinon.stub(axios, 'post');

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

Expand All @@ -140,7 +140,7 @@ describe('Testing Switcher Relay', () => {
config.relay = bodyRelay(RelayMethods.POST, RelayTypes.VALIDATION);
await config.save();

// Test
// test
const req = await request(app)
.post(`/criteria?key=${keyConfig}&showReason=true&showStrategy=true`)
.set('Authorization', `Bearer ${token}`)
Expand All @@ -163,10 +163,10 @@ describe('Testing Switcher Relay', () => {
});

test('RELAY_SUITE - Should return success when notifying relay using GET method', async () => {
// Mock
// mock
axiosStub = sinon.stub(axios, 'get');

// Given - altough it's not considered after invoking the relay
// given - altough it's not considered after invoking the relay
const mockRelayService = { data: { result: true, reason: 'Success' } };
axiosStub.returns(Promise.resolve(mockRelayService));

Expand All @@ -175,7 +175,7 @@ describe('Testing Switcher Relay', () => {
config.relay = bodyRelay(RelayMethods.GET, RelayTypes.NOTIFICATION);
await config.save();

// Test
// test
const req = await request(app)
.post(`/criteria?key=${keyConfig}&showReason=true&showStrategy=true`)
.set('Authorization', `Bearer ${token}`)
Expand All @@ -198,10 +198,10 @@ describe('Testing Switcher Relay', () => {
});

test('RELAY_SUITE - Should return success when notifying relay using POST method', async () => {
// Mock
// mock
axiosStub = sinon.stub(axios, 'post');

// Given - altough it's not considered after invoking the relay
// given - altough it's not considered after invoking the relay
const mockRelayService = { data: { result: true, reason: 'Success' } };
axiosStub.returns(Promise.resolve(mockRelayService));

Expand All @@ -210,7 +210,7 @@ describe('Testing Switcher Relay', () => {
config.relay = bodyRelay(RelayMethods.POST, RelayTypes.NOTIFICATION);
await config.save();

// Test
// test
const req = await request(app)
.post(`/criteria?key=${keyConfig}&showReason=true&showStrategy=true`)
.set('Authorization', `Bearer ${token}`)
Expand All @@ -233,10 +233,10 @@ describe('Testing Switcher Relay', () => {
});

test('RELAY_SUITE - Should return success when validating relay using GET method - no input', async () => {
// Mock
// mock
axiosStub = sinon.stub(axios, 'get');

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

Expand All @@ -249,7 +249,7 @@ describe('Testing Switcher Relay', () => {
await changeStrategy(configStrategyUSERId, undefined, false, EnvType.DEFAULT);
await changeStrategy(configStrategyCIDRId, undefined, false, EnvType.DEFAULT);

// Test
// test
const req = await request(app)
.post(`/criteria?key=${keyConfig}&showReason=true&showStrategy=true`)
.set('Authorization', `Bearer ${token}`)
Expand All @@ -262,10 +262,10 @@ describe('Testing Switcher Relay', () => {
});

test('RELAY_SUITE - Should return success when validating relay using POST method - no input', async () => {
// Mock
// mock
axiosStub = sinon.stub(axios, 'post');

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

Expand All @@ -278,7 +278,7 @@ describe('Testing Switcher Relay', () => {
await changeStrategy(configStrategyUSERId, undefined, false, EnvType.DEFAULT);
await changeStrategy(configStrategyCIDRId, undefined, false, EnvType.DEFAULT);

// Test
// test
const req = await request(app)
.post(`/criteria?key=${keyConfig}&showReason=true&showStrategy=true`)
.set('Authorization', `Bearer ${token}`)
Expand All @@ -291,7 +291,7 @@ describe('Testing Switcher Relay', () => {
});

test('RELAY_SUITE - Should NOT return success when validating relay using GET method - Service exception', async () => {
// Mock
// mock
axiosStub = sinon.stub(axios, 'get');
axiosStub.throwsException();

Expand All @@ -300,7 +300,7 @@ describe('Testing Switcher Relay', () => {
config.relay = bodyRelay(RelayMethods.GET, RelayTypes.VALIDATION);
await config.save();

// Test
// test
const req = await request(app)
.post(`/criteria?key=${keyConfig}&showReason=true&showStrategy=true`)
.set('Authorization', `Bearer ${token}`)
Expand All @@ -313,7 +313,7 @@ describe('Testing Switcher Relay', () => {
});

test('RELAY_SUITE - Should NOT return success when validating relay using POST method - Service exception', async () => {
// Mock
// mock
axiosStub = sinon.stub(axios, 'post');
axiosStub.throwsException();

Expand All @@ -322,7 +322,7 @@ describe('Testing Switcher Relay', () => {
config.relay = bodyRelay(RelayMethods.POST, RelayTypes.VALIDATION);
await config.save();

// Test
// test
const req = await request(app)
.post(`/criteria?key=${keyConfig}&showReason=true&showStrategy=true`)
.set('Authorization', `Bearer ${token}`)
Expand Down Expand Up @@ -358,7 +358,7 @@ describe('Testing Switcher Relay Validation', () => {
});

test('RELAY_SUITE - Should return Relay could not be reached - Relay HTTPS required', async () => {
// Given
// given
// HTTPS not required
process.env.RELAY_BYPASS_HTTPS = true;

Expand All @@ -370,7 +370,7 @@ describe('Testing Switcher Relay Validation', () => {
// HTTPS required
process.env.RELAY_BYPASS_HTTPS = false;

// Test
// test
const req = await request(app)
.post(`/criteria?key=${keyConfig}&showReason=true&showStrategy=true`)
.set('Authorization', `Bearer ${token}`)
Expand Down Expand Up @@ -415,7 +415,7 @@ describe('Testing Switcher Relay Verification', () => {
});

test('RELAY_SUITE - Should return Relay could not be reached - Not verified', async () => {
// Given
// given
// Verification required
process.env.RELAY_BYPASS_VERIFICATION = false;

Expand All @@ -424,7 +424,7 @@ describe('Testing Switcher Relay Verification', () => {
config.relay = bodyRelay('https://localhost:3001');
await config.save();

// Test
// test
const req = await request(app)
.post(`/criteria?key=${keyConfig}&showReason=true&showStrategy=true`)
.set('Authorization', `Bearer ${token}`)
Expand All @@ -445,10 +445,10 @@ describe('Testing Switcher Relay Verification', () => {
});

test('RELAY_SUITE - Should return success when validating verified relay', async () => {
// Mock
// mock
axiosStub = sinon.stub(axios, 'get');

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

Expand All @@ -466,7 +466,7 @@ describe('Testing Switcher Relay Verification', () => {
await config.save();
expect(config.relay.verified.get(EnvType.DEFAULT)).toBe(true);

// Test
// test
const req = await request(app)
.post(`/criteria?key=${keyConfig}&showReason=true&showStrategy=true`)
.set('Authorization', `Bearer ${token}`)
Expand Down
Loading