summaryrefslogtreecommitdiff
path: root/spec/graphql/mutations/releases/create_spec.rb
blob: d6305691dac796ab835ed7140df2ec1d615f52f4 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Mutations::Releases::Create do
  let_it_be(:project) { create(:project, :public, :repository) }
  let_it_be(:milestone_12_3) { create(:milestone, project: project, title: '12.3') }
  let_it_be(:milestone_12_4) { create(:milestone, project: project, title: '12.4') }
  let_it_be(:reporter) { create(:user) }
  let_it_be(:developer) { create(:user) }

  let(:mutation) { described_class.new(object: nil, context: { current_user: current_user }, field: nil) }

  let(:tag) { 'v1.1.0'}
  let(:ref) { 'master'}
  let(:name) { 'Version 1.0'}
  let(:description) { 'The first release :rocket:' }
  let(:released_at) { Time.parse('2018-12-10') }
  let(:milestones) { [milestone_12_3.title, milestone_12_4.title] }
  let(:assets) do
    {
      links: [
        {
          name: 'An asset link',
          url: 'https://gitlab.example.com/link',
          filepath: '/permanent/link',
          link_type: 'other'
        }
      ]
    }
  end

  let(:mutation_arguments) do
    {
      project_path: project.full_path,
      tag: tag,
      ref: ref,
      name: name,
      description: description,
      released_at: released_at,
      milestones: milestones,
      assets: assets
    }
  end

  around do |example|
    freeze_time { example.run }
  end

  before do
    project.add_reporter(reporter)
    project.add_developer(developer)
  end

  describe '#resolve' do
    subject(:resolve) do
      mutation.resolve(**mutation_arguments)
    end

    let(:new_release) { subject[:release] }

    context 'when the current user has access to create releases' do
      let(:current_user) { developer }

      it 'returns no errors' do
        expect(resolve).to include(errors: [])
      end

      it 'creates the release with the correct tag' do
        expect(new_release.tag).to eq(tag)
      end

      it 'creates the release with the correct name' do
        expect(new_release.name).to eq(name)
      end

      it 'creates the release with the correct description' do
        expect(new_release.description).to eq(description)
      end

      it 'creates the release with the correct released_at' do
        expect(new_release.released_at).to eq(released_at)
      end

      it 'creates the release with the correct created_at' do
        expect(new_release.created_at).to eq(Time.current)
      end

      it 'creates the release with the correct milestone associations' do
        expected_milestone_titles = [milestone_12_3.title, milestone_12_4.title]
        actual_milestone_titles = new_release.milestones.map { |m| m.title }

        # Right now the milestones are returned in a non-deterministic order.
        # `match_array` should be updated to `eq` once
        # https://gitlab.com/gitlab-org/gitlab/-/issues/259012 is addressed.
        expect(actual_milestone_titles).to match_array(expected_milestone_titles)
      end

      describe 'asset links' do
        let(:expected_link) { assets[:links].first }
        let(:new_link) { new_release.links.first }

        it 'creates a single asset link' do
          expect(new_release.links.size).to eq(1)
        end

        it 'creates the link with the correct name' do
          expect(new_link.name).to eq(expected_link[:name])
        end

        it 'creates the link with the correct url' do
          expect(new_link.url).to eq(expected_link[:url])
        end

        it 'creates the link with the correct link type' do
          expect(new_link.link_type).to eq(expected_link[:link_type])
        end

        it 'creates the link with the correct direct filepath' do
          expect(new_link.filepath).to eq(expected_link[:filepath])
        end
      end
    end

    context "when the current user doesn't have access to create releases" do
      let(:current_user) { reporter }

      it 'raises an error' do
        expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
      end
    end
  end
end