Skip to content

Commit

Permalink
test: fix of check updates plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Desvelao committed Dec 5, 2023
1 parent 9ca647e commit 54187b5
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { getInternalSavedObjectsClient, getWazuhCore } from '../../plugin-services';
import {
getInternalSavedObjectsClient,
getWazuhCore,
getWazuhCheckUpdatesServices,
} from '../../plugin-services';
import { getSavedObject } from './get-saved-object';

const mockedGetInternalObjectsClient = getInternalSavedObjectsClient as jest.Mock;
const mockedGetWazuhCore = getWazuhCore as jest.Mock;
const mockedGetInternalObjectsClient =
getInternalSavedObjectsClient as jest.Mock;
const mockedGetWazuhCheckUpdatesServices =
getWazuhCheckUpdatesServices as jest.Mock;
jest.mock('../../plugin-services');

describe('getSavedObject function', () => {
Expand All @@ -24,8 +30,13 @@ describe('getSavedObject function', () => {
mockedGetInternalObjectsClient.mockImplementation(() => ({
get: jest.fn().mockRejectedValue({ output: { statusCode: 404 } }),
}));
mockedGetWazuhCore.mockImplementation(() => ({
services: { log: jest.fn().mockImplementation(() => {}) },
mockedGetWazuhCheckUpdatesServices.mockImplementation(() => ({
logger: {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
},
}));

const response = await getSavedObject('type');
Expand All @@ -37,8 +48,13 @@ describe('getSavedObject function', () => {
mockedGetInternalObjectsClient.mockImplementation(() => ({
get: jest.fn().mockRejectedValue(new Error('getSavedObject error')),
}));
mockedGetWazuhCore.mockImplementation(() => ({
services: { log: jest.fn().mockImplementation(() => {}) },
mockedGetWazuhCheckUpdatesServices.mockImplementation(() => ({
logger: {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
},
}));

const promise = getSavedObject('type');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { getInternalSavedObjectsClient, getWazuhCore } from '../../plugin-services';
import {
getInternalSavedObjectsClient,
getWazuhCore,
getWazuhCheckUpdatesServices,
} from '../../plugin-services';
import { setSavedObject } from './set-saved-object';

const mockedGetInternalObjectsClient = getInternalSavedObjectsClient as jest.Mock;
const mockedGetWazuhCore = getWazuhCore as jest.Mock;
const mockedGetInternalObjectsClient =
getInternalSavedObjectsClient as jest.Mock;
const mockedGetWazuhCheckUpdatesServices =
getWazuhCheckUpdatesServices as jest.Mock;
jest.mock('../../plugin-services');

describe('setSavedObject function', () => {
Expand All @@ -14,11 +20,19 @@ describe('setSavedObject function', () => {
mockedGetInternalObjectsClient.mockImplementation(() => ({
create: () => ({ attributes: { hide_update_notifications: true } }),
}));
mockedGetWazuhCheckUpdatesServices.mockImplementation(() => ({
logger: {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
},
}));

const response = await setSavedObject(
'wazuh-check-updates-user-preferences',
{ hide_update_notifications: true },
'admin'
'admin',
);

expect(response).toEqual({ hide_update_notifications: true });
Expand All @@ -28,14 +42,19 @@ describe('setSavedObject function', () => {
mockedGetInternalObjectsClient.mockImplementation(() => ({
create: jest.fn().mockRejectedValue(new Error('setSavedObject error')),
}));
mockedGetWazuhCore.mockImplementation(() => ({
services: { log: jest.fn().mockImplementation(() => {}) },
mockedGetWazuhCheckUpdatesServices.mockImplementation(() => ({
logger: {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
},
}));

const promise = setSavedObject(
'wazuh-check-updates-user-preferences',
{ hide_update_notifications: true },
'admin'
'admin',
);

await expect(promise).rejects.toThrow('setSavedObject error');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { getSavedObject } from '../saved-object/get-saved-object';
import { setSavedObject } from '../saved-object/set-saved-object';
import { getWazuhCore } from '../../plugin-services';
import {
getWazuhCheckUpdatesServices,
getWazuhCore,
} from '../../plugin-services';
import { API_UPDATES_STATUS } from '../../../common/types';
import { getUpdates } from './get-updates';
import { SAVED_OBJECT_UPDATES } from '../../../common/constants';
Expand All @@ -12,6 +15,8 @@ const mockedSetSavedObject = setSavedObject as jest.Mock;
jest.mock('../saved-object/set-saved-object');

const mockedGetWazuhCore = getWazuhCore as jest.Mock;
const mockedGetWazuhCheckUpdatesServices =
getWazuhCheckUpdatesServices as jest.Mock;
jest.mock('../../plugin-services');

describe('getUpdates function', () => {
Expand Down Expand Up @@ -43,6 +48,21 @@ describe('getUpdates function', () => {
],
}));

mockedGetWazuhCore.mockImplementation(() => ({
serverAPIHostEntries: {
getHostsEntries: jest.fn(() => []),
},
}));

mockedGetWazuhCheckUpdatesServices.mockImplementation(() => ({
logger: {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
},
}));

const updates = await getUpdates();

expect(getSavedObject).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -76,7 +96,9 @@ describe('getUpdates function', () => {
mockedGetWazuhCore.mockImplementation(() => ({
controllers: {
WazuhHostsCtrl: jest.fn().mockImplementation(() => ({
getHostsEntries: jest.fn().mockImplementation(() => [{ id: 'api id' }]),
getHostsEntries: jest
.fn()
.mockImplementation(() => [{ id: 'api id' }]),
})),
},
services: {
Expand Down Expand Up @@ -108,6 +130,44 @@ describe('getUpdates function', () => {
},
}));
mockedSetSavedObject.mockImplementation(() => ({}));
mockedGetWazuhCore.mockImplementation(() => ({
api: {
client: {
asInternalUser: {
request: jest.fn().mockImplementation(() => ({
data: {
data: {
current_version: '4.3.1',
last_available_patch: {
description:
'## Manager\r\n\r\n### Fixed\r\n\r\n- Fixed a crash when overwrite rules are triggered...',
published_date: '2022-05-18T10:12:43Z',
semver: {
major: 4,
minor: 3,
patch: 8,
},
tag: 'v4.3.8',
title: 'Wazuh v4.3.8',
},
},
},
})),
},
},
},
serverAPIHostEntries: {
getHostsEntries: jest.fn(() => [{ id: 'api id' }]),
},
}));
mockedGetWazuhCheckUpdatesServices.mockImplementation(() => ({
logger: {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
},
}));

const updates = await getUpdates(true);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { getSavedObject } from '../saved-object/get-saved-object';
import { getUserPreferences } from './get-user-preferences';
import { SAVED_OBJECT_USER_PREFERENCES } from '../../../common/constants';
import { getWazuhCore } from '../../plugin-services';
import {
getWazuhCore,
getWazuhCheckUpdatesServices,
} from '../../plugin-services';

const mockedGetSavedObject = getSavedObject as jest.Mock;
jest.mock('../saved-object/get-saved-object');

const mockedGetWazuhCore = getWazuhCore as jest.Mock;
const mockedGetWazuhCheckUpdatesServices =
getWazuhCheckUpdatesServices as jest.Mock;
jest.mock('../../plugin-services');

describe('getUserPreferences function', () => {
Expand All @@ -25,10 +30,22 @@ describe('getUserPreferences function', () => {
hide_update_notifications: false,
}));

mockedGetWazuhCheckUpdatesServices.mockImplementation(() => ({
logger: {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
},
}));

const response = await getUserPreferences('admin');

expect(getSavedObject).toHaveBeenCalledTimes(1);
expect(getSavedObject).toHaveBeenCalledWith(SAVED_OBJECT_USER_PREFERENCES, 'admin');
expect(getSavedObject).toHaveBeenCalledWith(
SAVED_OBJECT_USER_PREFERENCES,
'admin',
);

expect(response).toEqual({
last_dismissed_updates: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { updateUserPreferences } from '.';
import { getSavedObject } from '../saved-object/get-saved-object';
import { setSavedObject } from '../saved-object/set-saved-object';
import { SAVED_OBJECT_USER_PREFERENCES } from '../../../common/constants';
import { getWazuhCore } from '../../plugin-services';
import {
getWazuhCore,
getWazuhCheckUpdatesServices,
} from '../../plugin-services';

const mockedGetSavedObject = getSavedObject as jest.Mock;
jest.mock('../saved-object/get-saved-object');
Expand All @@ -11,6 +14,8 @@ const mockedSetSavedObject = setSavedObject as jest.Mock;
jest.mock('../saved-object/set-saved-object');

const mockedGetWazuhCore = getWazuhCore as jest.Mock;
const mockedGetWazuhCheckUpdatesServices =
getWazuhCheckUpdatesServices as jest.Mock;
jest.mock('../../plugin-services');

describe('updateUserPreferences function', () => {
Expand All @@ -30,6 +35,14 @@ describe('updateUserPreferences function', () => {
}));

mockedSetSavedObject.mockImplementation(() => {});
mockedGetWazuhCheckUpdatesServices.mockImplementation(() => ({
logger: {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
},
}));

const response = await updateUserPreferences('admin', {
last_dismissed_updates: [
Expand All @@ -42,7 +55,10 @@ describe('updateUserPreferences function', () => {
});

expect(getSavedObject).toHaveBeenCalledTimes(1);
expect(getSavedObject).toHaveBeenCalledWith(SAVED_OBJECT_USER_PREFERENCES, 'admin');
expect(getSavedObject).toHaveBeenCalledWith(
SAVED_OBJECT_USER_PREFERENCES,
'admin',
);

expect(response).toEqual({
last_dismissed_updates: [
Expand Down

0 comments on commit 54187b5

Please sign in to comment.