-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: server settings and test assertions
Reworked ServerSettings to enhance clarity and logical grouping of configuration properties. Added `defaultToTrue` utility function to handle undefined feature flags, defaulting them to 'true' and ensuring consistent server settings initialization.
- Loading branch information
Showing
2 changed files
with
105 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,94 @@ | ||
import _ from 'lodash'; | ||
import { CLI_INTEGRATION_NAME } from '../../cli/contants/integration'; | ||
import { Configuration, IConfiguration, SeverityFilter } from '../configuration/configuration'; | ||
import { User } from '../user'; | ||
|
||
export type ServerSettings = { | ||
// Feature toggles | ||
activateSnykCodeSecurity?: string; | ||
activateSnykCodeQuality?: string; | ||
activateSnykOpenSource?: string; | ||
activateSnykIac?: string; | ||
|
||
// Endpoint path, and organization | ||
path?: string; | ||
cliPath?: string; | ||
endpoint?: string; | ||
organization?: string; | ||
|
||
// Authentication and parameters | ||
token?: string; | ||
automaticAuthentication?: string; | ||
additionalParams?: string; | ||
path?: string; | ||
manageBinariesAutomatically?: string; | ||
|
||
// Reporting and telemetry | ||
sendErrorReports?: string; | ||
organization?: string; | ||
enableTelemetry?: string; | ||
manageBinariesAutomatically?: string; | ||
cliPath?: string; | ||
token?: string; | ||
|
||
// Security and scanning settings | ||
filterSeverity?: SeverityFilter; | ||
scanningMode?: string; | ||
insecure?: string; | ||
|
||
// Trusted folders feature | ||
enableTrustedFoldersFeature?: string; | ||
trustedFolders?: string[]; | ||
insecure?: string; | ||
scanningMode?: string; | ||
|
||
// Snyk integration settings | ||
integrationName?: string; | ||
integrationVersion?: string; | ||
automaticAuthentication?: string; | ||
deviceId?: string; | ||
}; | ||
|
||
/** | ||
* Transforms a boolean or undefined value into a string representation. | ||
* It guarantees that undefined values are represented as 'true'. | ||
* This utility is used to ensure feature flags are enabled by default | ||
* when not explicitly set to false. | ||
* | ||
* @param {boolean | undefined} value - The value to transform. | ||
* @returns {string} - The string 'true' if the value is undefined or truthy, 'false' if the value is false. | ||
*/ | ||
export const defaultToTrue = (value: boolean | undefined): string => { | ||
return `${value !== undefined ? value : true}`; | ||
}; | ||
|
||
export class LanguageServerSettings { | ||
static async fromConfiguration(configuration: IConfiguration, user: User): Promise<ServerSettings> { | ||
const featuresConfiguration = configuration.getFeaturesConfiguration(); | ||
|
||
const iacEnabled = _.isUndefined(featuresConfiguration.iacEnabled) ? true : featuresConfiguration.iacEnabled; | ||
const codeSecurityEnabled = _.isUndefined(featuresConfiguration.codeSecurityEnabled) | ||
? true | ||
: featuresConfiguration.codeSecurityEnabled; | ||
const codeQualityEnabled = _.isUndefined(featuresConfiguration.codeQualityEnabled) | ||
? true | ||
: featuresConfiguration.codeQualityEnabled; | ||
const iacEnabled = defaultToTrue(featuresConfiguration.iacEnabled); | ||
const codeSecurityEnabled = defaultToTrue(featuresConfiguration.codeSecurityEnabled); | ||
const codeQualityEnabled = defaultToTrue(featuresConfiguration.codeQualityEnabled); | ||
|
||
return { | ||
activateSnykCodeSecurity: `${codeSecurityEnabled}`, | ||
activateSnykCodeQuality: `${codeQualityEnabled}`, | ||
activateSnykCodeSecurity: codeSecurityEnabled, | ||
activateSnykCodeQuality: codeQualityEnabled, | ||
activateSnykOpenSource: 'false', | ||
activateSnykIac: `${iacEnabled}`, | ||
enableTelemetry: `${configuration.shouldReportEvents}`, | ||
sendErrorReports: `${configuration.shouldReportErrors}`, | ||
activateSnykIac: iacEnabled, | ||
|
||
cliPath: configuration.getCliPath(), | ||
endpoint: configuration.snykOssApiEndpoint, | ||
additionalParams: configuration.getAdditionalCliParameters(), | ||
organization: configuration.organization, | ||
|
||
token: await configuration.getToken(), | ||
automaticAuthentication: 'false', | ||
additionalParams: configuration.getAdditionalCliParameters(), | ||
manageBinariesAutomatically: `${configuration.isAutomaticDependencyManagementEnabled()}`, | ||
|
||
sendErrorReports: `${configuration.shouldReportErrors}`, | ||
enableTelemetry: `${configuration.shouldReportEvents}`, | ||
|
||
filterSeverity: configuration.severityFilter, | ||
scanningMode: configuration.scanningMode, | ||
insecure: `${configuration.getInsecure()}`, | ||
|
||
enableTrustedFoldersFeature: 'true', | ||
trustedFolders: configuration.getTrustedFolders(), | ||
insecure: `${configuration.getInsecure()}`, | ||
scanningMode: configuration.scanningMode, | ||
|
||
integrationName: CLI_INTEGRATION_NAME, | ||
integrationVersion: await Configuration.getVersion(), | ||
deviceId: user.anonymousId, | ||
automaticAuthentication: 'false', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import assert from 'assert'; | ||
import { IConfiguration } from '../../../../snyk/common/configuration/configuration'; | ||
import { LanguageServerSettings, defaultToTrue } from '../../../../snyk/common/languageServer/settings'; | ||
import { User } from '../../../../snyk/common/user'; | ||
|
||
suite('LanguageServerSettings', () => { | ||
suite('defaultToTrue', () => { | ||
test('should return "true" for undefined values', () => { | ||
assert.strictEqual(defaultToTrue(undefined), 'true'); | ||
}); | ||
|
||
test('should return "true" for truthy values', () => { | ||
assert.strictEqual(defaultToTrue(true), 'true'); | ||
}); | ||
|
||
test('should return "false" for false values', () => { | ||
assert.strictEqual(defaultToTrue(false), 'false'); | ||
}); | ||
}); | ||
|
||
suite('fromConfiguration', () => { | ||
test('should generate server settings with default true values for undefined feature toggles', async () => { | ||
const mockUser = { anonymousId: 'anonymous-id' } as User; | ||
const mockConfiguration: IConfiguration = { | ||
shouldReportEvents: true, | ||
shouldReportErrors: false, | ||
snykOssApiEndpoint: 'https://dev.snyk.io/api', | ||
organization: 'my-org', | ||
// eslint-disable-next-line @typescript-eslint/require-await | ||
getToken: async () => 'snyk-token', | ||
getFeaturesConfiguration: () => ({}), // iacEnabled, codeSecurityEnabled, codeQualityEnabled are undefined | ||
getCliPath: () => '/path/to/cli', | ||
getAdditionalCliParameters: () => '--all-projects -d', | ||
getTrustedFolders: () => ['/trusted/path'], | ||
getInsecure: () => false, | ||
isAutomaticDependencyManagementEnabled: () => true, | ||
severityFilter: { critical: true, high: true, medium: true, low: false }, | ||
scanningMode: 'scan-mode', | ||
} as IConfiguration; | ||
|
||
const serverSettings = await LanguageServerSettings.fromConfiguration(mockConfiguration, mockUser); | ||
|
||
assert.strictEqual(serverSettings.activateSnykCodeSecurity, 'true'); | ||
assert.strictEqual(serverSettings.activateSnykCodeQuality, 'true'); | ||
assert.strictEqual(serverSettings.activateSnykIac, 'true'); | ||
assert.strictEqual(serverSettings.deviceId, 'anonymous-id'); | ||
|
||
assert.strictEqual(serverSettings.enableTelemetry, 'true'); | ||
assert.strictEqual(serverSettings.sendErrorReports, 'false'); | ||
assert.strictEqual(serverSettings.cliPath, '/path/to/cli'); | ||
|
||
assert.strictEqual(serverSettings.token, 'snyk-token'); | ||
}); | ||
}); | ||
}); |