Skip to content

Commit

Permalink
Add tests for unknown parent and context types
Browse files Browse the repository at this point in the history
  • Loading branch information
bischofmax committed May 28, 2024
1 parent 462b8d6 commit f9a684c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 27 deletions.
100 changes: 74 additions & 26 deletions apps/server/src/modules/sharing/uc/share-token.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,28 @@ describe('ShareTokenUC', () => {
});
});

describe('when parent type is not allowed', () => {
const setup = () => {
const user = userFactory.buildWithId();
authorizationService.getUserWithPermissions.mockResolvedValueOnce(user);

return { user };
};

it('should throw', async () => {
const { user } = setup();

await expect(
uc.createShareToken(user.id, {
parentId: '123',
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
parentType: 'unknown-type',
})
).rejects.toThrow(new NotImplementedException('Import Feature not implemented'));
});
});

describe('when restricted to same school', () => {
const setup = () => {
const schoolEntity = schoolEntityFactory.buildWithId();
Expand Down Expand Up @@ -698,42 +720,68 @@ describe('ShareTokenUC', () => {
});

describe('when restricted to same school', () => {
const setup = () => {
const schoolEntity = schoolEntityFactory.buildWithId();
const school = schoolFactory.build();
const user = userFactory.buildWithId({ school: schoolEntity });
const shareToken = shareTokenFactory.build({
context: { contextType: ShareTokenContextType.School, contextId: schoolEntity.id },
});
const parentName = 'name';
describe('when context type is school', () => {
const setup = () => {
const schoolEntity = schoolEntityFactory.buildWithId();
const school = schoolFactory.build();
const user = userFactory.buildWithId({ school: schoolEntity });
const shareToken = shareTokenFactory.build({
context: { contextType: ShareTokenContextType.School, contextId: schoolEntity.id },
});
const parentName = 'name';

service.lookupTokenWithParentName.mockResolvedValueOnce({ shareToken, parentName });
authorizationService.getUserWithPermissions.mockResolvedValueOnce(user);
authorizationService.getUserWithPermissions.mockResolvedValueOnce(user);

schoolService.getSchoolById.mockResolvedValueOnce(school);

return { user, shareToken, school };
};

service.lookupTokenWithParentName.mockResolvedValueOnce({ shareToken, parentName });
authorizationService.getUserWithPermissions.mockResolvedValueOnce(user);
authorizationService.getUserWithPermissions.mockResolvedValueOnce(user);
it('should check look up permissions', async () => {
const { user, shareToken } = setup();

schoolService.getSchoolById.mockResolvedValueOnce(school);
await uc.lookupShareToken(user.id, shareToken.token);

return { user, shareToken, school };
};
expect(authorizationService.checkAllPermissions).toHaveBeenCalledWith(user, [Permission.COURSE_CREATE]);
});

it('should check look up permissions', async () => {
const { user, shareToken } = setup();
it('should check context read permission', async () => {
const { user, shareToken, school } = setup();

await uc.lookupShareToken(user.id, shareToken.token);
await uc.lookupShareToken(user.id, shareToken.token);

expect(authorizationService.checkAllPermissions).toHaveBeenCalledWith(user, [Permission.COURSE_CREATE]);
expect(authorizationService.checkPermission).toHaveBeenCalledWith(
user,
school,
AuthorizationContextBuilder.read([])
);
});
});

it('should check context read permission', async () => {
const { user, shareToken, school } = setup();
describe('when context type is unknown', () => {
const setup = () => {
const schoolEntity = schoolEntityFactory.buildWithId();
const user = userFactory.buildWithId({ school: schoolEntity });
const shareToken = shareTokenFactory.build({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
context: { contextType: 'unknown', contextId: schoolEntity.id },
});
const parentName = 'name';

await uc.lookupShareToken(user.id, shareToken.token);
service.lookupTokenWithParentName.mockResolvedValueOnce({ shareToken, parentName });
authorizationService.getUserWithPermissions.mockResolvedValueOnce(user);

expect(authorizationService.checkPermission).toHaveBeenCalledWith(
user,
school,
AuthorizationContextBuilder.read([])
);
return { user, shareToken };
};

it('should throw', async () => {
const { user, shareToken } = setup();

await expect(uc.lookupShareToken(user.id, shareToken.token)).rejects.toThrow(NotImplementedException);
});
});
});

Expand Down
1 change: 0 additions & 1 deletion apps/server/src/modules/sharing/uc/share-token.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ export class ShareTokenUC {
await this.checkColumnBoardWritePermission(user, payload.parentId, Permission.COURSE_EDIT);
break;
default:
throw new NotImplementedException();
}
}

Expand Down

0 comments on commit f9a684c

Please sign in to comment.