Allow reshares with no root
This commit is contained in:
parent
a92f5dec9c
commit
5b12afebed
6 changed files with 72 additions and 14 deletions
|
|
@ -7,6 +7,10 @@ This entity represents a reshare of a [StatusMessage][status_message]. It inheri
|
||||||
The recipient must [fetch][fetching] the root from `root_author` if the post is not already known.
|
The recipient must [fetch][fetching] the root from `root_author` if the post is not already known.
|
||||||
When the `root_guid` is already available locally, the recipient must validate that it's from `root_author`.
|
When the `root_guid` is already available locally, the recipient must validate that it's from `root_author`.
|
||||||
|
|
||||||
|
A special case when the entity doesn't include `root_author` and `root_guid` is allowed. This case represents
|
||||||
|
a reshare for a deleted post. If there is only one of `root_author` and `root_guid` is present, then the entity
|
||||||
|
is not valid.
|
||||||
|
|
||||||
## Properties
|
## Properties
|
||||||
|
|
||||||
| Property | Type | Description |
|
| Property | Type | Description |
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,11 @@ module DiasporaFederation
|
||||||
# Fetch and receive root post from remote, if not available locally
|
# Fetch and receive root post from remote, if not available locally
|
||||||
# and validates if it's from the correct author
|
# and validates if it's from the correct author
|
||||||
def validate_root
|
def validate_root
|
||||||
|
return if root_author.nil? && root_guid.nil?
|
||||||
|
|
||||||
|
raise Entity::ValidationError, "#{self}: root_guid can't be nil if root_author is present" if root_guid.nil?
|
||||||
|
raise Entity::ValidationError, "#{self}: root_author can't be nil if root_guid is present" if root_author.nil?
|
||||||
|
|
||||||
root = RelatedEntity.fetch(root_author, "Post", root_guid)
|
root = RelatedEntity.fetch(root_author, "Post", root_guid)
|
||||||
|
|
||||||
return if root_author == root.author
|
return if root_author == root.author
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,9 @@ module DiasporaFederation
|
||||||
class ReshareValidator < Validation::Validator
|
class ReshareValidator < Validation::Validator
|
||||||
include Validation
|
include Validation
|
||||||
|
|
||||||
rule :root_author, %i[not_empty diaspora_id]
|
rule :root_author, :diaspora_id
|
||||||
|
|
||||||
rule :root_guid, :guid
|
rule :root_guid, guid: {nilable: true}
|
||||||
|
|
||||||
rule :author, %i[not_empty diaspora_id]
|
rule :author, %i[not_empty diaspora_id]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -74,6 +74,31 @@ XML
|
||||||
Entities::Reshare.from_xml(Nokogiri::XML(xml).root)
|
Entities::Reshare.from_xml(Nokogiri::XML(xml).root)
|
||||||
}.to raise_error Entity::ValidationError
|
}.to raise_error Entity::ValidationError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "validates a reshare with no root" do
|
||||||
|
data[:root_author] = nil
|
||||||
|
data[:root_guid] = nil
|
||||||
|
|
||||||
|
reshare = Entities::Reshare.from_xml(Nokogiri::XML(xml).root)
|
||||||
|
expect(reshare.root_author).to be_nil
|
||||||
|
expect(reshare.root_guid).to be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it "disallows root_author without root_guid" do
|
||||||
|
data[:root_guid] = nil
|
||||||
|
|
||||||
|
expect {
|
||||||
|
Entities::Reshare.from_xml(Nokogiri::XML(xml).root)
|
||||||
|
}.to raise_error Entity::ValidationError
|
||||||
|
end
|
||||||
|
|
||||||
|
it "disallows root_guid without root_author" do
|
||||||
|
data[:root_author] = nil
|
||||||
|
|
||||||
|
expect {
|
||||||
|
Entities::Reshare.from_xml(Nokogiri::XML(xml).root)
|
||||||
|
}.to raise_error Entity::ValidationError
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -3,20 +3,29 @@ module DiasporaFederation
|
||||||
let(:entity) { :reshare_entity }
|
let(:entity) { :reshare_entity }
|
||||||
it_behaves_like "a common validator"
|
it_behaves_like "a common validator"
|
||||||
|
|
||||||
%i[root_author author].each do |prop|
|
describe "#author" do
|
||||||
describe "##{prop}" do
|
it_behaves_like "a diaspora* ID validator" do
|
||||||
it_behaves_like "a diaspora* ID validator" do
|
let(:property) { :author }
|
||||||
let(:property) { prop }
|
let(:mandatory) { true }
|
||||||
let(:mandatory) { true }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
%i[root_guid guid].each do |prop|
|
describe "#guid" do
|
||||||
describe "##{prop}" do
|
it_behaves_like "a guid validator" do
|
||||||
it_behaves_like "a guid validator" do
|
let(:property) { :guid }
|
||||||
let(:property) { prop }
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#root_guid" do
|
||||||
|
it_behaves_like "a nilable guid validator" do
|
||||||
|
let(:property) { :root_guid }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#root_author" do
|
||||||
|
it_behaves_like "a diaspora* ID validator" do
|
||||||
|
let(:property) { :root_author }
|
||||||
|
let(:mandatory) { false }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -92,7 +92,7 @@ shared_examples "a diaspora* ID validator" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
shared_examples "a guid validator" do
|
shared_examples "common guid validator" do
|
||||||
it "validates a well-formed guid from redmatrix" do
|
it "validates a well-formed guid from redmatrix" do
|
||||||
validator = described_class.new(entity_stub(entity, property => "1234567890ABCDefgh_ijkl-mnopQR@example.com:3000"))
|
validator = described_class.new(entity_stub(entity, property => "1234567890ABCDefgh_ijkl-mnopQR@example.com:3000"))
|
||||||
|
|
||||||
|
|
@ -113,6 +113,10 @@ shared_examples "a guid validator" do
|
||||||
expect(validator).not_to be_valid
|
expect(validator).not_to be_valid
|
||||||
expect(validator.errors).to include(property)
|
expect(validator.errors).to include(property)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
shared_examples "a guid validator" do
|
||||||
|
include_examples "common guid validator"
|
||||||
|
|
||||||
it "must not be nil or empty" do
|
it "must not be nil or empty" do
|
||||||
[nil, ""].each do |val|
|
[nil, ""].each do |val|
|
||||||
|
|
@ -124,6 +128,17 @@ shared_examples "a guid validator" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
shared_examples "a nilable guid validator" do
|
||||||
|
include_examples "common guid validator"
|
||||||
|
|
||||||
|
it "can be nil" do
|
||||||
|
validator = described_class.new(entity_stub(entity, property => nil))
|
||||||
|
|
||||||
|
expect(validator).to be_valid
|
||||||
|
expect(validator.errors).to be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
shared_examples "a boolean validator" do
|
shared_examples "a boolean validator" do
|
||||||
it "validates a well-formed boolean" do
|
it "validates a well-formed boolean" do
|
||||||
[true, "true", false, "false"].each do |val|
|
[true, "true", false, "false"].each do |val|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue