File: timelogs_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (121 lines) | stat: -rw-r--r-- 3,625 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Timelogs', feature_category: :team_planning do
  include GraphqlHelpers

  let_it_be(:user) { create(:user) }
  let_it_be(:group) { create(:group, reporters: user) }
  let_it_be(:project) { create(:project, group: group) }
  let_it_be(:user_project) { create(:project, reporters: user) }

  let_it_be(:user_issue) { create(:issue, project: user_project) }
  let_it_be(:group_issue) { create(:issue, project: create(:project, group: group)) }
  let_it_be(:project_issue) { create(:issue, project: project) }

  let_it_be(:user_timelog) { create(:timelog, user: user, issue: user_issue) }
  let_it_be(:group_timelog) { create(:timelog, issue: group_issue) }
  let_it_be(:project_timelog) { create(:timelog, issue: project_issue) }

  let(:params) { {} }
  let(:group_id) { "gid://gitlab/Group/#{group.id}" }
  let(:project_id) { "gid://gitlab/Project/#{project.id}" }
  let(:current_user) { user }

  subject(:execute_query) { post_graphql(query, current_user: current_user) }

  shared_examples 'an OK response with no errors' do
    it 'returns no error message' do
      execute_query

      expect(response).to have_gitlab_http_status(:ok)
      expect(graphql_errors).to be_nil
    end
  end

  shared_examples 'a response containing the correct timelogs' do
    it 'returns the expected timelogs' do
      execute_query

      expect(graphql_data_at(:timelogs, :nodes).pluck('id')).to match_array(expected_timelogs)
    end
  end

  context 'with no parameters' do
    context 'when not an admin' do
      it 'returns an error message' do
        execute_query

        expect(response).to have_gitlab_http_status(:ok)
        expect(graphql_errors).to contain_exactly(
          a_hash_including('message' => 'Non-admin users must provide a group_id, project_id, or current username')
        )
      end
    end

    context 'when an admin' do
      let!(:current_user) { create(:user, :admin) }

      it_behaves_like 'an OK response with no errors'
    end
  end

  context 'with group_id parameter' do
    let(:params) { { group_id: group_id } }
    let(:expected_timelogs) do
      [
        "gid://gitlab/Timelog/#{project_timelog.id}",
        "gid://gitlab/Timelog/#{group_timelog.id}"
      ]
    end

    it_behaves_like 'an OK response with no errors'
    it_behaves_like 'a response containing the correct timelogs'
  end

  context 'with project_id parameter' do
    let(:params) { { project_id: project_id } }
    let(:expected_timelogs) { ["gid://gitlab/Timelog/#{project_timelog.id}"] }

    it_behaves_like 'an OK response with no errors'
    it_behaves_like 'a response containing the correct timelogs'
  end

  context 'with username parameter' do
    context 'when current user' do
      let(:params) { { username: user.username } }
      let(:expected_timelogs) { ["gid://gitlab/Timelog/#{user_timelog.id}"] }

      it_behaves_like 'an OK response with no errors'
      it_behaves_like 'a response containing the correct timelogs'
    end

    context 'when not current user' do
      let(:params) { { username: 'username' } }

      it 'returns an error message' do
        execute_query

        expect(response).to have_gitlab_http_status(:ok)
        expect(graphql_errors).to contain_exactly(
          a_hash_including('message' => 'Non-admin users must provide a group_id, project_id, or current username')
        )
      end
    end
  end

  def query
    timelog_nodes = <<~NODE
      nodes {
        id
      }
    NODE

    graphql_query_for(
      :timelogs,
      { **params },
      timelog_nodes
    )
  end
end