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

107 lines
3.2 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe GroupTree do
2018-03-17 18:26:18 +05:30
let(:group) { create(:group, :public) }
let(:user) { create(:user) }
controller(ApplicationController) do
# `described_class` is not available in this context
2020-03-13 15:44:24 +05:30
include GroupTree
2018-03-17 18:26:18 +05:30
def index
render_group_tree GroupsFinder.new(current_user).execute
end
end
before do
group.add_owner(user)
sign_in(user)
end
describe 'GET #index' do
2021-11-18 22:05:49 +05:30
shared_examples 'returns filtered groups' do
it 'filters groups' do
other_group = create(:group, name: 'filter')
other_group.add_owner(user)
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
get :index, params: { filter: 'filt' }, format: :json
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
expect(assigns(:groups)).to contain_exactly(other_group)
end
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
context 'for subgroups' do
it 'only renders root groups when no parent was given' do
create(:group, :public, parent: group)
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
get :index, format: :json
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
expect(assigns(:groups)).to contain_exactly(group)
end
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
it 'contains only the subgroup when a parent was given' do
subgroup = create(:group, :public, parent: group)
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
get :index, params: { parent_id: group.id }, format: :json
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
expect(assigns(:groups)).to contain_exactly(subgroup)
end
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
it 'allows filtering for subgroups and includes the parents for rendering' do
subgroup = create(:group, :public, parent: group, name: 'filter')
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
get :index, params: { filter: 'filt' }, format: :json
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
expect(assigns(:groups)).to contain_exactly(group, subgroup)
end
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
it 'does not include groups the user does not have access to' do
parent = create(:group, :private)
subgroup = create(:group, :private, parent: parent, name: 'filter')
subgroup.add_developer(user)
_other_subgroup = create(:group, :private, parent: parent, name: 'filte')
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
get :index, params: { filter: 'filt' }, format: :json
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
expect(assigns(:groups)).to contain_exactly(parent, subgroup)
end
2018-11-08 19:23:39 +05:30
2021-11-18 22:05:49 +05:30
it 'preloads parents regardless of pagination' do
allow(Kaminari.config).to receive(:default_per_page).and_return(1)
group = create(:group, :public)
subgroup = create(:group, :public, parent: group)
search_result = create(:group, :public, name: 'result', parent: subgroup)
2018-11-08 19:23:39 +05:30
2021-11-18 22:05:49 +05:30
get :index, params: { filter: 'resu' }, format: :json
2018-11-08 19:23:39 +05:30
2021-11-18 22:05:49 +05:30
expect(assigns(:groups)).to contain_exactly(group, subgroup, search_result)
end
2018-11-08 19:23:39 +05:30
end
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
context 'json content' do
it 'shows groups as json' do
get :index, format: :json
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
expect(json_response.first['id']).to eq(group.id)
end
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
context 'nested groups' do
it 'expands the tree when filtering' do
subgroup = create(:group, :public, parent: group, name: 'filter')
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
get :index, params: { filter: 'filt' }, format: :json
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
children_response = json_response.first['children']
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
expect(json_response.first['id']).to eq(group.id)
expect(children_response.first['id']).to eq(subgroup.id)
end
2018-03-17 18:26:18 +05:30
end
end
end
2021-11-18 22:05:49 +05:30
it_behaves_like 'returns filtered groups'
2018-03-17 18:26:18 +05:30
end
end