summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/commits_controller_spec.rb
blob: 9db1ac2a46cd667feda05ee029014ffda9f2fb1f (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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# frozen_string_literal: true

require 'spec_helper'

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

  before do
    project.add_maintainer(user)
  end

  context 'signed in' do
    before do
      sign_in(user)
    end

    describe "GET commits_root" do
      context "no ref is provided" do
        it 'redirects to the default branch of the project' do
          get(:commits_root,
              params: {
                namespace_id: project.namespace,
                project_id: project
              })

          expect(response).to redirect_to project_commits_path(project)
        end
      end
    end

    describe "GET show" do
      render_views

      context 'with file path' do
        before do
          get(:show,
              params: {
                namespace_id: project.namespace,
                project_id: project,
                id: id
              })
        end

        context "valid branch, valid file" do
          let(:id) { 'master/README.md' }

          it { is_expected.to respond_with(:success) }
        end

        context "valid branch, invalid file" do
          let(:id) { 'master/invalid-path.rb' }

          it { is_expected.to respond_with(:not_found) }
        end

        context "invalid branch, valid file" do
          let(:id) { 'invalid-branch/README.md' }

          it { is_expected.to respond_with(:not_found) }
        end

        context "branch with invalid format, valid file" do
          let(:id) { 'branch with space/README.md' }

          it { is_expected.to respond_with(:not_found) }
        end
      end

      context "when the ref name ends in .atom" do
        context "when the ref does not exist with the suffix" do
          before do
            get(:show,
                params: {
                  namespace_id: project.namespace,
                  project_id: project,
                  id: "master.atom"
                })
          end

          it "renders as atom" do
            expect(response).to be_success
            expect(response.content_type).to eq('application/atom+xml')
          end

          it 'renders summary with type=html' do
            expect(response.body).to include('<summary type="html">')
          end
        end

        context "when the ref exists with the suffix" do
          before do
            commit = project.repository.commit('master')

            allow_any_instance_of(Repository).to receive(:commit).and_call_original
            allow_any_instance_of(Repository).to receive(:commit).with('master.atom').and_return(commit)

            get(:show,
                params: {
                  namespace_id: project.namespace,
                  project_id: project,
                  id: "master.atom"
                })
          end

          it "renders as HTML" do
            expect(response).to be_success
            expect(response.content_type).to eq('text/html')
          end
        end
      end
    end

    describe "GET /commits/:id/signatures" do
      render_views

      before do
        expect(::Gitlab::GitalyClient).to receive(:allow_ref_name_caching).and_call_original unless id.include?(' ')

        get(:signatures,
            params: {
              namespace_id: project.namespace,
              project_id: project,
              id: id
            },
            format: :json)
      end

      context "valid branch" do
        let(:id) { 'master' }

        it { is_expected.to respond_with(:success) }
      end

      context "invalid branch format" do
        let(:id) { 'some branch' }

        it { is_expected.to respond_with(:not_found) }
      end
    end
  end

  context 'token authentication' do
    context 'public project' do
      it_behaves_like 'authenticates sessionless user', :show, :atom, public: true do
        before do
          public_project = create(:project, :repository, :public)

          default_params.merge!(namespace_id: public_project.namespace, project_id: public_project, id: "master.atom")
        end
      end
    end

    context 'private project' do
      it_behaves_like 'authenticates sessionless user', :show, :atom, public: false do
        before do
          private_project = create(:project, :repository, :private)
          private_project.add_maintainer(user)

          default_params.merge!(namespace_id: private_project.namespace, project_id: private_project, id: "master.atom")
        end
      end
    end
  end
end