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

* FIX: Limit user status to 100 characters

* FIX: Make sure the emoji is valid
This commit is contained in:
Bianca Nenciu 2023-01-27 16:32:27 +02:00 committed by GitHub
parent 8fc11215e1
commit b6f75e231c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View File

@ -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"))

View File

@ -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 descriptions 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"