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

Fix proposal etiquette and length validator with base64 images #9639

Merged
merged 4 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 7 additions & 3 deletions decidim-core/app/validators/etiquette_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
# This validator takes care of ensuring the validated content is
# respectful, doesn't use caps, and overall is meaningful.
class EtiquetteValidator < ActiveModel::EachValidator
include ActionView::Helpers::SanitizeHelper

def validate_each(record, attribute, value)
return if value.blank?

validate_caps(record, attribute, value)
validate_marks(record, attribute, value)
validate_caps_first(record, attribute, value)
text_value = strip_tags(value)

validate_caps(record, attribute, text_value)
validate_marks(record, attribute, text_value)
validate_caps_first(record, attribute, text_value)
end

private
Expand Down
15 changes: 15 additions & 0 deletions decidim-core/spec/validators/etiquette_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,19 @@ def self.model_name
it { is_expected.to be_valid }
end
end

context "when the text is written in HTML" do
let(:body) do
data = File.read(Decidim::Dev.asset("avatar.jpg"))
encoded = Base64.encode64(data)

<<~HTML
<p>Text before the image.</p>
<p><img src="data:image/png;base64,#{encoded.strip}"></p>
ahukkanen marked this conversation as resolved.
Show resolved Hide resolved
<p>Some other text after the image.</p>
HTML
end

it { is_expected.to be_valid }
end
end
7 changes: 5 additions & 2 deletions decidim-proposals/app/validators/proposal_length_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
# allows the minimum and maximum values to be lambdas allowing us to fetch the
# maximum length dynamically for each proposals component.
class ProposalLengthValidator < ActiveModel::EachValidator
include ActionView::Helpers::SanitizeHelper

def validate_each(record, attribute, value)
return if value.blank?

validate_min_length(record, attribute, value)
validate_max_length(record, attribute, value)
text_value = strip_tags(value)
validate_min_length(record, attribute, text_value)
validate_max_length(record, attribute, text_value)
end

private
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

require "spec_helper"

describe ProposalLengthValidator do
subject { validatable.new(body:) }

let(:validatable) do
Class.new do
def self.model_name
ActiveModel::Name.new(self, nil, "Validatable")
end

include Decidim::AttributeObject::Model
include ActiveModel::Validations

attribute :body

validates :body, proposal_length: {
minimum: 15,
maximum: ->(_record) { 100 }
}
end
end

context "when the text is too short" do
let(:body) { "Lorem ipsum d" }

it { is_expected.to be_invalid }
end

context "when the text is too long" do
let(:body) { "a" * 101 }

it { is_expected.to be_invalid }
end

context "when the text is written in HTML" do
let(:body) do
data = File.read(Decidim::Dev.asset("avatar.jpg"))
encoded = Base64.encode64(data)

<<~HTML
<p>Text before the image.</p>
<p><img src="data:image/png;base64,#{encoded.strip}"></p>
ahukkanen marked this conversation as resolved.
Show resolved Hide resolved
<p>Some other text after the image.</p>
HTML
end

it { is_expected.to be_valid }
end
end