summaryrefslogtreecommitdiff
path: root/spec/models/photo_spec.rb
blob: efef657fe092fddb900907648db0019a00fa1a1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require "rails_helper"

describe Photo do
  subject { Photo.new }

  describe "#upload" do
    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)
      expect(blob_storage).to have_received(:upload).with(upload_key, file)
      expect(blob_storage).to have_received(:upload).with(upload_key("large_"), file)
      expect(blob_storage).to have_received(:upload).with(upload_key("thumb_"), file)
    end

    it "sets the original filename" do
      subject.upload(file, blob_storage)
      expect(subject.original_filename).to eql(File.basename(file))
      expect(subject.image).to eql(File.basename(file))
    end

    it "specifies the content type" do
      subject.upload(file, blob_storage)
      expect(subject.content_type).to eql("image/jpeg")
    end

    it "applies the gps coordinates" do
      subject.upload(file, blob_storage)
      expect(subject.latitude).to eql(51.07296369444445)
      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("530990323da10ba4b8ab6a9809e9d694bd354831fd58afc96e18c708bfad5ef1")
    end

    def upload_key(prefix = '')
      "uploads/photo/image/#{subject.id}/#{prefix}#{File.basename(file)}"
    end
  end

  describe "#url_for" do
    let(:asset_host) { ENV['ASSET_HOST'] }

    before :each do
      subject.id = rand(100)
      subject.image = "blah.png"
    end

    it "returns the url to the large version" do
      expect(subject.url_for(:large)).to eql("#{asset_host}/uploads/photo/image/#{subject.id}/large_blah.png")
    end

    it "returns the url for the thumbnail version" do
      expect(subject.url_for(:thumb)).to eql("#{asset_host}/uploads/photo/image/#{subject.id}/thumb_blah.png")
    end

    it "returns the url for the original version" do
      expect(subject.url_for(:original)).to eql("#{asset_host}/uploads/photo/image/#{subject.id}/blah.png")
    end

    context "when the image is still being processed" do
      let(:large_processing_image_url) { path_to("large_default.png") }
      let(:thumb_processing_image_url) { path_to("thumb_default.png") }
      let(:original_processing_image_url) { path_to("original_default.png") }

      before { subject.image_processing=true }

      it "returns the path to the original processing image" do
        expect(subject.url_for(:original)).to eql(original_processing_image_url)
      end

      it "returns the path to a large processing image" do
        expect(subject.url_for(:large)).to eql(large_processing_image_url)
      end

      it "returns the path to a thumb processing image" do
        expect(subject.url_for(:thumb)).to eql(thumb_processing_image_url)
      end

      def path_to(image_filename)
        ActionController::Base.helpers.asset_path(
          image_filename,
          skip_pipeline: true
        )
      end
    end
  end
end