2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-29 03:56:52 -04:00
|
|
|
require 'spec_helper'
|
2015-11-05 05:03:02 -05:00
|
|
|
|
2017-06-29 00:13:10 -04:00
|
|
|
RSpec.describe Release do
|
2020-12-23 13:10:19 -05:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project) { create(:project, :public, :repository) }
|
2021-04-21 05:09:15 -04:00
|
|
|
|
2021-01-26 10:08:58 -05:00
|
|
|
let(:release) { create(:release, project: project, author: user) }
|
2015-11-06 09:43:59 -05:00
|
|
|
|
|
|
|
it { expect(release).to be_valid }
|
|
|
|
|
|
|
|
describe 'associations' do
|
2021-03-17 11:09:03 -04:00
|
|
|
it { is_expected.to belong_to(:project).touch(true) }
|
2018-12-12 04:56:43 -05:00
|
|
|
it { is_expected.to belong_to(:author).class_name('User') }
|
2019-01-02 01:01:15 -05:00
|
|
|
it { is_expected.to have_many(:links).class_name('Releases::Link') }
|
2019-09-16 11:06:26 -04:00
|
|
|
it { is_expected.to have_many(:milestones) }
|
|
|
|
it { is_expected.to have_many(:milestone_releases) }
|
2020-03-24 08:09:42 -04:00
|
|
|
it { is_expected.to have_many(:evidences).class_name('Releases::Evidence') }
|
2015-11-06 09:43:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validation' do
|
|
|
|
it { is_expected.to validate_presence_of(:project) }
|
2019-10-17 11:06:17 -04:00
|
|
|
it { is_expected.to validate_presence_of(:tag) }
|
2019-04-09 02:52:15 -04:00
|
|
|
|
|
|
|
context 'when a release exists in the database without a name' do
|
|
|
|
it 'does not require name' do
|
|
|
|
existing_release_without_name = build(:release, project: project, author: user, name: nil)
|
2021-11-03 17:10:35 -04:00
|
|
|
existing_release_without_name.save!(validate: false)
|
2019-04-09 02:52:15 -04:00
|
|
|
|
|
|
|
existing_release_without_name.description = "change"
|
2021-11-03 17:10:35 -04:00
|
|
|
existing_release_without_name.save!
|
2019-04-09 02:52:15 -04:00
|
|
|
existing_release_without_name.reload
|
|
|
|
|
|
|
|
expect(existing_release_without_name).to be_valid
|
|
|
|
expect(existing_release_without_name.description).to eq("change")
|
2019-11-12 16:06:30 -05:00
|
|
|
expect(existing_release_without_name.name).not_to be_nil
|
2019-04-09 02:52:15 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-03 05:38:59 -04:00
|
|
|
|
2021-04-29 08:09:58 -04:00
|
|
|
context 'when description of a release is longer than the limit' do
|
|
|
|
let(:description) { 'a' * (Gitlab::Database::MAX_TEXT_SIZE_LIMIT + 1) }
|
|
|
|
let(:release) { build(:release, project: project, description: description) }
|
|
|
|
|
|
|
|
it 'creates a validation error' do
|
|
|
|
release.validate
|
|
|
|
|
|
|
|
expect(release.errors.full_messages)
|
|
|
|
.to include("Description is too long (maximum is #{Gitlab::Database::MAX_TEXT_SIZE_LIMIT} characters)")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-03 05:38:59 -04:00
|
|
|
context 'when a release is tied to a milestone for another project' do
|
|
|
|
it 'creates a validation error' do
|
2019-09-16 11:06:26 -04:00
|
|
|
milestone = build(:milestone, project: create(:project))
|
2022-05-06 08:09:05 -04:00
|
|
|
|
|
|
|
expect { release.milestones << milestone }
|
|
|
|
.to raise_error(ActiveRecord::RecordInvalid,
|
|
|
|
'Validation failed: Release does not have the same project as the milestone')
|
2019-09-03 05:38:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a release is tied to a milestone linked to the same project' do
|
2019-09-16 11:06:26 -04:00
|
|
|
it 'successfully links this release to this milestone' do
|
|
|
|
milestone = build(:milestone, project: project)
|
|
|
|
expect { release.milestones << milestone }.to change { MilestoneRelease.count }.by(1)
|
2019-09-03 05:38:59 -04:00
|
|
|
end
|
|
|
|
end
|
2022-06-16 08:09:26 -04:00
|
|
|
|
|
|
|
context 'when creating new release' do
|
|
|
|
subject { build(:release, project: project, name: 'Release 1.0') }
|
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:author_id) }
|
|
|
|
|
|
|
|
context 'when feature flag is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(validate_release_with_author: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.not_to validate_presence_of(:author_id) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Mimic releases created before 11.7
|
|
|
|
# See: https://gitlab.com/gitlab-org/gitlab/-/blob/8e5a110b01f842d8b6a702197928757a40ce9009/app/models/release.rb#L14
|
|
|
|
context 'when updating existing release without author' do
|
|
|
|
let(:release) { create(:release, :legacy) }
|
|
|
|
|
|
|
|
it 'updates successfully' do
|
|
|
|
release.description += 'Update'
|
|
|
|
|
|
|
|
expect { release.save! }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
2015-11-06 09:43:59 -05:00
|
|
|
end
|
2019-01-02 01:01:15 -05:00
|
|
|
|
|
|
|
describe '#assets_count' do
|
2021-04-28 05:10:13 -04:00
|
|
|
subject { Release.find(release.id).assets_count }
|
2019-01-02 01:01:15 -05:00
|
|
|
|
|
|
|
it 'returns the number of sources' do
|
2019-10-31 11:06:41 -04:00
|
|
|
is_expected.to eq(Gitlab::Workhorse::ARCHIVE_FORMATS.count)
|
2019-01-02 01:01:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a links exists' do
|
|
|
|
let!(:link) { create(:release_link, release: release) }
|
|
|
|
|
|
|
|
it 'counts the link as an asset' do
|
2019-10-31 11:06:41 -04:00
|
|
|
is_expected.to eq(1 + Gitlab::Workhorse::ARCHIVE_FORMATS.count)
|
2019-01-02 01:01:15 -05:00
|
|
|
end
|
2019-05-03 09:29:20 -04:00
|
|
|
|
|
|
|
it "excludes sources count when asked" do
|
2021-04-28 05:10:13 -04:00
|
|
|
assets_count = Release.find(release.id).assets_count(except: [:sources])
|
2019-05-03 09:29:20 -04:00
|
|
|
expect(assets_count).to eq(1)
|
|
|
|
end
|
2019-01-02 01:01:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-17 11:09:13 -04:00
|
|
|
describe '.create' do
|
|
|
|
it "fills released_at using created_at if it's not set" do
|
2021-11-03 17:10:35 -04:00
|
|
|
release = create(:release, project: project, author: user, released_at: nil)
|
2020-07-17 11:09:13 -04:00
|
|
|
|
|
|
|
expect(release.released_at).to eq(release.created_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not change released_at if it's set explicitly" do
|
|
|
|
released_at = Time.zone.parse('2018-10-20T18:00:00Z')
|
|
|
|
|
2021-11-03 17:10:35 -04:00
|
|
|
release = create(:release, project: project, author: user, released_at: released_at)
|
2020-07-17 11:09:13 -04:00
|
|
|
|
|
|
|
expect(release.released_at).to eq(released_at)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-26 10:08:58 -05:00
|
|
|
describe '#update' do
|
2021-11-03 17:10:35 -04:00
|
|
|
subject { release.update!(params) }
|
2021-01-26 10:08:58 -05:00
|
|
|
|
|
|
|
context 'when links do not exist' do
|
|
|
|
context 'when params are specified for creation' do
|
|
|
|
let(:params) do
|
|
|
|
{ links_attributes: [{ name: 'test', url: 'https://www.google.com/' }] }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a link successfuly' do
|
|
|
|
is_expected.to eq(true)
|
|
|
|
|
|
|
|
expect(release.links.count).to eq(1)
|
|
|
|
expect(release.links.first.name).to eq('test')
|
|
|
|
expect(release.links.first.url).to eq('https://www.google.com/')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a link exists' do
|
|
|
|
let!(:link1) { create(:release_link, release: release, name: 'test1', url: 'https://www.google1.com/') }
|
|
|
|
let!(:link2) { create(:release_link, release: release, name: 'test2', url: 'https://www.google2.com/') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
release.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when params are specified for update' do
|
|
|
|
let(:params) do
|
|
|
|
{ links_attributes: [{ id: link1.id, name: 'new' }] }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the link successfully' do
|
|
|
|
is_expected.to eq(true)
|
|
|
|
|
|
|
|
expect(release.links.count).to eq(2)
|
|
|
|
expect(release.links.first.name).to eq('new')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when params are specified for deletion' do
|
|
|
|
let(:params) do
|
|
|
|
{ links_attributes: [{ id: link1.id, _destroy: true }] }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes the link successfuly' do
|
|
|
|
is_expected.to eq(true)
|
|
|
|
|
|
|
|
expect(release.links.count).to eq(1)
|
|
|
|
expect(release.links.first.name).to eq(link2.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-02 01:01:15 -05:00
|
|
|
describe '#sources' do
|
|
|
|
subject { release.sources }
|
|
|
|
|
|
|
|
it 'returns sources' do
|
|
|
|
is_expected.to all(be_a(Releases::Source))
|
|
|
|
end
|
|
|
|
end
|
2019-07-03 05:12:15 -04:00
|
|
|
|
|
|
|
describe '#upcoming_release?' do
|
|
|
|
context 'during the backfill migration when released_at could be nil' do
|
|
|
|
it 'handles a nil released_at value and returns false' do
|
|
|
|
allow(release).to receive(:released_at).and_return nil
|
|
|
|
|
|
|
|
expect(release.upcoming_release?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-17 11:06:17 -04:00
|
|
|
|
2019-11-19 04:06:16 -05:00
|
|
|
describe 'evidence' do
|
|
|
|
let(:release_with_evidence) { create(:release, :with_evidence, project: project) }
|
|
|
|
|
2019-10-17 11:06:17 -04:00
|
|
|
context 'when a release is deleted' do
|
|
|
|
it 'also deletes the associated evidence' do
|
2019-11-19 04:06:16 -05:00
|
|
|
release_with_evidence
|
2019-10-17 11:06:17 -04:00
|
|
|
|
2021-11-03 17:10:35 -04:00
|
|
|
expect { release_with_evidence.destroy! }.to change(Releases::Evidence, :count).by(-1)
|
2019-10-17 11:06:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-17 14:08:05 -04:00
|
|
|
|
2019-11-12 16:06:30 -05:00
|
|
|
describe '#name' do
|
|
|
|
context 'name is nil' do
|
|
|
|
before do
|
2021-11-03 17:10:35 -04:00
|
|
|
release.update!(name: nil)
|
2019-11-12 16:06:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns tag' do
|
|
|
|
expect(release.name).to eq(release.tag)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-11-19 04:06:16 -05:00
|
|
|
|
2020-01-10 13:07:43 -05:00
|
|
|
describe '#milestone_titles' do
|
2020-12-23 13:10:19 -05:00
|
|
|
let_it_be(:milestone_1) { create(:milestone, project: project, title: 'Milestone 1') }
|
|
|
|
let_it_be(:milestone_2) { create(:milestone, project: project, title: 'Milestone 2') }
|
|
|
|
let_it_be(:release) { create(:release, project: project, milestones: [milestone_1, milestone_2]) }
|
2020-01-10 13:07:43 -05:00
|
|
|
|
2020-12-23 13:10:19 -05:00
|
|
|
it { expect(release.milestone_titles).to eq("#{milestone_1.title}, #{milestone_2.title}")}
|
2020-01-10 13:07:43 -05:00
|
|
|
end
|
2015-11-05 05:03:02 -05:00
|
|
|
end
|