Skip to content

Commit

Permalink
Merge pull request #29 from SquirrelCorporation/chores-improve-tests
Browse files Browse the repository at this point in the history
Refactor import statements for SsmAnsible and SettingsKeys
  • Loading branch information
SquirrelDeveloper authored Jun 13, 2024
2 parents 640afc2 + a16eb98 commit b6b58dc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 38 deletions.
10 changes: 5 additions & 5 deletions server/src/core/startup/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DefaultValue, GeneralSettingsKeys } from 'ssm-shared-lib/distribution/enums/settings';
import { SettingsKeys } from 'ssm-shared-lib';
import { getFromCache } from '../../data/cache';
import initRedisValues from '../../data/cache/defaults';
import providerConf from '../../integrations/docker/registries/providers/provider.conf';
Expand All @@ -10,16 +10,16 @@ import { setAnsibleVersion } from '../system/version';

async function needConfigurationInit() {
logger.info(`[CONFIGURATION] - needInit`);
await DeviceAuthUseCases.saveAllDeviceAuthSshKeys();
void DeviceAuthUseCases.saveAllDeviceAuthSshKeys();

const version = await getFromCache(GeneralSettingsKeys.SCHEME_VERSION);
const version = await getFromCache(SettingsKeys.GeneralSettingsKeys.SCHEME_VERSION);

logger.info(`[CONFIGURATION] - needInit - Scheme Version: ${version}`);

if (version !== DefaultValue.SCHEME_VERSION) {
if (version !== SettingsKeys.DefaultValue.SCHEME_VERSION) {
await initRedisValues();
await PlaybookUseCases.initPlaybook();
await setAnsibleVersion();
void setAnsibleVersion();

providerConf
.filter(({ persist }) => persist)
Expand Down
1 change: 0 additions & 1 deletion server/src/integrations/shell/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import User from '../../data/database/model/User';
import AnsibleTaskRepo from '../../data/database/repository/AnsibleTaskRepo';
import DeviceAuthRepo from '../../data/database/repository/DeviceAuthRepo';
import logger from '../../logger';
import { DEFAULT_VAULT_ID, vaultEncrypt } from '../ansible-vault/vault';
import Inventory from '../ansible/utils/InventoryTransformer';
import { Ansible } from '../../types/typings';
import ansibleCmd from '../ansible/AnsibleCmd';
Expand Down
21 changes: 12 additions & 9 deletions server/src/tests/integrations/ansible/InventoryTransformer.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { describe, expect, test } from 'vitest';
import { SSHType } from 'ssm-shared-lib/distribution/enums/ansible';
import { SsmAnsible } from 'ssm-shared-lib';
import InventoryTransformer from '../../../integrations/ansible/utils/InventoryTransformer'; // replace with actual file path

describe('test InventoryTransformer', () => {
test('inventoryBuilder', () => {
const deviceAuth = {
device: { uuid: '1234-5678-9101', ip: '192.168.1.1' },
authType: SSHType.UserPassword,
authType: SsmAnsible.SSHType.UserPassword,
becomeMethod: 'sudo',
becomePass: 'password',
sshUser: 'root',
Expand Down Expand Up @@ -44,7 +44,7 @@ describe('test InventoryTransformer', () => {
test('inventoryBuilderForTarget', () => {
const deviceAuth = {
device: { uuid: '1234-5678-9102', ip: '192.168.1.2' },
authType: SSHType.UserPassword,
authType: SsmAnsible.SSHType.UserPassword,
becomeMethod: 'sudo',
becomePass: 'qwerty',
sshUser: 'admin',
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('test InventoryTransformer', () => {
test('inventoryBuilderForTargetWithSshKey', () => {
const deviceAuth = {
device: { uuid: '1234-5678-9102', ip: '192.168.1.2' },
authType: SSHType.KeyBased,
authType: SsmAnsible.SSHType.KeyBased,
becomeMethod: 'sudo',
becomePass: 'qwerty',
sshUser: 'admin',
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('test InventoryTransformer', () => {
test('inventoryBuilderForTargetWithSshKeyAndKeyPhrase', () => {
const deviceAuth = {
device: { uuid: '1234-5678-9102', ip: '192.168.1.2' },
authType: SSHType.KeyBased,
authType: SsmAnsible.SSHType.KeyBased,
becomeMethod: 'sudo',
becomePass: 'qwerty',
sshUser: 'admin',
Expand Down Expand Up @@ -154,7 +154,7 @@ describe('test InventoryTransformer', () => {
test('inventoryBuilder for deviceAuth with strict host key checking', () => {
const deviceAuth = {
device: { uuid: '1234-5678-9103', ip: '192.168.1.3' },
authType: SSHType.UserPassword,
authType: SsmAnsible.SSHType.UserPassword,
strictHostKeyChecking: true,
becomeMethod: 'su',
becomePass: 'adminpassword',
Expand Down Expand Up @@ -189,7 +189,10 @@ describe('test InventoryTransformer', () => {
});

test('inventoryBuilderForTarget for deviceAuth with undefined IP', () => {
const deviceAuth = { device: { uuid: '1234-5678-9104' }, authType: SSHType.UserPassword };
const deviceAuth = {
device: { uuid: '1234-5678-9104' },
authType: SsmAnsible.SSHType.UserPassword,
};
// @ts-expect-error partial type
const result = InventoryTransformer.inventoryBuilderForTarget([deviceAuth]);
const expectedResult = {
Expand All @@ -213,15 +216,15 @@ describe('test InventoryTransformer', () => {
test('inventoryBuilderForTarget with multiple devicesAuth', () => {
const deviceAuth1 = {
device: { uuid: '1234-5678-9105', ip: '192.168.1.4' },
authType: SSHType.UserPassword,
authType: SsmAnsible.SSHType.UserPassword,
becomeMethod: 'sudo',
becomePass: 'password1',
sshUser: 'root',
};

const deviceAuth2 = {
device: { uuid: '2234-5678-9106', ip: '192.168.1.5' },
authType: SSHType.UserPassword,
authType: SsmAnsible.SSHType.UserPassword,
becomeMethod: 'sudo',
becomePass: 'password2',
sshUser: 'root',
Expand Down
45 changes: 22 additions & 23 deletions server/src/tests/integrations/docker/DockerAPIHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ConnectConfig } from 'ssh2';
import { SSHType } from 'ssm-shared-lib/distribution/enums/ansible';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { SsmAnsible } from 'ssm-shared-lib';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import Device from '../../../data/database/model/Device';
import DeviceAuth from '../../../data/database/model/DeviceAuth';
import * as vault from '../../../integrations/ansible-vault/vault';
Expand All @@ -13,7 +12,7 @@ describe('getDockerSshConnectionOptions', () => {
return {
...(await importOriginal<typeof import('../../../data/database/repository/DeviceRepo')>()),
vaultDecrypt: async (value: string, vault: string) => {
return value;
return value + '-decrypted';
},
};
});
Expand All @@ -22,7 +21,7 @@ describe('getDockerSshConnectionOptions', () => {
let device: Device, deviceAuth: DeviceAuth;

beforeEach(() => {
const spy = vi.spyOn(vault, 'vaultDecrypt');
vi.spyOn(vault, 'vaultDecrypt');
// @ts-expect-error partial type
device = {
uuid: 'x',
Expand All @@ -32,7 +31,7 @@ describe('getDockerSshConnectionOptions', () => {
deviceAuth = {
sshPort: 22,
sshUser: 'apiuser',
sshKey: '!2#4%',
sshKey: 'sshkey',
// @ts-expect-error partial type
authType: '',
sshKeyPass: undefined,
Expand All @@ -49,7 +48,7 @@ describe('getDockerSshConnectionOptions', () => {
});

test('should handle key-based SSHType for default Docker SSH', async () => {
deviceAuth.authType = SSHType.KeyBased;
deviceAuth.authType = SsmAnsible.SSHType.KeyBased;
const result = DockerAPIHelper.getDockerSshConnectionOptions(device, deviceAuth);

expect(result).resolves.toMatchObject({
Expand All @@ -66,16 +65,16 @@ describe('getDockerSshConnectionOptions', () => {
port: 22,
passphrase: undefined,
username: 'apiuser',
privateKey: '!2#4%',
privateKey: 'sshkey-decrypted',
},
});

expect(vault.vaultDecrypt).toHaveBeenCalledTimes(1);
});

test('should handle user-password SSHType for default Docker SSH', async () => {
deviceAuth.authType = SSHType.UserPassword;
deviceAuth.sshPwd = '!2#4%';
deviceAuth.authType = SsmAnsible.SSHType.UserPassword;
deviceAuth.sshPwd = 'sshpwd';

const result = await DockerAPIHelper.getDockerSshConnectionOptions(device, deviceAuth);

Expand All @@ -91,7 +90,7 @@ describe('getDockerSshConnectionOptions', () => {
forceIPv6: undefined,
host: '0.0.0.0',
port: 22,
password: '!2#4%',
password: 'sshpwd-decrypted',
username: 'apiuser',
},
});
Expand All @@ -100,13 +99,13 @@ describe('getDockerSshConnectionOptions', () => {
});

test('should handle key-based SSHType for custom Docker SSH', async () => {
deviceAuth.authType = SSHType.UserPassword;
deviceAuth.sshPwd = '!2#4%';
deviceAuth.authType = SsmAnsible.SSHType.UserPassword;
deviceAuth.sshPwd = 'sshpwd';
deviceAuth.customDockerSSH = true;
deviceAuth.dockerCustomAuthType = SSHType.KeyBased;
deviceAuth.dockerCustomAuthType = SsmAnsible.SSHType.KeyBased;
deviceAuth.dockerCustomSshUser = '$customUser';
deviceAuth.dockerCustomSshKey = 'decrypted';
deviceAuth.dockerCustomSshKeyPass = 'decrypted';
deviceAuth.dockerCustomSshKey = 'sshkey';
deviceAuth.dockerCustomSshKeyPass = 'sshkeypass';

const result = await DockerAPIHelper.getDockerSshConnectionOptions(device, deviceAuth);

Expand All @@ -122,22 +121,22 @@ describe('getDockerSshConnectionOptions', () => {
forceIPv6: undefined,
host: '0.0.0.0',
port: 22,
passphrase: 'decrypted',
passphrase: 'sshkeypass-decrypted',
username: '$customUser',
privateKey: 'decrypted',
privateKey: 'sshkey-decrypted',
},
});

expect(vault.vaultDecrypt).toHaveBeenCalledTimes(2);
});

test('should handle user-password SSHType for custom Docker SSH', async () => {
deviceAuth.authType = SSHType.UserPassword;
deviceAuth.sshPwd = '!2#4%';
deviceAuth.authType = SsmAnsible.SSHType.UserPassword;
deviceAuth.sshPwd = 'sshpwd';
deviceAuth.customDockerSSH = true;
deviceAuth.dockerCustomAuthType = SSHType.UserPassword;
deviceAuth.dockerCustomAuthType = SsmAnsible.SSHType.UserPassword;
deviceAuth.dockerCustomSshUser = '$customUser';
deviceAuth.dockerCustomSshPwd = 'decrypted';
deviceAuth.dockerCustomSshPwd = 'sshcustompwd';

const result = await DockerAPIHelper.getDockerSshConnectionOptions(device, deviceAuth);

Expand All @@ -153,7 +152,7 @@ describe('getDockerSshConnectionOptions', () => {
forceIPv6: undefined,
host: '0.0.0.0',
port: 22,
password: 'decrypted',
password: 'sshcustompwd-decrypted',
username: '$customUser',
},
});
Expand Down

0 comments on commit b6b58dc

Please sign in to comment.