summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/refs_controller_spec.rb
blob: a7a8361ae20a458d56666a740ff97609de2a1b2b (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::RefsController, feature_category: :source_code_management do
  let_it_be(:project) { create(:project, :repository) }
  let(:user) { create(:user) }

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

  describe 'GET #switch' do
    using RSpec::Parameterized::TableSyntax

    let(:id) { 'master' }
    let(:params) do
      { destination: destination, namespace_id: project.namespace.to_param, project_id: project, id: id,
        ref_type: ref_type }
    end

    subject { get :switch, params: params }

    context 'when the use_ref_type_parameter feature flag is not enabled' do
      before do
        stub_feature_flags(use_ref_type_parameter: false)
      end

      where(:destination, :ref_type, :redirected_to) do
        'tree'           | nil     | lazy { project_tree_path(project, id) }
        'tree'           | 'heads' | lazy { project_tree_path(project, id) }
        'blob'           | nil     | lazy { project_blob_path(project, id) }
        'blob'           | 'heads' | lazy { project_blob_path(project, id) }
        'graph'          | nil     | lazy { project_network_path(project, id) }
        'graph'          | 'heads' | lazy { project_network_path(project, id) }
        'graphs'         | nil     | lazy { project_graph_path(project, id) }
        'graphs'         | 'heads' | lazy { project_graph_path(project, id) }
        'find_file'      | nil     | lazy { project_find_file_path(project, id) }
        'find_file'      | 'heads' | lazy { project_find_file_path(project, id) }
        'graphs_commits' | nil     | lazy { commits_project_graph_path(project, id) }
        'graphs_commits' | 'heads' | lazy { commits_project_graph_path(project, id) }
        'badges'         | nil     | lazy { project_settings_ci_cd_path(project, ref: id) }
        'badges'         | 'heads' | lazy { project_settings_ci_cd_path(project, ref: id) }
        'commits'        | nil     | lazy { project_commits_path(project, id) }
        'commits'        | 'heads' | lazy { project_commits_path(project, id) }
        'somethingelse'  | nil     | lazy { project_commits_path(project, id) }
        'somethingelse'  | 'heads' | lazy { project_commits_path(project, id) }
      end

      with_them do
        it 'redirects to destination' do
          expect(subject).to redirect_to(redirected_to)
        end
      end
    end

    context 'when the use_ref_type_parameter feature flag is enabled' do
      where(:destination, :ref_type, :redirected_to) do
        'tree'           | nil     | lazy { project_tree_path(project, id) }
        'tree'           | 'heads' | lazy { project_tree_path(project, id) }
        'blob'           | nil     | lazy { project_blob_path(project, id) }
        'blob'           | 'heads' | lazy { project_blob_path(project, id) }
        'graph'          | nil     | lazy { project_network_path(project, id) }
        'graph'          | 'heads' | lazy { project_network_path(project, id, ref_type: 'heads') }
        'graphs'         | nil     | lazy { project_graph_path(project, id) }
        'graphs'         | 'heads' | lazy { project_graph_path(project, id, ref_type: 'heads') }
        'find_file'      | nil     | lazy { project_find_file_path(project, id) }
        'find_file'      | 'heads' | lazy { project_find_file_path(project, id) }
        'graphs_commits' | nil     | lazy { commits_project_graph_path(project, id) }
        'graphs_commits' | 'heads' | lazy { commits_project_graph_path(project, id) }
        'badges'         | nil     | lazy { project_settings_ci_cd_path(project, ref: id) }
        'badges'         | 'heads' | lazy { project_settings_ci_cd_path(project, ref: id) }
        'commits'        | nil     | lazy { project_commits_path(project, id) }
        'commits'        | 'heads' | lazy { project_commits_path(project, id, ref_type: 'heads') }
        nil              | nil     | lazy { project_commits_path(project, id) }
        nil              | 'heads' | lazy { project_commits_path(project, id, ref_type: 'heads') }
      end

      with_them do
        it 'redirects to destination' do
          expect(subject).to redirect_to(redirected_to)
        end
      end
    end
  end

  describe 'GET #logs_tree' do
    let(:path) { 'foo/bar/baz.html' }

    def default_get(format = :html)
      get :logs_tree,
          params: {
            namespace_id: project.namespace.to_param,
            project_id: project,
            id: 'master',
            path: path
          },
          format: format
    end

    def xhr_get(format = :html, params = {})
      get :logs_tree, params: {
        namespace_id: project.namespace.to_param,
        project_id: project,
        id: 'master',
        path: path,
        format: format
      }.merge(params), xhr: true
    end

    it 'never throws MissingTemplate' do
      expect { default_get }.not_to raise_error
      expect { xhr_get(:json) }.not_to raise_error
      expect { xhr_get }.not_to raise_error
    end

    it 'renders 404 for HTML requests' do
      xhr_get

      expect(response).to be_not_found
    end

    context 'when ref is incorrect' do
      it 'returns 404 page' do
        xhr_get(:json, id: '.')

        expect(response).to be_not_found
      end
    end

    context 'when offset has an invalid format' do
      it 'renders JSON' do
        xhr_get(:json, offset: { wrong: :format })

        expect(response).to be_successful
        expect(json_response).to be_kind_of(Array)
      end
    end

    context 'when json is requested' do
      it 'renders JSON' do
        expect(::Gitlab::GitalyClient).to receive(:allow_ref_name_caching).and_call_original

        xhr_get(:json)

        expect(response).to be_successful
        expect(json_response).to be_kind_of(Array)
      end
    end
  end
end