Welcome to mirror list, hosted at ThFree Co, Russian Federation.

commit_references_spec.rb « project « graphql « api « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4b545adee12fd59232ca41f816b173750155d718 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Query.project(fullPath).commitReferences(commitSha)', feature_category: :source_code_management do
  include GraphqlHelpers
  include Presentable

  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:repository) { project.repository.raw }
  let_it_be(:current_user) { project.first_owner }
  let_it_be(:branches_names) { %w[master not-merged-branch v1.1.0] }
  let_it_be(:tag_name) { 'v1.0.0' }
  let_it_be(:commit_sha) { repository.commit.id }

  let(:post_query) { post_graphql(query, current_user: current_user) }
  let(:data) { graphql_data.dig(*path) }
  let(:base_args) { {} }
  let(:args) { base_args }

  shared_context 'with the limit argument' do
    context 'with limit of 2' do
      let(:args) { { limit: 2 } }

      it 'returns the right amount of refs' do
        post_query
        expect(data.count).to be <= 2
      end
    end

    context 'with limit of -2' do
      let(:args) { { limit: -2 } }

      it 'casts an argument error "limit must be greater then 0"' do
        post_query
        expect(graphql_errors).to include(custom_graphql_error(path - ['names'],
          'limit must be within 1..1000'))
      end
    end

    context 'with limit of 1001' do
      let(:args) { { limit: 1001 } }

      it 'casts an argument error "limit must be greater then 0"' do
        post_query
        expect(graphql_errors).to include(custom_graphql_error(path - ['names'],
          'limit must be within 1..1000'))
      end
    end
  end

  describe 'the path commitReferences should return nil' do
    let(:path) { %w[project commitReferences] }

    let(:query) do
      graphql_query_for(:project, { fullPath: project.full_path },
        query_graphql_field(
          :commitReferences,
          { commitSha: commit_sha },
          query_graphql_field(:tippingTags, :names)
        )
      )
    end

    context 'when commit does not exist' do
      let(:commit_sha) { '6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff4' }

      it 'commitReferences returns nil' do
        post_query
        expect(data).to eq(nil)
      end
    end

    context 'when sha length is incorrect' do
      let(:commit_sha) { 'foo' }

      it 'commitReferences returns nil' do
        post_query
        expect(data).to eq(nil)
      end
    end

    context 'when user is not authorized' do
      let(:commit_sha) { repository.commit.id }
      let(:current_user) { create(:user) }

      it 'commitReferences returns nil' do
        post_query
        expect(data).to eq(nil)
      end
    end
  end

  context 'with containing refs' do
    let(:base_args) { { excludeTipped: false } }
    let(:excluded_tipped_args) do
      hash = base_args.dup
      hash[:excludeTipped] = true
      hash
    end

    context 'with path Query.project(fullPath).commitReferences(commitSha).containingTags' do
      let_it_be(:commit_sha) { repository.find_tag(tag_name).target_commit.sha }
      let_it_be(:path) { %w[project commitReferences containingTags names] }
      let(:query) do
        graphql_query_for(
          :project,
          { fullPath: project.full_path },
          query_graphql_field(
            :commitReferences,
            { commitSha: commit_sha },
            query_graphql_field(:containingTags, args, :names)
          )
        )
      end

      context 'without excludeTipped argument' do
        it 'returns tags names containing the commit' do
          post_query
          expect(data).to eq(%w[v1.0.0 v1.1.0 v1.1.1])
        end
      end

      context 'with excludeTipped argument' do
        let_it_be(:ref_prefix) { Gitlab::Git::TAG_REF_PREFIX }

        let(:args) { excluded_tipped_args }

        it 'returns tags names containing the commit without the tipped tags' do
          excluded_refs = project.repository
                                 .refs_by_oid(oid: commit_sha, ref_patterns: [ref_prefix])
                                 .map { |n| n.delete_prefix(ref_prefix) }

          post_query
          expect(data).to eq(%w[v1.0.0 v1.1.0 v1.1.1] - excluded_refs)
        end
      end

      include_context 'with the limit argument'
    end

    context 'with path Query.project(fullPath).commitReferences(commitSha).containingBranches' do
      let_it_be(:ref_prefix) { Gitlab::Git::BRANCH_REF_PREFIX }
      let_it_be(:path) { %w[project commitReferences containingBranches names] }

      let(:query) do
        graphql_query_for(
          :project,
          { fullPath: project.full_path },
          query_graphql_field(
            :commitReferences,
            { commitSha: commit_sha },
            query_graphql_field(:containingBranches, args, :names)
          )
        )
      end

      context 'without excludeTipped argument' do
        it 'returns branch names containing the commit' do
          refs = project.repository.branch_names_contains(commit_sha)

          post_query

          expect(data).to eq(refs)
        end
      end

      context 'with excludeTipped argument' do
        let(:args) { excluded_tipped_args }

        it 'returns branch names containing the commit without the tipped branch' do
          refs = project.repository.branch_names_contains(commit_sha)

          excluded_refs = project.repository
                                 .refs_by_oid(oid: commit_sha, ref_patterns: [ref_prefix])
                                 .map { |n| n.delete_prefix(ref_prefix) }

          post_query

          expect(data).to eq(refs - excluded_refs)
        end
      end

      include_context 'with the limit argument'
    end
  end

  context 'with tipping refs' do
    context 'with path Query.project(fullPath).commitReferences(commitSha).tippingTags' do
      let(:commit_sha) { repository.find_tag(tag_name).dereferenced_target.sha }
      let(:path) { %w[project commitReferences tippingTags names] }

      let(:query) do
        graphql_query_for(
          :project,
          { fullPath: project.full_path },
          query_graphql_field(
            :commitReferences,
            { commitSha: commit_sha },
            query_graphql_field(:tippingTags, args, :names)
          )
        )
      end

      context 'with authorized user' do
        it 'returns tags names tipping the commit' do
          post_query

          expect(data).to eq([tag_name])
        end
      end

      include_context 'with the limit argument'
    end

    context 'with path Query.project(fullPath).commitReferences(commitSha).tippingBranches' do
      let(:path) { %w[project commitReferences tippingBranches names] }

      let(:query) do
        graphql_query_for(
          :project,
          { fullPath: project.full_path },
          query_graphql_field(
            :commitReferences,
            { commitSha: commit_sha },
            query_graphql_field(:tippingBranches, args, :names)
          )
        )
      end

      it 'returns branches names tipping the commit' do
        post_query

        expect(data).to eq(branches_names)
      end

      include_context 'with the limit argument'
    end
  end
end