Skip to content

Commit

Permalink
Add spaceID
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Nov 8, 2022
1 parent 540e4b4 commit c97cd5b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
4 changes: 4 additions & 0 deletions x-pack/plugins/cases/server/client/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export class CasesClientFactory {
const services = this.createServices({
unsecuredSavedObjectsClient,
esClient: scopedClusterClient,
request,
});

const userInfo = await this.getUserInfo(request);
Expand Down Expand Up @@ -146,9 +147,11 @@ export class CasesClientFactory {
private createServices({
unsecuredSavedObjectsClient,
esClient,
request,
}: {
unsecuredSavedObjectsClient: SavedObjectsClientContract;
esClient: ElasticsearchClient;
request: KibanaRequest;
}): CasesServices {
this.validateInitialization();

Expand Down Expand Up @@ -178,6 +181,7 @@ export class CasesClientFactory {
notifications: this.options.notifications,
security: this.options.securityPluginStart,
publicBaseUrl: this.options.publicBaseUrl,
spaceId: this.options.spacesPluginStart.spacesService.getSpaceId(request),
});

return {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/cases/server/client/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export const createCasesClientMockArgs = () => {
full_name: 'Damaged Raccoon',
profile_uid: 'u_J41Oh6L9ki-Vo2tOogS8WRTENzhHurGtRc87NgEAlkc_0',
},
spaceId: 'default',
externalReferenceAttachmentTypeRegistry: createExternalReferenceAttachmentTypeRegistryMock(),
persistableStateAttachmentTypeRegistry: createPersistableStateAttachmentTypeRegistryMock(),
securityStartPlugin: securityMock.createStart(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('EmailNotificationService', () => {
security: clientArgs.securityStartPlugin,
publicBaseUrl: 'https://example.com',
notifications,
spaceId: 'default',
});
});

Expand Down Expand Up @@ -130,11 +131,43 @@ describe('EmailNotificationService', () => {
});
});

it('adds a backlink URL correctly with spaceId', async () => {
emailNotificationService = new EmailNotificationService({
logger: clientArgs.logger,
security: clientArgs.securityStartPlugin,
publicBaseUrl: 'https://example.com',
notifications,
spaceId: 'test-space',
});

await emailNotificationService.notifyAssignees({
assignees,
theCase: caseSO,
});

expect(notifications.getEmailService().sendPlainTextEmail).toHaveBeenCalledWith({
context: {
relatedObjects: [
{
id: 'mock-id-1',
namespace: undefined,
type: 'cases',
},
],
},
message:
'You are assigned to an Elastic Kibana Case.\r\n\r\nTitle: Super Bad Security Issue\r\n\r\nStatus: open\r\n\r\nSeverity: low\r\n\r\nTags: defacement\r\n\r\n\r\n\r\n[View the case details](https://example.com/s/test-space/app/security/cases/mock-id-1)',
subject: '[Elastic][Cases] Super Bad Security Issue',
to: ['damaged_raccoon@elastic.co', 'physical_dinosaur@elastic.co', 'wet_dingo@elastic.co'],
});
});

it('does not include the backlink of the publicBaseUrl is not defined', async () => {
emailNotificationService = new EmailNotificationService({
logger: clientArgs.logger,
security: clientArgs.securityStartPlugin,
notifications,
spaceId: 'default',
});

await emailNotificationService.notifyAssignees({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,38 @@ export class EmailNotificationService implements NotificationService {
private readonly logger: Logger;
private readonly notifications: NotificationsPluginStart;
private readonly security: SecurityPluginStart;
private readonly spaceId: string;
private readonly publicBaseUrl?: IBasePath['publicBaseUrl'];

constructor({
logger,
notifications,
security,
publicBaseUrl,
spaceId,
}: {
logger: Logger;
notifications: NotificationsPluginStart;
security: SecurityPluginStart;
spaceId: string;
publicBaseUrl?: IBasePath['publicBaseUrl'];
}) {
this.logger = logger;
this.notifications = notifications;
this.security = security;
this.spaceId = spaceId;
this.publicBaseUrl = publicBaseUrl;
}

private static getTitle(theCase: CaseSavedObject) {
return `[Elastic][Cases] ${theCase.attributes.title}`;
}

private static getMessage(theCase: CaseSavedObject, publicBaseUrl?: IBasePath['publicBaseUrl']) {
private static getMessage(
theCase: CaseSavedObject,
spaceId: string,
publicBaseUrl?: IBasePath['publicBaseUrl']
) {
const lineBreak = '\r\n\r\n';
let message = `You are assigned to an Elastic Kibana Case.${lineBreak}`;
message = `${message}Title: ${theCase.attributes.title}${lineBreak}`;
Expand All @@ -62,6 +70,7 @@ export class EmailNotificationService implements NotificationService {
publicBaseUrl,
caseId: theCase.id,
owner: theCase.attributes.owner,
spaceId,
});

message = `${message}${lineBreak}[View the case details](${caseUrl})`;
Expand All @@ -86,7 +95,11 @@ export class EmailNotificationService implements NotificationService {
.map((user) => user.email);

const subject = EmailNotificationService.getTitle(theCase);
const message = EmailNotificationService.getMessage(theCase, this.publicBaseUrl);
const message = EmailNotificationService.getMessage(
theCase,
this.spaceId,
this.publicBaseUrl
);

await this.notifications.getEmailService().sendPlainTextEmail({
to,
Expand Down

0 comments on commit c97cd5b

Please sign in to comment.