forked from diaspora/diaspora_federation
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
194 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module DiasporaFederation | ||
module Entities | ||
# This entity is used to specify embed information about an URL that should be embedded. | ||
# | ||
# @see Validators::EmbedValidator | ||
class Embed < Entity | ||
# @!attribute [r] url | ||
# URL that should be embedded. | ||
# @return [String] url | ||
property :url, :string, optional: true | ||
|
||
# @!attribute [r] title | ||
# The title of the embedded URL. | ||
# @return [String] title | ||
property :title, :string, optional: true | ||
|
||
# @!attribute [r] description | ||
# The description of the embedded URL. | ||
# @return [String] description | ||
property :description, :string, optional: true | ||
|
||
# @!attribute [r] image | ||
# The image of the embedded URL. | ||
# @return [String] image | ||
property :image, :string, optional: true | ||
|
||
# @!attribute [r] nothing | ||
# True, if nothing should be embedded. | ||
# @return [String] nothing | ||
property :nothing, :boolean, optional: true | ||
|
||
# @return [String] string representation of this object | ||
def to_s | ||
"Embed#{":#{url}" if url}" | ||
end | ||
|
||
def validate | ||
super | ||
|
||
raise ValidationError, "URL set and 'nothing' is 'true'" if url && nothing | ||
raise ValidationError, "URL missing and 'nothing' is not 'true'" unless url || nothing | ||
end | ||
end | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module DiasporaFederation | ||
module Validators | ||
# This validates a {Entities::Embed}. | ||
class EmbedValidator < OptionalAwareValidator | ||
include Validation | ||
|
||
rule :url, :URI | ||
rule :title, length: {maximum: 255} | ||
rule :description, length: {maximum: 65_535} | ||
rule :image, URI: %i[host path] | ||
end | ||
end | ||
end |
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,56 @@ | ||
module DiasporaFederation | ||
describe Entities::Embed do | ||
let(:data) { Fabricate.attributes_for(:embed_entity) } | ||
|
||
let(:xml) { <<-XML } | ||
<embed> | ||
<url>#{data[:url]}</url> | ||
<title>#{data[:title]}</title> | ||
<description>#{data[:description]}</description> | ||
<image>#{data[:image]}</image> | ||
</embed> | ||
XML | ||
|
||
let(:json) { <<-JSON } | ||
{ | ||
"entity_type": "embed", | ||
"entity_data": { | ||
"url": "#{data[:url]}", | ||
"title": "#{data[:title]}", | ||
"description": "#{data[:description]}", | ||
"image": "#{data[:image]}" | ||
} | ||
} | ||
JSON | ||
|
||
let(:string) { "Embed:#{data[:url]}" } | ||
|
||
it_behaves_like "an Entity subclass" | ||
|
||
it_behaves_like "an XML Entity" | ||
|
||
it_behaves_like "a JSON Entity" | ||
|
||
describe "#validate" do | ||
it "allows 'url' to be set if 'nothing' is not true" do | ||
expect { Entities::Embed.new(data) }.not_to raise_error | ||
end | ||
|
||
it "allows 'url' to be missing if 'nothing' is true" do | ||
expect { Entities::Embed.new(nothing: true) }.not_to raise_error | ||
end | ||
|
||
it "doesn't allow 'url' to be set if 'nothing' is true" do | ||
expect { | ||
Entities::Embed.new(data.merge(nothing: true)) | ||
}.to raise_error Entity::ValidationError, "URL set and 'nothing' is 'true'" | ||
end | ||
|
||
it "doesn't allow 'url' to be missing if 'nothing' is not true" do | ||
expect { | ||
Entities::Embed.new({}) | ||
}.to raise_error Entity::ValidationError, "URL missing and 'nothing' is not 'true'" | ||
end | ||
end | ||
end | ||
end |
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
40 changes: 40 additions & 0 deletions
40
spec/lib/diaspora_federation/validators/embed_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,40 @@ | ||
module DiasporaFederation | ||
describe Validators::EmbedValidator do | ||
let(:entity) { :embed_entity } | ||
it_behaves_like "a common validator" | ||
|
||
describe "#url" do | ||
it_behaves_like "a property with a value validation/restriction" do | ||
let(:property) { :url } | ||
let(:wrong_values) { %w[https://asdf$%.com example.com] } | ||
let(:correct_values) { [nil, "https://example.org", "https://example.org/index.html"] } | ||
end | ||
end | ||
|
||
describe "#title" do | ||
it_behaves_like "a length validator" do | ||
let(:property) { :title } | ||
let(:length) { 255 } | ||
end | ||
end | ||
|
||
describe "#description" do | ||
it_behaves_like "a length validator" do | ||
let(:property) { :description } | ||
let(:length) { 65_535 } | ||
end | ||
end | ||
|
||
describe "#image" do | ||
it_behaves_like "a property with a value validation/restriction" do | ||
let(:property) { :image } | ||
let(:wrong_values) { %w[https://asdf$%.com example.com] } | ||
let(:correct_values) { [nil] } | ||
end | ||
|
||
it_behaves_like "a url path validator" do | ||
let(:property) { :image } | ||
end | ||
end | ||
end | ||
end |
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