Skip to content

Commit

Permalink
Add functional test and use classname for styling
Browse files Browse the repository at this point in the history
  • Loading branch information
ersin-erdal committed May 16, 2022
1 parent 72598c3 commit d0c0dfc
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
button[data-test-subj='deleteRuleButton'] {
.ruleActionsPopover__deleteButton {
color: $euiColorDangerText;
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export const RuleActionsPopover: React.FunctionComponent<RuleActionsPopoverProps
},
{
disabled: !canSaveRule,
className: 'ruleActionsPopover__deleteButton',
'data-test-subj': 'deleteRuleButton',
onClick: () => {
setIsPopoverOpen(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
button[data-test-subj='deleteRule'] {
.collapsedItemActions__deleteButton {
color: $euiColorDangerText;
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export const CollapsedItemActions: React.FunctionComponent<ComponentOpts> = ({
},
{
disabled: !item.isEditable,
className: 'collapsedItemActions__deleteButton',
'data-test-subj': 'deleteRule',
onClick: () => {
setIsPopoverOpen(!isPopoverOpen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
KibanaResponseFactory,
IKibanaResponse,
Logger,
SavedObject,
} from '@kbn/core/server';
import { schema } from '@kbn/config-schema';
import { InvalidatePendingApiKey } from '@kbn/alerting-plugin/server/types';
Expand Down Expand Up @@ -364,4 +365,46 @@ export function defineRoutes(
}
}
);

router.get(
{
path: '/api/alerting/rule/{id}/_get_api_key',
validate: {
params: schema.object({
id: schema.string(),
}),
},
},
async function (
context: RequestHandlerContext,
req: KibanaRequest<any, any, any, any>,
res: KibanaResponseFactory
): Promise<IKibanaResponse<any>> {
const { id } = req.params;
const [, { encryptedSavedObjects, spaces }] = await core.getStartServices();

const spaceId = spaces ? spaces.spacesService.getSpaceId(req) : 'default';

let namespace: string | undefined;
if (spaces && spaceId) {
namespace = spaces.spacesService.spaceIdToNamespace(spaceId);
}

try {
const {
attributes: { apiKey, apiKeyOwner },
}: SavedObject<RawRule> = await encryptedSavedObjects
.getClient({
includedHiddenTypes: ['alert'],
})
.getDecryptedAsInternalUser('alert', id, {
namespace,
});

return res.ok({ body: { apiKey, apiKeyOwner } });
} catch (err) {
return res.badRequest({ body: err });
}
}
);
}
10 changes: 10 additions & 0 deletions x-pack/test/alerting_api_integration/common/lib/alert_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ export class AlertUtils {
return request;
}

public getAPIKeyRequest(ruleId: string) {
const request = this.supertestWithoutAuth.get(
`${getUrlPrefix(this.space.id)}/api/alerting/rule/${ruleId}/_get_api_key`
);
if (this.user) {
return request.auth(this.user.username, this.user.password);
}
return request;
}

public getDisableRequest(alertId: string) {
const request = this.supertestWithoutAuth
.post(`${getUrlPrefix(this.space.id)}/api/alerting/rule/${alertId}/_disable`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default function alertingTests({ loadTestFile, getService }: FtrProviderC
loadTestFile(require.resolve('./get_alert_summary'));
loadTestFile(require.resolve('./rule_types'));
loadTestFile(require.resolve('./bulk_edit'));
loadTestFile(require.resolve('./retain_api_key'));
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import expect from '@kbn/expect';
import { UserAtSpaceScenarios } from '../../../scenarios';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
import { AlertUtils, getUrlPrefix, getTestRuleData, ObjectRemover } from '../../../../common/lib';

// eslint-disable-next-line import/no-default-export
export default function retainAPIKeyTests({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const supertestWithoutAuth = getService('supertestWithoutAuth');

describe('retain api key', () => {
const objectRemover = new ObjectRemover(supertest);

after(() => objectRemover.removeAll());

for (const scenario of UserAtSpaceScenarios) {
const { user, space } = scenario;
const alertUtils = new AlertUtils({ user, space, supertestWithoutAuth });

describe(scenario.id, () => {
it('should retain the api key when a rule is disabled and then enabled', async () => {
const { body: createdConnector } = await supertest
.post(`${getUrlPrefix(space.id)}/api/actions/connector`)
.set('kbn-xsrf', 'foo')
.send({
name: 'MY action',
connector_type_id: 'test.noop',
config: {},
secrets: {},
})
.expect(200);

const { body: createdRule } = await supertest
.post(`${getUrlPrefix(space.id)}/api/alerting/rule`)
.set('kbn-xsrf', 'foo')
.send(
getTestRuleData({
actions: [
{
id: createdConnector.id,
group: 'default',
params: {},
},
],
})
)
.expect(200);
objectRemover.add(space.id, createdRule.id, 'rule', 'alerting');

const {
body: { apiKey, apiKeyOwner },
} = await alertUtils.getAPIKeyRequest(createdRule.id);

await alertUtils.getDisableRequest(createdRule.id);

const {
body: { apiKey: apiKeyAfterDisable, apiKeyOwner: apiKeyOwnerAfterDisable },
} = await alertUtils.getAPIKeyRequest(createdRule.id);

switch (scenario.id) {
case 'no_kibana_privileges at space1':
case 'space_1_all at space2':
case 'global_read at space1':
case 'space_1_all_alerts_none_actions at space1':
case 'superuser at space1':
case 'space_1_all at space1':
case 'space_1_all_with_restricted_fixture at space1':
expect(apiKey).to.be(apiKeyAfterDisable);
expect(apiKeyOwner).to.be(apiKeyOwnerAfterDisable);
break;
default:
throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`);
}

await alertUtils.getEnableRequest(createdRule.id);

const {
body: { apiKey: apiKeyAfterEnable, apiKeyOwner: apiKeyOwnerAfterEnable },
} = await alertUtils.getAPIKeyRequest(createdRule.id);

switch (scenario.id) {
case 'no_kibana_privileges at space1':
case 'space_1_all at space2':
case 'global_read at space1':
case 'space_1_all_alerts_none_actions at space1':
case 'superuser at space1':
case 'space_1_all at space1':
case 'space_1_all_with_restricted_fixture at space1':
expect(apiKey).to.be(apiKeyAfterEnable);
expect(apiKeyOwner).to.be(apiKeyOwnerAfterEnable);
break;
default:
throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`);
}
});
});
}
});
}

0 comments on commit d0c0dfc

Please sign in to comment.