summaryrefslogtreecommitdiff
path: root/spec/services/infrastructure/blob_storage_spec.rb
blob: e9f0f3e803d680fc7369eabfc282beb84e988cbd (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
require "rails_helper"

if ENV['AWS_SECRET_ACCESS_KEY'].present?
  describe BlobStorage do
    let(:bucket) { ENV['FOG_DIRECTORY'] }
    subject { BlobStorage.new }

    let(:file) { File.join(Rails.root, 'spec/fixtures/images/gorilla.jpg') }

    context "when uploading" do
      let(:key) { "test#{SecureRandom.uuid}" }

      it "uploads to s3" do
        subject.upload(key, file)

        object = AWS::S3.new.buckets[bucket].objects[key]
        expect(object.exists?).to be_truthy
      end
    end

    describe "#download" do
      let(:key) { "test/upload/#{SecureRandom.uuid}" }

      it 'downloads a file from blob storage' do
        subject.upload(key, file)

        sha = subject.download(key) do |temp_file|
          Digest::SHA256.file(temp_file.path).hexdigest
        end
        expect(sha).to eql(Digest::SHA256.file(file).hexdigest)
      end
    end
  end
end