Validate Conversation subject

also reordered fields of Conversation
This commit is contained in:
Benjamin Neff 2016-07-08 21:07:48 +02:00
parent 4136fb973e
commit 0980294a0d
4 changed files with 24 additions and 16 deletions

View file

@ -4,6 +4,12 @@ module DiasporaFederation
#
# @see Validators::ConversationValidator
class Conversation < Entity
# @!attribute [r] author
# The diaspora* ID of the person initiated the conversation
# @see Person#author
# @return [String] diaspora* ID
property :author, xml_name: :diaspora_handle
# @!attribute [r] guid
# A random string of at least 16 chars
# @see Validation::Rule::Guid
@ -18,21 +24,15 @@ module DiasporaFederation
# @return [Time] Conversation creation time
property :created_at, default: -> { Time.now.utc }
# @!attribute [r] messages
# @return [[Entities::Message]] Messages of this conversation
entity :messages, [Entities::Message], default: []
# @!attribute [r] author
# The diaspora* ID of the person initiated the conversation
# @see Person#author
# @return [String] diaspora* ID
property :author, xml_name: :diaspora_handle
# @!attribute [r] participants
# The diaspora* IDs of the persons participating the conversation separated by ";"
# @return [String] participants diaspora* IDs
property :participants, xml_name: :participant_handles
# @!attribute [r] messages
# @return [[Entities::Message]] Messages of this conversation
entity :messages, [Entities::Message], default: []
private
def validate

View file

@ -4,13 +4,13 @@ module DiasporaFederation
class ConversationValidator < Validation::Validator
include Validation
rule :author, %i(not_empty diaspora_id)
rule :guid, :guid
rule :messages, :not_nil
rule :author, %i(not_empty diaspora_id)
rule :subject, [:not_empty, length: {maximum: 255}]
rule :participants, diaspora_id_count: {maximum: 20}
rule :messages, :not_nil
end
end
end

View file

@ -22,12 +22,12 @@ module DiasporaFederation
let(:xml) {
<<-XML
<conversation>
<diaspora_handle>#{data[:author]}</diaspora_handle>
<guid>#{parent.guid}</guid>
<subject>#{data[:subject]}</subject>
<created_at>#{data[:created_at]}</created_at>
#{data[:messages].map {|a| a.to_xml.to_s.indent(2) }.join("\n")}
<diaspora_handle>#{data[:author]}</diaspora_handle>
<participant_handles>#{data[:participants]}</participant_handles>
#{data[:messages].map {|a| a.to_xml.to_s.indent(2) }.join("\n")}
</conversation>
XML
}
@ -41,10 +41,10 @@ XML
let(:minimal_xml) {
<<-XML
<conversation>
<author>#{data[:author]}</author>
<guid>#{parent.guid}</guid>
<subject>#{data[:subject]}</subject>
<created_at>#{data[:created_at]}</created_at>
<author>#{data[:author]}</author>
<participant_handles>#{data[:participants]}</participant_handles>
</conversation>
XML

View file

@ -13,6 +13,14 @@ module DiasporaFederation
let(:property) { :guid }
end
describe "#subject" do
it_behaves_like "a property with a value validation/restriction" do
let(:property) { :subject }
let(:wrong_values) { [nil, "", "a" * 256] }
let(:correct_values) { ["a" * 255] }
end
end
describe "#messages" do
it_behaves_like "a property with a value validation/restriction" do
let(:property) { :messages }