summaryrefslogtreecommitdiff
path: root/spec/controllers/concerns/static_object_external_storage_spec.rb
blob: afb43d531c0de96b92c8e3e583823dd4c12eae90 (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
96
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe StaticObjectExternalStorage do
  controller(Projects::ApplicationController) do
    include StaticObjectExternalStorage

    before_action :redirect_to_external_storage, if: :static_objects_external_storage_enabled?

    def show
      head :ok
    end
  end

  let(:project) { create(:project, :public) }
  let(:user) { create(:user, static_object_token: 'hunter1') }

  before do
    project.add_developer(user)
    sign_in(user)
  end

  context 'when external storage is not configured' do
    it 'calls the action normally' do
      expect(Gitlab::CurrentSettings.static_objects_external_storage_url).to be_blank

      do_request

      expect(response).to have_gitlab_http_status(:ok)
    end
  end

  context 'when external storage is configured' do
    before do
      allow_any_instance_of(ApplicationSetting).to receive(:static_objects_external_storage_url).and_return('https://cdn.gitlab.com')
      allow_any_instance_of(ApplicationSetting).to receive(:static_objects_external_storage_auth_token).and_return('letmein')

      routes.draw { get '/:namespace_id/:id' => 'projects/application#show' }
    end

    context 'when external storage token is empty' do
      let(:base_redirect_url) { "https://cdn.gitlab.com/#{project.namespace.to_param}/#{project.to_param}" }

      context 'when project is public' do
        it 'redirects to external storage URL without adding a token parameter' do
          do_request

          expect(response).to redirect_to(base_redirect_url)
        end
      end

      context 'when project is not public' do
        let(:project) { create(:project, :private) }

        it 'redirects to external storage URL a token parameter added' do
          do_request

          expect(response).to redirect_to("#{base_redirect_url}?token=#{user.static_object_token}")
        end

        context 'when path includes extra parameters' do
          it 'includes the parameters in the redirect URL' do
            do_request(foo: 'bar')

            expect(response.location).to eq("#{base_redirect_url}?foo=bar&token=#{user.static_object_token}")
          end
        end
      end
    end

    context 'when external storage token is present' do
      context 'when token is correct' do
        it 'calls the action normally' do
          request.headers['X-Gitlab-External-Storage-Token'] = 'letmein'
          do_request

          expect(response).to have_gitlab_http_status(:ok)
        end
      end

      context 'when token is incorrect' do
        it 'return 403' do
          request.headers['X-Gitlab-External-Storage-Token'] = 'donotletmein'
          do_request

          expect(response).to have_gitlab_http_status(:forbidden)
        end
      end
    end
  end

  def do_request(extra_params = {})
    get :show, params: { namespace_id: project.namespace, id: project }.merge(extra_params)
  end
end