BigW Consortium Gitlab

labels_controller_spec.rb 7.45 KB
Newer Older
1 2 3
require 'spec_helper'

describe Projects::LabelsController do
4
  let(:group)   { create(:group) }
5
  let(:project) { create(:project, namespace: group) }
6 7 8 9
  let(:user)    { create(:user) }

  before do
    project.team << [user, :master]
10

11 12 13 14
    sign_in(user)
  end

  describe 'GET #index' do
15 16 17
    let!(:label_1) { create(:label, project: project, priority: 1, title: 'Label 1') }
    let!(:label_2) { create(:label, project: project, priority: 3, title: 'Label 2') }
    let!(:label_3) { create(:label, project: project, priority: 1, title: 'Label 3') }
18 19
    let!(:label_4) { create(:label, project: project, title: 'Label 4') }
    let!(:label_5) { create(:label, project: project, title: 'Label 5') }
20

21 22 23 24 25 26 27 28 29
    let!(:group_label_1) { create(:group_label, group: group, title: 'Group Label 1') }
    let!(:group_label_2) { create(:group_label, group: group, title: 'Group Label 2') }
    let!(:group_label_3) { create(:group_label, group: group, title: 'Group Label 3') }
    let!(:group_label_4) { create(:group_label, group: group, title: 'Group Label 4') }

    before do
      create(:label_priority, project: project, label: group_label_1, priority: 3)
      create(:label_priority, project: project, label: group_label_2, priority: 1)
    end
30 31

    context '@prioritized_labels' do
32 33 34
      before do
        list_labels
      end
35

36 37 38 39
      it 'does not include labels without priority' do
        list_labels

        expect(assigns(:prioritized_labels)).not_to include(group_label_3, group_label_4, label_4, label_5)
40 41 42
      end

      it 'is sorted by priority, then label title' do
43
        expect(assigns(:prioritized_labels)).to eq [group_label_2, label_1, label_3, group_label_1, label_2]
44 45 46
      end
    end

47
    context '@labels' do
48
      it 'is sorted by label title' do
49 50
        list_labels

51
        expect(assigns(:labels)).to eq [group_label_3, group_label_4, label_4, label_5]
52 53
      end

54
      it 'does not include labels with priority' do
55
        list_labels
56

57
        expect(assigns(:labels)).not_to include(group_label_2, label_1, label_3, group_label_1, label_2)
58
      end
59

60
      it 'does not include group labels when project does not belong to a group' do
61 62 63 64
        project.update(namespace: create(:namespace))

        list_labels

65
        expect(assigns(:labels)).not_to include(group_label_3, group_label_4)
66 67 68 69
      end
    end

    def list_labels
70
      get :index, namespace_id: project.namespace.to_param, project_id: project
71 72
    end
  end
73 74

  describe 'POST #generate' do
75
    context 'personal project' do
76
      let(:personal_project) { create(:project, namespace: user.namespace) }
77

78
      it 'creates labels' do
79
        post :generate, namespace_id: personal_project.namespace.to_param, project_id: personal_project
80

81
        expect(response).to have_gitlab_http_status(302)
82 83 84 85 86
      end
    end

    context 'project belonging to a group' do
      it 'creates labels' do
87
        post :generate, namespace_id: project.namespace.to_param, project_id: project
88

89
        expect(response).to have_gitlab_http_status(302)
90
      end
91 92
    end
  end
93 94 95 96 97

  describe 'POST #toggle_subscription' do
    it 'allows user to toggle subscription on project labels' do
      label = create(:label, project: project)

98
      toggle_subscription(label)
99

100
      expect(response).to have_gitlab_http_status(200)
101 102 103 104 105
    end

    it 'allows user to toggle subscription on group labels' do
      group_label = create(:group_label, group: group)

106
      toggle_subscription(group_label)
107

108
      expect(response).to have_gitlab_http_status(200)
109
    end
110 111

    def toggle_subscription(label)
112
      post :toggle_subscription, namespace_id: project.namespace.to_param, project_id: project, id: label.to_param
