11 Controller specs remain in mysql. ALMOST DONE

This commit is contained in:
Michael Sofaer and Raphael Sofaer 2010-12-24 14:12:55 -08:00
parent 82b226712f
commit 30d60021f4
7 changed files with 68 additions and 50 deletions

View file

@ -13,28 +13,31 @@ class CommentsController < ApplicationController
target = current_user.find_visible_post_by_id params[:post_id]
text = params[:text]
@comment = current_user.build_comment(text, :on => target)
if target
@comment = current_user.build_comment(text, :on => target)
if @comment.save(:safe => true)
raise 'MongoMapper failed to catch a failed save' unless @comment.id
Rails.logger.info("event=comment_create user=#{current_user.diaspora_handle} status=success comment=#{@comment.id}")
current_user.dispatch_comment(@comment)
if @comment.save
Rails.logger.info("event=comment_create user=#{current_user.diaspora_handle} status=success comment=#{@comment.id}")
current_user.dispatch_comment(@comment)
respond_to do |format|
format.js{
json = { :post_id => @comment.post_id,
:comment_id => @comment.id,
:html => render_to_string(
:partial => 'comments/comment',
:locals => { :hash => {
:comment => @comment,
:person => current_user,
}}
)
}
render(:json => json, :status => 201)
}
format.html{ render :nothing => true, :status => 201 }
respond_to do |format|
format.js{
json = { :post_id => @comment.post_id,
:comment_id => @comment.id,
:html => render_to_string(
:partial => 'comments/comment',
:locals => { :hash => {
:comment => @comment,
:person => current_user,
}}
)
}
render(:json => json, :status => 201)
}
format.html{ render :nothing => true, :status => 201 }
end
else
render :nothing => true, :status => 406
end
else
render :nothing => true, :status => 406

View file

@ -30,7 +30,7 @@ class Comment < ActiveRecord::Base
person.diaspora_handle
end
def diaspora_handle= nh
self.person = Person.where(:diaspora_handle => nh).first
self.person = Webfinger.new(nh).fetch
end
def post_guid
self.post.guid

View file

