Skip to content

Commit

Permalink
Polish activity log testing
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonkopliku committed Aug 2, 2024
1 parent 932ab4d commit f1f7c3f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 290 deletions.
142 changes: 13 additions & 129 deletions assets/js/common/ActivityLogDetailsModal/ActivityLogDetailModal.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,140 +4,24 @@ import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';

import { faker } from '@faker-js/faker';
import {
activityLogEntryFactory,
taggingMetadataFactory,
untaggingMetadataFactory,
} from '@lib/test-utils/factories/activityLog';
import { activityLogEntryFactory } from '@lib/test-utils/factories/activityLog';

import {
LOGIN_ATTEMPT,
RESOURCE_TAGGING,
RESOURCE_UNTAGGING,
API_KEY_GENERATION,
CHANGING_SUMA_SETTINGS,
CLEARING_SUMA_SETTINGS,
SAVING_SUMA_SETTINGS,
CLUSTER_CHECKS_EXECUTION_REQUEST,
PROFILE_UPDATE,
USER_CREATION,
USER_DELETION,
USER_MODIFICATION,
LEVEL_WARNING,
} from '@lib/model/activityLog';
import { ACTIVITY_TYPES, toLabel, toResource } from '@lib/model/activityLog';
import { toRenderedEntry } from '@common/ActivityLogOverview/ActivityLogOverview';
import ActivityLogDetailModal from '.';

