summaryrefslogtreecommitdiff
path: root/spec/finders/releases_finder_spec.rb
blob: 3dc01570d64dddcc5dd6b67dae708ef3c1621928 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ReleasesFinder do
  let(:user)       { create(:user) }
  let(:project)    { create(:project, :repository) }
  let(:params)     { {} }
  let(:repository) { project.repository }
  let(:v1_0_0)     { create(:release, project: project, tag: 'v1.0.0') }
  let(:v1_1_0)     { create(:release, project: project, tag: 'v1.1.0') }
  let(:finder) { described_class.new(project, user, params) }

  before do
    v1_0_0.update_attribute(:released_at, 2.days.ago)
    v1_1_0.update_attribute(:released_at, 1.day.ago)
  end

  describe '#execute' do
    subject { finder.execute(**args) }

    let(:args) { {} }

    context 'when the user is not part of the project' do
      it 'returns no releases' do
        is_expected.to be_empty
      end
    end

    context 'when the user is a project developer' do
      before do
        project.add_developer(user)
      end

      it 'sorts by release date' do
        is_expected.to be_present
        expect(subject.size).to eq(2)
        expect(subject).to eq([v1_1_0, v1_0_0])
      end

      it 'preloads associations' do
        expect(Release).to receive(:preloaded).once.and_call_original

        subject
      end

      context 'when preload is false' do
        let(:args) { { preload: false } }

        it 'does not preload associations' do
          expect(Release).not_to receive(:preloaded)

          subject
        end
      end

      # See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/27716
      context 'when tag is nil' do
        before do
          v1_0_0.update_column(:tag, nil)
        end

        it 'ignores rows with a nil tag' do
          expect(subject.size).to eq(1)
          expect(subject).to eq([v1_1_0])
        end
      end

      context 'when a tag parameter is passed' do
        let(:params) { { tag: 'v1.0.0' } }

        it 'only returns the release with the matching tag' do
          expect(subject).to eq([v1_0_0])
        end
      end
    end
  end
end