debian-mirror-gitlab/spec/controllers/concerns/issuable_collections_spec.rb

155 lines
4.2 KiB
Ruby
Raw Normal View History

2018-03-17 18:26:18 +05:30
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
2018-03-27 19:54:05 +05:30
def finder_type
IssuesFinder
end
2018-03-17 18:26:18 +05:30
end
controller = klass.new
allow(controller).to receive(:params).and_return(ActionController::Parameters.new(params))
2019-03-02 22:35:43 +05:30
allow(controller).to receive(:current_user).and_return(user)
2018-03-17 18:26:18 +05:30
controller
end
2019-03-02 22:35:43 +05:30
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
2018-11-20 20:47:30 +05:30
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
2018-03-17 18:26:18 +05:30
describe '#page_count_for_relation' do
let(:params) { { state: 'opened' } }
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
2019-02-15 15:39:39 +05:30
describe '#finder_options' do
2018-03-17 18:26:18 +05:30
let(:params) do
{
assignee_id: '1',
assignee_username: 'user1',
author_id: '2',
author_username: 'user2',
authorized_only: '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
2019-02-15 15:39:39 +05:30
it 'only allows whitelisted params' do
2018-03-17 18:26:18 +05:30
allow(controller).to receive(:cookies).and_return({})
2019-02-15 15:39:39 +05:30
allow(controller).to receive(:current_user).and_return(nil)
2018-03-17 18:26:18 +05:30
2019-02-15 15:39:39 +05:30
finder_options = controller.send(:finder_options)
2018-03-17 18:26:18 +05:30
2019-02-15 15:39:39 +05:30
expect(finder_options).to eq(ActionController::Parameters.new({
2018-03-17 18:26:18 +05:30
'assignee_id' => '1',
'assignee_username' => 'user1',
'author_id' => '2',
'author_username' => 'user2',
'label_name' => 'foo',
'milestone_title' => 'bar',
'my_reaction_emoji' => 'thumbsup',
2019-02-15 15:39:39 +05:30
'due_date' => '2017-01-01',
2018-03-17 18:26:18 +05:30
'scope' => 'all',
'search' => 'baz',
'sort' => 'priority',
'state' => 'opened'
2019-02-15 15:39:39 +05:30
}).permit!)
2018-03-17 18:26:18 +05:30
end
end
end