Skip to content

Commit

Permalink
Updated the jsdoc and some logics
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Chouhan <[email protected]>
  • Loading branch information
achouhan09 committed Jan 29, 2025
1 parent 0b8a0a1 commit d1f01d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
15 changes: 11 additions & 4 deletions src/manage_nsfs/manage_nsfs_validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,22 @@ function validate_boolean_string_value(value) {
}

/**
* validate_no_leading_whitespace validates that a given value is not empty or whitespace-only
* validate_no_leading_whitespace validates the given value does not contain leading whitespace or is not entirely whitespace.
*
* ### CLI Parsing Behavior:
* When passing arguments via CLI, if a flag's value starts with a space or consists only of whitespace,
* the shell may treat it differently:
* - **Leading spaces** might be preserved when quoted (`--access_key=" 123abc"`).
* - **Unquoted values with spaces** may cause incorrect parsing (`--access_key= 123abc` is interpreted as an empty value).
*
* @param {string} option
* @param {string} value
* @throws {ManageCLIError}
*/
function validate_no_leading_whitespace(option, value) {
// ensure values are not empty or whitespace only strings
if (value.trim() === '') {
const details = `flag ${option} cannot have an empty value or contain only whitespace. Please provide a valid value for ${option}`;
const details = `flag ${option} cannot start with a space, as leading whitespace is not allowed. Please provide a valid value for ${option} without leading spaces`;
throw_cli_error(ManageCLIError.InvalidArgument, details);
}
}
Expand Down Expand Up @@ -588,8 +595,8 @@ function _validate_access_keys(access_key, secret_key) {
throw_cli_error(ManageCLIError.MissingAccountAccessKeyFlag);
}

// checking access_key and secret_key do not contain only whitespace
if (access_key && secret_key) {
// checking access_key and secret_key do not contain leading space
if (access_key !== undefined && secret_key !== undefined) {
validate_no_leading_whitespace('access_key', access_key);
validate_no_leading_whitespace('secret_key', secret_key);
}
Expand Down
18 changes: 7 additions & 11 deletions src/test/unit_tests/jest_tests/test_nc_nsfs_account_cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,21 @@ describe('manage nsfs cli account flow', () => {
assert_account(account_symlink, account_options);
});

it('should fail - cli create account invalid access_key(leading space in access_key)', async () => {
const { type, name, uid, gid, secret_key } = defaults;
it('should fail - cli create account invalid access_key (leading space in access_key)', async () => {
const { type, name, uid, gid, access_key, secret_key } = defaults;
const account_options = { config_root, name, uid, gid, secret_key};
const action = ACTIONS.ADD;
const command = create_command(type, action, account_options);
const flag = 'access_key';
const value = ' 12345678912345678ABC'; // leading space for access_key
const res = await exec_manage_cli_add_leading_space_option(command, flag, value);
const res = await exec_manage_cli_add_leading_space_option(command, 'access_key', access_key);
expect(JSON.parse(res.stdout).error.message).toBe(ManageCLIError.InvalidArgument.message);
});

it('should fail - cli create account invalid secret_key(leading space in secret_key)', async () => {
const { type, name, uid, gid, access_key } = defaults;
it('should fail - cli create account invalid secret_key (leading space in secret_key)', async () => {
const { type, name, uid, gid, access_key, secret_key } = defaults;
const account_options = { config_root, name, uid, gid, access_key};
const action = ACTIONS.ADD;
const command = create_command(type, action, account_options);
const flag = 'secret_key';
const value = ' a234567891234567891212345678912345678912'; // leading space for secret_key
const res = await exec_manage_cli_add_leading_space_option(command, flag, value);
const res = await exec_manage_cli_add_leading_space_option(command, 'secret_key', secret_key);
expect(JSON.parse(res.stdout).error.message).toBe(ManageCLIError.InvalidArgument.message);
});

Expand Down Expand Up @@ -2233,7 +2229,7 @@ async function exec_manage_cli_add_empty_option(command, option) {
}

/**
* exec_manage_cli_add_leading_space_option adds a flag with value having leading space
* exec_manage_cli_add_leading_space_option modifies the command by appending a flag with a value that includes a leading space to test CLI parsing behavior
* @param {string} command
* @param {string} option
* @param {string} value
Expand Down

0 comments on commit d1f01d3

Please sign in to comment.