Skip to content

Commit

Permalink
Adds a test
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Ho <[email protected]>
  • Loading branch information
derek-ho committed Jul 24, 2024
1 parent d0488dd commit 8022171
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 2 deletions.
2 changes: 1 addition & 1 deletion public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import {
getDataSourceFromUrl,
} from './utils/datasource-utils';

async function hasApiPermission(core: CoreSetup): Promise<boolean | undefined> {
export async function hasApiPermission(core: CoreSetup): Promise<boolean | undefined> {
try {
const permissions = await core.http.get(API_ENDPOINT_PERMISSIONS_INFO);
return permissions.has_api_access || false;
Expand Down
199 changes: 199 additions & 0 deletions public/test/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* Copyright OpenSearch Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { coreMock } from '../../../../src/core/public/mocks';
import { SecurityPlugin } from '../plugin.ts';
import * as pluginModule from '../plugin'; // Import the entire module to mock specific functions

// Mock the hasApiPermission function
jest.mock('../plugin', () => {
const originalModule = jest.requireActual('../plugin');
return {
...originalModule,
hasApiPermission: jest.fn(), // Mock the function here
};
});

describe('SecurityPlugin', () => {
let plugin;
let coreSetup;
let coreStart;
let initializerContext;
let deps;

beforeEach(() => {
coreSetup = coreMock.createSetup();
coreStart = coreMock.createStart();
initializerContext = {
config: {
get: jest.fn().mockReturnValue({
readonly_mode: { roles: [] },
multitenancy: { enabled: true, enable_aggregation_view: false },
clusterPermissions: { include: [] },
indexPermissions: { include: [] },
disabledTransportCategories: { exclude: [] },
disabledRestCategories: { exclude: [] },
ui: { autologout: false },
}),
},
};
deps = {
dataSource: { dataSourceEnabled: true },
savedObjectsManagement: { createSetup: jest.fn() },
};
});

it('does not call register function for certain applications when getNavGroupEnabled is off', async () => {
// Mock hasApiPermission to return false
pluginModule.hasApiPermission.mockResolvedValue(false); // Access the mock via the imported module

// Instantiate the plugin after mocking
plugin = new SecurityPlugin(initializerContext);

// Override getNavGroupEnabled to return false
coreSetup.chrome.navGroup = {
...coreSetup.chrome.navGroup,
getNavGroupEnabled: () => false,
};
// Mock the core.application.register function
const registerSpy = jest.spyOn(coreSetup.application, 'register');

// Execute the setup function
await plugin.setup(coreSetup, deps);

// Assert that the register function was not called for specific applications
const registeredApps = registerSpy.mock.calls.map((call) => call[0].id);
const expectedApps = [
'security-dashboards-plugin_getstarted',
'security-dashboards-plugin_auth',
'security-dashboards-plugin_roles',
'security-dashboards-plugin_users',
'security-dashboards-plugin_permissions',
'security-dashboards-plugin_tenants',
'security-dashboards-plugin_auditlog',
];

expectedApps.forEach((app) => {
expect(registeredApps).not.toContain(app);
});
});

it('calls register function for certain applications when getNavGroupEnabled is on', async () => {
// Mock hasApiPermission to return true
pluginModule.hasApiPermission.mockResolvedValue(true); // Access the mock via the imported module

// Instantiate the plugin after mocking
plugin = new SecurityPlugin(initializerContext);

// Override getNavGroupEnabled to return true
coreSetup.chrome.navGroup = {
...coreSetup.chrome.navGroup,
getNavGroupEnabled: () => true,
};
// Mock the core.application.register function
const registerSpy = jest.spyOn(coreSetup.application, 'register');

// Execute the setup function
await plugin.setup(coreSetup, deps);

// Assert that the register function was called for specific applications
const registeredApps = registerSpy.mock.calls.map((call) => call[0].id);
const expectedApps = [
'security-dashboards-plugin_getstarted',
'security-dashboards-plugin_auth',
'security-dashboards-plugin_roles',
'security-dashboards-plugin_users',
'security-dashboards-plugin_permissions',
'security-dashboards-plugin_tenants',
'security-dashboards-plugin_auditlog',
];

expectedApps.forEach((app) => {
expect(registeredApps).toContain(app);
});
});

it('does not call register function for tenant app when multitenancy is off', async () => {
// Mock hasApiPermission to return true
pluginModule.hasApiPermission.mockResolvedValue(true);

// InitializerContext with multitenancy disabled
initializerContext = {
config: {
get: jest.fn().mockReturnValue({
readonly_mode: { roles: [] },
multitenancy: { enabled: false, enable_aggregation_view: false },
clusterPermissions: { include: [] },
indexPermissions: { include: [] },
disabledTransportCategories: { exclude: [] },
disabledRestCategories: { exclude: [] },
ui: { autologout: false },
}),
},
};

// Instantiate the plugin after mocking
plugin = new SecurityPlugin(initializerContext);

// Override getNavGroupEnabled to return true
coreSetup.chrome.navGroup = {
...coreSetup.chrome.navGroup,
getNavGroupEnabled: () => true,
};
// Mock the core.application.register function
const registerSpy = jest.spyOn(coreSetup.application, 'register');

// Execute the setup function
await plugin.setup(coreSetup, deps);

// Assert that the register function was not called for tenancy app
const registeredApps = registerSpy.mock.calls.map((call) => call[0].id);
const expectedApps = ['security-dashboards-plugin_tenants'];

expectedApps.forEach((app) => {
expect(registeredApps).not.toContain(app);
});
});
});
6 changes: 5 additions & 1 deletion public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import {
SavedObjectsManagementPluginStart,
} from '../../../src/plugins/saved_objects_management/public';
import { ManagementOverViewPluginSetup } from '../../../src/plugins/management_overview/public';
import { DataSourcePluginStart } from '../../../src/plugins/data_source/public/types';
import {
DataSourcePluginSetup,
DataSourcePluginStart,
} from '../../../src/plugins/data_source/public/types';
import { DataSourceManagementPluginSetup } from '../../../src/plugins/data_source_management/public';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand All @@ -31,6 +34,7 @@ export interface SecurityPluginSetupDependencies {
savedObjectsManagement: SavedObjectsManagementPluginSetup;
managementOverview?: ManagementOverViewPluginSetup;
dataSourceManagement?: DataSourceManagementPluginSetup;
dataSource?: DataSourcePluginSetup;
}

export interface Cluster {
Expand Down

0 comments on commit 8022171

Please sign in to comment.