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

feat: Add customizable log level for 'username already exists' error #9336

Open
wants to merge 23 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c3e9106
feat: Add customizable log level for 'username already exists' error
KrishDave1 Oct 10, 2024
a54e2c6
feat: Made the suggested changes
KrishDave1 Oct 11, 2024
ae26647
fix: Added 2 log levels for Testing and followed new ParseServerConfi…
KrishDave1 Oct 12, 2024
8db9580
Merge branch 'alpha' into feat-branch
mtrezza Oct 12, 2024
da1785c
fix: Added a loop in test to remove redundant code
KrishDave1 Oct 12, 2024
1bedc3c
Merge branch 'feat-branch' of https://github.com/KrishDave1/parse-ser…
KrishDave1 Oct 12, 2024
63b4f9b
feat: Added the both the tests inside one testcase
KrishDave1 Oct 12, 2024
d582e56
Simplified the code further
KrishDave1 Oct 12, 2024
6c21038
refactor
mtrezza Oct 13, 2024
a557e78
consider default level
mtrezza Oct 13, 2024
0a5aa42
fix typo
mtrezza Oct 13, 2024
f537c2c
add warnjs
mtrezza Oct 13, 2024
8e4ac7c
Merge branch 'alpha' into feat-branch
mtrezza Oct 13, 2024
5ec7c82
Removed the extra logger from RestWrite.js
KrishDave1 Oct 15, 2024
a14f5f2
Merge branch 'alpha' into feat-branch
mtrezza Oct 15, 2024
386d2c1
fix: Usernames will always be unique for testing
KrishDave1 Oct 16, 2024
f028be7
Merge branch 'feat-branch' of https://github.com/KrishDave1/parse-ser…
KrishDave1 Oct 16, 2024
dff8140
Merge branch 'alpha' into feat-branch
mtrezza Oct 16, 2024
3587fb8
un-fdescribe
mtrezza Oct 16, 2024
9f3c82a
fit
mtrezza Oct 16, 2024
f1bc586
refactor
mtrezza Oct 16, 2024
2a03838
Check again for info
KrishDave1 Oct 16, 2024
3451ebf
Merge branch 'alpha' into feat-branch
mtrezza Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions spec/ParseUser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4430,3 +4430,39 @@ describe('allowClientClassCreation option', () => {
defaultConfiguration.allowClientClassCreation = true;
});
});

describe('log levels', () => {
const logLevels = ['info', 'warn', 'error'];
const testLogLevels = ['info'];//[undefined, 'silent', 'info', 'warn', 'error'];

it_id('bd3929eb-85dd-4955-ac1d-5ba59ab1b9a3')(fit)('should use log level for username already exists error', async () => {
for (const testLogLevel of testLogLevels) {
await reconfigureServer({
logLevels: {
usernameAlreadyExists: testLogLevel,
},
});

// Set up logger spies
const logger = require('../lib/logger').logger;
logLevels.forEach(level => spyOn(logger, level).and.callFake(() => {}));

// Invoke error
const uniqueUsername = `user_${Date.now()}`;
await Parse.User.signUp(uniqueUsername, 'pass');
await expectAsync(Parse.User.signUp(uniqueUsername, 'pass')).toBeRejectedWith(
new Parse.Error(
Parse.Error.USERNAME_TAKEN,
'Account already exists for this username.'
)
);

// Verify log outputs
logLevels.forEach(level => {
const levelOrDefault = testLogLevel || 'error';
expect(logger[level]).toHaveBeenCalledTimes(level === levelOrDefault ? 1 : 0);
logger[level].calls.reset();
});
}
});
});
1 change: 1 addition & 0 deletions spec/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ if (silent) {
triggerAfter: 'silent',
triggerBeforeError: 'silent',
triggerBeforeSuccess: 'silent',
usernameAlreadyExists: 'silent',
};
}

Expand Down
6 changes: 6 additions & 0 deletions src/Options/Definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1117,4 +1117,10 @@ module.exports.LogLevels = {
'Log level used by the Cloud Code Triggers `beforeSave`, `beforeDelete`, `beforeFind`, `beforeLogin` on success. Default is `info`.',
default: 'info',
},
usernameAlreadyExists: {
env: 'PARSE_SERVER_LOG_LEVELS_USERNAME_ALREADY_EXISTS',
help:
'Log level for the username already exists error when trying to sign up. Default is `error`.',
default: 'error',
},
};
1 change: 1 addition & 0 deletions src/Options/docs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/Options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,8 @@ export interface LogLevels {
:DEFAULT: error
*/
cloudFunctionError: ?string;
/* Log level for the username already exists error when trying to sign up. Default is `error`.
:DEFAULT: error
*/
usernameAlreadyExists: ?string;
}
19 changes: 16 additions & 3 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,10 +739,11 @@ RestWrite.prototype._validateUserName = function () {
)
.then(results => {
if (results.length > 0) {
throw new Parse.Error(
const usernameError = new Parse.Error(
Parse.Error.USERNAME_TAKEN,
'Account already exists for this username.'
);
throw usernameError;
}
return;
});
Expand Down Expand Up @@ -1557,10 +1558,18 @@ RestWrite.prototype.runDatabaseOperation = function () {

// Quick check, if we were able to infer the duplicated field name
if (error && error.userInfo && error.userInfo.duplicated_field === 'username') {
throw new Parse.Error(
const usernameError = new Parse.Error(
Parse.Error.USERNAME_TAKEN,
'Account already exists for this username.'
);
if (this.config.logLevels.usernameAlreadyExists === 'info') {
logger.info(JSON.stringify(usernameError));
} else if (this.config.logLevels.usernameAlreadyExists === 'warn') {
logger.warn(JSON.stringify(usernameError));
} else if (this.config.logLevels.usernameAlreadyExists === 'error') {
logger.error(JSON.stringify(usernameError));
}
throw usernameError;
}

if (error && error.userInfo && error.userInfo.duplicated_field === 'email') {
Expand All @@ -1585,10 +1594,14 @@ RestWrite.prototype.runDatabaseOperation = function () {
)
.then(results => {
if (results.length > 0) {
throw new Parse.Error(
const usernameError = new Parse.Error(
Parse.Error.USERNAME_TAKEN,
'Account already exists for this username.'
);
if (this.config.logLevels.usernameAlreadyExists !== 'silent') {
logger[this.config.logLevels.usernameAlreadyExists](JSON.stringify(usernameError));
}
throw usernameError;
}
return this.config.database.find(
this.className,
Expand Down
Loading