summaryrefslogtreecommitdiff
path: root/spec/requests/projects/artifacts_controller_spec.rb
blob: d20866c0d44d41227b7a8eacae0a3cd49622c1b6 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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

  let(:build) { create(:ci_build, :success, :artifacts, pipeline: pipeline) }

  describe 'GET /:project/builds/artifacts/:ref_name/browse?job=name' do
    before do
      project.team << [user, :developer]

      login_as(user)
    end

    def path_from_ref(
      ref = pipeline.ref, job = build.name, path = 'browse')
      latest_succeeded_namespace_project_artifacts_path(
        project.namespace,
        project,
        [ref, path].join('/'),
        job: job)
    end

    context 'cannot find the build' do
      shared_examples 'not found' do
        it { expect(response).to have_http_status(:not_found) }
      end

      context 'has no such ref' do
        before do
          get path_from_ref('TAIL', build.name)
        end

        it_behaves_like 'not found'
      end

      context 'has no such build' do
        before do
          get path_from_ref(pipeline.ref, 'NOBUILD')
        end

        it_behaves_like 'not found'
      end

      context 'has no path' do
        before do
          get path_from_ref(pipeline.sha, build.name, '')
        end

        it_behaves_like 'not found'
      end
    end

    context 'found the build and redirect' do
      shared_examples 'redirect to the build' do
        it 'redirects' do
          path = browse_namespace_project_build_artifacts_path(
            project.namespace,
            project,
            build)

          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 path_from_ref('master')
        end

        it_behaves_like 'redirect to the build'
      end

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

          get path_from_ref('improve/awesome')
        end

        it_behaves_like 'redirect to the build'
      end

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

          get path_from_ref('improve/awesome', build.name, 'file/README.md')
        end

        it 'redirects' do
          path = file_namespace_project_build_artifacts_path(
            project.namespace,
            project,
            build,
            'README.md')

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