master
 1require "rails_helper"
 2
 3describe Photo do
 4  subject { Photo.new }
 5
 6  describe "#upload" do
 7    let(:file) { "#{Tempfile.new('gps').path}.jpg" }
 8    let(:blob_storage) { double(upload: true) }
 9
10    before { FileUtils.cp(File.join(Rails.root, 'spec/fixtures/images/gps.jpg'), file) }
11    after { FileUtils.rm(file) }
12
13    it "uploads each version to the blob storage" do
14      subject.id = rand(100)
15      subject.upload(file, blob_storage)
16      expect(blob_storage).to have_received(:upload).with(upload_key, file)
17      expect(blob_storage).to have_received(:upload).with(upload_key("large_"), file)
18      expect(blob_storage).to have_received(:upload).with(upload_key("thumb_"), file)
19    end
20
21    it "sets the original filename" do
22      subject.upload(file, blob_storage)
23      expect(subject.original_filename).to eql(File.basename(file))
24      expect(subject.image).to eql(File.basename(file))
25    end
26
27    it "specifies the content type" do
28      subject.upload(file, blob_storage)
29      expect(subject.content_type).to eql("image/jpeg")
30    end
31
32    it "applies the gps coordinates" do
33      subject.upload(file, blob_storage)
34      expect(subject.latitude).to eql(51.07296369444445)
35      expect(subject.longitude).to eql(-114.101799)
36    end
37
38    it "applies the sha256 of the file" do
39      subject.upload(file, blob_storage)
40      expect(subject.sha256).to eql("530990323da10ba4b8ab6a9809e9d694bd354831fd58afc96e18c708bfad5ef1")
41    end
42
43    def upload_key(prefix = '')
44      "uploads/photo/image/#{subject.id}/#{prefix}#{File.basename(file)}"
45    end
46  end
47
48  describe "#url_for" do
49    let(:asset_host) { ENV['ASSET_HOST'] }
50
51    before :each do
52      subject.id = rand(100)
53      subject.image = "blah.png"
54    end
55
56    it "returns the url to the large version" do
57      expect(subject.url_for(:large)).to eql("#{asset_host}/uploads/photo/image/#{subject.id}/large_blah.png")
58    end
59
60    it "returns the url for the thumbnail version" do
61      expect(subject.url_for(:thumb)).to eql("#{asset_host}/uploads/photo/image/#{subject.id}/thumb_blah.png")
62    end
63
64    it "returns the url for the original version" do
65      expect(subject.url_for(:original)).to eql("#{asset_host}/uploads/photo/image/#{subject.id}/blah.png")
66    end
67
68    context "when the image is still being processed" do
69      let(:large_processing_image_url) { path_to("large_default.png") }
70      let(:thumb_processing_image_url) { path_to("thumb_default.png") }
71      let(:original_processing_image_url) { path_to("original_default.png") }
72
73      before { subject.image_processing=true }
74
75      it "returns the path to the original processing image" do
76        expect(subject.url_for(:original)).to eql(original_processing_image_url)
77      end
78
79      it "returns the path to a large processing image" do
80        expect(subject.url_for(:large)).to eql(large_processing_image_url)
81      end
82
83      it "returns the path to a thumb processing image" do
84        expect(subject.url_for(:thumb)).to eql(thumb_processing_image_url)
85      end
86
87      def path_to(image_filename)
88        ActionController::Base.helpers.asset_path(
89          image_filename,
90          skip_pipeline: true
91        )
92      end
93    end
94  end
95end