summaryrefslogtreecommitdiff
path: root/spec/workers/remove_expired_group_links_worker_spec.rb
blob: 9557aa3086ca52133128b06654dbfb2f49986ee8 (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
# frozen_string_literal: true

require 'spec_helper'

describe RemoveExpiredGroupLinksWorker do
  describe '#perform' do
    context 'ProjectGroupLinks' do
      let!(:expired_project_group_link) { create(:project_group_link, expires_at: 1.hour.ago) }
      let!(:project_group_link_expiring_in_future) { create(:project_group_link, expires_at: 10.days.from_now) }
      let!(:non_expiring_project_group_link) { create(:project_group_link, expires_at: nil) }

      it 'removes expired group links' do
        expect { subject.perform }.to change { ProjectGroupLink.count }.by(-1)
        expect(ProjectGroupLink.find_by(id: expired_project_group_link.id)).to be_nil
      end

      it 'leaves group links that expire in the future' do
        subject.perform
        expect(project_group_link_expiring_in_future.reload).to be_present
      end

      it 'leaves group links that do not expire at all' do
        subject.perform
        expect(non_expiring_project_group_link.reload).to be_present
      end
    end

    context 'GroupGroupLinks' do
      let(:mock_destroy_service) { instance_double(Groups::GroupLinks::DestroyService) }

      before do
        allow(Groups::GroupLinks::DestroyService).to(
          receive(:new).and_return(mock_destroy_service))
      end

      context 'expired GroupGroupLink exists' do
        before do
          create(:group_group_link, expires_at: 1.hour.ago)
        end

        it 'calls Groups::GroupLinks::DestroyService' do
          expect(mock_destroy_service).to receive(:execute).once

          subject.perform
        end
      end

      context 'expired GroupGroupLink does not exist' do
        it 'does not call Groups::GroupLinks::DestroyService' do
          expect(mock_destroy_service).not_to receive(:execute)

          subject.perform
        end
      end
    end
  end
end