-
Notifications
You must be signed in to change notification settings - Fork 81
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
Joao-vi
wants to merge
14
commits into
production
Choose a base branch
from
refactor/add-user-id-validation-to-permissions-model
base: production
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+66
−19
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b32a91d
Add user ID validation and custom error handling in ModelWithPermissions
Joao-vi b88a824
Refactor save methods in ModelWithPermissions to validate user before…
Joao-vi ad127cf
Update ModelWithPermissions tests to use dynamic user ID and remove i…
Joao-vi 2cf50f5
Remove user validation from saveMultiple method in ModelWithPermissions
Joao-vi f0acbad
Update ModelWithPermissions tests to use testingDB for user ID genera…
Joao-vi b90a4b8
Add user context mocking for entity creation tests in entitySavingMan…
Joao-vi 786c836
Refactor validateUser method to be static in ModelWithPermissions
Joao-vi bf22936
Merge branch 'production' into refactor/add-user-id-validation-to-per…
Joao-vi 8bea8d1
Merge branch 'production' into refactor/add-user-id-validation-to-per…
daneryl c062c2b
Remove duplicate import of ObjectId in ModelWithPermissions.ts
Joao-vi e05fba7
Fix user ID validation to check for undefined _id property before con…
Joao-vi 6a4bb59
Fix permissions test to ensure _id is defined before conversion
Joao-vi 373d2e0
Update ModelWithPermissions tests to use unique user IDs for permissions
Joao-vi b8c6459
Merge branch 'production' of github.com:huridocs/uwazi into refactor/…
Joao-vi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ describe('ModelWithPermissions', () => { | |
permissions: { type: mongoose.Schema.Types.Mixed, select: false }, | ||
fixed: Boolean, | ||
}); | ||
const userId1 = testingDB.id(); | ||
const userId2 = testingDB.id(); | ||
const readDocId = testingDB.id(); | ||
const writeDocId = testingDB.id(); | ||
const writeDoc2Id = testingDB.id(); | ||
|
@@ -34,19 +36,25 @@ describe('ModelWithPermissions', () => { | |
_id: readDocId, | ||
name: 'readDoc', | ||
published: false, | ||
permissions: [{ refId: 'user1', type: PermissionType.USER, level: AccessLevels.READ }], | ||
permissions: [ | ||
{ refId: userId1.toString(), type: PermissionType.USER, level: AccessLevels.READ }, | ||
], | ||
fixed: true, | ||
}, | ||
{ | ||
_id: writeDocId, | ||
name: 'writeDoc', | ||
permissions: [{ refId: 'user1', type: PermissionType.USER, level: AccessLevels.WRITE }], | ||
permissions: [ | ||
{ refId: userId1.toString(), type: PermissionType.USER, level: AccessLevels.WRITE }, | ||
], | ||
fixed: true, | ||
}, | ||
{ | ||
_id: writeDoc2Id, | ||
name: 'writeDoc2', | ||
permissions: [{ refId: 'user1', type: PermissionType.USER, level: AccessLevels.WRITE }], | ||
permissions: [ | ||
{ refId: userId1.toString(), type: PermissionType.USER, level: AccessLevels.WRITE }, | ||
], | ||
fixed: true, | ||
}, | ||
{ | ||
|
@@ -58,7 +66,9 @@ describe('ModelWithPermissions', () => { | |
{ | ||
_id: otherOwnerId, | ||
name: 'no shared with user', | ||
permissions: [{ refId: 'user2', type: PermissionType.USER, level: AccessLevels.WRITE }], | ||
permissions: [ | ||
{ refId: userId2.toString(), type: PermissionType.USER, level: AccessLevels.WRITE }, | ||
], | ||
fixed: true, | ||
}, | ||
{ | ||
|
@@ -81,7 +91,9 @@ describe('ModelWithPermissions', () => { | |
{ | ||
_id: deleteDocId, | ||
name: 'docToDelete', | ||
permissions: [{ refId: 'user1', type: PermissionType.USER, level: AccessLevels.WRITE }], | ||
permissions: [ | ||
{ refId: userId1.toString(), type: PermissionType.USER, level: AccessLevels.WRITE }, | ||
], | ||
}, | ||
{ | ||
_id: public2Id, | ||
|
@@ -105,7 +117,7 @@ describe('ModelWithPermissions', () => { | |
describe('collaborator user', () => { | ||
beforeAll(async () => { | ||
jest.spyOn(permissionsContext, 'getUserInContext').mockReturnValue({ | ||
_id: 'user1', | ||
_id: userId1, | ||
username: 'User 1', | ||
email: '[email protected]', | ||
role: 'collaborator', | ||
|
@@ -229,10 +241,11 @@ describe('ModelWithPermissions', () => { | |
|
||
it('should add the user in the permissions property of the new doc', async () => { | ||
const saved = await model.save({ name: 'newDoc' }); | ||
|
||
expect(saved).toEqual( | ||
expect.objectContaining({ | ||
name: 'newDoc', | ||
permissions: [{ refId: 'user1', type: 'user', level: 'write' }], | ||
permissions: [{ refId: userId1.toString(), type: 'user', level: 'write' }], | ||
}) | ||
); | ||
}); | ||
|
@@ -303,11 +316,11 @@ describe('ModelWithPermissions', () => { | |
expect(saved).toMatchObject([ | ||
{ | ||
name: 'newDoc', | ||
permissions: [{ refId: 'user1', type: 'user', level: 'write' }], | ||
permissions: [{ refId: userId1.toString(), type: 'user', level: 'write' }], | ||
}, | ||
{ | ||
name: 'newDoc2', | ||
permissions: [{ refId: 'user1', type: 'user', level: 'write' }], | ||
permissions: [{ refId: userId1.toString(), type: 'user', level: 'write' }], | ||
}, | ||
]); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { permissionsContext } from 'api/permissions/permissionsContext'; | |
import { UserSchema } from 'shared/types/userType'; | ||
import { UserRole } from 'shared/types/userSchema'; | ||
import { DataType } from 'api/odm'; | ||
import { ObjectId } from 'mongodb'; | ||
|
||
export class UserInContextMockFactory { | ||
spy: jest.SpyInstance | undefined; | ||
|
@@ -11,12 +12,14 @@ export class UserInContextMockFactory { | |
} | ||
|
||
mockEditorUser() { | ||
this.mock({ | ||
_id: 'userId', | ||
const user = { | ||
_id: new ObjectId(), | ||
role: UserRole.EDITOR, | ||
username: 'editorUser', | ||
email: '[email protected]', | ||
}); | ||
}; | ||
this.mock(user); | ||
return user; | ||
} | ||
|
||
restore() { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, is there any code example of this so I can replicate same behavior here ?