113
    end
114
  end
115 116 117 118 119

  describe 'POST #promote' do
    let!(:promoted_label_name) { "Promoted Label" }
    let!(:label_1) { create(:label, title: promoted_label_name, project: project) }

120
    context 'not group reporters' do
121
      it 'denies access' do
122
        post :promote, namespace_id: project.namespace.to_param, project_id: project, id: label_1.to_param
123

124
        expect(response).to have_gitlab_http_status(404)
125 126 127
      end
    end

128
    context 'group reporter' do
129
      before do
130
        group.add_reporter(user)
131 132 133
      end

      it 'gives access' do
134
        post :promote, namespace_id: project.namespace.to_param, project_id: project, id: label_1.to_param
135 136 137 138 139

        expect(response).to redirect_to(namespace_project_labels_path)
      end

      it 'promotes the label' do
140
        post :promote, namespace_id: project.namespace.to_param, project_id: project, id: label_1.to_param
141 142 143 144 145 146 147 148 149 150 151 152 153

        expect(Label.where(id: label_1.id)).to be_empty
        expect(GroupLabel.find_by(title: promoted_label_name)).not_to be_nil
      end

      context 'service raising InvalidRecord' do
        before do
          expect_any_instance_of(Labels::PromoteService).to receive(:execute) do |label|
            raise ActiveRecord::RecordInvalid.new(label_1)
          end
        end

        it 'returns to label list' do
154
          post :promote, namespace_id: project.namespace.to_param, project_id: project, id: label_1.to_param
155 156 157 158 159
          expect(response).to redirect_to(namespace_project_labels_path)
        end
      end
    end
  end
160 161 162 163 164 165 166 167 168 169 170 171 172

  describe '#ensure_canonical_path' do
    before do
      sign_in(user)
    end

    context 'for a GET request' do
      context 'when requesting the canonical path' do
        context 'non-show path' do
          context 'with exactly matching casing' do
            it 'does not redirect' do
              get :index, namespace_id: project.namespace, project_id: project.to_param

173
              expect(response).not_to have_gitlab_http_status(301)
174 175 176 177 178 179 180
            end
          end

          context 'with different casing' do
            it 'redirects to the correct casing' do
              get :index, namespace_id: project.namespace, project_id: project.to_param.upcase

181
              expect(response).to redirect_to(project_labels_path(project))
182 183 184 185 186 187 188 189 190 191 192 193
              expect(controller).not_to set_flash[:notice]
            end
          end
        end
      end

      context 'when requesting a redirected path' do
        let!(:redirect_route) { project.redirect_routes.create(path: project.full_path + 'old') }

        it 'redirects to the canonical path' do
          get :index, namespace_id: project.namespace, project_id: project.to_param + 'old'

194
          expect(response).to redirect_to(project_labels_path(project))
195 196 197 198 199 200 201 202 203 204 205
          expect(controller).to set_flash[:notice].to(project_moved_message(redirect_route, project))
        end
      end
    end
  end

  context 'for a non-GET request' do
    context 'when requesting the canonical path with different casing' do
      it 'does not 404' do
        post :generate, namespace_id: project.namespace, project_id: project

206
        expect(response).not_to have_gitlab_http_status(404)
207 208 209 210 211
      end

      it 'does not redirect to the correct casing' do
        post :generate, namespace_id: project.namespace, project_id: project

212
        expect(response).not_to have_gitlab_http_status(301)
213 214 215 216 217 218 219 220 221
      end
    end

    context 'when requesting a redirected path' do
      let!(:redirect_route) { project.redirect_routes.create(path: project.full_path + 'old') }

      it 'returns not found' do
        post :generate, namespace_id: project.namespace, project_id: project.to_param + 'old'

222
        expect(response).to have_gitlab_http_status(404)
223 224 225 226 227 228 229
      end
    end
  end

  def project_moved_message(redirect_route, project)
    "Project '#{redirect_route.path}' was moved to '#{project.full_path}'. Please update any links and bookmarks that may still have the old path."
  end
230
end