issuable_collections_spec.rb 5.2 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6 7 8 9 10 11 12
require 'spec_helper'

describe IssuableCollections do
  let(:user) { create(:user) }

  let(:controller) do
    klass = Class.new do
      def self.helper_method(name); end

      include IssuableCollections
13 14 15 16

      def finder_type
        IssuesFinder
      end
17 18 19 20
    end

    controller = klass.new

21
    allow(controller).to receive(:params).and_return(ActionController::Parameters.new(params))
22
    allow(controller).to receive(:current_user).and_return(user)
23 24 25 26

    controller
  end

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
  describe '#set_sort_order_from_user_preference' do
    describe 'when sort param given' do
      let(:params) { { sort: 'updated_desc' } }

      context 'when issuable_sorting_field is defined' do
        before do
          controller.class.define_method(:issuable_sorting_field) { :issues_sort}
        end

        it 'sets user_preference with the right value' do
          controller.send(:set_sort_order_from_user_preference)

          expect(user.user_preference.reload.issues_sort).to eq('updated_desc')
        end
      end

      context 'when no issuable_sorting_field is defined on the controller' do
        it 'does not touch user_preference' do
          allow(user).to receive(:user_preference)

          controller.send(:set_sort_order_from_user_preference)

          expect(user).not_to have_received(:user_preference)
        end
      end
    end

    context 'when a user sorting preference exists' do
      let(:params) { {} }

      before do
        controller.class.define_method(:issuable_sorting_field) { :issues_sort }
      end

      it 'returns the set preference' do
        user.user_preference.update(issues_sort: 'updated_asc')

        sort_preference = controller.send(:set_sort_order_from_user_preference)

        expect(sort_preference).to eq('updated_asc')
      end
    end
  end

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
  describe '#set_set_order_from_cookie' do
    describe 'when sort param given' do
      let(:cookies) { {} }
      let(:params) { { sort: 'downvotes_asc' } }

      it 'sets the cookie with the right values and flags' do
        allow(controller).to receive(:cookies).and_return(cookies)

        controller.send(:set_sort_order_from_cookie)

        expect(cookies['issue_sort']).to eq({ value: 'popularity', secure: false, httponly: false })
      end
    end

    describe 'when cookie exists' do
      let(:cookies) { { 'issue_sort' => 'id_asc' } }
      let(:params) { {} }

      it 'sets the cookie with the right values and flags' do
        allow(controller).to receive(:cookies).and_return(cookies)

        controller.send(:set_sort_order_from_cookie)

        expect(cookies['issue_sort']).to eq({ value: 'created_asc', secure: false, httponly: false })
      end
    end
  end

99
  describe '#page_count_for_relation' do
100 101
    let(:params) { { state: 'opened' } }

102 103 104 105 106 107 108
    it 'returns the number of pages' do
      relation = double(:relation, limit_value: 20)
      pages = controller.send(:page_count_for_relation, relation, 28)

      expect(pages).to eq(2)
    end
  end
109

110
  describe '#finder_options' do
111
    before do
112
      allow(controller).to receive(:cookies).and_return({})
113
      allow(controller).to receive(:current_user).and_return(nil)
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 168 169 170 171 172 173 174 175 176 177 178
    end

    subject { controller.send(:finder_options).to_h }

    context 'scalar params' do
      let(:params) do
        {
          assignee_id: '1',
          assignee_username: 'user1',
          author_id: '2',
          author_username: 'user2',
          authorized_only: 'yes',
          confidential: true,
          due_date: '2017-01-01',
          group_id: '3',
          iids: '4',
          label_name: 'foo',
          milestone_title: 'bar',
          my_reaction_emoji: 'thumbsup',
          non_archived: 'true',
          project_id: '5',
          scope: 'all',
          search: 'baz',
          sort: 'priority',
          state: 'opened',
          invalid_param: 'invalid_param'
        }
      end

      it 'only allows whitelisted params' do
        is_expected.to include({
          'assignee_id' => '1',
          'assignee_username' => 'user1',
          'author_id' => '2',
          'author_username' => 'user2',
          'confidential' => true,
          'label_name' => 'foo',
          'milestone_title' => 'bar',
          'my_reaction_emoji' => 'thumbsup',
          'due_date' => '2017-01-01',
          'scope' => 'all',
          'search' => 'baz',
          'sort' => 'priority',
          'state' => 'opened'
        })

        is_expected.not_to include('invalid_param')
      end
    end

    context 'array params' do
      let(:params) do
        {
          assignee_username: %w[user1 user2],
          label_name: %w[label1 label2],
          invalid_param: 'invalid_param',
          invalid_array: ['param']
        }
      end

      it 'only allows whitelisted params' do
        is_expected.to include({
          'label_name' => %w[label1 label2],
          'assignee_username' => %w[user1 user2]
        })
179

180 181
        is_expected.not_to include('invalid_param', 'invalid_array')
      end
182
    end
183 184 185 186 187 188 189 190 191 192 193

    context 'search using an issue iid' do
      let(:params) { { search: "#5" } }

      it 'mutates the search into a filter by iid' do
        is_expected.to include({
            'iids' => '5',
            'search' => nil
        })
      end
    end
194
  end
195
end