Skip to content
Extraits de code Groupes Projets
photo_spec.rb 3,66 ko
Newer Older
  • Learn to ignore specific revisions
  • Raphael's avatar
    Raphael a validé
    require File.dirname(__FILE__) + '/../spec_helper'
    
    describe Photo do
    
    Raphael's avatar
    Raphael a validé
      before do
        @user = Factory.create(:user)
    
        @user.person.save
    
    
        @fixture_filename = 'button.png'
        @fixture_name = File.dirname(__FILE__) + '/../fixtures/button.png'
    
    Raphael's avatar
    Raphael a validé
        @fail_fixture_name = File.dirname(__FILE__) + '/../fixtures/msg.xml'
    
        @album = Album.create(:name => "foo", :person => @user.person)
        @photo = Photo.new(:person => @user.person, :album => @album)
    
    Raphael's avatar
    Raphael a validé
      end
    
    ilya's avatar
    ilya a validé
    
      it 'should have a constructor' do
    
    maxwell's avatar
    maxwell a validé
        image = File.open(@fixture_name)    
    
        photo = Photo.instantiate(:person => @user.person, :album => @album, :user_file => [image]) 
    
        photo.created_at.nil?.should be false
    
    ilya's avatar
    ilya a validé
        photo.image.read.nil?.should be false
      end
    
    
    Raphael's avatar
    Raphael a validé
      it 'should save a photo' do
    
    ilya's avatar
    ilya a validé
        @photo.image.store! File.open(@fixture_name)
    
        @photo.save.should == true
        binary = @photo.image.read
    
        fixture_binary = File.open(@fixture_name).read
    
    Raphael's avatar
    Raphael a validé
        binary.should == fixture_binary
      end
    
        photo = Photo.new(:person => @user.person)
    
    ilya's avatar
    ilya a validé
        photo.image = File.open(@fixture_name)
    
        photo.save
        photo.valid?.should be false
    
        photo.album = Album.create(:name => "foo", :person => @user.person)
    
        photo.save
        Photo.first.album.name.should == 'foo'
      end
    
    
      it 'should have a caption' do
        @photo.image.store! File.open(@fixture_name)
        @photo.caption = "cool story, bro"
        @photo.save
        Photo.first.caption.should == "cool story, bro"
      end
    
    
      it 'should remove its reference in user profile if it is referred' do
        @photo.image.store! File.open(@fixture_name)
        @photo.save
    
        @user.profile.image_url = @photo.image.url(:thumb_medium)
        @user.save
    
        @user.person.save
    
    
        User.first.profile.image_url.should == @photo.image.url(:thumb_medium)
        @photo.destroy
        User.first.profile.image_url.should be nil
      end
    
    
      it 'should not use the imported filename as the url' do
        @photo.image.store! File.open(@fixture_name)
        @photo.image.url.include?(@fixture_filename).should be false
    
        @photo.image.url(:thumb_medium).include?("/" + @fixture_filename).should be false
    
      describe 'non-image files' do
        it 'should not store' do
          file = File.open(@fail_fixture_name)
          @photo.image.should_receive(:check_whitelist!)
          lambda {
            @photo.image.store! file
          }.should raise_error
        end
    
    Raphael's avatar
    Raphael a validé
    
      end
    
      describe 'with encryption' do
        
        before do
          unstub_mocha_stubs
        end
        
        after do
          stub_signature_verification
        end
    
    Raphael's avatar
    Raphael a validé
    
    
    Raphael's avatar
    Raphael a validé
        it 'should save a signed photo' do
    
          photo  = @user.post(:photo, :album => @album, :user_file => [File.open(@fixture_name)])
    
    ilya's avatar
    ilya a validé
          photo.save.should == true
    
          photo.signature_valid?.should be true
    
    
      describe 'remote photos' do
        it 'should write the url on serialization' do 
          @photo.image = File.open(@fixture_name)
    
    ilya's avatar
    ilya a validé
          @photo.image.store!
          @photo.save
    
    maxwell's avatar
    maxwell a validé
      
    
          xml = @photo.to_xml.to_s
    
    maxwell's avatar
    maxwell a validé
    
    
          xml.include?(@photo.image.url).should be true
    
        it 'should have an album id on serialization' do
    
          @photo.image.store! File.open(@fixture_name)
    
          xml = @photo.to_xml.to_s
    
          xml.include?(@photo.album_id.to_s).should be true
    
    
        it 'should set the remote_photo on marshalling' do
          @photo.image.store! File.open(@fixture_name)
    
          @photo.save
          @photo.reload
    
    ilya's avatar
    ilya a validé
          
          url = @photo.url
          thumb_url = @photo.url :thumb_medium
    
    
          xml = @photo.to_diaspora_xml
          id = @photo.id
    
          @photo.destroy
          @user.receive xml
          
          new_photo = Photo.first(:id => id)
          new_photo.url.nil?.should be false
    
    ilya's avatar
    ilya a validé
          new_photo.url.include?(url).should be true
          new_photo.url(:thumb_medium).include?(thumb_url).should be true
    
    Raphael's avatar
    Raphael a validé
    end