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

Draft
wants to merge 11 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.

  • Create a function to validate applications with SMTP.

  • validate the email input to accept username too
    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

image

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

@0oM4R
Copy link
Contributor

0oM4R commented Sep 24, 2024

I can't test the solutions, #3449 and discourse not responding for more than one hour

@samaradel
Copy link
Contributor Author

samaradel commented Sep 24, 2024

@0oM4R you should test the field validation to see if it accepts email or username in the discourse without problems

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;
};


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

/**
* 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

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.


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.

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.

});

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

});

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

@samaradel samaradel marked this pull request as draft October 1, 2024 08:08
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