summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/compare_controller_spec.rb
blob: b03c4b52de67c451a9c6266ab0967c714570732f (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
166
167
require 'spec_helper'

describe Projects::CompareController do
  let(:project) { create(:project) }
  let(:user) { create(:user) }
  let(:ref_from) { "improve%2Fawesome" }
  let(:ref_to) { "feature" }

  before do
    sign_in(user)
    project.team << [user, :master]
  end

  it 'compare shows some diffs' do
    get(:show,
        namespace_id: project.namespace.to_param,
        project_id: project.to_param,
        from: ref_from,
        to: ref_to)

    expect(response).to be_success
    expect(assigns(:diffs).diff_files.first).not_to be_nil
    expect(assigns(:commits).length).to be >= 1
  end

  it 'compare shows some diffs with ignore whitespace change option' do
    get(:show,
        namespace_id: project.namespace.to_param,
        project_id: project.to_param,
        from: '08f22f25',
        to: '66eceea0',
        w: 1)

    expect(response).to be_success
    diff_file = assigns(:diffs).diff_files.first
    expect(diff_file).not_to be_nil
    expect(assigns(:commits).length).to be >= 1
    # without whitespace option, there are more than 2 diff_splits
    diff_splits = diff_file.diff.diff.split("\n")
    expect(diff_splits.length).to be <= 2
  end

  describe 'non-existent refs' do
    it 'uses invalid source ref' do
      get(:show,
          namespace_id: project.namespace.to_param,
          project_id: project.to_param,
          from: 'non-existent',
          to: ref_to)

      expect(response).to be_success
      expect(assigns(:diffs).diff_files.to_a).to eq([])
      expect(assigns(:commits)).to eq([])
    end

    it 'uses invalid target ref' do
      get(:show,
          namespace_id: project.namespace.to_param,
          project_id: project.to_param,
          from: ref_from,
          to: 'non-existent')

      expect(response).to be_success
      expect(assigns(:diffs)).to eq(nil)
      expect(assigns(:commits)).to eq(nil)
    end

    it 'redirects back to index when params[:from] is empty and preserves params[:to]' do
      post(:create,
           namespace_id: project.namespace.to_param,
           project_id: project.to_param,
           from: '',
           to: 'master')

      expect(response).to redirect_to(namespace_project_compare_index_path(project.namespace, project, to: 'master'))
    end

    it 'redirects back to index when params[:to] is empty and preserves params[:from]' do
      post(:create,
           namespace_id: project.namespace.to_param,
           project_id: project.to_param,
           from: 'master',
           to: '')

      expect(response).to redirect_to(namespace_project_compare_index_path(project.namespace, project, from: 'master'))
    end

    it 'redirects back to index when params[:from] and params[:to] are empty' do
      post(:create,
           namespace_id: project.namespace.to_param,
           project_id: project.to_param,
           from: '',
           to: '')

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

  describe 'GET diff_for_path' do
    def diff_for_path(extra_params = {})
      params = {
        namespace_id: project.namespace.to_param,
        project_id: project.to_param
      }

      get :diff_for_path, params.merge(extra_params)
    end

    let(:existing_path) { 'files/ruby/feature.rb' }

    context 'when the from and to refs exist' do
      context 'when the user has access to the project' do
        context 'when the path exists in the diff' do
          it 'disables diff notes' do
            diff_for_path(from: ref_from, to: ref_to, old_path: existing_path, new_path: existing_path)

            expect(assigns(:diff_notes_disabled)).to be_truthy
          end

          it 'only renders the diffs for the path given' do
            expect(controller).to receive(:render_diff_for_path).and_wrap_original do |meth, diffs|
              expect(diffs.diff_files.map(&:new_path)).to contain_exactly(existing_path)
              meth.call(diffs)
            end

            diff_for_path(from: ref_from, to: ref_to, old_path: existing_path, new_path: existing_path)
          end
        end

        context 'when the path does not exist in the diff' do
          before { diff_for_path(from: ref_from, to: ref_to, old_path: existing_path.succ, new_path: existing_path.succ) }

          it 'returns a 404' do
            expect(response).to have_http_status(404)
          end
        end
      end

      context 'when the user does not have access to the project' do
        before do
          project.team.truncate
          diff_for_path(from: ref_from, to: ref_to, old_path: existing_path, new_path: existing_path)
        end

        it 'returns a 404' do
          expect(response).to have_http_status(404)
        end
      end
    end

    context 'when the from ref does not exist' do
      before { diff_for_path(from: ref_from.succ, to: ref_to, old_path: existing_path, new_path: existing_path) }

      it 'returns a 404' do
        expect(response).to have_http_status(404)
      end
    end

    context 'when the to ref does not exist' do
      before { diff_for_path(from: ref_from, to: ref_to.succ, old_path: existing_path, new_path: existing_path) }

      it 'returns a 404' do
        expect(response).to have_http_status(404)
      end
    end
  end
end