callbacks not running on photo save

This commit is contained in:
Raphael 2010-07-17 14:09:21 -07:00
parent 40435d6891
commit 7d7c39e3dc
5 changed files with 50 additions and 25 deletions

View file

@ -1,6 +1,8 @@
class Photo < Post class Photo < Post
require 'carrierwave/orm/mongomapper' require 'carrierwave/orm/mongomapper'
include MongoMapper::Document include MongoMapper::Document
before_validation {puts "I'M GONNA VALIDATE"}
before_save {puts "I'M GONNA SAVE"}
before_create {puts "I'M GONNA CREATE"}
mount_uploader :image, ImageUploader mount_uploader :image, ImageUploader
end end

View file

@ -3,6 +3,7 @@
"" ""
end end
def verify_creator_signature def verify_creator_signature
#creator_signature = sign if creator_signature.nil? && person == User.owner
verify_signature(creator_signature, person) verify_signature(creator_signature, person)
end end
@ -10,21 +11,24 @@
return false unless signature && person.key_fingerprint return false unless signature && person.key_fingerprint
validity = nil validity = nil
GPGME::verify(signature, signable_string, GPGME::verify(signature, signable_string,
{:armor => true, :always_trust => true}){ |sig| {:armor => true, :always_trust => true}){ |signature_analysis|
validity = sig.status == GPGME::GPG_ERR_NO_ERROR && puts signature_analysis
sig.fpr == person.key_fingerprint validity = signature_analysis.status == GPGME::GPG_ERR_NO_ERROR &&
signature_analysis.fpr == person.key_fingerprint
} }
return validity return validity
end end
protected protected
def sign_if_mine def sign_if_mine
puts "In sign_if_mine"
if self.person == User.owner if self.person == User.owner
self.creator_signature = sign self.creator_signature = sign
end end
end end
def sign def sign
puts "signing"
sign_with_key(User.owner.key) sign_with_key(User.owner.key)
end end

View file

@ -3,20 +3,38 @@ require File.dirname(__FILE__) + '/../spec_helper'
describe Photo do describe Photo do
before do before do
@user = Factory.create(:user) @user = Factory.create(:user)
@fixture_name = File.dirname(__FILE__) + '/../fixtures/bp.jpeg'
end end
it 'should save a photo to GridFS' do it 'should save a photo to GridFS' do
photo = Photo.new(:person => @user) photo = Photo.new(:person => @user)
fixture_name = File.dirname(__FILE__) + '/../fixtures/bp.jpeg' file = File.open(@fixture_name)
file = File.open(fixture_name)
photo.image = file photo.image = file
photo.save.should == true photo.save.should == true
binary = photo.image.read binary = photo.image.read
fixture_binary = File.open(fixture_name).read fixture_binary = File.open(@fixture_name).read
binary.should == fixture_binary binary.should == fixture_binary
end end
it 'should create thumbnails' do it 'should create thumbnails' do
pending('need to figure this out... tearing issue') pending('need to figure this out... tearing issue')
end end
describe 'with encryption' do
before do
unstub_mocha_stubs
end
after do
stub_signature_verification
end
it 'should save a signed photo to GridFS' do
photo = Photo.new(:person => @user)
photo.image = File.open(@fixture_name)
#photo.creator_signature = photo.send(:sign)
photo.verify_creator_signature.should be true
photo.save.should == true
end
end end
end

View file

@ -42,29 +42,30 @@ end
end end
def stub_signature_verification def stub_signature_verification
post_models = []
get_models.each{ |model| get_models.each{ |model|
puts model constant_model = model.camelize.constantize
if constant_model == Post || constant_model.superclass == Post
post_models << constant_model
end
}
post_models.each{ | model|
model.any_instance.stubs(:verify_creator_signature).returns(true)
} }
Post.any_instance.stubs(:verify_creator_signature).returns(true)
StatusMessage.any_instance.stubs(:verify_creator_signature).returns(true)
Blog.any_instance.stubs(:verify_creator_signature).returns(true)
Bookmark.any_instance.stubs(:verify_creator_signature).returns(true)
Comment.any_instance.stubs(:verify_creator_signature).returns(true)
Comment.any_instance.stubs(:verify_post_creator_signature).returns(true) Comment.any_instance.stubs(:verify_post_creator_signature).returns(true)
Photo.any_instance.stubs(:verify_creator_signature).returns(true)
Person.any_instance.stubs(:remove_key).returns(true) Person.any_instance.stubs(:remove_key).returns(true)
User.any_instance.stubs(:remove_key).returns(true) User.any_instance.stubs(:remove_key).returns(true)
end end
def unstub_mocha_stubs def unstub_mocha_stubs
Mocha::Mockery.instance.stubba.unstub_all Mocha::Mockery.instance.stubba.unstub_all
end end
def get_models def get_models
models = [] models = []
Dir.glob( RAILS_ROOT + '/app/models/*' ).each do |f| Dir.glob( File.dirname(__FILE__) + '/../app/models/*' ).each do |f|
models << File.basename( f ).gsub( /^(.+).rb/, '\1') models << File.basename( f ).gsub( /^(.+).rb/, '\1')
end end
models models