Skip to content

Commit

Permalink
Add authorization unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Apr 10, 2024
1 parent 850ea25 commit f434538
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,49 @@ describe('AlertingAuthorization', () => {
`"Unauthorized by \\"myOtherApp\\" to create \\"myType\\" alert"`
);
});

test('checks additional privileges correctly', async () => {
const { authorization } = mockSecurity();
const checkPrivileges: jest.MockedFunction<
ReturnType<typeof authorization.checkPrivilegesDynamicallyWithRequest>
> = jest.fn();
authorization.checkPrivilegesDynamicallyWithRequest.mockReturnValue(checkPrivileges);
const alertAuthorization = new AlertingAuthorization({
request,
authorization,
ruleTypeRegistry,
features,
getSpace,
getSpaceId,
});

checkPrivileges.mockResolvedValueOnce({
username: 'some-user',
hasAllRequested: true,
privileges: { kibana: [] },
});

await alertAuthorization.ensureAuthorized({
ruleTypeId: 'myType',
consumer: 'myApp',
operation: WriteOperations.Create,
entity: AlertingAuthorizationEntity.Rule,
additionalPrivileges: ['test/create'],
});

expect(ruleTypeRegistry.get).toHaveBeenCalledWith('myType');

expect(authorization.actions.alerting.get).toHaveBeenCalledTimes(1);
expect(authorization.actions.alerting.get).toHaveBeenCalledWith(
'myType',
'myApp',
'rule',
'create'
);
expect(checkPrivileges).toHaveBeenCalledWith({
kibana: [mockAuthorizationAction('myType', 'myApp', 'rule', 'create'), 'test/create'],
});
});
});

describe('getFindAuthorizationFilter', () => {
Expand Down

0 comments on commit f434538

Please sign in to comment.