BigW Consortium Gitlab

artifacts_controller_spec.rb 4.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
require 'spec_helper'

describe Projects::ArtifactsController do
  let(:user) { create(:user) }
  let(:project) { create(:project, :repository) }

  let(:pipeline) do
    create(:ci_pipeline,
            project: project,
            sha: project.commit.sha,
            ref: project.default_branch,
            status: 'success')
  end

15
  let(:job) { create(:ci_build, :success, :artifacts, pipeline: pipeline) }
16 17 18 19 20 21 22 23 24

  before do
    project.team << [user, :developer]

    sign_in(user)
  end

  describe 'GET download' do
    it 'sends the artifacts file' do
25
      expect(controller).to receive(:send_file).with(job.artifacts_file.path, disposition: 'attachment').and_call_original
26

27
      get :download, namespace_id: project.namespace, project_id: project, job_id: job
28 29 30 31 32 33
    end
  end

  describe 'GET browse' do
    context 'when the directory exists' do
      it 'renders the browse view' do
34
        get :browse, namespace_id: project.namespace, project_id: project, job_id: job, path: 'other_artifacts_0.1.2'
35 36 37 38 39 40 41

        expect(response).to render_template('projects/artifacts/browse')
      end
    end

    context 'when the directory does not exist' do
      it 'responds Not Found' do
42
        get :browse, namespace_id: project.namespace, project_id: project, job_id: job, path: 'unknown'
43 44 45 46 47 48 49 50 51

        expect(response).to be_not_found
      end
    end
  end

  describe 'GET file' do
    context 'when the file exists' do
      it 'renders the file view' do
52
        get :file, namespace_id: project.namespace, project_id: project, job_id: job, path: 'ci_artifacts.txt'
53 54 55 56 57 58 59

        expect(response).to render_template('projects/artifacts/file')
      end
    end

    context 'when the file does not exist' do
      it 'responds Not Found' do
60
        get :file, namespace_id: project.namespace, project_id: project, job_id: job, path: 'unknown'
61 62 63 64 65 66 67 68 69

        expect(response).to be_not_found
      end
    end
  end

  describe 'GET raw' do
    context 'when the file exists' do
      it 'serves the file using workhorse' do
70
        get :raw, namespace_id: project.namespace, project_id: project, job_id: job, path: 'ci_artifacts.txt'
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

        send_data = response.headers[Gitlab::Workhorse::SEND_DATA_HEADER]

        expect(send_data).to start_with('artifacts-entry:')

        base64_params = send_data.sub(/\Aartifacts\-entry:/, '')
        params = JSON.parse(Base64.urlsafe_decode64(base64_params))

        expect(params.keys).to eq(%w(Archive Entry))
        expect(params['Archive']).to end_with('build_artifacts.zip')
        expect(params['Entry']).to eq(Base64.encode64('ci_artifacts.txt'))
      end
    end
  end

  describe 'GET latest_succeeded' do
87
    def params_from_ref(ref = pipeline.ref, job_name = job.name, path = 'browse')
88 89 90 91
      {
        namespace_id: project.namespace,
        project_id: project,
        ref_name_and_path: File.join(ref, path),
92
        job: job_name
93 94 95
      }
    end

96
    context 'cannot find the job' do
97 98 99 100 101 102
      shared_examples 'not found' do
        it { expect(response).to have_http_status(:not_found) }
      end

      context 'has no such ref' do
        before do
103
          get :latest_succeeded, params_from_ref('TAIL', job.name)
104 105 106 107 108
        end

        it_behaves_like 'not found'
      end

109
      context 'has no such job' do
110 111 112 113 114 115 116 117 118
        before do
          get :latest_succeeded, params_from_ref(pipeline.ref, 'NOBUILD')
        end

        it_behaves_like 'not found'
      end

      context 'has no path' do
        before do
119
          get :latest_succeeded, params_from_ref(pipeline.sha, job.name, '')
120 121 122 123 124 125
        end

        it_behaves_like 'not found'
      end
    end

126 127
    context 'found the job and redirect' do
      shared_examples 'redirect to the job' do
128
        it 'redirects' do
129
          path = browse_project_job_artifacts_path(project, job)
130 131 132 133 134 135 136 137 138 139 140 141 142

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

      context 'with regular branch' do
        before do
          pipeline.update(ref: 'master',
                          sha: project.commit('master').sha)

          get :latest_succeeded, params_from_ref('master')
        end

143
        it_behaves_like 'redirect to the job'
144 145 146 147 148 149 150 151 152 153
      end

      context 'with branch name containing slash' do
        before do
          pipeline.update(ref: 'improve/awesome',
                          sha: project.commit('improve/awesome').sha)

          get :latest_succeeded, params_from_ref('improve/awesome')
        end

154
        it_behaves_like 'redirect to the job'
155 156 157 158 159 160 161
      end

      context 'with branch name and path containing slashes' do
        before do
          pipeline.update(ref: 'improve/awesome',
                          sha: project.commit('improve/awesome').sha)

162
          get :latest_succeeded, params_from_ref('improve/awesome', job.name, 'file/README.md')
163 164 165
        end

        it 'redirects' do
166
          path = file_project_job_artifacts_path(project, job, 'README.md')
167 168 169 170 171 172 173

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