File: custom_attributes_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 (190 lines) | stat: -rw-r--r-- 7,445 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
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
# frozen_string_literal: true

RSpec.shared_examples 'custom attributes endpoints' do |attributable_name|
  let!(:custom_attribute1) { attributable.custom_attributes.create! key: 'foo', value: 'foo' }
  let!(:custom_attribute2) { attributable.custom_attributes.create! key: 'bar', value: 'bar' }

  describe "GET /#{attributable_name} with custom attributes filter", :aggregate_failures do
    before do
      other_attributable
    end

    context 'with an unauthorized user' do
      it 'does not filter by custom attributes' do
        get api("/#{attributable_name}", user), params: { custom_attributes: { foo: 'foo', bar: 'bar' } }

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response.pluck('id')).to include(attributable.id, other_attributable.id)
      end
    end

    context 'with an authorized user' do
      it 'filters by custom attributes' do
        get api("/#{attributable_name}", admin, admin_mode: true), params: { custom_attributes: { foo: 'foo', bar: 'bar' } }

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response.size).to be 1
        expect(json_response.first['id']).to eq attributable.id
      end
    end
  end

  describe "GET /#{attributable_name} with custom attributes", :aggregate_failures do
    before do
      other_attributable
    end

    context 'with an unauthorized user' do
      it 'does not include custom attributes' do
        get api("/#{attributable_name}", user), params: { with_custom_attributes: true }

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).not_to be_empty
        expect(json_response.first).not_to include 'custom_attributes'
      end
    end

    context 'with an authorized user' do
      it 'does not include custom attributes by default' do
        get api("/#{attributable_name}", admin, admin_mode: true)

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).not_to be_empty
        expect(json_response.first).not_to include 'custom_attributes'
      end

      it 'includes custom attributes if requested' do
        get api("/#{attributable_name}", admin, admin_mode: true), params: { with_custom_attributes: true }

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).not_to be_empty

        attributable_response = json_response.find { |r| r['id'] == attributable.id }
        other_attributable_response = json_response.find { |r| r['id'] == other_attributable.id }

        expect(attributable_response['custom_attributes']).to contain_exactly(
          { 'key' => 'foo', 'value' => 'foo' },
          { 'key' => 'bar', 'value' => 'bar' }
        )

        expect(other_attributable_response['custom_attributes']).to eq []
      end
    end
  end

  describe "GET /#{attributable_name}/:id with custom attributes", :aggregate_failures do
    context 'with an unauthorized user' do
      it 'does not include custom attributes' do
        get api("/#{attributable_name}/#{attributable.id}", user), params: { with_custom_attributes: true }

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).not_to include 'custom_attributes'
      end
    end

    context 'with an authorized user' do
      it 'does not include custom attributes by default' do
        get api("/#{attributable_name}/#{attributable.id}", admin, admin_mode: true)

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).not_to include 'custom_attributes'
      end

      it 'includes custom attributes if requested' do
        get api("/#{attributable_name}/#{attributable.id}", admin, admin_mode: true), params: { with_custom_attributes: true }

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response['custom_attributes']).to contain_exactly(
          { 'key' => 'foo', 'value' => 'foo' },
          { 'key' => 'bar', 'value' => 'bar' }
        )
      end
    end
  end

  describe "GET /#{attributable_name}/:id/custom_attributes", :aggregate_failures do
    context 'with an unauthorized user' do
      subject { get api("/#{attributable_name}/#{attributable.id}/custom_attributes", user) }

      it_behaves_like 'an unauthorized API user'
    end

    context 'with an authorized user' do
      it 'returns all custom attributes' do
        get api("/#{attributable_name}/#{attributable.id}/custom_attributes", admin, admin_mode: true)

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).to contain_exactly(
          { 'key' => 'foo', 'value' => 'foo' },
          { 'key' => 'bar', 'value' => 'bar' }
        )
      end
    end
  end

  describe "GET /#{attributable_name}/:id/custom_attributes/:key", :aggregate_failures do
    context 'with an unauthorized user' do
      subject { get api("/#{attributable_name}/#{attributable.id}/custom_attributes/foo", user) }

      it_behaves_like 'an unauthorized API user'
    end

    context 'with an authorized user' do
      it 'returns a single custom attribute' do
        get api("/#{attributable_name}/#{attributable.id}/custom_attributes/foo", admin, admin_mode: true)

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).to eq({ 'key' => 'foo', 'value' => 'foo' })
      end
    end
  end

  describe "PUT /#{attributable_name}/:id/custom_attributes/:key", :aggregate_failures do
    context 'with an unauthorized user' do
      subject { put api("/#{attributable_name}/#{attributable.id}/custom_attributes/foo", user), params: { value: 'new' } }

      it_behaves_like 'an unauthorized API user'
    end

    context 'with an authorized user' do
      it 'creates a new custom attribute' do
        expect do
          put api("/#{attributable_name}/#{attributable.id}/custom_attributes/new", admin, admin_mode: true), params: { value: 'new' }
        end.to change { attributable.custom_attributes.count }.by(1)

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).to eq({ 'key' => 'new', 'value' => 'new' })
        expect(attributable.custom_attributes.find_by(key: 'new').value).to eq 'new'
      end

      it 'updates an existing custom attribute' do
        expect do
          put api("/#{attributable_name}/#{attributable.id}/custom_attributes/foo", admin, admin_mode: true), params: { value: 'new' }
        end.not_to change { attributable.custom_attributes.count }

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).to eq({ 'key' => 'foo', 'value' => 'new' })
        expect(custom_attribute1.reload.value).to eq 'new'
      end
    end
  end

  describe "DELETE /#{attributable_name}/:id/custom_attributes/:key", :aggregate_failures do
    context 'with an unauthorized user' do
      subject { delete api("/#{attributable_name}/#{attributable.id}/custom_attributes/foo", user) }

      it_behaves_like 'an unauthorized API user'
    end

    context 'with an authorized user' do
      it 'deletes an existing custom attribute' do
        expect do
          delete api("/#{attributable_name}/#{attributable.id}/custom_attributes/foo", admin, admin_mode: true)
        end.to change { attributable.custom_attributes.count }.by(-1)

        expect(response).to have_gitlab_http_status(:no_content)
        expect(attributable.custom_attributes.find_by(key: 'foo')).to be_nil
      end
    end
  end
end