describe('ActivityLogDetailModal component', () => {
const scenarios = [
{
name: LOGIN_ATTEMPT,
entry: activityLogEntryFactory.build({
type: LOGIN_ATTEMPT,
metadata: {},
}),
expectedActivityType: 'Login Attempt',
expectedResource: 'Application',
},
{
name: RESOURCE_TAGGING,
entry: activityLogEntryFactory.build({
type: RESOURCE_TAGGING,
metadata: taggingMetadataFactory.build({
resource_type: 'host',
}),
}),
expectedActivityType: 'Tag Added',
expectedResource: 'Host',
},
{
name: RESOURCE_UNTAGGING,
entry: activityLogEntryFactory.build({
type: RESOURCE_UNTAGGING,
level: LEVEL_WARNING,
metadata: untaggingMetadataFactory.build({
resource_type: 'cluster',
}),
}),
expectedActivityType: 'Tag Removed',
expectedResource: 'Cluster',
},
{
name: API_KEY_GENERATION,
entry: activityLogEntryFactory.build({
type: API_KEY_GENERATION,
}),
expectedActivityType: 'API Key Generated',
expectedResource: 'API Key',
},
{
name: SAVING_SUMA_SETTINGS,
entry: activityLogEntryFactory.build({
type: SAVING_SUMA_SETTINGS,
}),
expectedActivityType: 'SUMA Settings Saved',
expectedResource: 'SUMA Settings',
},
{
name: CHANGING_SUMA_SETTINGS,
entry: activityLogEntryFactory.build({
type: CHANGING_SUMA_SETTINGS,
}),
expectedActivityType: 'SUMA Settings Changed',
expectedResource: 'SUMA Settings',
},
{
name: CLEARING_SUMA_SETTINGS,
entry: activityLogEntryFactory.build({
type: CLEARING_SUMA_SETTINGS,
}),
expectedActivityType: 'SUMA Settings Cleared',
expectedResource: 'SUMA Settings',
},
{
name: USER_CREATION,
entry: activityLogEntryFactory.build({
type: USER_CREATION,
}),
expectedActivityType: 'User Created',
expectedResource: 'User',
userRelatedActivity: true,
},
{
name: USER_MODIFICATION,
entry: activityLogEntryFactory.build({
type: USER_MODIFICATION,
}),
expectedActivityType: 'User Modified',
expectedResource: 'User',
userRelatedActivity: true,
},
{
name: USER_DELETION,
entry: activityLogEntryFactory.build({
type: USER_DELETION,
}),
expectedActivityType: 'User Deleted',
expectedResource: 'User',
userRelatedActivity: true,
},
{
name: PROFILE_UPDATE,
entry: activityLogEntryFactory.build({
type: PROFILE_UPDATE,
}),
expectedActivityType: 'Profile Updated',
expectedResource: 'Profile',
},
{
name: CLUSTER_CHECKS_EXECUTION_REQUEST,
entry: activityLogEntryFactory.build({
type: CLUSTER_CHECKS_EXECUTION_REQUEST,
}),
expectedActivityType: 'Checks Execution Requested',
expectedResource: 'Cluster',
},
];
const scenarios = ACTIVITY_TYPES.map((activityType) => {
const entry = activityLogEntryFactory.build({
type: activityType,
});
return {
name: activityType,
entry,
expectedActivityType: toLabel(entry),
expectedResource: toResource(entry),
};
});

it.each(scenarios)(
'should render detail for activity entry `$name`',
Expand Down
184 changes: 23 additions & 161 deletions assets/js/common/ActivityLogOverview/ActivityLogOverview.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,8 @@ import { faker } from '@faker-js/faker';

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {
activityLogEntryFactory,
taggingMetadataFactory,
untaggingMetadataFactory,
} from '@lib/test-utils/factories/activityLog';
import {
LOGIN_ATTEMPT,
RESOURCE_TAGGING,
RESOURCE_UNTAGGING,
API_KEY_GENERATION,
CHANGING_SUMA_SETTINGS,
CLEARING_SUMA_SETTINGS,
SAVING_SUMA_SETTINGS,
CLUSTER_CHECKS_EXECUTION_REQUEST,
PROFILE_UPDATE,
USER_CREATION,
USER_DELETION,
USER_MODIFICATION,
LEVEL_DEBUG,
LEVEL_ERROR,
LEVEL_INFO,
LEVEL_WARNING,
} from '@lib/model/activityLog';
import { activityLogEntryFactory } from '@lib/test-utils/factories/activityLog';
import { ACTIVITY_TYPES, toMessage } from '@lib/model/activityLog';
import '@testing-library/jest-dom';

import ActivityLogOverview from '.';
Expand All @@ -43,144 +22,27 @@ describe('Activity Log Overview', () => {
expect(screen.getByText('Loading...')).toBeVisible();
});

const scenarios = [
{
name: LOGIN_ATTEMPT,
entry: activityLogEntryFactory.build({
actor: 'admin',
type: LOGIN_ATTEMPT,
level: LEVEL_DEBUG,
metadata: {},
}),
expectedUser: 'admin',
expectedMessage: 'User logged in',
expectedLevel: 'debug',
},
{
name: RESOURCE_TAGGING,
entry: activityLogEntryFactory.build({
actor: 'foo',
type: RESOURCE_TAGGING,
level: LEVEL_INFO,
metadata: taggingMetadataFactory.build({
resource_type: 'host',
added_tag: 'bar',
resource_id: 'foo-bar',
}),
}),
expectedUser: 'foo',
expectedMessage: 'Tag "bar" added to "foo-bar"',
expectedLevel: 'info',
},
{
name: RESOURCE_UNTAGGING,
entry: activityLogEntryFactory.build({
actor: 'bar',
type: RESOURCE_UNTAGGING,
level: LEVEL_WARNING,
metadata: untaggingMetadataFactory.build({
resource_type: 'cluster',
removed_tag: 'foo',
resource_id: 'bar-foo',
}),
}),
expectedUser: 'bar',
expectedMessage: 'Tag "foo" removed from "bar-foo"',
expectedLevel: 'warning',
},
{
name: API_KEY_GENERATION,
entry: activityLogEntryFactory.build({
actor: 'baz',
type: API_KEY_GENERATION,
level: LEVEL_ERROR,
}),
expectedUser: 'baz',
expectedMessage: 'API Key was generated',
expectedLevel: 'error',
},
{
name: SAVING_SUMA_SETTINGS,
entry: activityLogEntryFactory.build({
actor: 'user-1',
type: SAVING_SUMA_SETTINGS,
}),
expectedUser: 'user-1',
expectedMessage: 'SUMA Settings was saved',
},
{
name: CHANGING_SUMA_SETTINGS,
entry: activityLogEntryFactory.build({
actor: 'user-2',
type: CHANGING_SUMA_SETTINGS,
}),
expectedUser: 'user-2',
expectedMessage: 'SUMA Settings was changed',
},
{
name: CLEARING_SUMA_SETTINGS,
entry: activityLogEntryFactory.build({
actor: 'user-3',
type: CLEARING_SUMA_SETTINGS,
}),
expectedUser: 'user-3',
expectedMessage: 'SUMA Settings was cleared',
},
{
name: USER_CREATION,
entry: activityLogEntryFactory.build({
actor: 'user-4',
type: USER_CREATION,
}),
expectedUser: 'user-4',
expectedMessage: 'User was created',
},
{
name: USER_MODIFICATION,
entry: activityLogEntryFactory.build({
actor: 'user-5',
type: USER_MODIFICATION,
}),
expectedUser: 'user-5',
expectedMessage: 'User was modified',
},
{
name: USER_DELETION,
entry: activityLogEntryFactory.build({
actor: 'user-6',
type: USER_DELETION,
}),
expectedUser: 'user-6',
expectedMessage: 'User was deleted',
},
{
name: PROFILE_UPDATE,
entry: activityLogEntryFactory.build({
actor: 'user-7',
type: PROFILE_UPDATE,
}),
expectedUser: 'user-7',
expectedMessage: 'User modified profile',
},
{
name: CLUSTER_CHECKS_EXECUTION_REQUEST,
entry: activityLogEntryFactory.build({
actor: 'user-8',
type: CLUSTER_CHECKS_EXECUTION_REQUEST,
}),
expectedUser: 'user-8',
expectedMessage: 'Checks execution requested for cluster',
},
{
name: 'unknown activity type',
entry: activityLogEntryFactory.build({
actor: 'user-9',
type: 'foo_bar',
}),
expectedUser: 'user-9',
expectedMessage: 'foo_bar',
},
];
const scenarios = ACTIVITY_TYPES.map((activityType) => {
const entry = activityLogEntryFactory.build({
type: activityType,
});
const { actor, level } = entry;
return {
name: activityType,
entry,
expectedUser: actor,
expectedMessage: toMessage(entry),
expectedLevel: level,
};
}).concat({
name: 'unknown activity type',
entry: activityLogEntryFactory.build({
actor: 'user-9',
type: 'foo_bar',
}),
expectedUser: 'user-9',
expectedMessage: 'foo_bar',
});

it.each(scenarios)(
'should render log entry for activity `$name`',
Expand Down

0 comments on commit f1f7c3f

Please sign in to comment.