summaryrefslogtreecommitdiff
path: root/spec/services/projects/download_service_spec.rb
blob: f12e09c58c36fb0f53bc9e8c0fb4c3e047bf8a82 (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
require 'spec_helper'

describe Projects::DownloadService do
  describe 'File service' do
    before do
      @user = create :user
      @project = create :project, creator_id: @user.id, namespace: @user.namespace
    end

    context 'for a URL that is not on whitelist' do
      before do
        url = 'https://code.jquery.com/jquery-2.1.4.min.js'
        @link_to_file = download_file(@project, url)
      end

      it { expect(@link_to_file).to eq(nil) }
    end

    context 'for URLs that are on the whitelist' do
      before do
        sham_rack_app = ShamRack.at('mycompany.fogbugz.com').stub
        sham_rack_app.register_resource('/rails_sample.jpg', File.read(Rails.root + 'spec/fixtures/rails_sample.jpg'), 'image/jpg')
        sham_rack_app.register_resource('/doc_sample.txt', File.read(Rails.root + 'spec/fixtures/doc_sample.txt'), 'text/plain')
      end

      after do
        ShamRack.unmount_all
      end

      context 'an image file' do
        before do
          url = 'http://mycompany.fogbugz.com/rails_sample.jpg'
          @link_to_file = download_file(@project, url)
        end

        it { expect(@link_to_file).to have_key('alt') }
        it { expect(@link_to_file).to have_key('url') }
        it { expect(@link_to_file).to have_key('is_image') }
        it { expect(@link_to_file['is_image']).to be true }
        it { expect(@link_to_file['url']).to match("/#{@project.path_with_namespace}") }
        it { expect(@link_to_file['url']).to match('rails_sample.jpg') }
        it { expect(@link_to_file['alt']).to eq('rails_sample') }
      end

      context 'a txt file' do
        before do
          url = 'http://mycompany.fogbugz.com/doc_sample.txt'
          @link_to_file = download_file(@project, url)
        end

        it { expect(@link_to_file).to have_key('alt') }
        it { expect(@link_to_file).to have_key('url') }
        it { expect(@link_to_file).to have_key('is_image') }
        it { expect(@link_to_file['is_image']).to be false }
        it { expect(@link_to_file['url']).to match("/#{@project.path_with_namespace}") }
        it { expect(@link_to_file['url']).to match('doc_sample.txt') }
        it { expect(@link_to_file['alt']).to eq('doc_sample.txt') }
      end
    end
  end

  def download_file(repository, url)
    Projects::DownloadService.new(repository, url).execute
  end
end