File: uploads_controller_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (145 lines) | stat: -rw-r--r-- 4,293 bytes parent folder | download
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Groups::UploadsController, feature_category: :portfolio_management do
  include WorkhorseHelpers

  let(:model) { create(:group, :public) }
  let(:params) do
    { group_id: model }
  end

  let(:other_model) { create(:group, :public) }
  let(:other_params) do
    { group_id: other_model }
  end

  let(:legacy_version) { UploadsActions::ID_BASED_UPLOAD_PATH_VERSION - 1 }

  it_behaves_like 'handle uploads' do
    let(:uploader_class) { NamespaceFileUploader }
  end

  context 'with a moved group' do
    let!(:upload) { create(:upload, :issuable_upload, :with_file, model: model) }
    let(:group) { model }
    let(:old_path) { group.to_param + 'old' }
    let!(:redirect_route) { model.redirect_routes.create!(path: old_path) }
    let(:upload_path) { File.basename(upload.path) }

    it 'redirects to a file with the proper extension' do
      get :show, params: { group_id: old_path, filename: upload_path, secret: upload.secret }

      expect(response.location).to eq(show_group_uploads_url(group, upload.secret, upload_path))
      expect(response.location).to end_with(upload.path)
      expect(response).to have_gitlab_http_status(:redirect)
    end
  end

  describe "GET #show" do
    let(:user)  { create(:user) }
    let(:filename) { "rails_sample.jpg" }
    let!(:upload) { create(:upload, :namespace_upload, :with_file, model: model, filename: filename) }

    let(:show_upload) do
      get :show, params: params.merge(secret: upload.secret, filename: filename)
    end

    it 'responds with status 404' do
      show_upload

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

    context 'with legacy upload' do
      let!(:upload) do
        create(:upload, :namespace_upload, :with_file, model: model, filename: filename, version: legacy_version)
      end

      context 'when the group is public' do
        before do
          model.update_attribute(:visibility_level, Gitlab::VisibilityLevel::PUBLIC)
        end

        context "when not signed in" do
          it "responds with appropriate status" do
            show_upload

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

          context 'when uploader class does not match the upload' do
            let!(:upload) do
              create(:upload, :issuable_upload, :with_file, model: model, filename: filename, version: legacy_version)
            end

            it 'responds with status 404' do
              show_upload

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

          context 'when filename does not match' do
            let(:invalid_filename) { 'invalid_filename.jpg' }

            it 'responds with status 404' do
              get :show, params: params.merge(secret: upload.secret, filename: invalid_filename)

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

        context "when signed in" do
          before do
            sign_in(user)
          end

          context "when the user doesn't have access to the model" do
            it "responds with status 200" do
              show_upload

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

      context 'when the group is private' do
        before do
          model.update_attribute(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
        end

        context "when not signed in" do
          it "responds with appropriate status" do
            show_upload

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

        context "when signed in" do
          before do
            sign_in(user)
          end

          context "when the user doesn't have access to the model" do
            it "responds with status 200" do
              show_upload

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

  def post_authorize(verified: true)
    request.headers.merge!(workhorse_internal_api_request_header) if verified

    post :authorize, params: { group_id: model.full_path }, format: :json
  end
end