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 7 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
60 changes: 60 additions & 0 deletions spec/ParseUser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4430,3 +4430,63 @@ describe('allowClientClassCreation option', () => {
defaultConfiguration.allowClientClassCreation = true;
});
});

describe('custom log levels for username already exists error', () => {
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
const logger = require('../lib/logger').logger;
let warnSpy, errorSpy, infoSpy;

beforeEach(() => {
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
warnSpy = spyOn(logger, 'warn').and.callThrough();
errorSpy = spyOn(logger, 'error').and.callThrough();
infoSpy = spyOn(logger, 'info').and.callThrough();
});

afterEach(() => {
warnSpy.calls.reset();
errorSpy.calls.reset();
infoSpy.calls.reset();
});

it('should respect custom log levels for username already exists error', async () => {
const logLevels = ['warn', 'info'];

for (const logLevel of logLevels) {
await reconfigureServer({
logLevels: {
usernameAlreadyExists: logLevel,
},
});

const username = `existingUser_${logLevel}`;
mtrezza marked this conversation as resolved.
Show resolved Hide resolved

const user = new Parse.User();
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
user.setUsername(username);
user.setPassword('password');
await user.signUp();

const duplicateUser = new Parse.User();
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
duplicateUser.setUsername(username);
duplicateUser.setPassword('password');

try {
await duplicateUser.signUp();
fail('Should have thrown an error');
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
expect(error.code).toBe(Parse.Error.USERNAME_TAKEN);
if (logLevel === 'warn') {
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
expect(warnSpy).toHaveBeenCalled();
expect(infoSpy).not.toHaveBeenCalled();
} else if (logLevel === 'info') {
expect(infoSpy).toHaveBeenCalled();
expect(warnSpy).not.toHaveBeenCalled();
}
expect(errorSpy).not.toHaveBeenCalled();
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
}

// Reset spies for the next iteration
warnSpy.calls.reset();
errorSpy.calls.reset();
infoSpy.calls.reset();
}
});
});
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;
}
6 changes: 5 additions & 1 deletion src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -1557,10 +1557,14 @@ 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.'
);

return this.config.loggerController[this.config.logLevels.usernameAlreadyExists](
JSON.stringify(usernameError)
);
}

if (error && error.userInfo && error.userInfo.duplicated_field === 'email') {
Expand Down