BigW Consortium Gitlab

group_tree_spec.rb 2.51 KB
Newer Older
1 2 3 4 5 6 7
require 'spec_helper'

describe GroupTree do
  let(:group) { create(:group, :public) }
  let(:user) { create(:user) }

  controller(ApplicationController) do
8 9
    # `described_class` is not available in this context
    include GroupTree # rubocop:disable RSpec/DescribedClass
10 11

    def index
12
      render_group_tree GroupsFinder.new(current_user).execute
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    end
  end

  before do
    group.add_owner(user)
    sign_in(user)
  end

  describe 'GET #index' do
    it 'filters groups' do
      other_group = create(:group, name: 'filter')
      other_group.add_owner(user)

      get :index, filter: 'filt', format: :json

      expect(assigns(:groups)).to contain_exactly(other_group)
    end

    context 'for subgroups', :nested_groups do
      it 'only renders root groups when no parent was given' do
        create(:group, :public, parent: group)

        get :index, format: :json

        expect(assigns(:groups)).to contain_exactly(group)
      end

      it 'contains only the subgroup when a parent was given' do
        subgroup = create(:group, :public, parent: group)

        get :index, parent_id: group.id, format: :json

        expect(assigns(:groups)).to contain_exactly(subgroup)
      end
47

48
      it 'allows filtering for subgroups and includes the parents for rendering' do
49 50 51 52
        subgroup = create(:group, :public, parent: group, name: 'filter')

        get :index, filter: 'filt', format: :json

53
        expect(assigns(:groups)).to contain_exactly(group, subgroup)
54
      end
55 56 57 58 59 60 61 62 63 64 65

      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')

        get :index, filter: 'filt', format: :json

        expect(assigns(:groups)).to contain_exactly(parent, subgroup)
      end
66 67 68 69 70 71 72 73
    end

    context 'json content' do
      it 'shows groups as json' do
        get :index, format: :json

        expect(json_response.first['id']).to eq(group.id)
      end
74 75 76 77 78 79 80 81 82 83 84 85 86

      context 'nested groups', :nested_groups do
        it 'expands the tree when filtering' do
          subgroup = create(:group, :public, parent: group, name: 'filter')

          get :index, filter: 'filt', format: :json

          children_response = json_response.first['children']

          expect(json_response.first['id']).to eq(group.id)
          expect(children_response.first['id']).to eq(subgroup.id)
        end
      end
87 88 89
    end
  end
end