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 .mocharc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit: true
33 changes: 12 additions & 21 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import js from '@eslint/js';
import globals from 'globals';
import { defineConfig } from "eslint/config";

export default [
{
...js.configs.recommended,
files: ['src/**/*.js']
export default defineConfig({
files: ['src/**/*.js'],
rules: {
quotes: ['error', 'single'],
semi: ['error', 'always'],
curly: ['error', 'multi-line'],
'no-unused-vars': ['warn', { argsIgnorePattern: '^_' }]
},
{
files: ['src/**/*.js'],
rules: {
quotes: ['error', 'single'],
semi: ['error', 'always'],
curly: ['error', 'multi-line'],
},
languageOptions: {
ecmaVersion: 2022,
sourceType: 'module',
globals: {
...globals.node
}
}
languageOptions: {
ecmaVersion: 2022,
sourceType: 'module'
}
];
});
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@
"src/"
],
"devDependencies": {
"@babel/eslint-parser": "^7.28.5",
"@typescript-eslint/eslint-plugin": "^8.52.0",
"@typescript-eslint/parser": "^8.52.0",
"c8": "^10.1.3",
"@typescript-eslint/eslint-plugin": "^8.57.1",
"@typescript-eslint/parser": "^8.57.1",
"c8": "^11.0.0",
"chai": "^6.2.2",
"env-cmd": "^11.0.0",
"eslint": "^9.39.2",
"eslint": "^10.1.0",
"mocha": "^11.7.5",
"mocha-sonarqube-reporter": "^1.0.2",
"sinon": "^21.0.1"
"sinon": "^21.0.3"
},
"repository": {
"type": "git",
Expand Down
28 changes: 27 additions & 1 deletion tests/helper/dummy-cert.pem
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
-----BEGIN CERTIFICATE-----
==
MIIFBTCCAu2gAwIBAgIQWgDyEtjUtIDz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QhDconl
k7Kgh8b0vi+/XuWu7CN8n/UPeg1vo3G+taXirrytthQinAHGwc/UdbOygJa9zuBc
VyqoH3CXTXDInT+8a+c3aEVMJ2St+pSn4ed+WkDp8ijsijvEyFwE47hulW0Ltzjg
9fOV5Pmrg/zxWbRuL+k0DBDHEJennCsAen7c35Pmx7jpmJ/HtgRhcnz0yjSBvyIw
6L1QIupkCv2SBODT/xDD3gfQQyKv6roV4G2EhfEyAsWpmojxjCUCGiyg97FvDtm/
NK2LSc9lybKxB73I2+P2G3CaWpvvpAiHCVu30jW8GCxKdfhsXtnIy2imskQqVZ2m
0Pmxobb28Tucr7xBK7CtwvPrb79os7u2XP3O5f9b/H66GNyRrglRXlrYjI1oGYL/
f4I1n/Sgusda6WvA6C190kxjU15Y12mHU4+BxyR9cx2hhGS9fAjMZKJss28qxvz6
Axu4CaDmRNZpK/pQrXF17yXCXkmEWgvSOEZy6Z9pcbLIVEGckV/iVeq0AOo2pkg9
p4QRIy0tK2diRENLSF2KysFwbY6B26BFeFs3v1sYVRhFW9nLkOrQVporCS0KyZmf
wVD89qSTlnctLcZnIavjKsKUu1nA1iU0yYMdYepKR7lWbnwhdx3ewok=
-----END CERTIFICATE-----
118 changes: 118 additions & 0 deletions tests/switcher-context.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { assert } from 'chai';
import { stub } from 'sinon';

import FetchFacade from '../src/lib/utils/fetchFacade.js';
import { Client } from '../switcher-client.js';
import { given, generateAuth, generateResult, assertReject } from './helper/utils.js';
import { removeAgent } from '../src/lib/remote.js';

describe('Switcher Remote:', function () {

let contextSettings;
let fetchStub;

beforeEach(function() {
fetchStub = stub(FetchFacade, 'fetch');

contextSettings = {
apiKey: '[api_key]',
domain: 'Business',
component: 'business-service',
environment: 'default',
url: 'http://localhost:3000'
};
});

afterEach(function() {
fetchStub.restore();
});

it('should throw when certPath is invalid', function() {
assert.throws(() => Client.buildContext(contextSettings, { certPath: 'invalid' }),
'Invalid certificate path \'invalid\'');
});

it('should NOT throw when certPath is valid', function() {
assert.doesNotThrow(() => Client.buildContext(contextSettings, { certPath: './tests/helper/dummy-cert.pem' }));
removeAgent();
});

it('should be invalid - Missing API url field', async function () {

Check failure on line 40 in tests/switcher-context.test.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add at least one assertion to this test case.

See more on https://sonarcloud.io/project/issues?id=switcherapi_switcher-client-master&issues=AZ0XTf2I4T-oZG71nK-W&open=AZ0XTf2I4T-oZG71nK-W&pullRequest=218
// given
given(fetchStub, 0, { json: () => generateAuth('[auth_token]', 5), status: 200 });

// test
Client.buildContext({ url: undefined, apiKey: 'apiKey', domain: 'domain', component: 'component', environment: 'default' });
let switcher = Client.getSwitcher();

await switcher
.checkValue('User 1')
.checkNetwork('192.168.0.1')
.prepare('MY_FLAG');

await assertReject(assert, switcher.isItOn(), 'Something went wrong: URL is required');
});

it('should be invalid - Missing API Key field', async function () {

Check failure on line 56 in tests/switcher-context.test.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add at least one assertion to this test case.

See more on https://sonarcloud.io/project/issues?id=switcherapi_switcher-client-master&issues=AZ0XTf2I4T-oZG71nK-X&open=AZ0XTf2I4T-oZG71nK-X&pullRequest=218
// given
given(fetchStub, 0, { json: () => generateAuth('[auth_token]', 5), status: 200 });

// test
Client.buildContext({ url: 'url', apiKey: undefined, domain: 'domain', component: 'component', environment: 'default' });
let switcher = Client.getSwitcher();

await switcher
.checkValue('User 1')
.checkNetwork('192.168.0.1')
.prepare('MY_FLAG');

await assertReject(assert, switcher.isItOn(), 'Something went wrong: API Key is required');
});

it('should be invalid - Missing key field', async function () {

Check failure on line 72 in tests/switcher-context.test.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add at least one assertion to this test case.

See more on https://sonarcloud.io/project/issues?id=switcherapi_switcher-client-master&issues=AZ0XTf2I4T-oZG71nK-Y&open=AZ0XTf2I4T-oZG71nK-Y&pullRequest=218
// given
given(fetchStub, 0, { json: () => generateAuth('[auth_token]', 5), status: 200 });
given(fetchStub, 1, { json: () => generateResult(undefined) });

// test
Client.buildContext(contextSettings);
let switcher = Client.getSwitcher();
await switcher
.checkValue('User 1')
.checkNetwork('192.168.0.1')
.prepare(undefined);

await assertReject(assert, switcher.isItOn(), 'Something went wrong: Missing key field');
});

it('should be invalid - Missing component field', async function () {

Check failure on line 88 in tests/switcher-context.test.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add at least one assertion to this test case.

See more on https://sonarcloud.io/project/issues?id=switcherapi_switcher-client-master&issues=AZ0XTf2I4T-oZG71nK-Z&open=AZ0XTf2I4T-oZG71nK-Z&pullRequest=218
// given
given(fetchStub, 0, { json: () => generateAuth('[auth_token]', 5), status: 200 });
given(fetchStub, 1, { json: () => generateResult(undefined) });

// test
Client.buildContext({ url: 'url', apiKey: 'apiKey', domain: 'domain', component: undefined, environment: 'default' });
let switcher = Client.getSwitcher();

await assertReject(assert, switcher
.checkValue('User 1')
.checkNetwork('192.168.0.1')
.isItOn('MY_FLAG'), 'Something went wrong: Component is required');
});

it('should be invalid - Missing token field', async function () {

Check failure on line 103 in tests/switcher-context.test.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add at least one assertion to this test case.

See more on https://sonarcloud.io/project/issues?id=switcherapi_switcher-client-master&issues=AZ0XTf2I4T-oZG71nK-a&open=AZ0XTf2I4T-oZG71nK-a&pullRequest=218
// given
given(fetchStub, 0, { json: () => generateAuth(undefined, 1), status: 200 });
given(fetchStub, 1, { json: () => generateResult(undefined) });

// test
Client.buildContext(contextSettings);
let switcher = Client.getSwitcher();

await assertReject(assert, switcher
.checkValue('User 1')
.checkNetwork('192.168.0.1')
.isItOn('MY_FLAG'), 'Something went wrong: Missing token field');
});

});
Loading