-
Notifications
You must be signed in to change notification settings - Fork 10k
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
Chore: break LDAP manager into smaller pieces to improve unit tests #26994
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3ab1411
break LDAP manager into smaller pieces to improve unit tests
ggazzo 132ffcc
Merge branch 'develop' into chore/ldap-tests
ggazzo 2829f33
Merge remote-tracking branch 'origin/develop' into chore/ldap-tests
ggazzo 5225ba2
Fix
ggazzo e6ecd32
Merge branch 'chore/ldap-tests' of github.com:RocketChat/Rocket.Chat …
ggazzo 4259d77
Apply suggestions from code review
ggazzo 307e087
Apply suggestions from code review
ggazzo 05b5638
Added few tests
ggazzo d2aeaa8
Merge branch 'develop' into chore/ldap-tests
tassoevan File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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
209 changes: 209 additions & 0 deletions
209
apps/meteor/ee/server/lib/ldap/copyCustomFieldsLDAP.spec.ts
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 |
---|---|---|
@@ -0,0 +1,209 @@ | ||
import type { IImportUser, ILDAPEntry } from '@rocket.chat/core-typings'; | ||
import { expect, spy } from 'chai'; | ||
|
||
import { copyCustomFieldsLDAP } from './copyCustomFieldsLDAP'; | ||
import type { Logger } from '../../../../app/logger/server'; | ||
|
||
describe('LDAP copyCustomFieldsLDAP', () => { | ||
it('should copy custom fields from ldapUser to rcUser', () => { | ||
const ldapUser = { | ||
mail: 'test@test.com', | ||
givenName: 'Test', | ||
} as unknown as ILDAPEntry; | ||
|
||
const userData = { | ||
name: 'Test', | ||
username: 'test', | ||
} as unknown as IImportUser; | ||
|
||
copyCustomFieldsLDAP( | ||
{ | ||
ldapUser, | ||
userData, | ||
syncCustomFields: true, | ||
customFieldsSettings: JSON.stringify({ | ||
mappedGivenName: { type: 'text', required: false }, | ||
}), | ||
customFieldsMap: JSON.stringify({ | ||
givenName: 'mappedGivenName', | ||
}), | ||
}, | ||
{ | ||
debug: () => undefined, | ||
error: () => undefined, | ||
} as unknown as Logger, | ||
); | ||
|
||
expect(userData).to.have.property('customFields'); | ||
expect(userData.customFields).to.be.eql({ mappedGivenName: 'Test' }); | ||
}); | ||
|
||
it('should copy custom fields from ldapUser to rcUser already having other custom fields', () => { | ||
const ldapUser = { | ||
mail: 'test@test.com', | ||
givenName: 'Test', | ||
} as unknown as ILDAPEntry; | ||
|
||
const userData = { | ||
name: 'Test', | ||
username: 'test', | ||
customFields: { | ||
custom: 'Test', | ||
}, | ||
} as unknown as IImportUser; | ||
|
||
copyCustomFieldsLDAP( | ||
{ | ||
ldapUser, | ||
userData, | ||
syncCustomFields: true, | ||
customFieldsSettings: JSON.stringify({ | ||
mappedGivenName: { type: 'text', required: false }, | ||
}), | ||
customFieldsMap: JSON.stringify({ | ||
givenName: 'mappedGivenName', | ||
}), | ||
}, | ||
{ | ||
debug: () => undefined, | ||
error: () => undefined, | ||
} as unknown as Logger, | ||
); | ||
|
||
expect(userData).to.have.property('customFields'); | ||
expect(userData.customFields).to.be.eql({ custom: 'Test', mappedGivenName: 'Test' }); | ||
}); | ||
|
||
it('should not copy custom fields from ldapUser to rcUser if syncCustomFields is false', () => { | ||
const ldapUser = { | ||
mail: 'test@test.com', | ||
givenName: 'Test', | ||
} as unknown as ILDAPEntry; | ||
|
||
const userData = { | ||
name: 'Test', | ||
username: 'test', | ||
} as unknown as IImportUser; | ||
|
||
copyCustomFieldsLDAP( | ||
{ | ||
ldapUser, | ||
userData, | ||
syncCustomFields: false, | ||
customFieldsSettings: JSON.stringify({ | ||
mappedGivenName: { type: 'text', required: false }, | ||
}), | ||
customFieldsMap: JSON.stringify({ | ||
givenName: 'mappedGivenName', | ||
}), | ||
}, | ||
{ | ||
debug: () => undefined, | ||
error: () => undefined, | ||
} as unknown as Logger, | ||
); | ||
|
||
expect(userData).to.not.have.property('customFields'); | ||
}); | ||
|
||
it('should call logger.error if customFieldsSettings is not a valid JSON', () => { | ||
const debug = spy(); | ||
const error = spy(); | ||
const ldapUser = { | ||
mail: 'test@test.com', | ||
givenName: 'Test', | ||
} as unknown as ILDAPEntry; | ||
|
||
const userData = { | ||
name: 'Test', | ||
username: 'test', | ||
} as unknown as IImportUser; | ||
|
||
copyCustomFieldsLDAP( | ||
{ | ||
ldapUser, | ||
userData, | ||
syncCustomFields: true, | ||
customFieldsSettings: `${JSON.stringify({ | ||
mappedGivenName: { type: 'text', required: false }, | ||
})}}`, | ||
customFieldsMap: JSON.stringify({ | ||
givenName: 'mappedGivenName', | ||
}), | ||
}, | ||
{ | ||
debug, | ||
error, | ||
} as unknown as Logger, | ||
); | ||
expect(error).to.have.been.called.exactly(1); | ||
}); | ||
it('should call logger.error if customFieldsMap is not a valid JSON', () => { | ||
const debug = spy(); | ||
const error = spy(); | ||
const ldapUser = { | ||
mail: 'test@test.com', | ||
givenName: 'Test', | ||
} as unknown as ILDAPEntry; | ||
|
||
const userData = { | ||
name: 'Test', | ||
username: 'test', | ||
} as unknown as IImportUser; | ||
|
||
copyCustomFieldsLDAP( | ||
{ | ||
ldapUser, | ||
userData, | ||
syncCustomFields: true, | ||
customFieldsSettings: JSON.stringify({ | ||
mappedGivenName: { type: 'text', required: false }, | ||
}), | ||
customFieldsMap: `${JSON.stringify({ | ||
givenName: 'mappedGivenName', | ||
})}}`, | ||
}, | ||
{ | ||
debug, | ||
error, | ||
} as unknown as Logger, | ||
); | ||
expect(error).to.have.been.called.exactly(1); | ||
}); | ||
|
||
it('should call logger.debug if some custom fields are not mapped but still mapping the other fields', () => { | ||
const debug = spy(); | ||
const error = spy(); | ||
const ldapUser = { | ||
mail: 'test@test.com', | ||
givenName: 'Test', | ||
} as unknown as ILDAPEntry; | ||
|
||
const userData = { | ||
name: 'Test', | ||
username: 'test', | ||
} as unknown as IImportUser; | ||
|
||
copyCustomFieldsLDAP( | ||
{ | ||
ldapUser, | ||
userData, | ||
syncCustomFields: true, | ||
customFieldsSettings: JSON.stringify({ | ||
mappedGivenName: { type: 'text', required: false }, | ||
}), | ||
customFieldsMap: JSON.stringify({ | ||
givenName: 'mappedGivenName', | ||
test: 'test', | ||
}), | ||
}, | ||
{ | ||
debug, | ||
error, | ||
} as unknown as Logger, | ||
); | ||
expect(debug).to.have.been.called.exactly(1); | ||
expect(userData).to.have.property('customFields'); | ||
expect(userData.customFields).to.be.eql({ mappedGivenName: 'Test' }); | ||
}); | ||
}); |
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.
Not a problem worth reverting for but this file should've been moved to a "server" folder now that it only works in the server.