From f3f967f9f7f73a98a6f1f2cd4ddabf96bd075e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sun, 19 Feb 2023 12:10:26 +0800 Subject: [PATCH] Prevent signups with reserved usernames closes #12 --- app/models/user.rb | 2 ++ spec/features/signup_spec.rb | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 4f49b81..f12c8ce 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -18,6 +18,8 @@ class User < ApplicationRecord validates_format_of :cn, without: /\A-/, if: Proc.new{ |u| u.cn.present? }, message: "is invalid. Usernames need to start with a letter." + validates_format_of :cn, without: /\A(#{Setting.reserved_usernames.join('|')})\z/i, + message: "has already been taken" validates_uniqueness_of :email validates :email, email: true diff --git a/spec/features/signup_spec.rb b/spec/features/signup_spec.rb index 9dff0df..9959661 100644 --- a/spec/features/signup_spec.rb +++ b/spec/features/signup_spec.rb @@ -109,5 +109,11 @@ RSpec.describe "Signup", type: :feature do expect(page).to have_content("confirm your address") end end + + scenario "Reserved usernames" do + fill_in "user_cn", with: "accounts" + click_button "Continue" + expect(page).to have_content("Username has already been taken") + end end end