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

Merged
merged 18 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 13 additions & 8 deletions packages/playground/src/components/smtp_server.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@
<input-validator
:value="$props.modelValue.username"
:rules="[
validators.required('Email is required.'),
validators.isEmail('Please provide a valid email address.'),
v => {
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('Email or Username cannot exceed 50 characters.', 50),
(v: string) => {
return (
validators.isEmail('Please provide a valid email address.')(v) &&
(validators.IsAlphanumericExpectDashAndUnderscore(
'Username should consist of letters, numbers, dashs and underscores only.'
)(v))
);
},
]"
#="{ props }"
>
<input-tooltip :tooltip="isDiscourse ? 'SMTP admin email, Username or API key.' : 'SMTP admin email'">
<input-tooltip tooltip="SMTP admin email, Username or API key.">
<v-text-field
label="Admin Email"
label="Admin Email or Username"
placeholder="[email protected]"
v-model="$props.modelValue.username"
v-bind="props"
Expand All @@ -46,7 +52,7 @@
:rules="[
validators.required('Password is required.'),
validators.minLength('Password must be at least 6 characters.', 6),
validators.maxLength('Password cannot exceed 50 characters.', 50),
validators.maxLength('Password cannot exceed 69 characters.', 69),
samaradel marked this conversation as resolved.
Show resolved Hide resolved
validators.pattern('Password should not contain whitespaces.', {
pattern: /^[^\s]+$/,
}),
Expand Down Expand Up @@ -139,7 +145,6 @@ defineProps<{
tls?: boolean;
email?: boolean;
persistent?: boolean;
isDiscourse?: boolean;
}>();
</script>

Expand Down
16 changes: 16 additions & 0 deletions packages/playground/src/utils/validators.ts
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

Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ export function IsAlphanumericExpectUnderscore(msg: string) {
};
}

/**
* Returns a validation function that checks if the value only contains alphanumeric characters, dashes, and underscores.
*
* @param {string} msg - The error message to return if the validation fails.
*
* @returns {(value: string) => { message: string, requiredTrue: boolean }} - A function that takes a string value as input and returns an object with an error message and a requiredTrue flag if the validation fails.
*/

export function IsAlphanumericExpectDashAndUnderscore(msg: string) {
return (value: string) => {
if (!/^[a-zA-Z0-9_-]+$/.test(value)) {
return { message: msg, requiredTrue: true };
}
};
}

export function isAfter(msg: string, date?: string) {
return (value: string) => {
if (!validator.isAfter(value, date)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/src/weblets/tf_discourse.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
</template>

<template #mail>
<SmtpServer v-model="smtp" :persistent="true" :tls="true" :is-discourse="true">
<SmtpServer v-model="smtp" persistent tls>
Discourse needs SMTP service so please configure these settings properly.
</SmtpServer>
</template>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, expect, it } from "vitest";

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

const validator = IsAlphanumericExpectDashAndUnderscore(
"Username should consist of letters, numbers, dashs and underscores only.",
);

describe("IsAlphanumericExpectDashAndUnderscore", () => {
it("returns an error message for input with spaces", () => {
const result = validator("hello world!");
expect(result).toEqual({
message: "Username should consist of letters, numbers, dashs and underscores only.",
requiredTrue: true,
});
});

it("returns an error message for input with special characters", () => {
const result = validator("hello@world");
expect(result).toEqual({
message: "Username should consist of letters, numbers, dashs and underscores only.",
requiredTrue: true,
});
});

it("returns undefined for valid username that contains underscore", () => {
const result = validator("hello_world");
expect(result).toBeUndefined();
});

it("returns undefined for valid username that contains dash", () => {
const result = validator("hello-world");
expect(result).toBeUndefined();
});

it("returns undefined for valid username", () => {
const result = validator("hello");
expect(result).toBeUndefined();
});

it("returns undefined for valid username/email that starts with numbers", () => {
const result = validator("4me");
expect(result).toBeUndefined();
});
});
Loading