Skip to content

Commit

Permalink
tests for turnOffPolicyProtections()
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-tavares committed Aug 8, 2023
1 parent 9c7b859 commit d263339
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import { ALL_APP_FEATURE_KEYS } from '../../../common';
import { turnOffPolicyProtections } from './turn_off_policy_protections';
import { FleetPackagePolicyGenerator } from '../../../common/endpoint/data_generators/fleet_package_policy_generator';
import type { PolicyData } from '../../../common/endpoint/types';
import { policyFactoryWithoutPaidFeatures } from '../../../common/endpoint/models/policy_config';
import type { PackagePolicyClient } from '@kbn/fleet-plugin/server';
import type { PromiseResolvedValue } from '../../../common/endpoint/types/utility_types';
import { setPolicyToEventCollectionOnly } from '../../../common/endpoint/models/policy_config_helpers';

describe('Turn Off Policy Protections Migration', () => {
let esClient: ElasticsearchClient;
Expand Down Expand Up @@ -59,7 +59,9 @@ describe('Turn Off Policy Protections Migration', () => {
return policy;
}

policy.inputs[0].config.policy.value = policyFactoryWithoutPaidFeatures();
policy.inputs[0].config.policy.value = setPolicyToEventCollectionOnly(
policy.inputs[0].config.policy.value
);

return policy;
};
Expand Down Expand Up @@ -100,7 +102,7 @@ describe('Turn Off Policy Protections Migration', () => {
});

bulkUpdateResponse = {
updatedPolicies: [page1Items[1], page2Items[0]],
updatedPolicies: [page1Items[0], page2Items[1]],
failedPolicies: [],
};

Expand All @@ -113,9 +115,37 @@ describe('Turn Off Policy Protections Migration', () => {
await callTurnOffPolicyProtections();

expect(fleetServices.packagePolicy.list as jest.Mock).toHaveBeenCalledTimes(3);
expect(fleetServices.packagePolicy.bulkUpdate as jest.Mock).toHaveBeenCalledWith(
fleetServices.internalSoClient,
esClient,
[
expect.objectContaining({ id: bulkUpdateResponse.updatedPolicies![0].id }),
expect.objectContaining({ id: bulkUpdateResponse.updatedPolicies![1].id }),
],
{ user: { username: 'elastic' } }
);
expect(logger.info).toHaveBeenCalledWith(
'Found 2 policies that need updates:\n' +
`Policy [${bulkUpdateResponse.updatedPolicies![0].id}][${
bulkUpdateResponse.updatedPolicies![0].name
}] updated to disable protections. Trigger: [property [mac.malware.mode] is set to [prevent]]\n` +
`Policy [${bulkUpdateResponse.updatedPolicies![1].id}][${
bulkUpdateResponse.updatedPolicies![1].name
}] updated to disable protections. Trigger: [property [mac.malware.mode] is set to [prevent]]`
);
expect(logger.info).toHaveBeenCalledWith('Done. All updates applied successfully');
});

// FIXME:PT working here
expect(true).toBe(false);
it('should log failures', async () => {
bulkUpdateResponse.failedPolicies.push({
error: new Error('oh oh'),
packagePolicy: bulkUpdateResponse.updatedPolicies![0],
});
await callTurnOffPolicyProtections();

expect(logger.error).toHaveBeenCalledWith(
expect.stringContaining('Done. 1 out of 2 failed to update:')
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const turnOffPolicyProtections = async (
} while (hasMoreData);

if (updates.length > 0) {
log.info(`Found ${updates.length} policies that need updates`);
log.info(`Found ${updates.length} policies that need updates:\n${messages.join('\n')}`);

const bulkUpdateResponse = await fleetServices.packagePolicy.bulkUpdate(
internalSoClient,
Expand All @@ -92,18 +92,12 @@ export const turnOffPolicyProtections = async (

if (bulkUpdateResponse.failedPolicies.length > 0) {
log.error(
`Done. ${
bulkUpdateResponse.failedPolicies.length
} out of ${total} failed to update:\n${JSON.stringify(
bulkUpdateResponse.failedPolicies,
null,
2
)}`
`Done. ${bulkUpdateResponse.failedPolicies.length} out of ${
updates.length
} failed to update:\n${JSON.stringify(bulkUpdateResponse.failedPolicies, null, 2)}`
);
} else {
log.info(
`Done. The following ${updates.length} updates were applied:\n${messages.join('\n')}`
);
log.info(`Done. All updates applied successfully`);
}
} else {
log.info(`Done. Checked ${total} policies and no updates needed`);
Expand Down

0 comments on commit d263339

Please sign in to comment.