2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-13 11:16:30 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-03 23:08:05 -04:00
|
|
|
RSpec.describe GroupTree do
|
2017-09-13 11:16:30 -04:00
|
|
|
let(:group) { create(:group, :public) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
controller(ApplicationController) do
|
2017-09-14 03:01:29 -04:00
|
|
|
# `described_class` is not available in this context
|
2019-12-20 04:24:38 -05:00
|
|
|
include GroupTree
|
2017-09-13 11:16:30 -04:00
|
|
|
|
|
|
|
def index
|
2017-10-09 06:02:40 -04:00
|
|
|
render_group_tree GroupsFinder.new(current_user).execute
|
2017-09-13 11:16:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #index' do
|
2021-09-27 11:10:13 -04:00
|
|
|
shared_examples 'returns filtered groups' do
|
|
|
|
it 'filters groups' do
|
|
|
|
other_group = create(:group, name: 'filter')
|
|
|
|
other_group.add_owner(user)
|
2017-09-13 11:16:30 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
get :index, params: { filter: 'filt' }, format: :json
|
2017-09-13 11:16:30 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
expect(assigns(:groups)).to contain_exactly(other_group)
|
|
|
|
end
|
2017-09-13 11:16:30 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
context 'for subgroups' do
|
|
|
|
it 'only renders root groups when no parent was given' do
|
|
|
|
create(:group, :public, parent: group)
|
2017-09-13 11:16:30 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
get :index, format: :json
|
2017-09-13 11:16:30 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
expect(assigns(:groups)).to contain_exactly(group)
|
|
|
|
end
|
2017-09-13 11:16:30 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
it 'contains only the subgroup when a parent was given' do
|
|
|
|
subgroup = create(:group, :public, parent: group)
|
2017-09-13 11:16:30 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
get :index, params: { parent_id: group.id }, format: :json
|
2017-09-13 11:16:30 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
expect(assigns(:groups)).to contain_exactly(subgroup)
|
|
|
|
end
|
2017-09-14 03:01:29 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
it 'allows filtering for subgroups and includes the parents for rendering' do
|
|
|
|
subgroup = create(:group, :public, parent: group, name: 'filter')
|
2017-09-14 03:01:29 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
get :index, params: { filter: 'filt' }, format: :json
|
2017-09-14 03:01:29 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
expect(assigns(:groups)).to contain_exactly(group, subgroup)
|
|
|
|
end
|
2017-10-09 06:02:40 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
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')
|
2017-10-09 06:02:40 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
get :index, params: { filter: 'filt' }, format: :json
|
2017-10-09 06:02:40 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
expect(assigns(:groups)).to contain_exactly(parent, subgroup)
|
|
|
|
end
|
2018-07-05 05:16:08 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
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-07-05 05:16:08 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
get :index, params: { filter: 'resu' }, format: :json
|
2018-07-05 05:16:08 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
expect(assigns(:groups)).to contain_exactly(group, subgroup, search_result)
|
|
|
|
end
|
2018-07-05 05:16:08 -04:00
|
|
|
end
|
2017-09-13 11:16:30 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
context 'json content' do
|
|
|
|
it 'shows groups as json' do
|
|
|
|
get :index, format: :json
|
2017-09-13 11:16:30 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
expect(json_response.first['id']).to eq(group.id)
|
|
|
|
end
|
2017-09-14 03:01:29 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
context 'nested groups' do
|
|
|
|
it 'expands the tree when filtering' do
|
|
|
|
subgroup = create(:group, :public, parent: group, name: 'filter')
|
2017-09-14 03:01:29 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
get :index, params: { filter: 'filt' }, format: :json
|
2017-09-14 03:01:29 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
children_response = json_response.first['children']
|
2017-09-14 03:01:29 -04:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
expect(json_response.first['id']).to eq(group.id)
|
|
|
|
expect(children_response.first['id']).to eq(subgroup.id)
|
|
|
|
end
|
2017-09-14 03:01:29 -04:00
|
|
|
end
|
|
|
|
end
|
2017-09-13 11:16:30 -04:00
|
|
|
end
|
2021-09-27 11:10:13 -04:00
|
|
|
|
|
|
|
it_behaves_like 'returns filtered groups'
|
2017-09-13 11:16:30 -04:00
|
|
|
end
|
|
|
|
end
|