mirror of
https://github.com/discourse/discourse.git
synced 2025-03-28 16:45:41 +08:00
DEV: Rewrite username validator specs without hardcoded length limits (#23102)
This commit is contained in:
parent
2d782c7b00
commit
ee61fe5a2b
@ -27,39 +27,47 @@ RSpec.describe UsernameValidator do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
let(:max_username_length) { User.maximum("length(username)") }
|
||||||
|
let(:min_username_length) { User.minimum("length(username)") }
|
||||||
|
|
||||||
shared_examples "ASCII username" do
|
shared_examples "ASCII username" do
|
||||||
it "is invalid when the username is blank" do
|
it "is invalid when the username is blank" do
|
||||||
expect_invalid("", error_message: I18n.t(:"user.username.blank"))
|
expect_invalid("", error_message: I18n.t(:"user.username.blank"))
|
||||||
end
|
end
|
||||||
|
|
||||||
it "is invalid when the username is too short" do
|
it "is invalid when the username is too short" do
|
||||||
SiteSetting.min_username_length = 4
|
SiteSetting.min_username_length = min_username_length
|
||||||
|
|
||||||
expect_invalid("a", "ab", "abc", error_message: I18n.t(:"user.username.short", min: 4))
|
usernames = min_username_length.times.map { |i| "a" * i }.filter(&:present?)
|
||||||
|
|
||||||
|
expect_invalid(
|
||||||
|
*usernames,
|
||||||
|
error_message: I18n.t(:"user.username.short", min: min_username_length),
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "is valid when the username has the minimum length" do
|
it "is valid when the username has the minimum length" do
|
||||||
SiteSetting.min_username_length = 4
|
SiteSetting.min_username_length = min_username_length
|
||||||
|
|
||||||
expect_valid("abcd")
|
expect_valid("a" * min_username_length)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "is invalid when the username is too long" do
|
it "is invalid when the username is too long" do
|
||||||
SiteSetting.max_username_length = 8
|
SiteSetting.max_username_length = max_username_length
|
||||||
|
|
||||||
expect_invalid(
|
expect_invalid(
|
||||||
"abcdefghi",
|
"a" * (max_username_length + 1),
|
||||||
error_message: I18n.t(:"user.username.long", max: 8),
|
error_message: I18n.t(:"user.username.long", max: max_username_length),
|
||||||
failure_reason: "Should be invalid as username length > #{SiteSetting.max_username_length}",
|
failure_reason: "Should be invalid as username length > #{max_username_length}",
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "is valid when the username has the maximum length" do
|
it "is valid when the username has the maximum length" do
|
||||||
SiteSetting.max_username_length = 8
|
SiteSetting.max_username_length = max_username_length
|
||||||
|
|
||||||
expect_valid(
|
expect_valid(
|
||||||
"abcdefgh",
|
"a" * max_username_length,
|
||||||
failure_reason: "Should be valid as username length = #{SiteSetting.max_username_length}",
|
failure_reason: "Should be valid as username length = #{max_username_length}",
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -135,37 +143,41 @@ RSpec.describe UsernameValidator do
|
|||||||
before { SiteSetting.min_username_length = 1 }
|
before { SiteSetting.min_username_length = 1 }
|
||||||
|
|
||||||
it "is invalid when the username is too short" do
|
it "is invalid when the username is too short" do
|
||||||
SiteSetting.min_username_length = 3
|
SiteSetting.min_username_length = min_username_length
|
||||||
|
|
||||||
expect_invalid("鳥", "পাখি", error_message: I18n.t(:"user.username.short", min: 3))
|
usernames = min_username_length.times.map { |i| "鳥" * i }.filter(&:present?)
|
||||||
|
|
||||||
|
expect_invalid(
|
||||||
|
*usernames,
|
||||||
|
error_message: I18n.t(:"user.username.short", min: min_username_length),
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "is valid when the username has the minimum length" do
|
it "is valid when the username has the minimum length" do
|
||||||
SiteSetting.min_username_length = 2
|
SiteSetting.min_username_length = min_username_length
|
||||||
|
|
||||||
expect_valid("পাখি", "طائر")
|
expect_valid("ط" * min_username_length)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "is invalid when the username is too long" do
|
it "is invalid when the username is too long" do
|
||||||
SiteSetting.max_username_length = 8
|
SiteSetting.max_username_length = max_username_length
|
||||||
|
|
||||||
expect_invalid(
|
expect_invalid(
|
||||||
"חוטב_עצים",
|
"ם" * (max_username_length + 1),
|
||||||
"Holzfäller",
|
"äl" * (max_username_length + 1),
|
||||||
error_message: I18n.t(:"user.username.long", max: 8),
|
error_message: I18n.t(:"user.username.long", max: max_username_length),
|
||||||
failure_reason:
|
failure_reason: "Should be invalid as username length are > #{max_username_length}",
|
||||||
"Should be invalid as username length are > #{SiteSetting.max_username_length}",
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "is valid when the username has the maximum length" do
|
it "is valid when the username has the maximum length" do
|
||||||
SiteSetting.max_username_length = 9
|
SiteSetting.max_username_length = max_username_length
|
||||||
|
|
||||||
expect_valid(
|
expect_valid(
|
||||||
"Дровосек",
|
"Д" * (max_username_length),
|
||||||
"چوب-لباسی",
|
"س" * (max_username_length),
|
||||||
"தமிழ்-தமிழ்",
|
"மி" * (max_username_length),
|
||||||
failure_reason: "Should be valid as usernames are <= #{SiteSetting.max_username_length}",
|
failure_reason: "Should be valid as usernames are <= #{max_username_length}",
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user