summaryrefslogtreecommitdiff
path: root/spec/policies/group_member_policy_spec.rb
blob: a4f3301a06481fbf38b45f7a2397e8f8b796eeb6 (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
# frozen_string_literal: true

require 'spec_helper'

describe GroupMemberPolicy do
  let(:guest) { create(:user) }
  let(:owner) { create(:user) }
  let(:group) { create(:group, :private) }

  before do
    group.add_guest(guest)
    group.add_owner(owner)
  end

  let(:member_related_permissions) do
    [:update_group_member, :destroy_group_member]
  end

  let(:membership) { current_user.members.first }

  subject { described_class.new(current_user, membership) }

  def expect_allowed(*permissions)
    permissions.each { |p| is_expected.to be_allowed(p) }
  end

  def expect_disallowed(*permissions)
    permissions.each { |p| is_expected.not_to be_allowed(p) }
  end

  context 'with guest user' do
    let(:current_user) { guest }

    it do
      expect_disallowed(:member_related_permissions)
    end
  end

  context 'with one owner' do
    let(:current_user) { owner }

    it do
      expect_disallowed(:destroy_group_member)
      expect_disallowed(:update_group_member)
    end
  end

  context 'with more than one owner' do
    let(:current_user) { owner }

    before do
      group.add_owner(create(:user))
    end

    it do
      expect_allowed(:destroy_group_member)
      expect_allowed(:update_group_member)
    end
  end

  context 'with the group parent' do
    let(:current_user) { create :user }
    let(:subgroup) { create(:group, :private, parent: group)}

    before do
      group.add_owner(owner)
      subgroup.add_owner(current_user)
    end

    it do
      expect_allowed(:destroy_group_member)
      expect_allowed(:update_group_member)
    end
  end

  context 'without group parent' do
    let(:current_user) { create :user }
    let(:subgroup) { create(:group, :private)}

    before do
      subgroup.add_owner(current_user)
    end

    it do
      expect_disallowed(:destroy_group_member)
      expect_disallowed(:update_group_member)
    end
  end

  context 'without group parent with two owners' do
    let(:current_user) { create :user }
    let(:other_user) { create :user }
    let(:subgroup) { create(:group, :private)}

    before do
      subgroup.add_owner(current_user)
      subgroup.add_owner(other_user)
    end

    it do
      expect_allowed(:destroy_group_member)
      expect_allowed(:update_group_member)
    end
  end
end