Skip to content
Extraits de code Groupes Projets
Valider 7d7c39e3 rédigé par Raphael's avatar Raphael
Parcourir les fichiers

callbacks not running on photo save

parent 40435d68
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
class Photo < Post
require 'carrierwave/orm/mongomapper'
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
end
......@@ -36,15 +36,15 @@ class Post
self.first(:person_id => person.id, :order => '_id desc')
end
def self.my_newest
self.newest(User.owner)
end
def self.my_newest
self.newest(User.owner)
end
def self.newest_by_email(email)
self.newest(Person.first(:email => email))
end
#ENCRYPTION
before_validation :sign_if_mine
before_validation :sign_if_mine
validates_true_for :creator_signature, :logic => lambda {self.verify_creator_signature}
xml_accessor :creator_signature
......
......@@ -3,6 +3,7 @@
""
end
def verify_creator_signature
#creator_signature = sign if creator_signature.nil? && person == User.owner
verify_signature(creator_signature, person)
end
......@@ -10,21 +11,24 @@
return false unless signature && person.key_fingerprint
validity = nil
GPGME::verify(signature, signable_string,
{:armor => true, :always_trust => true}){ |sig|
validity = sig.status == GPGME::GPG_ERR_NO_ERROR &&
sig.fpr == person.key_fingerprint
{:armor => true, :always_trust => true}){ |signature_analysis|
puts signature_analysis
validity = signature_analysis.status == GPGME::GPG_ERR_NO_ERROR &&
signature_analysis.fpr == person.key_fingerprint
}
return validity
end
protected
def sign_if_mine
puts "In sign_if_mine"
if self.person == User.owner
self.creator_signature = sign
end
end
def sign
puts "signing"
sign_with_key(User.owner.key)
end
......
......@@ -3,20 +3,38 @@ require File.dirname(__FILE__) + '/../spec_helper'
describe Photo do
before do
@user = Factory.create(:user)
@fixture_name = File.dirname(__FILE__) + '/../fixtures/bp.jpeg'
end
it 'should save a photo to GridFS' do
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.save.should == true
binary = photo.image.read
fixture_binary = File.open(fixture_name).read
fixture_binary = File.open(@fixture_name).read
binary.should == fixture_binary
end
it 'should create thumbnails' do
pending('need to figure this out... tearing issue')
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
......@@ -42,30 +42,31 @@ end
end
def stub_signature_verification
post_models = []
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.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)
post_models.each{ | model|
model.any_instance.stubs(:verify_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)
User.any_instance.stubs(:remove_key).returns(true)
end
def unstub_mocha_stubs
Mocha::Mockery.instance.stubba.unstub_all
end
def get_models
models = []
Dir.glob( RAILS_ROOT + '/app/models/*' ).each do |f|
models << File.basename( f ).gsub( /^(.+).rb/, '\1')
def get_models
models = []
Dir.glob( File.dirname(__FILE__) + '/../app/models/*' ).each do |f|
models << File.basename( f ).gsub( /^(.+).rb/, '\1')
end
models
end
models
end
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter