-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
feat: Add compatibility for MongoDB Atlas Serverless and AWS Amazon DocumentDB with collation options enableCollationCaseComparison
, convertEmailToLowercase
, convertUsernameToLowercase
#8805
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
Merged
mtrezza
merged 5 commits into
parse-community:alpha
from
E38-Software:supportMongoServerless
Nov 13, 2023
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1950772
feat: disable collation, transformEmailToLowerCase and transformUsern…
mattia1208 f292d28
Update src/Options/index.js
mattia1208 622dffa
Update src/Options/index.js
mattia1208 7bff03c
Change options name
mattia1208 c8b6129
Fix enableCollationCaseComparison, caseInsensitive and test
mattia1208 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 hidden or 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
const Config = require('../lib/Config'); | ||
const DatabaseController = require('../lib/Controllers/DatabaseController.js'); | ||
const validateQuery = DatabaseController._validateQuery; | ||
|
||
|
@@ -361,6 +362,253 @@ describe('DatabaseController', function () { | |
done(); | ||
}); | ||
}); | ||
|
||
describe('disableCollation', () => { | ||
const dummyStorageAdapter = { | ||
find: () => Promise.resolve([]), | ||
watch: () => Promise.resolve(), | ||
getAllClasses: () => Promise.resolve([]), | ||
}; | ||
|
||
beforeEach(() => { | ||
Config.get(Parse.applicationId).schemaCache.clear(); | ||
}); | ||
|
||
it('should force caseInsensitive to false with disableCollation option', async () => { | ||
const databaseController = new DatabaseController(dummyStorageAdapter, { | ||
disableCollation: true, | ||
}); | ||
const spy = spyOn(dummyStorageAdapter, 'find'); | ||
spy.and.callThrough(); | ||
await databaseController.find('SomeClass', {}, { caseInsensitive: true }); | ||
expect(spy.calls.all()[0].args[3].caseInsensitive).toEqual(false); | ||
}); | ||
|
||
it('should support caseInsensitive without disableCollation option', async () => { | ||
const databaseController = new DatabaseController(dummyStorageAdapter, {}); | ||
const spy = spyOn(dummyStorageAdapter, 'find'); | ||
spy.and.callThrough(); | ||
await databaseController.find('_User', {}, { caseInsensitive: true }); | ||
expect(spy.calls.all()[0].args[3].caseInsensitive).toEqual(true); | ||
}); | ||
|
||
it_only_db('mongo')('should create insensitive indexes without disableCollation', async () => { | ||
await reconfigureServer({ | ||
databaseURI: 'mongodb://localhost:27017/disableCollationFalse', | ||
databaseAdapter: undefined, | ||
}); | ||
const user = new Parse.User(); | ||
await user.save({ | ||
username: 'example', | ||
password: 'password', | ||
email: '[email protected]', | ||
}); | ||
const schemas = await Parse.Schema.all(); | ||
const UserSchema = schemas.find(({ className }) => className === '_User'); | ||
expect(UserSchema.indexes).toEqual({ | ||
_id_: { _id: 1 }, | ||
username_1: { username: 1 }, | ||
case_insensitive_username: { username: 1 }, | ||
case_insensitive_email: { email: 1 }, | ||
email_1: { email: 1 }, | ||
}); | ||
}); | ||
|
||
it_only_db('mongo')('should not create insensitive indexes with disableCollation', async () => { | ||
await reconfigureServer({ | ||
disableCollation: true, | ||
databaseURI: 'mongodb://localhost:27017/disableCollationTrue', | ||
databaseAdapter: undefined, | ||
}); | ||
const user = new Parse.User(); | ||
await user.save({ | ||
username: 'example', | ||
password: 'password', | ||
email: '[email protected]', | ||
}); | ||
const schemas = await Parse.Schema.all(); | ||
const UserSchema = schemas.find(({ className }) => className === '_User'); | ||
expect(UserSchema.indexes).toEqual({ | ||
_id_: { _id: 1 }, | ||
username_1: { username: 1 }, | ||
email_1: { email: 1 }, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('transformEmailToLowerCase', () => { | ||
const dummyStorageAdapter = { | ||
createObject: () => Promise.resolve({ ops: [{}] }), | ||
findOneAndUpdate: () => Promise.resolve({}), | ||
watch: () => Promise.resolve(), | ||
getAllClasses: () => | ||
Promise.resolve([ | ||
{ | ||
className: '_User', | ||
fields: { email: 'String' }, | ||
indexes: {}, | ||
classLevelPermissions: { protectedFields: {} }, | ||
}, | ||
]), | ||
}; | ||
const dates = { | ||
createdAt: { iso: undefined, __type: 'Date' }, | ||
updatedAt: { iso: undefined, __type: 'Date' }, | ||
}; | ||
|
||
it('should not transform email to lower case without transformEmailToLowerCase option on create', async () => { | ||
const databaseController = new DatabaseController(dummyStorageAdapter, {}); | ||
const spy = spyOn(dummyStorageAdapter, 'createObject'); | ||
spy.and.callThrough(); | ||
await databaseController.create('_User', { | ||
email: '[email protected]', | ||
}); | ||
expect(spy.calls.all()[0].args[2]).toEqual({ | ||
email: '[email protected]', | ||
...dates, | ||
}); | ||
}); | ||
|
||
it('should transform email to lower case with transformEmailToLowerCase option on create', async () => { | ||
const databaseController = new DatabaseController(dummyStorageAdapter, { | ||
transformEmailToLowerCase: true, | ||
}); | ||
const spy = spyOn(dummyStorageAdapter, 'createObject'); | ||
spy.and.callThrough(); | ||
await databaseController.create('_User', { | ||
email: '[email protected]', | ||
}); | ||
expect(spy.calls.all()[0].args[2]).toEqual({ | ||
email: '[email protected]', | ||
...dates, | ||
}); | ||
}); | ||
|
||
it('should not transform email to lower case without transformEmailToLowerCase option on update', async () => { | ||
const databaseController = new DatabaseController(dummyStorageAdapter, {}); | ||
const spy = spyOn(dummyStorageAdapter, 'findOneAndUpdate'); | ||
spy.and.callThrough(); | ||
await databaseController.update('_User', { id: 'example' }, { email: '[email protected]' }); | ||
expect(spy.calls.all()[0].args[3]).toEqual({ | ||
email: '[email protected]', | ||
}); | ||
}); | ||
|
||
it('should transform email to lower case with transformEmailToLowerCase option on update', async () => { | ||
const databaseController = new DatabaseController(dummyStorageAdapter, { | ||
transformEmailToLowerCase: true, | ||
}); | ||
const spy = spyOn(dummyStorageAdapter, 'findOneAndUpdate'); | ||
spy.and.callThrough(); | ||
await databaseController.update('_User', { id: 'example' }, { email: '[email protected]' }); | ||
expect(spy.calls.all()[0].args[3]).toEqual({ | ||
email: '[email protected]', | ||
}); | ||
}); | ||
|
||
it('should not find a case insensitive user by email with transformEmailToLowerCase', async () => { | ||
await reconfigureServer({ transformEmailToLowerCase: true }); | ||
const user = new Parse.User(); | ||
await user.save({ email: '[email protected]', password: 'password' }); | ||
|
||
const query = new Parse.Query(Parse.User); | ||
query.equalTo('email', '[email protected]'); | ||
const result = await query.find({ useMasterKey: true }); | ||
expect(result.length).toEqual(0); | ||
|
||
const query2 = new Parse.Query(Parse.User); | ||
query2.equalTo('email', '[email protected]'); | ||
const result2 = await query2.find({ useMasterKey: true }); | ||
expect(result2.length).toEqual(1); | ||
}); | ||
}); | ||
|
||
describe('transformUsernameToLowerCase', () => { | ||
const dummyStorageAdapter = { | ||
createObject: () => Promise.resolve({ ops: [{}] }), | ||
findOneAndUpdate: () => Promise.resolve({}), | ||
watch: () => Promise.resolve(), | ||
getAllClasses: () => | ||
Promise.resolve([ | ||
{ | ||
className: '_User', | ||
fields: { username: 'String' }, | ||
indexes: {}, | ||
classLevelPermissions: { protectedFields: {} }, | ||
}, | ||
]), | ||
}; | ||
const dates = { | ||
createdAt: { iso: undefined, __type: 'Date' }, | ||
updatedAt: { iso: undefined, __type: 'Date' }, | ||
}; | ||
|
||
it('should not transform username to lower case without transformUsernameToLowerCase option on create', async () => { | ||
const databaseController = new DatabaseController(dummyStorageAdapter, {}); | ||
const spy = spyOn(dummyStorageAdapter, 'createObject'); | ||
spy.and.callThrough(); | ||
await databaseController.create('_User', { | ||
username: 'EXAMPLE', | ||
}); | ||
expect(spy.calls.all()[0].args[2]).toEqual({ | ||
username: 'EXAMPLE', | ||
...dates, | ||
}); | ||
}); | ||
|
||
it('should transform username to lower case with transformUsernameToLowerCase option on create', async () => { | ||
const databaseController = new DatabaseController(dummyStorageAdapter, { | ||
transformUsernameToLowerCase: true, | ||
}); | ||
const spy = spyOn(dummyStorageAdapter, 'createObject'); | ||
spy.and.callThrough(); | ||
await databaseController.create('_User', { | ||
username: 'EXAMPLE', | ||
}); | ||
expect(spy.calls.all()[0].args[2]).toEqual({ | ||
username: 'example', | ||
...dates, | ||
}); | ||
}); | ||
|
||
it('should not transform username to lower case without transformUsernameToLowerCase option on update', async () => { | ||
const databaseController = new DatabaseController(dummyStorageAdapter, {}); | ||
const spy = spyOn(dummyStorageAdapter, 'findOneAndUpdate'); | ||
spy.and.callThrough(); | ||
await databaseController.update('_User', { id: 'example' }, { username: 'EXAMPLE' }); | ||
expect(spy.calls.all()[0].args[3]).toEqual({ | ||
username: 'EXAMPLE', | ||
}); | ||
}); | ||
|
||
it('should transform username to lower case with transformUsernameToLowerCase option on update', async () => { | ||
const databaseController = new DatabaseController(dummyStorageAdapter, { | ||
transformUsernameToLowerCase: true, | ||
}); | ||
const spy = spyOn(dummyStorageAdapter, 'findOneAndUpdate'); | ||
spy.and.callThrough(); | ||
await databaseController.update('_User', { id: 'example' }, { username: 'EXAMPLE' }); | ||
expect(spy.calls.all()[0].args[3]).toEqual({ | ||
username: 'example', | ||
}); | ||
}); | ||
|
||
it('should not find a case insensitive user by username with transformUsernameToLowerCase', async () => { | ||
await reconfigureServer({ transformUsernameToLowerCase: true }); | ||
const user = new Parse.User(); | ||
await user.save({ username: 'EXAMPLE', password: 'password' }); | ||
|
||
const query = new Parse.Query(Parse.User); | ||
query.equalTo('username', 'EXAMPLE'); | ||
const result = await query.find({ useMasterKey: true }); | ||
expect(result.length).toEqual(0); | ||
|
||
const query2 = new Parse.Query(Parse.User); | ||
query2.equalTo('username', 'example'); | ||
const result2 = await query2.find({ useMasterKey: true }); | ||
expect(result2.length).toEqual(1); | ||
}); | ||
}); | ||
}); | ||
|
||
function buildCLP(pointerNames) { | ||
|
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.