summaryrefslogtreecommitdiff
path: root/spec/services/releases/links/update_service_spec.rb
blob: 40756c7eced692fd14be4f831aa4a2fdf2d71a5f (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Releases::Links::UpdateService, feature_category: :release_orchestration do
  let(:service) { described_class.new(release, user, params) }
  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:user) { create(:user) }
  let_it_be(:release) { create(:release, project: project, author: user, tag: 'v1.1.0') }

  let(:release_link) do
    create(
      :release_link,
      release: release,
      name: 'awesome-app.dmg',
      url: 'https://example.com/download/awesome-app.dmg'
    )
  end

  let(:params) { { name: name, url: url, direct_asset_path: direct_asset_path, link_type: link_type } }
  let(:name) { 'link' }
  let(:url) { 'https://example.com' }
  let(:direct_asset_path) { '/path' }
  let(:link_type) { 'other' }

  before do
    project.add_developer(user)
  end

  describe '#execute' do
    subject(:execute) { service.execute(release_link) }

    let(:updated_link) { execute.payload[:link] }

    it 'successfully updates a release link' do
      is_expected.to be_success

      expect(updated_link).to have_attributes(
        name: name,
        url: url,
        filepath: direct_asset_path,
        link_type: link_type
      )
    end

    context 'when user does not have access to update release link' do
      before do
        project.add_guest(user)
      end

      it 'returns an error' do
        is_expected.to be_error
        expect(execute.message).to include('Access Denied')
      end
    end

    context 'when url is invalid' do
      let(:url) { 'not_a_url' }

      it 'returns an error' do
        is_expected.to be_error
        expect(execute.message[0]).to include('Url is blocked')
      end
    end

    context 'when both direct_asset_path and filepath are provided' do
      let(:params) { super().merge(filepath: '/filepath') }

      it 'prefers direct_asset_path' do
        is_expected.to be_success

        expect(updated_link.filepath).to eq(direct_asset_path)
      end
    end

    context 'when only filepath is set' do
      let(:params) { super().merge(filepath: '/filepath') }
      let(:direct_asset_path) { nil }

      it 'uses filepath' do
        is_expected.to be_success

        expect(updated_link.filepath).to eq('/filepath')
      end
    end
  end
end