diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-17 19:05:49 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-17 19:05:49 +0300 |
commit | 43a25d93ebdabea52f99b05e15b06250cd8f07d7 (patch) | |
tree | dceebdc68925362117480a5d672bcff122fb625b /spec/requests/api/topics_spec.rb | |
parent | 20c84b99005abd1c82101dfeff264ac50d2df211 (diff) |
Add latest changes from gitlab-org/gitlab@16-0-stable-eev16.0.0-rc42
Diffstat (limited to 'spec/requests/api/topics_spec.rb')
-rw-r--r-- | spec/requests/api/topics_spec.rb | 95 |
1 files changed, 60 insertions, 35 deletions
diff --git a/spec/requests/api/topics_spec.rb b/spec/requests/api/topics_spec.rb index 14719292557..560f22c94be 100644 --- a/spec/requests/api/topics_spec.rb +++ b/spec/requests/api/topics_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe API::Topics, feature_category: :projects do +RSpec.describe API::Topics, :aggregate_failures, feature_category: :projects do include WorkhorseHelpers let_it_be(:file) { fixture_file_upload('spec/fixtures/dk.png') } @@ -14,9 +14,11 @@ RSpec.describe API::Topics, feature_category: :projects do let_it_be(:admin) { create(:user, :admin) } let_it_be(:user) { create(:user) } - describe 'GET /topics', :aggregate_failures do + let(:path) { '/topics' } + + describe 'GET /topics' do it 'returns topics ordered by total_projects_count' do - get api('/topics') + get api(path) expect(response).to have_gitlab_http_status(:ok) expect(response).to include_pagination_headers @@ -40,13 +42,13 @@ RSpec.describe API::Topics, feature_category: :projects do let_it_be(:topic_4) { create(:topic, name: 'unassigned topic', total_projects_count: 0) } it 'returns topics without assigned projects' do - get api('/topics'), params: { without_projects: true } + get api(path), params: { without_projects: true } expect(json_response.map { |t| t['id'] }).to contain_exactly(topic_4.id) end it 'returns topics without assigned projects' do - get api('/topics'), params: { without_projects: false } + get api(path), params: { without_projects: false } expect(json_response.map { |t| t['id'] }).to contain_exactly(topic_1.id, topic_2.id, topic_3.id, topic_4.id) end @@ -66,7 +68,7 @@ RSpec.describe API::Topics, feature_category: :projects do with_them do it 'returns filtered topics' do - get api('/topics'), params: { search: search } + get api(path), params: { search: search } expect(json_response.map { |t| t['name'] }).to eq(result) end @@ -97,7 +99,7 @@ RSpec.describe API::Topics, feature_category: :projects do with_them do it 'returns paginated topics' do - get api('/topics'), params: params + get api(path), params: params expect(json_response.map { |t| t['name'] }).to eq(result) end @@ -105,7 +107,7 @@ RSpec.describe API::Topics, feature_category: :projects do end end - describe 'GET /topic/:id', :aggregate_failures do + describe 'GET /topic/:id' do it 'returns topic' do get api("/topics/#{topic_2.id}") @@ -130,10 +132,14 @@ RSpec.describe API::Topics, feature_category: :projects do end end - describe 'POST /topics', :aggregate_failures do + describe 'POST /topics' do + let(:params) { { name: 'my-topic', title: 'My Topic' } } + + it_behaves_like 'POST request permissions for admin mode' + context 'as administrator' do it 'creates a topic' do - post api('/topics/', admin), params: { name: 'my-topic', title: 'My Topic' } + post api('/topics/', admin, admin_mode: true), params: params expect(response).to have_gitlab_http_status(:created) expect(json_response['name']).to eq('my-topic') @@ -142,7 +148,7 @@ RSpec.describe API::Topics, feature_category: :projects do it 'creates a topic with avatar and description' do workhorse_form_with_file( - api('/topics/', admin), + api('/topics/', admin, admin_mode: true), file_key: :avatar, params: { name: 'my-topic', title: 'My Topic', description: 'my description...', avatar: file } ) @@ -160,14 +166,14 @@ RSpec.describe API::Topics, feature_category: :projects do end it 'returns 400 if name is not unique (case insensitive)' do - post api('/topics/', admin), params: { name: topic_1.name.downcase, title: 'My Topic' } + post api('/topics/', admin, admin_mode: true), params: { name: topic_1.name.downcase, title: 'My Topic' } expect(response).to have_gitlab_http_status(:bad_request) expect(json_response['message']['name']).to eq(['has already been taken']) end it 'returns 400 if title is missing' do - post api('/topics/', admin), params: { name: 'my-topic' } + post api('/topics/', admin, admin_mode: true), params: { name: 'my-topic' } expect(response).to have_gitlab_http_status(:bad_request) expect(json_response['error']).to eql('title is missing') @@ -176,7 +182,7 @@ RSpec.describe API::Topics, feature_category: :projects do context 'as normal user' do it 'returns 403 Forbidden' do - post api('/topics/', user), params: { name: 'my-topic', title: 'My Topic' } + post api('/topics/', user), params: params expect(response).to have_gitlab_http_status(:forbidden) end @@ -184,17 +190,23 @@ RSpec.describe API::Topics, feature_category: :projects do context 'as anonymous' do it 'returns 401 Unauthorized' do - post api('/topics/'), params: { name: 'my-topic', title: 'My Topic' } + post api('/topics/'), params: params expect(response).to have_gitlab_http_status(:unauthorized) end end end - describe 'PUT /topics', :aggregate_failures do + describe 'PUT /topics' do + let(:params) { { name: 'my-topic' } } + + it_behaves_like 'PUT request permissions for admin mode' do + let(:path) { "/topics/#{topic_3.id}" } + end + context 'as administrator' do it 'updates a topic' do - put api("/topics/#{topic_3.id}", admin), params: { name: 'my-topic' } + put api("/topics/#{topic_3.id}", admin, admin_mode: true), params: params expect(response).to have_gitlab_http_status(:ok) expect(json_response['name']).to eq('my-topic') @@ -203,7 +215,7 @@ RSpec.describe API::Topics, feature_category: :projects do it 'updates a topic with avatar and description' do workhorse_form_with_file( - api("/topics/#{topic_3.id}", admin), + api("/topics/#{topic_3.id}", admin, admin_mode: true), method: :put, file_key: :avatar, params: { description: 'my description...', avatar: file } @@ -215,7 +227,7 @@ RSpec.describe API::Topics, feature_category: :projects do end it 'keeps avatar when updating other fields' do - put api("/topics/#{topic_1.id}", admin), params: { name: 'my-topic' } + put api("/topics/#{topic_1.id}", admin, admin_mode: true), params: params expect(response).to have_gitlab_http_status(:ok) expect(json_response['name']).to eq('my-topic') @@ -223,13 +235,13 @@ RSpec.describe API::Topics, feature_category: :projects do end it 'returns 404 for non existing id' do - put api("/topics/#{non_existing_record_id}", admin), params: { name: 'my-topic' } + put api("/topics/#{non_existing_record_id}", admin, admin_mode: true), params: params expect(response).to have_gitlab_http_status(:not_found) end it 'returns 400 for invalid `id` parameter' do - put api('/topics/invalid', admin), params: { name: 'my-topic' } + put api('/topics/invalid', admin, admin_mode: true), params: params expect(response).to have_gitlab_http_status(:bad_request) expect(json_response['error']).to eql('id is invalid') @@ -237,7 +249,7 @@ RSpec.describe API::Topics, feature_category: :projects do context 'with blank avatar' do it 'removes avatar' do - put api("/topics/#{topic_1.id}", admin), params: { avatar: '' } + put api("/topics/#{topic_1.id}", admin, admin_mode: true), params: { avatar: '' } expect(response).to have_gitlab_http_status(:ok) expect(json_response['avatar_url']).to be_nil @@ -245,7 +257,7 @@ RSpec.describe API::Topics, feature_category: :projects do end it 'removes avatar besides other changes' do - put api("/topics/#{topic_1.id}", admin), params: { name: 'new-topic-name', avatar: '' } + put api("/topics/#{topic_1.id}", admin, admin_mode: true), params: { name: 'new-topic-name', avatar: '' } expect(response).to have_gitlab_http_status(:ok) expect(json_response['name']).to eq('new-topic-name') @@ -254,7 +266,7 @@ RSpec.describe API::Topics, feature_category: :projects do end it 'does not remove avatar in case of other errors' do - put api("/topics/#{topic_1.id}", admin), params: { name: topic_2.name, avatar: '' } + put api("/topics/#{topic_1.id}", admin, admin_mode: true), params: { name: topic_2.name, avatar: '' } expect(response).to have_gitlab_http_status(:bad_request) expect(topic_1.reload.avatar_url).not_to be_nil @@ -264,7 +276,7 @@ RSpec.describe API::Topics, feature_category: :projects do context 'as normal user' do it 'returns 403 Forbidden' do - put api("/topics/#{topic_3.id}", user), params: { name: 'my-topic' } + put api("/topics/#{topic_3.id}", user), params: params expect(response).to have_gitlab_http_status(:forbidden) end @@ -272,29 +284,37 @@ RSpec.describe API::Topics, feature_category: :projects do context 'as anonymous' do it 'returns 401 Unauthorized' do - put api("/topics/#{topic_3.id}"), params: { name: 'my-topic' } + put api("/topics/#{topic_3.id}"), params: params expect(response).to have_gitlab_http_status(:unauthorized) end end end - describe 'DELETE /topics', :aggregate_failures do + describe 'DELETE /topics/:id' do + let(:params) { { name: 'my-topic' } } + context 'as administrator' do - it 'deletes a topic' do - delete api("/topics/#{topic_3.id}", admin), params: { name: 'my-topic' } + it 'deletes a topic with admin mode' do + delete api("/topics/#{topic_3.id}", admin, admin_mode: true), params: params expect(response).to have_gitlab_http_status(:no_content) end + it 'deletes a topic without admin mode' do + delete api("/topics/#{topic_3.id}", admin, admin_mode: false), params: params + + expect(response).to have_gitlab_http_status(:forbidden) + end + it 'returns 404 for non existing id' do - delete api("/topics/#{non_existing_record_id}", admin), params: { name: 'my-topic' } + delete api("/topics/#{non_existing_record_id}", admin, admin_mode: true), params: params expect(response).to have_gitlab_http_status(:not_found) end it 'returns 400 for invalid `id` parameter' do - delete api('/topics/invalid', admin), params: { name: 'my-topic' } + delete api('/topics/invalid', admin, admin_mode: true), params: params expect(response).to have_gitlab_http_status(:bad_request) expect(json_response['error']).to eql('id is invalid') @@ -303,7 +323,7 @@ RSpec.describe API::Topics, feature_category: :projects do context 'as normal user' do it 'returns 403 Forbidden' do - delete api("/topics/#{topic_3.id}", user), params: { name: 'my-topic' } + delete api("/topics/#{topic_3.id}", user), params: params expect(response).to have_gitlab_http_status(:forbidden) end @@ -311,16 +331,21 @@ RSpec.describe API::Topics, feature_category: :projects do context 'as anonymous' do it 'returns 401 Unauthorized' do - delete api("/topics/#{topic_3.id}"), params: { name: 'my-topic' } + delete api("/topics/#{topic_3.id}"), params: params expect(response).to have_gitlab_http_status(:unauthorized) end end end - describe 'POST /topics/merge', :aggregate_failures do + describe 'POST /topics/merge' do + it_behaves_like 'POST request permissions for admin mode' do + let(:path) { '/topics/merge' } + let(:params) { { source_topic_id: topic_3.id, target_topic_id: topic_2.id } } + end + context 'as administrator' do - let_it_be(:api_url) { api('/topics/merge', admin) } + let_it_be(:api_url) { api('/topics/merge', admin, admin_mode: true) } it 'merge topics' do post api_url, params: { source_topic_id: topic_3.id, target_topic_id: topic_2.id } |