File: create_shared_examples.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 (147 lines) | stat: -rw-r--r-- 4,440 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
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
# frozen_string_literal: true

RSpec.shared_examples 'issuable supports timelog creation mutation' do
  let(:mutation_response) { graphql_mutation_response(:timelog_create) }
  let(:spent_at) { '2022-11-16T12:59:35+0100' }
  let(:mutation) { graphql_mutation(:timelogCreate, variables) }

  let(:variables) do
    {
      'time_spent' => time_spent,
      'spent_at' => spent_at,
      'summary' => 'Test summary',
      'issuable_id' => issuable.to_global_id.to_s
    }
  end

  context 'when the user is anonymous' do
    before do
      post_graphql_mutation(mutation, current_user: current_user)
    end

    it_behaves_like 'a mutation that returns a top-level access error'
  end

  context 'when the user is a guest member of the namespace' do
    let(:current_user) { create(:user) }

    before do
      users_container.add_guest(current_user)

      post_graphql_mutation(mutation, current_user: current_user)
    end

    it_behaves_like 'a mutation that returns a top-level access error'
  end

  context 'when user has permissions to create a timelog' do
    let(:current_user) { author }

    before do
      users_container.add_reporter(current_user)
    end

    context 'with valid data' do
      it 'creates the timelog' do
        expect do
          post_graphql_mutation(mutation, current_user: current_user)
        end.to change { Timelog.count }.by(1)

        expect(response).to have_gitlab_http_status(:success)
        expect(mutation_response['errors']).to be_empty
        expect(mutation_response['timelog']).to include(
          'timeSpent' => 3600,
          # This also checks that the ISO time was converted to UTC
          'spentAt' => '2022-11-16T11:59:35Z',
          'summary' => 'Test summary'
        )
      end
    end

    context 'when spent_at is not provided', time_travel_to: '2024-04-23 22:50:00 +0200' do
      let(:variables) do
        {
          'time_spent' => time_spent,
          'summary' => 'Test summary',
          'issuable_id' => issuable.to_global_id.to_s
        }
      end

      it 'creates the timelog using the current time' do
        expect do
          post_graphql_mutation(mutation, current_user: current_user)
        end.to change { Timelog.count }.by(1)

        expect(response).to have_gitlab_http_status(:success)
        expect(mutation_response['errors']).to be_empty
        expect(mutation_response['timelog']).to include(
          'timeSpent' => 3600,
          'spentAt' => '2024-04-23T20:50:00Z',
          'summary' => 'Test summary'
        )
      end
    end

    context 'with invalid time_spent' do
      let(:time_spent) { '3h e' }

      it 'returns an error' do
        expect do
          post_graphql_mutation(mutation, current_user: current_user)
        end.to change { Timelog.count }.by(0)

        expect(response).to have_gitlab_http_status(:success)
        expect(mutation_response['errors']).to match_array(
          ['Time spent must be formatted correctly. For example: 1h 30m.'])
        expect(mutation_response['timelog']).to be_nil
      end
    end
  end
end

RSpec.shared_examples 'issuable does not support timelog creation mutation' do
  let(:mutation_response) { graphql_mutation_response(:timelog_create) }
  let(:mutation) do
    variables = {
      'time_spent' => time_spent,
      'spent_at' => '2022-11-16T12:59:35+0100',
      'summary' => 'Test summary',
      'issuable_id' => issuable.to_global_id.to_s
    }
    graphql_mutation(:timelogCreate, variables)
  end

  context 'when the user is anonymous' do
    before do
      post_graphql_mutation(mutation, current_user: current_user)
    end

    it_behaves_like 'a mutation that returns a top-level access error'
  end

  context 'when the user is a guest member of the namespace' do
    let(:current_user) { create(:user) }

    before do
      users_container.add_guest(current_user)

      post_graphql_mutation(mutation, current_user: current_user)
    end

    it_behaves_like 'a mutation that returns top-level errors' do
      let(:match_errors) { contain_exactly(include('is not a valid ID for')) }
    end
  end

  context 'when user has permissions to create a timelog' do
    let(:current_user) { author }

    before do
      users_container.add_reporter(current_user)
    end

    it_behaves_like 'a mutation that returns top-level errors' do
      let(:match_errors) { contain_exactly(include('is not a valid ID for')) }
    end
  end
end