Commit 0de7f024

mo khan <mo@mokhan.ca>
2014-06-07 23:20:18
make a copy of the original photo to modify in the tests.
1 parent 968ef78
Changed files (2)
app
models
spec
app/models/photo.rb
@@ -1,5 +1,6 @@
 class Photo < ActiveRecord::Base
   belongs_to :creation, counter_cache: true, touch: true
+  attr_accessor :sha256
 
   def watermark
     return '' if creation.nil?
@@ -21,6 +22,7 @@ class Photo < ActiveRecord::Base
     self.content_type = image.content_type
     self.latitude, self.longitude = image.geolocation
     self.image_processing = nil
+    self.sha256 = image.sha256
     versions.each do |version|
       version.adjust(image)
       blob_storage.upload(version.create_key, image.path)
spec/models/photo_spec.rb
@@ -4,9 +4,12 @@ describe Photo do
   subject { Photo.new }
 
   describe "#upload" do
-    let(:file) { File.join(Rails.root, 'spec/fixtures/images/gps.jpg') }
+    let(:file) { "#{Tempfile.new('gps').path}.jpg" }
     let(:blob_storage) { double(upload: true) }
 
+    before { FileUtils.cp(File.join(Rails.root, 'spec/fixtures/images/gps.jpg'), file) }
+    after { FileUtils.rm(file) }
+
     it "uploads each version to the blob storage" do
       subject.id = rand(100)
       subject.upload(file, blob_storage)
@@ -17,8 +20,8 @@ describe Photo do
 
     it "sets the original filename" do
       subject.upload(file, blob_storage)
-      subject.original_filename.should == "gps.jpg"
-      subject.image.should == "gps.jpg"
+      subject.original_filename.should == File.basename(file)
+      subject.image.should == File.basename(file)
     end
 
     it "specifies the content type" do
@@ -32,8 +35,13 @@ describe Photo do
       expect(subject.longitude).to eql(-114.101799)
     end
 
+    it "applies the sha256 of the file" do
+      subject.upload(file, blob_storage)
+      expect(subject.sha256).to eql("a1b1b9b8b22d3a4a3523ebb0dc2c57c685938427e12e8a6439fbab104da6b1d8")
+    end
+
     def upload_key(prefix = '')
-      "uploads/photo/image/#{subject.id}/#{prefix}gps.jpg"
+      "uploads/photo/image/#{subject.id}/#{prefix}#{File.basename(file)}"
     end
   end