@ -126,7 +126,7 @@ class Person < ActiveRecord::Base
#hcard_profile = HCard.find profile.hcard.first[:href]
Rails.logger.info("event=webfinger_marshal valid=#{new_person.valid?} target=#{new_person.diaspora_handle}")
new_person.url = hcard[:url]
new_person.create_profile(:first_name => hcard[:given_name],
new_person.profile = Profile.create!(:first_name => hcard[:given_name],
:last_name => hcard[:family_name],
:image_url => hcard[:photo],
:image_url_medium => hcard[:photo_medium],

View file

@ -80,7 +80,7 @@ module Diaspora
def remove_contact(bad_person)
contact = contact_for(bad_person)
posts = raw_visible_posts.where(:person_id => bad_person.id)
posts = raw_visible_posts.where(:person_id => bad_person.id).all
visibilities = PostVisibility.joins(:post, :aspect).where(
:posts => {:person_id => bad_person.id},
:aspects => {:user_id => self.id}
@ -88,7 +88,7 @@ module Diaspora
visibility_ids = visibilities.map{|v| v.id}
PostVisibility.where(:id => visibility_ids).delete_all
posts.each do |post|
if post.user_refs < 1
if post.post_visibilities(true).count < 1
post.destroy
end
end

View file

@ -253,21 +253,22 @@ describe PeopleController do
image_url = user.person.profile.image_url
put :update, @params
user.person.reload
user.person.profile.image_url.should == image_url
Person.find(user.person.id).profile.image_url.should == image_url
end
end
it 'does not allow mass assignment' do
person = user.person
new_user = Factory.create(:user)
person.owner_id.should == user.id
put :update, :id => user.person.id, :owner_id => new_user.id
user.person.reload.owner_id.should_not == new_user.id
Person.find(person.id).owner_id.should == user.id
end
it 'does not overwrite the profile diaspora handle' do
handle_params = {:id => user.person.id,
:profile => {:diaspora_handle => 'abc@a.com'} }
put :update, handle_params
user.person.reload.profile[:diaspora_handle].should_not == 'abc@a.com'
Person.find(user.person.id).profile[:diaspora_handle].should_not == 'abc@a.com'
end
end
end

View file

@ -135,7 +135,7 @@ describe Photo do
@photo.destroy
user2.receive xml, @user.person
new_photo = Photo.find(id)
new_photo = Photo.where(:guid => @photo.guid).first
new_photo.url.nil?.should be false
new_photo.url.include?(url).should be true
new_photo.url(:thumb_medium).include?(thumb_url).should be true

View file

@ -99,8 +99,6 @@ describe User do
person = Factory(:person)
user.activate_contact(person, aspect)
post = Factory.create(:status_message, :person => person)
puts
pp post
post.post_visibilities.should be_empty
user.receive post.to_diaspora_xml, person
aspect.post_visibilities.reset
@ -112,7 +110,21 @@ describe User do
user.disconnected_by(person)
}.should change(Post, :count).by(-1)
end
it 'deletes post_visibilities on disconnected by' do
person = Factory(:person)
user.activate_contact(person, aspect)
post = Factory.create(:status_message, :person => person)
post.post_visibilities.should be_empty
user.receive post.to_diaspora_xml, person
aspect.post_visibilities.reset
aspect.posts(true).should include(post)
post.post_visibilities.reset
post.post_visibilities.length.should == 1
lambda {
user.disconnected_by(person)
}.should change{post.post_visibilities(true).count}.by(-1)
end
it 'should keep track of user references for one person ' do
@status_message.reload
@status_message.user_refs.should == 2
@ -143,9 +155,6 @@ describe User do
connect_users(user, aspect, user3, aspect3)
@post = user.post :status_message, :message => "hello", :to => aspect.id
user2.receive @post.to_diaspora_xml, user.person
user3.receive @post.to_diaspora_xml, user.person
@comment = user3.comment('tada',:on => @post)
@comment.post_creator_signature = @comment.sign_with_key(user.encryption_key)
@xml = @comment.to_diaspora_xml
@ -156,33 +165,38 @@ describe User do
local_person = user3.person
user2.reload.raw_visible_posts.size.should == 1
post_in_db = user2.raw_visible_posts.first
post_in_db = StatusMessage.find(@post.id)
post_in_db.comments.should == []
user2.receive(@xml, user.person)
post_in_db.comments.reset
post_in_db.comments.include?(@comment).should be true
post_in_db.comments.first.person.should == local_person
lambda{
user2.receive(@xml, user.person)
}.should change{StatusMessage.find(@post.id).comments.count}.by(1)
end
it 'should correctly marshal a stranger for the downstream user' do
remote_person = user3.person
remote_person.delete
remote_person = user3.person.dup
user3.person.delete
user3.delete
remote_person.id = nil
#stubs async webfinger
Person.should_receive(:by_account_identifier).and_return{ |handle| if handle == user.person.diaspora_handle; user.person.save
user.person; else; remote_person.save; remote_person; end }
Person.should_receive(:by_account_identifier).twice.and_return{ |handle|
if handle == user.person.diaspora_handle
user.person.save
user.person
else
remote_person.profile = Factory(:profile)
remote_person.save!
remote_person
end
}
user2.reload.raw_visible_posts.size.should == 1
post_in_db = user2.raw_visible_posts.first
post_in_db = StatusMessage.find(@post.id)
post_in_db.comments.should == []
user2.receive(@xml, user.person)
post_in_db.reload
post_in_db.comments.include?(@comment).should be true
post_in_db.comments.first.person.should == remote_person
lambda{
user2.receive(@xml, user.person)
}.should change{StatusMessage.find(@post.id).comments.count}.by(1)
end
end