Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user ID validation and custom error handling in ModelWithPermissions #7605

Open
wants to merge 9 commits into
base: production
Choose a base branch
from

Conversation

Joao-vi
Copy link
Collaborator

@Joao-vi Joao-vi commented Jan 21, 2025

fixes #7389

@Joao-vi Joao-vi self-assigned this Jan 21, 2025
Copy link
Member

@RafaPolit RafaPolit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some minor comments. There are some e2e failing, please rerun the failed tests until they are green. Thanks.

@@ -13,6 +13,7 @@ import {
UwaziQueryOptions,
EnforcedWithId,
} from './model';
import { ObjectId } from 'mongodb';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to adhere to the hierarchy of imports:

  • external modules
  • absolute paths
  • relative paths

@@ -127,20 +128,44 @@ const controlPermissionsData = <T>(
return { ...data, permissions: undefined };
};

export class InvalidUserIdError extends Error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure about the Error practices we defined. Are we extending native JS Error or are we using our Uwazi Error? I thought we agreed the latter, but maybe I misunderstood something?

private static validateUser(user: DataType<UserSchema> | undefined) {
try {
if (typeof user === 'undefined') return;
ObjectId.createFromHexString(user?._id?.toString()!);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it your IDE Typescript demanding all these type conditionals? If you are already returning for user === undefined, what is the possibility of here user needing the ? ? And toString() with a !. If you have _id and it is of the right class, how can toString() still require the !?

const entity = { title: 'newEntity', template: template1Id };
const { entity: savedEntity } = await saveEntity(entity, { ...reqData });

expect(savedEntity.permissions).toEqual([
{ level: 'write', refId: 'userId', type: 'user' },
{ level: 'write', refId: mockedUser._id?.toString(), type: 'user' },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under what condition can the _id of mockedUser._id can be undefined? I'm concerned about the IDE configuration and typing throughout these commit.

@@ -18,6 +18,7 @@ describe('ModelWithPermissions', () => {
permissions: { type: mongoose.Schema.Types.Mixed, select: false },
fixed: Boolean,
});
const userId = testingDB.id();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file used user1 and user2 (in line 67 for example).

Probably these are not being used in the test, but maybe it's worth it to create a:

const user1Id = testingDB.id();
const user2Id = testingDB.id();

and replicate the original structure but removing the deprecated practice of assigning strings to the user?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants