Merge branch 'master' of github.com:diaspora/diaspora_rails into pivots

Conflicts:
	app/models/user.rb
	spec/models/user_spec.rb
This commit is contained in:
ilya 2010-08-17 22:58:46 -07:00
commit 55b4d4758f
16 changed files with 496 additions and 603 deletions

View file

@ -1,9 +1,6 @@
class UsersController < ApplicationController class UsersController < ApplicationController
before_filter :authenticate_user!, :except => [:new, :create] before_filter :authenticate_user!, :except => [:new, :create]
def index
@users = User.sort(:created_at.desc).all
end
def show def show
@user= User.first(:id => params[:id]) @user= User.first(:id => params[:id])
@user_profile = @user.person.profile @user_profile = @user.person.profile

View file

@ -6,13 +6,13 @@ class User
key :friend_ids, Array key :friend_ids, Array
key :pending_request_ids, Array key :pending_request_ids, Array
key :post_ids, Array key :visible_post_ids, Array
one :person, :class_name => 'Person', :foreign_key => :owner_id one :person, :class_name => 'Person', :foreign_key => :owner_id
many :friends, :in => :friend_ids, :class_name => 'Person' many :friends, :in => :friend_ids, :class_name => 'Person'
many :pending_requests, :in => :pending_request_ids, :class_name => 'Request' many :pending_requests, :in => :pending_request_ids, :class_name => 'Request'
many :posts, :in => :post_ids, :class_name => 'Post' many :raw_visible_posts, :in => :visible_post_ids, :class_name => 'Post'
many :groups, :class_name => 'Group' many :groups, :class_name => 'Group'
@ -26,12 +26,12 @@ class User
#validates_true_for :email, :logic => lambda {self.pivotal_email?} #validates_true_for :email, :logic => lambda {self.pivotal_email?}
def allowed_email? def self.allowed_email?(email)
email.include?('@pivotallabs.com') || email.include?("@joindiaspora.com") email.include?('@pivotallabs.com') || email.include?("@joindiaspora.com")
end end
def pivotal_or_diaspora_only def pivotal_or_diaspora_only
raise "pivotal only" unless allowed_email? raise "pivotal only" unless User.allowed_email?(self.email)
end end
ensure_index :email ensure_index :email
@ -60,16 +60,16 @@ class User
post.socket_to_uid(id) if post.respond_to?(:socket_to_uid) post.socket_to_uid(id) if post.respond_to?(:socket_to_uid)
self.posts << post self.raw_visible_posts << post
self.save self.save
post post
end end
def posts_for( opts = {} ) def visible_posts( opts = {} )
if opts[:group] if opts[:by_members_of]
group = self.groups.find_by_id( opts[:group].id ) group = self.groups.find_by_id( opts[:by_members_of].id )
self.posts.find_all_by_person_id( (group.person_ids + [self.person.id] ), :order => "created_at desc") self.raw_visible_posts.find_all_by_person_id( (group.person_ids + [self.person.id] ), :order => "created_at desc")
end end
end end
@ -193,8 +193,8 @@ class User
groups.each{|g| g.person_ids.delete( bad_friend.id )} groups.each{|g| g.person_ids.delete( bad_friend.id )}
self.save self.save
self.posts.find_all_by_person_id( bad_friend.id ).each{|post| self.raw_visible_posts.find_all_by_person_id( bad_friend.id ).each{|post|
self.post_ids.delete( post.id ) self.visible_post_ids.delete( post.id )
post.user_refs -= 1 post.user_refs -= 1
(post.user_refs > 0 || post.person.owner.nil? == false) ? post.save : post.destroy (post.user_refs > 0 || post.person.owner.nil? == false) ? post.save : post.destroy
} }
@ -257,21 +257,6 @@ class User
person.profile = object person.profile = object
person.save person.save
elsif object.is_a?(Post) && object.verify_creator_signature == true
Rails.logger.debug("Saving post: #{object.inspect}")
object.user_refs += 1
object.save
self.posts << object
self.save
group = groups.first
Rails.logger.info("pushing a message to group: #{group.name}")
object.socket_to_uid(id, :group_id => group.id) if (object.respond_to?(:socket_to_uid) && !self.owns?(object))
dispatch_comment object if object.is_a?(Comment) && !owns?(object)
elsif object.is_a?(Comment) && object.verify_post_creator_signature elsif object.is_a?(Comment) && object.verify_post_creator_signature
if object.verify_creator_signature || object.person.nil? if object.verify_creator_signature || object.person.nil?
@ -280,10 +265,14 @@ class User
elsif object.verify_creator_signature == true elsif object.verify_creator_signature == true
Rails.logger.debug("Saving object: #{object}") Rails.logger.debug("Saving object: #{object}")
object.user_refs += 1
object.save object.save
self.raw_visible_posts << object
self.save
group = groups.find_by_person_id(object.person.id) groups = groups_with_person(object.person)
object.socket_to_uid(id, :group_id => group.id) if (object.respond_to?(:socket_to_uid) && !self.owns?(object)) object.socket_to_uid(id, :group_id => groups.first.id) if (object.respond_to?(:socket_to_uid) && !self.owns?(object))
end end
end end
@ -322,6 +311,10 @@ class User
def seed_groups def seed_groups
group(:name => "Pivots") group(:name => "Pivots")
end end
def groups_with_person person
groups.select {|group| group.person_ids.include? person.id}
end
protected protected
def setup_person def setup_person

View file

@ -1,6 +1,6 @@
Diaspora::Application.routes.draw do |map| Diaspora::Application.routes.draw do |map|
resources :people resources :people
resources :users, :only => [:edit, :show] resources :users, :only => [:edit, :show, :update]
resources :status_messages resources :status_messages
resources :comments resources :comments
resources :requests resources :requests

View file

@ -11,6 +11,4 @@ $(document).ready( function() {
$("."+vars['g']).addClass('selected'); $("."+vars['g']).addClass('selected');
$("#group img").load(function(){$(this).fadeIn("slow");});
}); });

View file

@ -67,10 +67,9 @@ header {
position: relative; position: relative;
margin: -2em; margin: -2em;
margin-bottom: 20px; margin-bottom: 20px;
color: #555555; color: black;
background-color: #2b2726; background-color: #333333;
background-color: black; border-bottom: 3px solid black;
border-bottom: 3px solid #333333;
padding: 6px 0; padding: 6px 0;
padding-top: 0; } padding-top: 0; }
header #diaspora_text { header #diaspora_text {
@ -420,7 +419,7 @@ h1.big_text {
display: none; } display: none; }
#group { #group {
color: #333333; } color: black; }
#group ul { #group ul {
margin: 0; margin: 0;
padding: 0; padding: 0;
@ -431,25 +430,25 @@ h1.big_text {
#group ul > li.selected, #group ul > li.selected a { #group ul > li.selected, #group ul > li.selected a {
color: white; color: white;
font-weight: bold; font-weight: bold;
font-size: 18px; } font-size: 18px;
text-shadow: 0 2px 0px black; }
#group a { #group a {
color: #333333; color: #aaaaaa;
font-weight: normal; } font-weight: normal; }
#group #friend_pictures .add_new { #group #friend_pictures .add_new {
position: relative; position: relative;
display: inline-block; display: inline-block;
height: 40px; height: 40px;
width: 40px; width: 40px;
background-color: #222222; background-color: black;
text-align: center; text-align: center;
font-size: 40px; font-size: 40px;
line-height: 33px; line-height: 40px;
top: -9px; } top: -6px; }
#group #friend_pictures .add_new:hover { #group #friend_pictures .add_new:hover {
background: #999999; background: #999999;
color: black; } color: black; }
#group #friend_pictures img { #group #friend_pictures img {
display: none;
height: 40px; } height: 40px; }
#add_photo_loader { #add_photo_loader {
@ -459,7 +458,7 @@ h1.big_text {
top: 4px; } top: 4px; }
#user_menu { #user_menu {
background: #333333; background: black;
padding: 5px; padding: 5px;
margin: 0; margin: 0;
list-style: none; } list-style: none; }

View file

@ -71,12 +71,11 @@ header
:position relative :position relative
:margin -2em :margin -2em
:bottom 20px :bottom 20px
:color #555 :color #000
:background :background
:color #2B2726 :color #333
:color #000
:border :border
:bottom 3px solid #333 :bottom 3px solid #000
:padding 6px 0 :padding 6px 0
:top 0 :top 0
@ -524,7 +523,7 @@ h1.big_text
:display none :display none
#group #group
:color #333 :color #000
ul ul
:margin 0 :margin 0
:padding 0 :padding 0
@ -542,7 +541,7 @@ h1.big_text
:size 18px :size 18px
a a
:color #333 :color #aaa
:font :font
:weight normal :weight normal
@ -553,18 +552,17 @@ h1.big_text
:height 40px :height 40px
:width 40px :width 40px
:background :background
:color #222 :color #000
:text-align center :text-align center
:font-size 40px :font-size 40px
:line-height 33px :line-height 40px
:top -9px :top -6px
&:hover &:hover
:background #999 :background #999
:color #000 :color #000
img img
:display none
:height 40px :height 40px
#add_photo_loader #add_photo_loader
@ -574,7 +572,7 @@ h1.big_text
:top 4px :top 4px
#user_menu #user_menu
:background #333 :background #000
:padding 5px :padding 5px
:margin 0 :margin 0
:list-style none :list-style none

View file

@ -1,32 +0,0 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe Blog do
before do
@user = Factory.create(:user, :email => "bob@aol.com")
end
it "should have a title and body" do
n = Blog.new
n.valid?.should be false
n.title = "jimmy"
n.valid?.should be false
n.body = "wales"
n.valid?.should be true
end
describe "XML" do
it 'should serialize to XML' do
body = Factory.create(:blog, :title => "yessir", :body => "penguins", :person => @user.person)
body.to_xml.to_s.should include "<title>yessir</title>"
body.to_xml.to_s.should include "<body>penguins</body>"
end
it 'should marshal serialized XML to object' do
xml = "<blog>\n <title>yessir</title>\n <body>I hate WALRUSES!</body>\n</blog>"
parsed = Blog.from_xml(xml)
parsed.body.should == "I hate WALRUSES!"
end
end
end

View file

@ -1,80 +0,0 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe Bookmark do
it "should have a link" do
bookmark = Factory.build(:bookmark, :link => nil)
bookmark.valid?.should be false
bookmark.link = "http://angjoo.com/"
bookmark.valid?.should be true
end
it 'should validate its link' do
bookmark = Factory.build(:bookmark)
#invalid links
bookmark.link = "zsdvzxdg"
bookmark.valid?.should == false
bookmark.link = "sdfasfa.c"
bookmark.valid?.should == false
bookmark.link = "http://.com/"
bookmark.valid?.should == false
bookmark.link = "http://www..com/"
bookmark.valid?.should == false
bookmark.link = "http:/www.asodij.com/"
bookmark.valid?.should == false
bookmark.link = "https:/www.asodij.com/"
bookmark.valid?.should == false
bookmark.link = "http:///www.asodij.com/"
bookmark.valid?.should == false
end
it 'should clean links' do
bad_links = [
"google.com",
"www.google.com",
"google.com/",
"www.google.com/",
"http://google.com",
"http://www.google.com"
]
bad_links.each{ |link|
Bookmark.clean_link(link).should satisfy{ |link|
/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix.match(link)
}
}
end
describe "XML" do
it 'should serialize to XML' do
u = Factory.create(:user)
message = Factory.create(:bookmark, :title => "Reddit", :link => "http://reddit.com/", :person => u.person)
message.to_xml.to_s.should include "<title>Reddit</title>"
message.to_xml.to_s.should include "<link>http://reddit.com/</link>"
end
it 'should marshal serialized XML to object' do
xml = "<bookmark><title>Reddit</message><link>http://reddit.com/</link></bookmark>"
parsed = Bookmark.from_xml(xml)
parsed.title.should == "Reddit"
parsed.link.should == "http://reddit.com/"
parsed.valid?.should be_true
end
end
describe 'with encryption' do
before do
unstub_mocha_stubs
@user = Factory.create(:user)
end
after do
stub_signature_verification
end
it 'should save a signed bookmark' do
bookmark = @user.post(:bookmark, :title => "I love cryptography", :link => "http://pgp.mit.edu/")
bookmark.created_at.should_not be nil
end
end
end

View file

@ -9,7 +9,6 @@ describe Person do
@group2 = @user2.group(:name => "Abscence of Babes") @group2 = @user2.group(:name => "Abscence of Babes")
end end
it 'should not allow two people with the same email' do it 'should not allow two people with the same email' do
person_two = Factory.build(:person, :url => @person.email) person_two = Factory.build(:person, :url => @person.email)
person_two.valid?.should == false person_two.valid?.should == false
@ -19,125 +18,129 @@ describe Person do
before do before do
@xml = @person.to_xml.to_s @xml = @person.to_xml.to_s
end end
it 'should serialize to xml' do it 'should serialize to xml' do
(@xml.include? "person").should == true @xml.include?("person").should == true
end end
it 'should have a profile in its xml' do it 'should have a profile in its xml' do
(@xml.include? "first_name").should == true @xml.include?("first_name").should == true
end end
end end
it 'should know when a post belongs to it' do it 'should know when a post belongs to it' do
person_message = Factory.create(:status_message, :person => @person) person_message = Factory.create(:status_message, :person => @person)
person_two = Factory.create(:person) person_two = Factory.create(:person)
@person.owns?(person_message).should be true @person.owns? (person_message).should be true
person_two.owns?(person_message).should be false person_two.owns?(person_message).should be false
end end
it 'should delete all of user except comments upon user deletion' do it 'should delete all of user except comments upon user deletion' do
f = Factory.create(:person) person = Factory.create(:person)
Factory.create(:status_message, :person => f) Factory.create(:status_message, :person => person)
Factory.create(:blog, :person => f) Factory.create(:status_message, :person => person)
Factory.create(:bookmark, :person => f) Factory.create(:status_message, :person => person)
Factory.create(:status_message, :person => f) Factory.create(:status_message, :person => person)
s = Factory.create(:status_message, :person => @person)
status_message = Factory.create(:status_message, :person => @person)
Factory.create(:comment, :person_id => f.id, :text => "yes i do", :post => s) Factory.create(:comment, :person_id => person.id, :text => "yes i do", :post => status_message)
Factory.create(:comment, :person_id => f.id, :text => "i love you", :post => s) Factory.create(:comment, :person_id => person.id, :text => "i love you", :post => status_message)
Factory.create(:comment, :person_id => f.id, :text => "hello", :post => s) Factory.create(:comment, :person_id => person.id, :text => "hello", :post => status_message)
Factory.create(:comment, :person_id => @person.id, :text => "you are creepy", :post => s) Factory.create(:comment, :person_id => @person.id, :text => "you are creepy", :post => status_message)
f.destroy person.destroy
Post.count.should == 1 Post.count.should == 1
Comment.all.count.should == 4 Comment.all.count.should == 4
s.comments.count.should == 4 status_message.comments.count.should == 4
end end
describe "unfriending" do describe "unfriending" do
it 'should delete an orphaned friend' do it 'should delete an orphaned friend' do
request = @user.send_friend_request_to @person.receive_url, @group.id request = @user.send_friend_request_to @person.receive_url, @group.id
@user.activate_friend(@person, @group) @user.activate_friend(@person, @group)
@user.reload @user.reload
Person.all.count.should == 3 Person.all.count.should == 3
@user.friends.count.should == 1 @user.friends.count.should == 1
@user.unfriend(@person) @user.unfriend(@person)
@user.reload @user.reload
@user.friends.count.should == 0 @user.friends.count.should == 0
Person.all.count.should == 2 Person.all.count.should == 2
end end
it 'should not delete an un-orphaned friend' do it 'should not delete an un-orphaned friend' do
request = @user.send_friend_request_to @person.receive_url, @group.id request = @user.send_friend_request_to @person.receive_url, @group.id
request2 = @user2.send_friend_request_to @person.receive_url, @group2.id request2 = @user2.send_friend_request_to @person.receive_url, @group2.id
@user.activate_friend(@person, @group) @user.activate_friend (@person, @group)
@user2.activate_friend(@person, @group2) @user2.activate_friend(@person, @group2)
@user.reload @user.reload
@user2.reload @user2.reload
Person.all.count.should == 3 Person.all.count.should == 3
@user.friends.count.should == 1 @user.friends.count.should == 1
@user2.friends.count.should == 1 @user2.friends.count.should == 1
@user.unfriend(@person) @user.unfriend(@person)
@user.reload @user.reload
@user2.reload @user2.reload
@user.friends.count.should == 0 @user.friends.count.should == 0
@user2.friends.count.should == 1 @user2.friends.count.should == 1
Person.all.count.should == 3 Person.all.count.should == 3
end end
end end
describe 'searching' do describe 'searching' do
before do before do
@friend_one = Factory.create(:person) @friend_one = Factory.create(:person)
@friend_two = Factory.create(:person) @friend_two = Factory.create(:person)
@friend_three = Factory.create(:person) @friend_three = Factory.create(:person)
@friend_four = Factory.create(:person) @friend_four = Factory.create(:person)
@friend_one.profile.first_name = "Robert" @friend_one.profile.first_name = "Robert"
@friend_one.profile.last_name = "Grimm" @friend_one.profile.last_name = "Grimm"
@friend_one.profile.save @friend_one.profile.save
@friend_two.profile.first_name = "Eugene" @friend_two.profile.first_name = "Eugene"
@friend_two.profile.last_name = "Weinstein" @friend_two.profile.last_name = "Weinstein"
@friend_two.save @friend_two.save
@friend_three.profile.first_name = "Yevgeniy" @friend_three.profile.first_name = "Yevgeniy"
@friend_three.profile.last_name = "Dodis" @friend_three.profile.last_name = "Dodis"
@friend_three.save @friend_three.save
@friend_four.profile.first_name = "Casey" @friend_four.profile.first_name = "Casey"
@friend_four.profile.last_name = "Grippi" @friend_four.profile.last_name = "Grippi"
@friend_four.save @friend_four.save
end end
it 'should yield search results on partial names' do it 'should yield search results on partial names' do
people = Person.search("Eu") people = Person.search("Eu")
people.include?(@friend_two).should == true people.include?(@friend_two).should == true
people.include?(@friend_one).should == false people.include?(@friend_one).should == false
people.include?(@friend_three).should == false people.include?(@friend_three).should == false
people.include?(@friend_four).should == false people.include?(@friend_four).should == false
people = Person.search("Wei") people = Person.search("Wei")
people.include?(@friend_two).should == true people.include?(@friend_two).should == true
people.include?(@friend_one).should == false people.include?(@friend_one).should == false
people.include?(@friend_three).should == false people.include?(@friend_three).should == false
people.include?(@friend_four).should == false people.include?(@friend_four).should == false
people = Person.search("Gri") people = Person.search("Gri")
people.include?(@friend_one).should == true people.include?(@friend_one).should == true
people.include?(@friend_four).should == true people.include?(@friend_four).should == true
people.include?(@friend_two).should == false people.include?(@friend_two).should == false
people.include?(@friend_three).should == false people.include?(@friend_three).should == false
end end
it 'should search by email exactly' do it 'should search by email exactly' do
Person.by_webfinger(@friend_one.email).should == @friend_one Person.by_webfinger(@friend_one.email).should == @friend_one
end end

View file

@ -10,35 +10,34 @@ describe Post do
before do before do
@person_one = Factory.create(:person, :email => "some@dudes.com") @person_one = Factory.create(:person, :email => "some@dudes.com")
@person_two = Factory.create(:person, :email => "other@dudes.com") @person_two = Factory.create(:person, :email => "other@dudes.com")
(2..4).each {|n| Blog.create(:title => "title #{n}", :body => "test #{n}", :person => @person_one)} (2..4).each { |n| Factory.create(:status_message, :message => "test #{n}", :person => @person_one) }
(5..8).each { |n| Blog.create(:title => "title #{n}",:body => "test #{n}", :person => @user.person)} (5..8).each { |n| Factory.create(:status_message, :message => "test #{n}", :person => @user.person)}
(9..11).each { |n| Blog.create(:title => "title #{n}",:body => "test #{n}", :person => @person_two)} (9..11).each { |n| Factory.create(:status_message, :message => "test #{n}", :person => @person_two) }
Factory.create(:status_message, :person => @user) Factory.create(:status_message, :person => @user)
Factory.create(:bookmark, :person => @user) Factory.create(:status_message, :person => @user)
end end
it "should give the most recent blog title and body from owner" do it "should give the most recent status_message title and body from owner" do
blog = Blog.newest_for(@user.person) status_message = StatusMessage.newest_for(@user.person)
blog.person.email.should == @user.person.email status_message.person.email.should == @user.person.email
blog.class.should == Blog status_message.class.should == StatusMessage
blog.title.should == "title 8" status_message.message.should == "test 8"
blog.body.should == "test 8"
end end
end end
describe "stream" do describe "stream" do
before do before do
@owner = Factory.build(:user) @owner = Factory.build(:user)
@person_one = Factory.create(:person, :email => "some@dudes.com") @person_one = Factory.create(:person, :email => "some@dudes.com")
@person_two = Factory.create(:person, :email => "other@dudes.com") @person_two = Factory.create(:person, :email => "other@dudes.com")
Factory.create(:status_message, :message => "puppies", :created_at => Time.now+1, :person => @owner.person) Factory.create(:status_message, :message => "puppies", :created_at => Time.now+1, :person => @owner.person)
Factory.create(:bookmark, :title => "Reddit", :link => "http://reddit.com", :created_at => Time.now+2, :person => @person_one) Factory.create(:status_message, :message => "http://reddit.com", :created_at => Time.now+2, :person => @person_one)
Factory.create(:status_message, :message => "kittens", :created_at => Time.now+3, :person => @person_two) Factory.create(:status_message, :message => "kittens", :created_at => Time.now+3, :person => @person_two)
Factory.create(:blog, :title => "Bears", :body => "Bear's body", :created_at => Time.now+4, :person => @owner.person) Factory.create(:status_message, :message => "Bear's body", :created_at => Time.now+4, :person => @owner.person)
Factory.create(:bookmark, :title => "Google", :link => "http://google.com", :created_at => Time.now+5, :person => @person_two) Factory.create(:status_message, :message => "Google", :created_at => Time.now+5, :person => @person_two)
end end
it "should get all posts for a specified user" do it "should get all posts for a specified user" do
@ -52,7 +51,7 @@ describe Post do
describe 'xml' do describe 'xml' do
it 'should serialize to xml with its person' do it 'should serialize to xml with its person' do
message = Factory.create(:status_message, :person => @user.person) message = Factory.create(:status_message, :person => @user.person)
(message.to_xml.to_s.include? @user.person.email).should == true message.to_xml.to_s.include?(@user.person.email).should == true
end end
end end

View file

@ -14,12 +14,11 @@ describe Request do
end end
it 'should generate xml for the User as a Person' do it 'should generate xml for the User as a Person' do
request = @user.send_friend_request_to "http://www.google.com/", @group.id request = @user.send_friend_request_to "http://www.google.com/", @group.id
xml = request.to_xml.to_s xml = request.to_xml.to_s
xml.include?(@user.person.email).should be true xml.include?(@user.email).should be true
xml.include?(@user.url).should be true xml.include?(@user.url).should be true
xml.include?(@user.profile.first_name).should be true xml.include?(@user.profile.first_name).should be true
xml.include?(@user.profile.last_name).should be true xml.include?(@user.profile.last_name).should be true

View file

@ -0,0 +1,122 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe User do
before do
@user = Factory.create :user
@group = @user.group(:name => 'heroes')
@user2 = Factory.create(:user)
@group2 = @user2.group(:name => 'losers')
#Factory.create :friend, @user
friend_users(@user, @group, @user2, @group2)
end
it 'should be able to parse and store a status message from xml' do
status_message = @user2.post :status_message, :message => 'store this!'
person = @user2.person
xml = status_message.to_diaspora_xml
@user2.destroy
status_message.destroy
StatusMessage.all.size.should == 0
@user.receive( xml )
person.posts.first.message.should == 'store this!'
StatusMessage.all.size.should == 1
end
describe 'post refs' do
before do
@user3 = Factory.create(:user)
@group3 = @user3.group(:name => 'heroes')
end
it "should add the post to that user's posts when a user posts it" do
status_message = @user.post :status_message, :message => "hi"
@user.reload
@user.raw_visible_posts.include?(status_message).should be true
end
it 'should be removed on unfriending' do
status_message = @user2.post :status_message, :message => "hi"
@user.receive status_message.to_diaspora_xml
@user.reload
@user.raw_visible_posts.count.should == 1
@user.unfriend(@user2.person)
@user.reload
@user.raw_visible_posts.count.should == 0
Post.count.should be 1
end
it 'should be remove a post if the noone links to it' do
status_message = @user2.post :status_message, :message => "hi"
@user.receive status_message.to_diaspora_xml
@user.reload
@user.raw_visible_posts.count.should == 1
person = @user2.person
@user2.destroy
@user.unfriend(person)
@user.reload
@user.raw_visible_posts.count.should == 0
Post.count.should be 0
end
it 'should keep track of user references for one person ' do
status_message = @user2.post :status_message, :message => "hi"
@user.receive status_message.to_diaspora_xml
@user.reload
@user.raw_visible_posts.count.should == 1
status_message.reload
status_message.user_refs.should == 1
@user.unfriend(@user2.person)
status_message.reload
@user.reload
@user.raw_visible_posts.count.should == 0
status_message.reload
status_message.user_refs.should == 0
Post.count.should be 1
end
it 'should not override userrefs on receive by another person' do
@user3.activate_friend(@user2.person, @group3)
status_message = @user2.post :status_message, :message => "hi"
@user.receive status_message.to_diaspora_xml
@user3.receive status_message.to_diaspora_xml
@user.reload
@user3.reload
@user.raw_visible_posts.count.should == 1
status_message.reload
status_message.user_refs.should == 2
@user.unfriend(@user2.person)
status_message.reload
@user.reload
@user.raw_visible_posts.count.should == 0
status_message.reload
status_message.user_refs.should == 1
Post.count.should be 1
end
end
end

View file

