BigW Consortium Gitlab

group_member_spec.rb 2.63 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan committed
3
describe GroupMember, models: true do
4 5 6 7 8 9 10 11 12 13 14 15
  describe '.access_level_roles' do
    it 'returns Gitlab::Access.options_with_owner' do
      expect(described_class.access_level_roles).to eq(Gitlab::Access.options_with_owner)
    end
  end

  describe '.access_levels' do
    it 'returns Gitlab::Access.options_with_owner' do
      expect(described_class.access_levels).to eq(Gitlab::Access.sym_options_with_owner)
    end
  end

16
  describe '.add_users' do
17 18 19 20
    it 'adds the given users to the given group' do
      group = create(:group)
      users = create_list(:user, 2)

21
      described_class.add_users(
22 23 24 25 26 27 28 29 30
        group,
        [users.first.id, users.second],
        described_class::MASTER
      )

      expect(group.users).to include(users.first, users.second)
    end
  end

31
  describe 'notifications' do
32
    describe "#after_create" do
33
      it "sends email to user" do
34
        membership = build(:group_member)
35 36 37

        allow(membership).to receive(:notification_service).
          and_return(double('NotificationService').as_null_object)
38
        expect(membership).to receive(:notification_service)
39

40 41 42 43 44 45
        membership.save
      end
    end

    describe "#after_update" do
      before do
46
        @group_member = create :group_member
47 48
        allow(@group_member).to receive(:notification_service).
          and_return(double('NotificationService').as_null_object)
49 50
      end

51
      it "sends email to user" do
52 53
        expect(@group_member).to receive(:notification_service)
        @group_member.update_attribute(:access_level, GroupMember::MASTER)
54 55 56
      end

      it "does not send an email when the access level has not changed" do
57 58
        expect(@group_member).not_to receive(:notification_service)
        @group_member.update_attribute(:access_level, GroupMember::OWNER)
59 60
      end
    end
61

62 63
    describe '#after_accept_request' do
      it 'calls NotificationService.accept_group_access_request' do
64
        member = create(:group_member, user: build(:user), requested_at: Time.now)
65 66 67

        expect_any_instance_of(NotificationService).to receive(:new_group_member)

68
        member.__send__(:after_accept_request)
69 70 71
      end
    end

72 73 74 75 76
    describe '#real_source_type' do
      subject { create(:group_member).real_source_type }

      it { is_expected.to eq 'Group' }
    end
77
  end
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

  describe '#update_two_factor_requirement' do
    let(:user) { build :user }
    let(:group_member) { build :group_member, user: user }

    it 'is called after creation and deletion' do
      expect(user).to receive(:update_two_factor_requirement)

      group_member.save

      expect(user).to receive(:update_two_factor_requirement)

      group_member.destroy
    end
  end
93
end