summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/project/milestones_spec.rb
blob: a577c367fe54baf77d93bffa40b3a6e33ff5ccbe (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'getting milestone listings nested in a project' do
  include GraphqlHelpers

  let_it_be(:today) { Time.now.utc.to_date }
  let_it_be(:project) { create(:project, :repository, :public) }
  let_it_be(:current_user) { create(:user) }

  let_it_be(:no_dates) { create(:milestone, project: project, title: 'no dates') }
  let_it_be(:no_end) { create(:milestone, project: project, title: 'no end', start_date: today - 10.days) }
  let_it_be(:no_start) { create(:milestone, project: project, title: 'no start', due_date: today - 5.days) }
  let_it_be(:fully_past) { create(:milestone, project: project, title: 'past', start_date: today - 10.days, due_date: today - 5.days) }
  let_it_be(:covers_today) { create(:milestone, project: project, title: 'present', start_date: today - 5.days, due_date: today + 5.days) }
  let_it_be(:fully_future) { create(:milestone, project: project, title: 'future', start_date: today + 5.days, due_date: today + 10.days) }
  let_it_be(:closed) { create(:milestone, :closed, project: project) }

  let(:results) { graphql_data_at(:project, :milestones, :nodes) }

  let(:search_params) { nil }

  def query_milestones(fields)
    graphql_query_for(
      :project,
      { full_path: project.full_path },
      query_graphql_field(:milestones, search_params,
        [
          query_graphql_field(:nodes, nil, %i[id title])
        ])
    )
  end

  def result_list(expected)
    expected.map do |milestone|
      a_graphql_entity_for(milestone)
    end
  end

  let(:query) do
    query_milestones(all_graphql_fields_for('Milestone', max_depth: 1))
  end

  let(:all_milestones) do
    [no_dates, no_end, no_start, fully_past, fully_future, covers_today, closed]
  end

  it_behaves_like 'a working graphql query' do
    before do
      post_graphql(query, current_user: current_user)
    end
  end

  shared_examples 'searching with parameters' do
    it 'finds the right milestones' do
      post_graphql(query, current_user: current_user)

      expect(results).to match_array(result_list(expected))
    end
  end

  context 'the user does not have access' do
    let_it_be(:project) { create(:project) }
    let_it_be(:milestones) { create_list(:milestone, 2, project: project) }

    it 'is nil' do
      post_graphql(query, current_user: current_user)

      expect(graphql_data_at(:project)).to be_nil
    end

    context 'the user has access' do
      let(:expected) { milestones }

      before do
        project.add_guest(current_user)
      end

      it_behaves_like 'searching with parameters'
    end
  end

  context 'there are no search params' do
    let(:search_params) { nil }
    let(:expected) { all_milestones }

    it_behaves_like 'searching with parameters'
  end

  context 'the search params do not match anything' do
    let(:search_params) { { title: 'wibble' } }
    let(:expected) { [] }

    it_behaves_like 'searching with parameters'
  end

  context 'searching by state:closed' do
    let(:search_params) { { state: :closed } }
    let(:expected) { [closed] }

    it_behaves_like 'searching with parameters'
  end

  context 'searching by state:active' do
    let(:search_params) { { state: :active } }
    let(:expected) { all_milestones - [closed] }

    it_behaves_like 'searching with parameters'
  end

  context 'searching by title' do
    let(:search_params) { { title: 'no start' } }
    let(:expected) { [no_start] }

    it_behaves_like 'searching with parameters'
  end

  context 'searching by search_title' do
    let(:search_params) { { search_title: 'no' } }
    let(:expected) { [no_dates, no_start, no_end] }

    it_behaves_like 'searching with parameters'
  end

  context 'searching by containing_date' do
    let(:search_params) { { containing_date: (today - 7.days).iso8601 } }
    let(:expected) { [no_start, no_end, fully_past] }

    it_behaves_like 'searching with parameters'
  end

  context 'searching by containing_date = today' do
    let(:search_params) { { containing_date: today.iso8601 } }
    let(:expected) { [no_end, covers_today] }

    it_behaves_like 'searching with parameters'
  end

  context 'searching by custom range' do
    let(:expected) { [no_end, fully_future] }
    let(:search_params) do
      {
        start_date: (today + 6.days).iso8601,
        end_date: (today + 7.days).iso8601
      }
    end

    it_behaves_like 'searching with parameters'
  end

  context 'using timeframe argument' do
    let(:expected) { [no_end, fully_future] }
    let(:search_params) do
      {
        timeframe: {
          start: (today + 6.days).iso8601,
          end: (today + 7.days).iso8601
        }
      }
    end

    it_behaves_like 'searching with parameters'
  end

  describe 'timeframe validations' do
    let(:vars) do
      {
        path: project.full_path,
        start: (today + 6.days).iso8601,
        end: (today + 7.days).iso8601
      }
    end

    it_behaves_like 'a working graphql query' do
      before do
        query = <<~GQL
          query($path: ID!, $start: Date!, $end: Date!) {
            project(fullPath: $path) {
              milestones(timeframe: { start: $start, end: $end }) {
                nodes { id }
              }
            }
          }
        GQL

        post_graphql(query, current_user: current_user, variables: vars)
      end
    end

    it 'is invalid to provide timeframe and start_date/end_date' do
      query = <<~GQL
        query($path: ID!, $tstart: Date!, $tend: Date!, $start: Time!, $end: Time!) {
          project(fullPath: $path) {
            milestones(timeframe: { start: $tstart, end: $tend }, startDate: $start, endDate: $end) {
              nodes { id }
            }
          }
        }
      GQL

      post_graphql(query, current_user: current_user,
                          variables: vars.merge(vars.transform_keys { |k| :"t#{k}" }))

      expect(graphql_errors).to contain_exactly(a_hash_including('message' => include('deprecated in favor of timeframe')))
    end

    it 'is invalid to invert the timeframe arguments' do
      query = <<~GQL
        query($path: ID!, $start: Date!, $end: Date!) {
          project(fullPath: $path) {
            milestones(timeframe: { start: $end, end: $start }) {
              nodes { id }
            }
          }
        }
      GQL

      post_graphql(query, current_user: current_user, variables: vars)

      expect(graphql_errors).to contain_exactly(a_hash_including('message' => include('start must be before end')))
    end
  end
end