@ -0,0 +1,223 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe User do
before do
@user = Factory.create(:user)
@group = @user.group(:name => 'heroes')
end
describe 'friend requesting' do
it "should assign a request to a group" do
friend = Factory.create(:person)
group = @user.group(:name => "Dudes")
group.requests.size.should == 0
@user.send_friend_request_to(friend.receive_url, group.id)
group.reload
group.requests.size.should == 1
end
it "should be able to accept a pending friend request" do
friend = Factory.create(:person)
r = Request.instantiate(:to => @user.receive_url, :from => friend)
r.save
Person.all.count.should == 2
Request.for_user(@user).all.count.should == 1
@user.accept_friend_request(r.id, @group.id)
Request.for_user(@user).all.count.should == 0
end
it 'should be able to ignore a pending friend request' do
friend = Factory.create(:person)
r = Request.instantiate(:to => @user.receive_url, :from => friend)
r.save
Person.count.should == 2
@user.ignore_friend_request(r.id)
Person.count.should == 1
Request.count.should == 0
end
it 'should not be able to friend request an existing friend' do friend = Factory.create(:person)
@user.friends << friend
@user.save
@user.send_friend_request_to( friend.receive_url, @group.id ).should be nil
end
describe 'multiple users accepting/rejecting the same person' do
before do
@person_one = Factory.create :person
@person_one.save
@user2 = Factory.create :user
@group2 = @user2.group(:name => "group two")
@user.pending_requests.empty?.should be true
@user.friends.empty?.should be true
@user2.pending_requests.empty?.should be true
@user2.friends.empty?.should be true
@request = Request.instantiate(:to => @user.receive_url, :from => @person_one)
@request_two = Request.instantiate(:to => @user2.receive_url, :from => @person_one)
@request_three = Request.instantiate(:to => @user2.receive_url, :from => @user.person)
@req_xml = @request.to_diaspora_xml
@req_two_xml = @request_two.to_diaspora_xml
@req_three_xml = @request_three.to_diaspora_xml
@request.destroy
@request_two.destroy
@request_three.destroy
end
it 'should befriend the user other user on the same pod' do
@user2.receive @req_three_xml
@user2.pending_requests.size.should be 1
@user2.accept_friend_request @request_three.id, @group2.id
@user2.friends.include?(@user.person).should be true
Person.all.count.should be 3
end
it 'should not delete the ignored user on the same pod' do
@user2.receive @req_three_xml
@user2.pending_requests.size.should be 1
@user2.ignore_friend_request @request_three.id
@user2.friends.include?(@user.person).should be false
Person.all.count.should be 3
end
it 'should both users should befriend the same person' do
@user.receive @req_xml
@user.pending_requests.size.should be 1
@user.accept_friend_request @request.id, @group.id
@user.friends.include?(@person_one).should be true
@user2.receive @req_two_xml
@user2.pending_requests.size.should be 1
@user2.accept_friend_request @request_two.id, @group2.id
@user2.friends.include?(@person_one).should be true
Person.all.count.should be 3
end
it 'should keep the person around if one of the users rejects him' do
@user.receive @req_xml
@user.pending_requests.size.should be 1
@user.accept_friend_request @request.id, @group.id
@user.friends.include?(@person_one).should be true
@user2.receive @req_two_xml
@user2.pending_requests.size.should be 1
@user2.ignore_friend_request @request_two.id
@user2.friends.include?(@person_one).should be false
Person.all.count.should be 3
end
it 'should not keep the person around if the users ignores them' do
@user.receive @req_xml
@user.pending_requests.size.should be 1
@user.ignore_friend_request @user.pending_requests.first.id
@user.friends.include?(@person_one).should be false
@user2.receive @req_two_xml
@user2.pending_requests.size.should be 1
@user2.ignore_friend_request @user2.pending_requests.first.id#@request_two.id
@user2.friends.include?(@person_one).should be false
Person.all.count.should be 2
end
end
describe 'a user accepting rejecting multiple people' do
before do
@person_one = Factory.create :person
@person_two = Factory.create :person
@user.pending_requests.empty?.should be true
@user.friends.empty?.should be true
@request = Request.instantiate(:to => @user.receive_url, :from => @person_one)
@request_two = Request.instantiate(:to => @user.receive_url, :from => @person_two)
end
after do
@user.receive_friend_request @request
@person_two.destroy
@user.pending_requests.size.should be 1
@user.friends.size.should be 0
@user.receive_friend_request @request_two
@user.pending_requests.size.should be 2
@user.friends.size.should be 0
@user.accept_friend_request @request.id, @group.id
@user.pending_requests.size.should be 1
@user.friends.size.should be 1
@user.friends.include?(@person_one).should be true
@user.ignore_friend_request @request_two.id
@user.pending_requests.size.should be 0
@user.friends.size.should be 1
@user.friends.include?(@person_two).should be false
end
end
describe 'unfriending' do
before do
@user2 = Factory.create :user
@group2 = @user2.group(:name => "Gross people")
request = @user.send_friend_request_to( @user2.receive_url, @group.id)
request.reverse_for @user2
@user2.activate_friend(@user.person, @group2)
@user.receive request.to_diaspora_xml
end
it 'should unfriend the other user on the same seed' do
@user.reload
@user2.reload
@user.friends.count.should == 1
@user2.friends.count.should == 1
@user.person.user_refs.should == 1
@user2.person.user_refs.should == 1
@user2.unfriend @user.person
@user2.friends.count.should be 0
@user.person.reload
@user.person.user_refs.should == 0
@user.unfriended_by @user2.person
@user2.person.reload
@user2.person.user_refs.should == 0
@group.reload
@group2.reload
@group.people.count.should == 0
@group2.people.count.should == 0
end
end
end
end

View file

@ -0,0 +1,42 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe User do
before do
@user = Factory.create(:user)
@group = @user.group(:name => 'heroes')
@group2 = @user.group(:name => 'losers')
@user2 = Factory.create :user
@user2_group = @user2.group(:name => 'dudes')
friend_users(@user, @group, @user2, @user2_group)
@user3 = Factory.create :user
@user3_group = @user3.group(:name => 'dudes')
friend_users(@user, @group2, @user3, @user3_group)
@user4 = Factory.create :user
@user4_group = @user4.group(:name => 'dudes')
friend_users(@user, @group2, @user4, @user4_group)
end
it 'should generate a valid stream for a group of people' do
status_message1 = @user2.post :status_message, :message => "hi"
status_message2 = @user3.post :status_message, :message => "heyyyy"
status_message3 = @user4.post :status_message, :message => "yooo"
@user.receive status_message1.to_diaspora_xml
@user.receive status_message2.to_diaspora_xml
@user.receive status_message3.to_diaspora_xml
@user.reload
@user.visible_posts(:by_members_of => @group).include?(status_message1).should be true
@user.visible_posts(:by_members_of => @group).include?(status_message2).should be false
@user.visible_posts(:by_members_of => @group).include?(status_message3).should be false
@user.visible_posts(:by_members_of => @group2).include?(status_message1).should be false
@user.visible_posts(:by_members_of => @group2).include?(status_message2).should be true
@user.visible_posts(:by_members_of => @group2).include?(status_message3).should be true
end
end

