Skip to content

A platform for community discussion. Free, open, simple.

FIX: Limit user status to 100 characters (#20040)

Bianca Nenciub6f75e231cb4
diff --git a/app/models/user_status.rb b/app/models/user_status.rb
index 08cb9dea1ea..fea716017d7 100644
--- a/app/models/user_status.rb
+++ b/app/models/user_status.rb
@@ -1,8 +1,12 @@
# frozen_string_literal: true
class UserStatus < ActiveRecord::Base
+ MAX_DESCRIPTION_LENGTH = 100
+
belongs_to :user
+ validate :emoji_exists
+ validates :description, length: { maximum: MAX_DESCRIPTION_LENGTH }
validate :ends_at_greater_than_set_at,
if: Proc.new { |t| t.will_save_change_to_set_at? || t.will_save_change_to_ends_at? }
@@ -10,6 +14,10 @@ class UserStatus < ActiveRecord::Base
ends_at && ends_at < Time.zone.now
end
+ def emoji_exists
+ emoji && Emoji.exists?(emoji)
+ end
+
def ends_at_greater_than_set_at
if ends_at && set_at > ends_at
errors.add(:ends_at, I18n.t("user_status.errors.ends_at_should_be_greater_than_set_at"))
diff --git a/spec/requests/user_status_controller_spec.rb b/spec/requests/user_status_controller_spec.rb
index 3d77fd93a26..baa2a839185 100644
--- a/spec/requests/user_status_controller_spec.rb
+++ b/spec/requests/user_status_controller_spec.rb
@@ -75,6 +75,27 @@ RSpec.describe UserStatusController do
expect(response.status).to eq(400)
end
+ it "validates emoji" do
+ put "/user-status.json", params: { description: "invalid_emoji_name" }
+ expect(response.status).to eq(400)
+ end
+
+ it "limits description’s length" do
+ put "/user-status.json",
+ params: {
+ emoji: "tooth",
+ description: "x" * UserStatus::MAX_DESCRIPTION_LENGTH,
+ }
+ expect(response.status).to eq(200)
+
+ put "/user-status.json",
+ params: {
+ emoji: "tooth",
+ description: "x" * (UserStatus::MAX_DESCRIPTION_LENGTH + 1),
+ }
+ expect(response.status).to eq(422)
+ end
+
it "sets user status" do
status = "off to dentist"
status_emoji = "tooth"