-
-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport 'Fix proposal etiquette and length validator with base64 ima…
…ges' to v0.27 (#10009) * Fix proposal etiquette and length validator with base64 images (#9639) * Fix proposal etiquette and length validator with base64 images * Use strip_tags instead of nokogiri not to include hidden content Content e.g. in `<script>` tags should be automatically hidden, so this should not be included in the validation either. * Fix the expected base64 mime types * Add omitted value in keyword argument Co-authored-by: Antti Hukkanen <antti.hukkanen@mainiotech.fi>
- Loading branch information
1 parent
0d58f71
commit 23851e6
Showing
4 changed files
with
79 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
decidim-proposals/spec/validators/proposal_length_validator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: 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/jpeg;base64,#{encoded.strip}"></p> | ||
<p>Some other text after the image.</p> | ||
HTML | ||
end | ||
|
||
it { is_expected.to be_valid } | ||
end | ||
end |