View file

@ -6,179 +6,6 @@ describe User do
@group = @user.group(:name => 'heroes') @group = @user.group(:name => 'heroes')
end end
describe 'friend requesting' do
it "should assign a request to a group" do
friend = Factory.create(:person)
group = @user.group(:name => "Dudes")
group.requests.size.should == 0
@user.send_friend_request_to(friend.receive_url, group.id)
group.reload
group.requests.size.should == 1
end
it "should be able to accept a pending friend request" do
friend = Factory.create(:person)
r = Request.instantiate(:to => @user.receive_url, :from => friend)
r.save
Person.all.count.should == 2
Request.for_user(@user).all.count.should == 1
@user.accept_friend_request(r.id, @group.id)
Request.for_user(@user).all.count.should == 0
end
it 'should be able to ignore a pending friend request' do
friend = Factory.create(:person)
r = Request.instantiate(:to => @user.receive_url, :from => friend)
r.save
Person.count.should == 2
@user.ignore_friend_request(r.id)
Person.count.should == 1
Request.count.should == 0
end
it 'should not be able to friend request an existing friend' do friend = Factory.create(:person)
@user.friends << friend
@user.save
@user.send_friend_request_to( friend.receive_url, @group.id ).should be nil
end
describe 'multiple users accepting/rejecting the same person' do
before do
@person_one = Factory.create :person
@person_one.save
@user2 = Factory.create :user
@group2 = @user2.group(:name => "group two")
@user.pending_requests.empty?.should be true
@user.friends.empty?.should be true
@user2.pending_requests.empty?.should be true
@user2.friends.empty?.should be true
@request = Request.instantiate(:to => @user.receive_url, :from => @person_one)
@request_two = Request.instantiate(:to => @user2.receive_url, :from => @person_one)
@request_three = Request.instantiate(:to => @user2.receive_url, :from => @user.person)
@req_xml = @request.to_diaspora_xml
@req_two_xml = @request_two.to_diaspora_xml
@req_three_xml = @request_three.to_diaspora_xml
@request.destroy
@request_two.destroy
@request_three.destroy
end
it 'should befriend the user other user on the same pod' do
@user2.receive @req_three_xml
@user2.pending_requests.size.should be 1
@user2.accept_friend_request @request_three.id, @group2.id
@user2.friends.include?(@user.person).should be true
Person.all.count.should be 3
end
it 'should not delete the ignored user on the same pod' do
@user2.receive @req_three_xml
@user2.pending_requests.size.should be 1
@user2.ignore_friend_request @request_three.id
@user2.friends.include?(@user.person).should be false
Person.all.count.should be 3
end
it 'should both users should befriend the same person' do
@user.receive @req_xml
@user.pending_requests.size.should be 1
@user.accept_friend_request @request.id, @group.id
@user.friends.include?(@person_one).should be true
@user2.receive @req_two_xml
@user2.pending_requests.size.should be 1
@user2.accept_friend_request @request_two.id, @group2.id
@user2.friends.include?(@person_one).should be true
Person.all.count.should be 3
end
it 'should keep the person around if one of the users rejects him' do
@user.receive @req_xml
@user.pending_requests.size.should be 1
@user.accept_friend_request @request.id, @group.id
@user.friends.include?(@person_one).should be true
@user2.receive @req_two_xml
@user2.pending_requests.size.should be 1
@user2.ignore_friend_request @request_two.id
@user2.friends.include?(@person_one).should be false
Person.all.count.should be 3
end
it 'should not keep the person around if the users ignores them' do
@user.receive @req_xml
@user.pending_requests.size.should be 1
@user.ignore_friend_request @user.pending_requests.first.id
@user.friends.include?(@person_one).should be false
@user2.receive @req_two_xml
@user2.pending_requests.size.should be 1
@user2.ignore_friend_request @user2.pending_requests.first.id#@request_two.id
@user2.friends.include?(@person_one).should be false
Person.all.count.should be 2
end
end
describe 'a user accepting rejecting multiple people' do
before do
@person_one = Factory.create :person
@person_two = Factory.create :person
@user.pending_requests.empty?.should be true
@user.friends.empty?.should be true
@request = Request.instantiate(:to => @user.receive_url, :from => @person_one)
@request_two = Request.instantiate(:to => @user.receive_url, :from => @person_two)
end
after do
@user.receive_friend_request @request
@person_two.destroy
@user.pending_requests.size.should be 1
@user.friends.size.should be 0
@user.receive_friend_request @request_two
@user.pending_requests.size.should be 2
@user.friends.size.should be 0
@user.accept_friend_request @request.id, @group.id
@user.pending_requests.size.should be 1
@user.friends.size.should be 1
@user.friends.include?(@person_one).should be true
@user.ignore_friend_request @request_two.id
@user.pending_requests.size.should be 0
@user.friends.size.should be 1
@user.friends.include?(@person_two).should be false
end
end
end
describe 'profiles' do describe 'profiles' do
it 'should be able to update their profile and send it to their friends' do it 'should be able to update their profile and send it to their friends' do
Factory.create(:person) Factory.create(:person)
@ -191,205 +18,4 @@ describe User do
@user.profile.image_url.should == "http://clown.com" @user.profile.image_url.should == "http://clown.com"
end end
end end
describe 'receiving' do
before do
@user2 = Factory.create(:user)
@user.friends << @user2.person
@user2.friends << @user.person
@user.person.user_refs += 1
@user2.person.user_refs += 1
@user.save
@user2.save
end
it 'should be able to parse and store a status message from xml' do
status_message = @user2.post :status_message, :message => 'store this!'
person = @user2.person
xml = status_message.to_diaspora_xml
@user2.destroy
status_message.destroy
StatusMessage.all.size.should == 0
@user.receive( xml )
person.posts.first.message.should == 'store this!'
StatusMessage.all.size.should == 1
end
end
describe 'unfriending' do
before do
@user2 = Factory.create :user
@group2 = @user2.group(:name => "Gross people")
request = @user.send_friend_request_to( @user2.receive_url, @group.id)
request.reverse_for @user2
@user2.activate_friend(@user.person, @group2)
@user.receive request.to_diaspora_xml
end
it 'should unfriend the other user on the same seed' do
@user.reload
@user2.reload
@user.friends.count.should == 1
@user2.friends.count.should == 1
@user.person.user_refs.should == 1
@user2.person.user_refs.should == 1
@user2.unfriend @user.person
@user2.friends.count.should be 0
@user.person.reload
@user.person.user_refs.should == 0
@user.unfriended_by @user2.person
@user2.person.reload
@user2.person.user_refs.should == 0
@group.reload
@group2.reload
@group.people.count.should == 0
@group2.people.count.should == 0
end
end
describe 'post refs' do
before do
@user2 = Factory.create(:user)
@user.activate_friend( @user2.person, @group)
@user3 = Factory.create(:user)
@group3 = @user3.group(:name => 'heroes')
@user.posts.count.should == 0
end
it "should add the post to that user's posts when a user posts it" do
status_message = @user.post :status_message, :message => "hi"
@user.reload
@user.posts.include?(status_message).should be true
end
it 'should be removed on unfriending' do
status_message = @user2.post :status_message, :message => "hi"
@user.receive status_message.to_diaspora_xml
@user.reload
@user.posts.count.should == 1
@user.unfriend(@user2.person)
@user.reload
@user.posts.count.should == 0
Post.count.should be 1
end
it 'should be remove a post if the noone links to it' do
status_message = @user2.post :status_message, :message => "hi"
@user.receive status_message.to_diaspora_xml
@user.reload
@user.posts.count.should == 1
person = @user2.person
@user2.destroy
@user.unfriend(person)
@user.reload
@user.posts.count.should == 0
Post.count.should be 0
end
it 'should keep track of user references for one person ' do
status_message = @user2.post :status_message, :message => "hi"
@user.receive status_message.to_diaspora_xml
@user.reload
@user.posts.count.should == 1
status_message.reload
status_message.user_refs.should == 1
@user.unfriend(@user2.person)
status_message.reload
@user.reload
@user.posts.count.should == 0
status_message.reload
status_message.user_refs.should == 0
Post.count.should be 1
end
it 'should not override userrefs on receive by another person' do
@user3 = Factory.create :user
@user3.activate_friend(@user2.person, @group3)
status_message = @user2.post :status_message, :message => "hi"
@user.receive status_message.to_diaspora_xml
@user3.receive status_message.to_diaspora_xml
@user.reload
@user3.reload
@user.posts.count.should == 1
status_message.reload
status_message.user_refs.should == 2
@user.unfriend(@user2.person)
status_message.reload
@user.reload
@user.posts.count.should == 0
status_message.reload
status_message.user_refs.should == 1
Post.count.should be 1
end
describe 'group streams' do
before do
@group = @user.group(:name => 'heroes')
@group2 = @user.group(:name => 'stuff')
@user2 = Factory.create :user
@user.activate_friend(@user2.person, @group)
@user3 = Factory.create :user
@user.activate_friend(@user3.person, @group2)
@user4 = Factory.create :user
@user.activate_friend(@user4.person, @group2)
end
it 'should generate a valid stream for a group of people' do
status_message1 = @user2.post :status_message, :message => "hi"
status_message2 = @user3.post :status_message, :message => "heyyyy"
status_message3 = @user4.post :status_message, :message => "yooo"
@user.receive status_message1.to_diaspora_xml
@user.receive status_message2.to_diaspora_xml
@user.receive status_message3.to_diaspora_xml
@user.reload
@user.posts_for(:group => @group).include?(status_message1).should be true
@user.posts_for(:group => @group).include?(status_message2).should be false
@user.posts_for(:group => @group).include?(status_message3).should be false
@user.posts_for(:group => @group2).include?(status_message1).should be false
@user.posts_for(:group => @group2).include?(status_message2).should be true
@user.posts_for(:group => @group2).include?(status_message3).should be true
end
end
end
end end

View file

@ -26,7 +26,7 @@ RSpec.configure do |config|
config.before(:each) do config.before(:each) do
DatabaseCleaner.start DatabaseCleaner.start
stub_sockets_controller stub_sockets_controller
User.stub(:pivotal_only).and_return(:true) User.stub!(:allowed_email?).and_return(:true)
end end
config.after(:each) do config.after(:each) do
@ -77,3 +77,9 @@ end
def message_queue def message_queue
Post.send(:class_variable_get, :@@queue) Post.send(:class_variable_get, :@@queue)
end end
def friend_users(user1, group1, user2, group2)
request = user1.send_friend_request_to(user2.receive_url, group1.id)
reversed_request = user2.accept_friend_request( request.id, group2.id)
user1.receive reversed_request.to_diaspora_xml
end