summaryrefslogtreecommitdiff
path: root/spec/finders/group_members_finder_spec.rb
blob: 34649097f701e7b43704577201cf2853dde636cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# frozen_string_literal: true

require 'spec_helper'

describe GroupMembersFinder, '#execute' do
  let(:group) { create(:group) }
  let(:nested_group) { create(:group, parent: group) }
  let(:deeper_nested_group) { create(:group, parent: nested_group) }
  let(:user1) { create(:user) }
  let(:user2) { create(:user) }
  let(:user3) { create(:user) }
  let(:user4) { create(:user) }
  let(:user5) { create(:user, :two_factor_via_otp) }

  it 'returns members for top-level group' do
    member1 = group.add_maintainer(user1)
    member2 = group.add_maintainer(user2)
    member3 = group.add_maintainer(user3)

    result = described_class.new(group).execute

    expect(result.to_a).to match_array([member3, member2, member1])
  end

  it 'returns members & inherited members for nested group by default' do
    group.add_developer(user2)
    nested_group.request_access(user4)
    member1 = group.add_maintainer(user1)
    member3 = nested_group.add_maintainer(user2)
    member4 = nested_group.add_maintainer(user3)

    result = described_class.new(nested_group).execute

    expect(result.to_a).to match_array([member1, member3, member4])
  end

  it 'does not return inherited members for nested group if requested' do
    group.add_maintainer(user1)
    group.add_developer(user2)
    member2 = nested_group.add_maintainer(user2)
    member3 = nested_group.add_maintainer(user3)

    result = described_class.new(nested_group).execute(include_relations: [:direct])

    expect(result.to_a).to match_array([member2, member3])
  end

  it 'returns only inherited members for nested group if requested' do
    group.add_developer(user2)
    nested_group.request_access(user4)
    member1 = group.add_maintainer(user1)
    nested_group.add_maintainer(user2)
    nested_group.add_maintainer(user3)

    result = described_class.new(nested_group).execute(include_relations: [:inherited])

    expect(result.to_a).to match_array([member1])
  end

  it 'does not return nil if `inherited only` relation is requested on root group' do
    group.add_developer(user2)

    result = described_class.new(group).execute(include_relations: [:inherited])

    expect(result).not_to be_nil
  end

  it 'returns members for descendant groups if requested' do
    member1 = group.add_maintainer(user2)
    member2 = group.add_maintainer(user1)
    nested_group.add_maintainer(user2)
    member3 = nested_group.add_maintainer(user3)
    member4 = nested_group.add_maintainer(user4)

    result = described_class.new(group).execute(include_relations: [:direct, :descendants])

    expect(result.to_a).to match_array([member1, member2, member3, member4])
  end

  it 'returns searched members if requested' do
    group.add_maintainer(user2)
    group.add_developer(user3)
    member = group.add_maintainer(user1)

    result = described_class.new(group).execute(params: { search: user1.name })

    expect(result.to_a).to match_array([member])
  end

  it 'returns nothing if search only in inherited relation' do
    group.add_maintainer(user2)
    group.add_developer(user3)
    group.add_maintainer(user1)

    result = described_class.new(group).execute(include_relations: [:inherited], params: { search: user1.name })

    expect(result.to_a).to match_array([])
  end

  it 'returns searched member only from nested_group if search only in inherited relation' do
    group.add_maintainer(user2)
    group.add_developer(user3)
    nested_group.add_maintainer(create(:user, name: user1.name))
    member = group.add_maintainer(user1)

    result = described_class.new(nested_group).execute(include_relations: [:inherited], params: { search: member.user.name })

    expect(result.to_a).to contain_exactly(member)
  end

  it 'returns members with two-factor auth if requested by owner' do
    group.add_owner(user2)
    group.add_maintainer(user1)
    member = group.add_maintainer(user5)

    result = described_class.new(group, user2).execute(params: { two_factor: 'enabled' })

    expect(result.to_a).to contain_exactly(member)
  end

  it 'returns members without two-factor auth if requested by owner' do
    member1 = group.add_owner(user2)
    member2 = group.add_maintainer(user1)
    member_with_2fa = group.add_maintainer(user5)

    result = described_class.new(group, user2).execute(params: { two_factor: 'disabled' })

    expect(result.to_a).not_to include(member_with_2fa)
    expect(result.to_a).to match_array([member1, member2])
  end
end