require File.dirname(__FILE__) + '/../spec_helper'
include ApplicationHelper
describe "parser in application helper" do
before do
@user = Factory.create(:user, :email => "bob@aol.com")
@friend =Factory.create(:friend, :email => "bill@gates.com")
end
it "should not store posts from me" do
status_messages = []
10.times { status_messages << Factory.build(:status_message, :person => @user)}
xml = Post.build_xml_for(status_messages)
store_objects_from_xml(xml)
StatusMessage.count.should == 0
end
it 'should discard posts where it does not know the type' do
xml = "#{Friend.first.email}\n Here is another message\n a@a.com\n a@a.com\n a@a.com\n\n HEY DUDE\n a@a.com\n a@a.com\n a@a.com\n"
store_objects_from_xml(xml)
Post.count.should == 2
Post.first.person.email.should == Friend.first.email
end
it "should reject xml with no sender" do
xml = "\n Here is another message\n a@a.com\n a@a.com\n a@a.com\n\n HEY DUDE\n a@a.com\n a@a.com\n a@a.com\n"
store_objects_from_xml(xml)
Post.count.should == 0
end
it "should reject xml with a sender not in the database" do
xml = "foo@example.com\n Here is another message\n a@a.com\n a@a.com\n a@a.com\n\n HEY DUDE\n a@a.com\n a@a.com\n a@a.com\n"
store_objects_from_xml(xml)
Post.count.should == 0
end
it 'should discard types which are not of type post' do
xml = "#{Friend.first.email}\n Here is another message\n a@a.com\n a@a.com\n a@a.com\n\n HEY DUDE\n a@a.com\n a@a.com\n a@a.com\n"
store_objects_from_xml(xml)
Post.count.should == 2
Post.first.person.email.should == Friend.first.email
end
describe "parsing compliant XML object" do
before do
status_messages = []
10.times { status_messages << Factory.build(:status_message)}
@xml = Post.build_xml_for(status_messages)
end
it 'should be able to parse the sender\'s unique id' do
parse_sender_id_from_xml(@xml).should == @user.email
end
it 'should be able to parse the body\'s contents' do
body = parse_body_contents_from_xml(@xml).to_s
body.should_not include "
"
body.should_not include ""
body.should_not include ""
body.should_not include ""
body.should include ""
body.should include ""
end
it 'should be able to extract all posts to an array' do
posts = parse_objects_from_xml(@xml)
posts.is_a?(Array).should be true
posts.count.should == 10
end
it 'should be able to correctly handle comments' do
friend = Factory.create(:friend)
post = Factory.create(:status_message)
xml = "#{Friend.first.email}\n Freedom!\n #{friend.id}\n #{post.id}}\n"
objects = parse_objects_from_xml(xml)
comment = objects.first
comment.text.should == "Freedom!"
comment.person.should == friend
comment.post.should == post
end
end
end