Skip to content

Commit

Permalink
[ML] Migrate server side Mocha tests to Jest. (#65651) (#65687)
Browse files Browse the repository at this point in the history
Migrates job validation related server side tests from Mocha to Jest.
  • Loading branch information
walterra authored May 8, 2020
1 parent 7860777 commit 35374f1
Show file tree
Hide file tree
Showing 18 changed files with 298 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/

import expect from '@kbn/expect';
import { estimateBucketSpanFactory } from '../bucket_span_estimator';
import { APICaller } from 'kibana/server';

import { ES_AGGREGATION } from '../../../common/constants/aggregation_types';

import { estimateBucketSpanFactory, BucketSpanEstimatorData } from './bucket_span_estimator';

// Mock callWithRequest with the ability to simulate returning different
// permission settings. On each call using `ml.privilegeCheck` we retrieve
Expand All @@ -14,7 +17,7 @@ import { estimateBucketSpanFactory } from '../bucket_span_estimator';
// sufficient permissions should be returned, the second time insufficient
// permissions.
const permissions = [false, true];
const callWithRequest = method => {
const callWithRequest: APICaller = (method: string) => {
return new Promise(resolve => {
if (method === 'ml.privilegeCheck') {
resolve({
Expand All @@ -28,34 +31,19 @@ const callWithRequest = method => {
return;
}
resolve({});
});
}) as Promise<any>;
};

const callWithInternalUser = () => {
const callWithInternalUser: APICaller = () => {
return new Promise(resolve => {
resolve({});
});
}) as Promise<any>;
};

// mock xpack_main plugin
function mockXpackMainPluginFactory(isEnabled = false, licenseType = 'platinum') {
return {
info: {
isAvailable: () => true,
feature: () => ({
isEnabled: () => isEnabled,
}),
license: {
getType: () => licenseType,
},
},
};
}

// mock configuration to be passed to the estimator
const formConfig = {
aggTypes: ['count'],
duration: {},
const formConfig: BucketSpanEstimatorData = {
aggTypes: [ES_AGGREGATION.COUNT],
duration: { start: 0, end: 1 },
fields: [null],
index: '',
query: {
Expand All @@ -64,58 +52,45 @@ const formConfig = {
must_not: [],
},
},
splitField: undefined,
timeField: undefined,
};

describe('ML - BucketSpanEstimator', () => {
it('call factory', () => {
expect(function() {
estimateBucketSpanFactory(callWithRequest, callWithInternalUser);
}).to.not.throwError('Not initialized.');
estimateBucketSpanFactory(callWithRequest, callWithInternalUser, false);
}).not.toThrow('Not initialized.');
});

it('call factory and estimator with security disabled', done => {
expect(function() {
const estimateBucketSpan = estimateBucketSpanFactory(
callWithRequest,
callWithInternalUser,
mockXpackMainPluginFactory()
true
);

estimateBucketSpan(formConfig).catch(catchData => {
expect(catchData).to.be('Unable to retrieve cluster setting search.max_buckets');
expect(catchData).toBe('Unable to retrieve cluster setting search.max_buckets');

done();
});
}).to.not.throwError('Not initialized.');
}).not.toThrow('Not initialized.');
});

it('call factory and estimator with security enabled and sufficient permissions.', done => {
it('call factory and estimator with security enabled.', done => {
expect(function() {
const estimateBucketSpan = estimateBucketSpanFactory(
callWithRequest,
callWithInternalUser,
mockXpackMainPluginFactory(true)
false
);
estimateBucketSpan(formConfig).catch(catchData => {
expect(catchData).to.be('Unable to retrieve cluster setting search.max_buckets');
expect(catchData).toBe('Unable to retrieve cluster setting search.max_buckets');

done();
});
}).to.not.throwError('Not initialized.');
});

it('call factory and estimator with security enabled and insufficient permissions.', done => {
expect(function() {
const estimateBucketSpan = estimateBucketSpanFactory(
callWithRequest,
callWithInternalUser,
mockXpackMainPluginFactory(true)
);

estimateBucketSpan(formConfig).catch(catchData => {
expect(catchData).to.be('Insufficient permissions to call bucket span estimation.');
done();
});
}).to.not.throwError('Not initialized.');
}).not.toThrow('Not initialized.');
});
});
17 changes: 11 additions & 6 deletions x-pack/plugins/ml/server/models/job_validation/job_validation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@

import { APICaller } from 'kibana/server';
import { TypeOf } from '@kbn/config-schema';

import { DeepPartial } from '../../../common/types/common';

import { validateJobSchema } from '../../routes/schemas/job_validation_schema';

type ValidateJobPayload = TypeOf<typeof validateJobSchema>;
import { ValidationMessage } from './messages';

export type ValidateJobPayload = TypeOf<typeof validateJobSchema>;

export function validateJob(
callAsCurrentUser: APICaller,
payload: ValidateJobPayload,
kbnVersion: string,
callAsInternalUser: APICaller,
isSecurityDisabled: boolean
): string[];
payload?: DeepPartial<ValidateJobPayload>,
kbnVersion?: string,
callAsInternalUser?: APICaller,
isSecurityDisabled?: boolean
): Promise<ValidationMessage[]>;
Loading

0 comments on commit 35374f1

Please sign in to comment.