Skip to content

Commit

Permalink
Add more tests to ensure cached versions of artifacts are correct
Browse files Browse the repository at this point in the history
  • Loading branch information
madirey committed Jul 7, 2020
1 parent a8216e1 commit 1456697
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,15 @@ export class ManifestManagerMock extends ManifestManager {
}

export const getManifestManagerMock = (opts?: {
cache?: ExceptionsCache;
packageConfigService?: PackageConfigServiceMock;
savedObjectsClient?: ReturnType<typeof savedObjectsClientMock.create>;
}): ManifestManagerMock => {
let cache = new ExceptionsCache(5);
if (opts?.cache !== undefined) {
cache = opts.cache;
}

let packageConfigService = getPackageConfigServiceMock();
if (opts?.packageConfigService !== undefined) {
packageConfigService = opts.packageConfigService;
Expand All @@ -99,7 +105,7 @@ export const getManifestManagerMock = (opts?: {

const manifestManager = new ManifestManagerMock({
artifactClient: getArtifactClientMock(savedObjectsClient),
cache: new ExceptionsCache(5),
cache,
// @ts-ignore
packageConfigService,
exceptionListClient: listMock.getExceptionListClient(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
*/

import { savedObjectsClientMock } from 'src/core/server/mocks';
import { ArtifactConstants, ManifestConstants, Manifest } from '../../../lib/artifacts';
import {
ArtifactConstants,
ManifestConstants,
Manifest,
ExceptionsCache,
} from '../../../lib/artifacts';
import { getPackageConfigServiceMock, getManifestManagerMock } from './manifest_manager.mock';

describe('manifest_manager', () => {
Expand All @@ -23,6 +28,43 @@ describe('manifest_manager', () => {
expect(manifestWrapper!.manifest).toBeInstanceOf(Manifest);
});

test('ManifestManager populates cache properly', async () => {
const cache = new ExceptionsCache(5);
const manifestManager = getManifestManagerMock({ cache });
const manifestWrapper = await manifestManager.refresh();
expect(manifestWrapper!.diffs).toEqual([
{
id:
'endpoint-exceptionlist-linux-1.0.0-d34a1f6659bd86fc2023d7477aa2e5d2055c9c0fb0a0f10fae76bf8b94bebe49',
type: 'add',
},
]);
const diff = manifestWrapper!.diffs[0];
const entry = JSON.parse(cache.get(diff!.id)!);
expect(entry).toEqual({
exceptions_list: [
{
entries: [
{
field: 'nested.field',
operator: 'included',
type: 'exact_cased',
value: 'some value',
},
],
field: 'some.parentField',
type: 'nested',
},
{
field: 'some.not.nested.field',
operator: 'included',
type: 'exact_cased',
value: 'some value',
},
],
});
});

test('ManifestManager can dispatch manifest', async () => {
const packageConfigService = getPackageConfigServiceMock();
const manifestManager = getManifestManagerMock({ packageConfigService });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export class ManifestManager {
const artifact = newManifest.getArtifact(diff.id);
try {
await this.artifactClient.createArtifact(artifact);

// Cache the body of the artifact
this.cache.set(diff.id, Buffer.from(artifact.body, 'base64').toString());
} catch (err) {
Expand Down
55 changes: 55 additions & 0 deletions x-pack/test/api_integration/apis/endpoint/artifacts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,61 @@ export default function (providerContext: FtrProviderContext) {
});
});

it('should download an artifact with correct hash from cache', async () => {
await supertestWithoutAuth
.get(
'/api/endpoint/artifacts/download/endpoint-exceptionlist-linux-1.0.0/d162f0302cbf419038ade7ea978e0a7ade7aad317fedefe455ff38dfa28b7cff'
)
.set('kbn-xsrf', 'xxx')
.set('authorization', `ApiKey ${agentAccessAPIKey}`)
.send()
.expect(200)
.expect((response) => {
JSON.parse(response.text);
})
.then(async () => {
await supertestWithoutAuth
.get(
'/api/endpoint/artifacts/download/endpoint-exceptionlist-linux-1.0.0/d162f0302cbf419038ade7ea978e0a7ade7aad317fedefe455ff38dfa28b7cff'
)
.set('kbn-xsrf', 'xxx')
.set('authorization', `ApiKey ${agentAccessAPIKey}`)
.send()
.expect(200)
.expect((response) => {
const artifactJson = JSON.parse(response.text);
expect(artifactJson).to.eql({
exceptions_list: [
{
field: 'actingProcess.file.signer',
operator: 'included',
type: 'exact_cased',
value: 'Elastic, N.V.',
},
{
entries: [
{
field: 'signer',
operator: 'included',
type: 'exact_cased',
value: 'Evil',
},
{
field: 'trusted',
operator: 'included',
type: 'exact_cased',
value: 'true',
},
],
field: 'file.signature',
type: 'nested',
},
],
});
});
});
});

it('should fail on invalid api key', async () => {
await supertestWithoutAuth
.get(
Expand Down

0 comments on commit 1456697

Please sign in to comment.