master
 1require "rails_helper"
 2
 3describe Image do
 4  describe "#filename" do
 5    it "returns the filename" do
 6      expect(Image.new("/Users/mo/blah.png").filename).to eql("blah.png")
 7    end
 8
 9    it "sanitizes the filename" do
10      expect(Image.new("/Users/mo/blah huh.png").filename).to eql("blah_huh.png")
11    end
12  end
13
14  describe "#sha256" do
15    let(:path) { File.join(Rails.root, 'spec/fixtures/images/gps.jpg') }
16
17    it "gives the SHA256 of the image" do
18      expect(Image.new(path).sha256).to eql("530990323da10ba4b8ab6a9809e9d694bd354831fd58afc96e18c708bfad5ef1")
19    end
20  end
21
22  describe "#geolocation" do
23    let(:exif) { double }
24    subject { Image.new('blah.jpg', exif) }
25
26    it "parses the geolocation info" do
27      coordinates = [rand(100), rand(50)]
28      allow(exif).to receive(:parse_geolocation_from).with('blah.jpg').and_return(coordinates)
29      expect(subject.geolocation).to eql(coordinates)
30    end
31  end
32
33  describe "#content_type" do
34    it "returns the correct content type for jpegs" do
35      expect(Image.new('blah.jpg').content_type).to eql('image/jpeg')
36      expect(Image.new('blah.jpeg').content_type).to eql('image/jpeg')
37    end
38
39    it "returns the correct content type for png" do
40      expect(Image.new('blah.png').content_type).to eql('image/png')
41    end
42
43    it "returns the correct content type for gif" do
44      expect(Image.new('blah.gif').content_type).to eql('image/gif')
45    end
46
47    it "returns the correct content type for bmp" do
48      expect(Image.new('blah.bmp').content_type).to eql('image/bmp')
49    end
50
51    it "returns the correct content type for tif" do
52      expect(Image.new('blah.tif').content_type).to eql('image/tiff')
53    end
54  end
55
56  it "raises an errorwhen the file is not in the whitelist" do
57    expect(-> { Image.new('blah.exe') }).to raise_error(/not in the whitelist/)
58  end
59
60  context "resizing" do
61    let(:path) { "#{Tempfile.new('gps').path}.jpg" }
62    subject { Image.new(path) }
63
64    before :each do
65      FileUtils.cp(File.join(Rails.root, 'spec/fixtures/images/gps.jpg'), path)
66    end
67
68    it "resizes the image to fit" do
69      subject.resize_to_fit(width: 570, height: 630)
70
71      image = MiniMagick::Image.open(path)
72      expect(image[:width]).to eql(473)
73      expect(image[:height]).to eql(630)
74    end
75
76    it "resizes the image to fill" do
77      subject.resize_to_fill(width: 130, height: 90)
78
79      image = MiniMagick::Image.open(path)
80      expect(image[:width]).to eql(130)
81      expect(image[:height]).to eql(90)
82    end
83
84    it "adds a watermark" do
85      expect(-> { subject.watermark("testing") }).to_not raise_error
86      #`open #{path}`
87    end
88  end
89end