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

Allow using username and email in SMTP configuration #3398

Open
wants to merge 14 commits into
base: development
Choose a base branch
from

Conversation

samaradel
Copy link
Contributor

@samaradel samaradel commented Sep 10, 2024

Description

  • Remove isDiscourse check and apply adding email or username for all apps with SMTP.
  • Add the minimum and maximum lengths for the username and email.
  • validate the email input to accept the username too.
  • Accept usernames that contain dashes and underscores and they must start with an alphabetical character and validate them.

image

  • limit the password length to 69 chars as per sendgrid documentation
    image

Changes

image

Related Issues

Tested Scenarios

  • Check if the user used an email or username in the Email/username input and validate it.
  • Check if the input accepts less than 2 characters.
  • Check if the input accepts more than 50 characters.
  • Check if the username or email can start with anything except an alphabetical character.
  • Check if we could leave the input empty

Documentation PR

Checklist

  • Tests included
  • Build pass
  • Documentation
  • Code format and docstrings
  • Screenshots/Video attached (needed for UI changes)

@samaradel samaradel changed the title Remove isDiscourse check and apply adding email or username for all a… Allow username or email in SMTP configuration Sep 10, 2024
@samaradel samaradel changed the title Allow username or email in SMTP configuration Allow using username and email in SMTP configuration Sep 10, 2024
@samaradel samaradel marked this pull request as draft September 11, 2024 12:36
@samaradel samaradel marked this pull request as ready for review September 19, 2024 08:29
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@0oM4R
Copy link
Contributor

0oM4R commented Sep 22, 2024

please mention the idea behind the validation rules, 69 characters, and the regex of the username

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add unit tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

@0oM4R 0oM4R left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we need to include those changes in manual Please open an issue for it in https://github.com/threefoldtech/info_grid

},
validators.required('Email or Username is required.'),
(v: string) => {
return validators.isValidSmtp(v);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we accepts a username now i think we should add min char validation to be at least two chars

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I discussed it with him earlier today, you can go a head :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@samaradel samaradel marked this pull request as draft September 26, 2024 08:39
@samaradel
Copy link
Contributor Author

i think we need to include those changes in manual Please open an issue for it in https://github.com/threefoldtech/info_grid

I see all mail server apps in the manual don't mention anything about validation, it is just about filling fields, I don't think it's necessary

@0oM4R
Copy link
Contributor

0oM4R commented Sep 26, 2024

i think we need to include those changes in manual Please open an issue for it in https://github.com/threefoldtech/info_grid

I see all mail server apps in the manual don't mention anything about validation, it is just about filling fields, I don't think it's necessary

what i mean is to mention that we accepts username not only email,
image

@samaradel
Copy link
Contributor Author

@0oM4R you mean the Admin Email point, ok I will create one .. thanks :)

- Add space checker in validator function
- Add unit test for smtp validation function
@samaradel samaradel marked this pull request as ready for review September 29, 2024 14:42
* @returns {{ message: string }} - An object with an error message if the input is invalid, or undefined if the input is valid.
*/

export function isValidSmtp(input: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1- where is the return type of the function?
2-if the function is named isX is expect it to return a boolean. True if X and false if not. To return null that may denote a failure e.g failure to allocate memory, or no data available in such context.

So we have multiple options

1- return a boolean (and discard the error messages, keep it constant to invalid smtp information)
2- return a boolean with a context, e.g boolean, null in case of success and boolean and error string in case of failure so [boolean, string|null] 3- return a boolean (success) or an error message (failure)boolean | string`
4- return a new type e.g Result that encapsulates the success/failure status, and message/error fields

type Result<T> = {
  success: true;
  value: T;
} | {
  success: false;
  error: string;
};

Copy link
Contributor Author

@samaradel samaradel Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. the function is deleted now and the validation is created in the input validation rules.


import { isValidSmtp } from "../../src/utils/validators";

describe("isValidSmtp", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably need more cases e.g empty string, multiple invalid forms of email as well, usage of special characters

Copy link
Contributor Author

@samaradel samaradel Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as the function is deleted, the test case is deleted too and you could check it in the current rules.

/**
* Validates an SMTP input string.
*
* Checks if the input string is a valid email address and does not contain special characters.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it only email? if so change the input parameter to email

Copy link
Contributor Author

@samaradel samaradel Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It now accepts usernames and email in the same input and it can validate each.

export function isValidSmtp(input: string) {
const emailValidation = isEmail("Please provide a valid email address.")(input);
const username = /[!@#$%^&*()\s_+\-={}:<>?,./]/.test(input);
if (username && emailValidation) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this condition, does that mean if it's email and a username (doesn't have special characters) then it's true?

shouldn't it be like that

const isValidEmail = isEmail("Please provide a valid email address.")(input);
const hasSpecialCharacters = /[!@#$%^&*()\s_+\-={}:<>?,./]/.test(input);

if (hasSpecialCharacters && isValidEmail) {
      return { message: "Please provide a valid username or email" };
}

Which I still don't understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant here it could accept both and validate if it's not a valid email or username with a special character.


export function isValidSmtp(input: string) {
const emailValidation = isEmail("Please provide a valid email address.")(input);
const username = /[!@#$%^&*()\s_+\-={}:<>?,./]/.test(input);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could have been simplified instead of matching against special characters you could have just made it match against valid characters which are a-z[A-Z0-9]+

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could have been simplified instead of matching against special characters you could have just made it match against valid characters which are a-z[A-Z0-9]+

Please note this is the right way, given there's a very long of list of unhandled utf8 input, that's why you should state the allowed ones as long as you can iterate them, instead of chasing a very long list of symbols.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, the regex was implemented separately in the validator function and used in the input validation.

import { isValidSmtp } from "../../src/utils/validators";

describe("isValidSmtp", () => {
it("returns nothing for valid username and email", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I truly don't understand can you point out the username and the email because there's only just one input name input which looks like an email.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in the current validation rules.

});

it("returns an error message for special characters in username", () => {
const input = "invalid_email";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_ is not the only special character

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course not but I used one of them to test as I can't test all of them (too many cases).

});

it("returns an error message for white spaces in username", () => {
const input = "invalid username";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there're more types of spaces beyond " " there's is \t and \v

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noted in the current validation.

@samaradel samaradel marked this pull request as draft October 1, 2024 08:08
- Accept usernames that contain dashs and underscores and it must start with an alphabetical character and validate them
@samaradel samaradel marked this pull request as ready for review October 2, 2024 10:25
@samaradel samaradel marked this pull request as draft October 2, 2024 11:58
@samaradel samaradel marked this pull request as ready for review October 2, 2024 12:52
return isDiscourse ? undefined : validators.isEmail('Please provide a valid email address.')(v);
validators.required('Email or Username is required.'),
validators.minLength('Username must be at least 2 characters.', 2),
validators.maxLength('Username must be at least 50 characters.', 50),
Copy link
Contributor

@0oM4R 0oM4R Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't use the at least on max length

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also is this validation on user name only? what if the email exceeds the 50 char ? the length validation message will appear

Copy link
Contributor

@0oM4R 0oM4R left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the validation need a revisit
Screenshot from 2024-10-08 11-13-30
Screenshot from 2024-10-08 11-13-39

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants