Skip to content

Commit

Permalink
Add SecurityService transfer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBartusek committed May 19, 2024
1 parent d6930fe commit d5aa028
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
40 changes: 40 additions & 0 deletions apps/api/src/security/security.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('SecurityService', () => {
updateRule: jest.fn(),
paginateRules: jest.fn(),
ruleExist: jest.fn(),
getOwner: jest.fn(),
};

beforeEach(async () => {
Expand All @@ -42,6 +43,10 @@ describe('SecurityService', () => {
service = module.get<SecurityService>(SecurityService);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should be defined', () => {
expect(service).toBeDefined();
});
Expand Down Expand Up @@ -116,4 +121,39 @@ describe('SecurityService', () => {
expect(mockAclService.paginateRules).toHaveBeenCalledWith(organizationId, pageQueryDto);
});
});

describe('Ownership transfer', () => {
it('should transfer organization ownership', async () => {
const organization = new Types.ObjectId();
const user = new Types.ObjectId();

mockAclService.ruleExist.mockImplementation((org, target) => {
return organization.equals(org) && target.equals(user);
});

await service.transferOwnership(organization, user);

expect(mockAclService.updateRule).toBeCalledWith(
organization,
user,
OrganizationSecurityRole.OWNER,
);
});

it('should not transfer ownership to the same user', async () => {
const organization = new Types.ObjectId();
const user = new Types.ObjectId();
mockAclService.getOwner.mockResolvedValue(user);

expect(service.transferOwnership(organization, user)).rejects.toThrow(BadRequestException);
});

it('should not transfer ownership to the user that is not part of organization', async () => {
const organization = new Types.ObjectId();
const user = new Types.ObjectId();
mockAclService.ruleExist.mockResolvedValue(false);

expect(service.transferOwnership(organization, user)).rejects.toThrow(BadRequestException);
});
});
});
2 changes: 1 addition & 1 deletion apps/api/src/security/security.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class SecurityService {
async transferOwnership(organization: Types.ObjectId, to: Types.ObjectId) {
const owner = await this.organizationAclService.getOwner(organization);

if (owner.equals(to)) {
if (owner && owner.equals(to)) {
throw new BadRequestException('Provided user is already owner of this organization');
}

Expand Down

0 comments on commit d5aa028

Please sign in to comment.