Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kc committed Jul 26, 2023
1 parent 6ab26f7 commit 06eeee6
Show file tree
Hide file tree
Showing 50 changed files with 2,201 additions and 360 deletions.
5 changes: 1 addition & 4 deletions __tests__/getters/getCurl.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { getCurl } from '../../src/getters';
import { runOsquery, OsType } from '../../src/runOsquery';

const replacer = (key: any, value: { toString: () => any }) =>
typeof value === 'bigint' ? value.toString() : value;

jest.mock('../../src/runOsquery', () => ({
runOsquery: jest.fn(),
OsType: {
Expand All @@ -27,7 +24,7 @@ describe('getCurl', () => {
result: 'result',
},
];
mockRunOsquery.mockResolvedValue(JSON.stringify(expectedCurl, replacer));
mockRunOsquery.mockResolvedValue(JSON.stringify(expectedCurl));

await expect(getCurl()).resolves.toEqual(expectedCurl);
expect(mockRunOsquery).toHaveBeenCalledWith('SELECT * FROM curl', [
Expand Down
43 changes: 41 additions & 2 deletions __tests__/getters/getCurlCertificate.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
test('getCurlCertificate.test.ts', () => {
expect(true).toBe(true);
import { getCurlCertificate } from '../../src/getters';
import { runOsquery, OsType } from '../../src/runOsquery';

// Mock runOsquery function
jest.mock('../../src/runOsquery', () => ({
runOsquery: jest.fn(),
OsType: {
LINUX: 'linux',
DARWIN: 'darwin',
WINDOWS_NT: 'windows_nt',
},
}));

describe('getCurlCertificate', () => {
it('resolves with curl_certificate table on success', async () => {
const mockRunOsquery = runOsquery as jest.MockedFunction<typeof runOsquery>;
const expectedCurlCertificate = [
{
hostname: 'example.com',
common_name: 'Example Common Name',
organization: 'Example Organization',
// ... add all other properties as per the structure ...
pem: 'Example PEM',
},
];
mockRunOsquery.mockResolvedValue(JSON.stringify(expectedCurlCertificate));

await expect(getCurlCertificate()).resolves.toEqual(expectedCurlCertificate);
expect(mockRunOsquery).toHaveBeenCalledWith('SELECT * FROM curl_certificate', [
OsType.LINUX,
OsType.DARWIN,
OsType.WINDOWS_NT,
]);
});

it('rejects with error on command failure', async () => {
const mockRunOsquery = runOsquery as jest.MockedFunction<typeof runOsquery>;
mockRunOsquery.mockRejectedValue('Command failed');

await expect(getCurlCertificate()).rejects.toEqual('Command failed');
});
});
51 changes: 49 additions & 2 deletions __tests__/getters/getEc2InstanceMetadata.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
test('getEc2InstanceMetadata.test.ts', () => {
expect(true).toBe(true);
import { getEc2InstanceMetadata } from '../../src/getters';
import { runOsquery, OsType } from '../../src/runOsquery';

jest.mock('../../src/runOsquery', () => ({
runOsquery: jest.fn(),
OsType: {
LINUX: 'linux',
DARWIN: 'darwin',
WINDOWS_NT: 'windows_nt',
},
}));

describe('getEc2InstanceMetadata', () => {
it('resolves with ec2_instance_metadata table on success', async () => {
const mockRunOsquery = runOsquery as jest.MockedFunction<typeof runOsquery>;
const expectedEc2InstanceMetadata = [
{
instance_id: 'i-0abcd1234efgh5678',
instance_type: 't2.micro',
architecture: 'x86_64',
region: 'us-west-2',
availability_zone: 'us-west-2a',
local_hostname: 'ip-192-0-2-0',
local_ipv4: '192.0.2.0',
mac: '0A:1B:2C:3D:4E:5F',
security_groups: 'sg-0abcd1234efgh5678',
iam_arn: 'arn:aws:iam::123456789012:user/David',
ami_id: 'ami-0abcd1234efgh5678',
reservation_id: 'r-0abcd1234efgh5678',
account_id: '123456789012',
ssh_public_key: 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F...',
},
];
mockRunOsquery.mockResolvedValue(JSON.stringify(expectedEc2InstanceMetadata));

await expect(getEc2InstanceMetadata()).resolves.toEqual(expectedEc2InstanceMetadata);
expect(mockRunOsquery).toHaveBeenCalledWith('SELECT * FROM ec2_instance_metadata', [
OsType.LINUX,
OsType.DARWIN,
OsType.WINDOWS_NT,
]);
});

it('rejects with error on command failure', async () => {
const mockRunOsquery = runOsquery as jest.MockedFunction<typeof runOsquery>;
mockRunOsquery.mockRejectedValue('Command failed');

await expect(getEc2InstanceMetadata()).rejects.toEqual('Command failed');
});
});
40 changes: 38 additions & 2 deletions __tests__/getters/getEc2InstanceTags.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
test('getEc2InstanceTags.test.ts', () => {
expect(true).toBe(true);
import { getEc2InstanceTags } from '../../src/getters';
import { runOsquery, OsType } from '../../src/runOsquery';

jest.mock('../../src/runOsquery', () => ({
runOsquery: jest.fn(),
OsType: {
LINUX: 'linux',
DARWIN: 'darwin',
WINDOWS_NT: 'windows_nt',
},
}));

describe('getEc2InstanceTags', () => {
it('resolves with ec2_instance_tags table on success', async () => {
const mockRunOsquery = runOsquery as jest.MockedFunction<typeof runOsquery>;
const expectedEc2InstanceTags = [
{
instance_id: 'i-0abcd1234efgh5678',
key: 'Name',
value: 'My Instance',
},
];
mockRunOsquery.mockResolvedValue(JSON.stringify(expectedEc2InstanceTags));

await expect(getEc2InstanceTags()).resolves.toEqual(expectedEc2InstanceTags);
expect(mockRunOsquery).toHaveBeenCalledWith('SELECT * FROM ec2_instance_tags', [
OsType.LINUX,
OsType.DARWIN,
OsType.WINDOWS_NT,
]);
});

it('rejects with error on command failure', async () => {
const mockRunOsquery = runOsquery as jest.MockedFunction<typeof runOsquery>;
mockRunOsquery.mockRejectedValue('Command failed');

await expect(getEc2InstanceTags()).rejects.toEqual('Command failed');
});
});
40 changes: 38 additions & 2 deletions __tests__/getters/getEtcHosts.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
test('getEtcHosts.test.ts', () => {
expect(true).toBe(true);
import { getEtcHosts } from '../../src/getters';
import { runOsquery, OsType } from '../../src/runOsquery';

jest.mock('../../src/runOsquery', () => ({
runOsquery: jest.fn(),
OsType: {
LINUX: 'linux',
DARWIN: 'darwin',
WINDOWS_NT: 'windows_nt',
},
}));

describe('getEtcHosts', () => {
it('resolves with etc_hosts table on success', async () => {
const mockRunOsquery = runOsquery as jest.MockedFunction<typeof runOsquery>;
const expectedEtcHosts = [
{
address: '127.0.0.1',
hostnames: 'localhost',
pid_with_namespace: 0,
},
];
mockRunOsquery.mockResolvedValue(JSON.stringify(expectedEtcHosts));

await expect(getEtcHosts()).resolves.toEqual(expectedEtcHosts);
expect(mockRunOsquery).toHaveBeenCalledWith('SELECT * FROM etc_hosts', [
OsType.LINUX,
OsType.DARWIN,
OsType.WINDOWS_NT,
]);
});

it('rejects with error on command failure', async () => {
const mockRunOsquery = runOsquery as jest.MockedFunction<typeof runOsquery>;
mockRunOsquery.mockRejectedValue('Command failed');

await expect(getEtcHosts()).rejects.toEqual('Command failed');
});
});
41 changes: 39 additions & 2 deletions __tests__/getters/getEtcProtocols.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
test('getEtcProtocols.test.ts', () => {
expect(true).toBe(true);
import { getEtcProtocols } from '../../src/getters';
import { runOsquery, OsType } from '../../src/runOsquery';

jest.mock('../../src/runOsquery', () => ({
runOsquery: jest.fn(),
OsType: {
LINUX: 'linux',
DARWIN: 'darwin',
WINDOWS_NT: 'windows_nt',
},
}));

describe('getEtcProtocols', () => {
it('resolves with etc_protocols table on success', async () => {
const mockRunOsquery = runOsquery as jest.MockedFunction<typeof runOsquery>;
const expectedEtcProtocols = [
{
name: 'tcp',
number: 6,
alias: 'TCP',
comment: 'Transmission Control Protocol',
},
];
mockRunOsquery.mockResolvedValue(JSON.stringify(expectedEtcProtocols));

await expect(getEtcProtocols()).resolves.toEqual(expectedEtcProtocols);
expect(mockRunOsquery).toHaveBeenCalledWith('SELECT * FROM etc_protocols', [
OsType.LINUX,
OsType.DARWIN,
OsType.WINDOWS_NT,
]);
});

it('rejects with error on command failure', async () => {
const mockRunOsquery = runOsquery as jest.MockedFunction<typeof runOsquery>;
mockRunOsquery.mockRejectedValue('Command failed');

await expect(getEtcProtocols()).rejects.toEqual('Command failed');
});
});
42 changes: 40 additions & 2 deletions __tests__/getters/getEtcServices.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
test('getEtcServices.test.ts', () => {
expect(true).toBe(true);
import { getEtcServices } from '../../src/getters';
import { runOsquery, OsType } from '../../src/runOsquery';

jest.mock('../../src/runOsquery', () => ({
runOsquery: jest.fn(),
OsType: {
LINUX: 'linux',
DARWIN: 'darwin',
WINDOWS_NT: 'windows_nt',
},
}));

describe('getEtcServices', () => {
it('resolves with etc_services table on success', async () => {
const mockRunOsquery = runOsquery as jest.MockedFunction<typeof runOsquery>;
const expectedEtcServices = [
{
name: 'http',
port: 80,
protocol: 'tcp',
aliases: '',
comment: 'World Wide Web HTTP',
},
];
mockRunOsquery.mockResolvedValue(JSON.stringify(expectedEtcServices));

await expect(getEtcServices()).resolves.toEqual(expectedEtcServices);
expect(mockRunOsquery).toHaveBeenCalledWith('SELECT * FROM etc_services', [
OsType.LINUX,
OsType.DARWIN,
OsType.WINDOWS_NT,
]);
});

it('rejects with error on command failure', async () => {
const mockRunOsquery = runOsquery as jest.MockedFunction<typeof runOsquery>;
mockRunOsquery.mockRejectedValue('Command failed');

await expect(getEtcServices()).rejects.toEqual('Command failed');
});
});
63 changes: 61 additions & 2 deletions __tests__/getters/getFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,62 @@
test('getFile.test.ts', () => {
expect(true).toBe(true);
import { getFile } from '../../src/getters';
import { runOsquery, OsType } from '../../src/runOsquery';

jest.mock('../../src/runOsquery', () => ({
runOsquery: jest.fn(),
OsType: {
LINUX: 'linux',
DARWIN: 'darwin',
WINDOWS_NT: 'windows_nt',
},
}));

describe('getFile', () => {
it('resolves with file table on success', async () => {
const mockRunOsquery = runOsquery as jest.MockedFunction<typeof runOsquery>;
const expectedFile = [
{
path: '/path/to/file',
directory: '/path/to',
filename: 'file',
inode: 123456,
uid: 1001,
gid: 1001,
mode: 'rw-r--r--',
device: 64769,
size: 4096,
block_size: 4096,
atime: 1587152400,
mtime: 1587152400,
ctime: 1587152400,
btime: 1587152400,
hard_links: 1,
symlink: 0,
type: 'regular',
attributes: '',
volume_serial: '',
file_id: '',
file_version: '',
product_version: '',
original_filename: '',
bsd_flags: '',
pid_with_namespace: 0,
mount_namespace_id: '',
},
];
mockRunOsquery.mockResolvedValue(JSON.stringify(expectedFile));

await expect(getFile()).resolves.toEqual(expectedFile);
expect(mockRunOsquery).toHaveBeenCalledWith('SELECT * FROM file', [
OsType.LINUX,
OsType.DARWIN,
OsType.WINDOWS_NT,
]);
});

it('rejects with error on command failure', async () => {
const mockRunOsquery = runOsquery as jest.MockedFunction<typeof runOsquery>;
mockRunOsquery.mockRejectedValue('Command failed');

await expect(getFile()).rejects.toEqual('Command failed');
});
});
Loading

0 comments on commit 06eeee6

Please sign in to comment.