From c3e3ea81f399a1eed89ed05a027633061c615cd1 Mon Sep 17 00:00:00 2001 From: "j.seto" <jseto@gitlab.com> Date: Wed, 14 Feb 2024 14:19:51 -0500 Subject: [PATCH] Refactor group approval rule api tests Contributes to https://gitlab.com/gitlab-org/gitlab/-/issues/440639 EE: true --- .../api/entities/group_approval_rule_spec.rb | 39 ++++++++++ .../requests/api/group_approval_rules_spec.rb | 77 ++++++++----------- 2 files changed, 71 insertions(+), 45 deletions(-) create mode 100644 ee/spec/lib/ee/api/entities/group_approval_rule_spec.rb diff --git a/ee/spec/lib/ee/api/entities/group_approval_rule_spec.rb b/ee/spec/lib/ee/api/entities/group_approval_rule_spec.rb new file mode 100644 index 000000000000..78e6f88330cd --- /dev/null +++ b/ee/spec/lib/ee/api/entities/group_approval_rule_spec.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe EE::API::Entities::GroupApprovalRule, feature_category: :source_code_management do + subject(:hash) { described_class.new(approval_rule).as_json } + + let(:approval_rule) { build(:approval_group_rule) } + + it 'exposes attributes' do + expect(hash.keys).to match_array(%i[ + id + name + rule_type + eligible_approvers + approvals_required + users + groups + contains_hidden_groups + applies_to_all_protected_branches + ]) + end + + context 'when multiple_approval_rules feature is available' do + before do + stub_licensed_features(multiple_approval_rules: true) + end + + it 'exposes protected branches' do + expect(hash.has_key?(:protected_branches)).to be_truthy + end + end + + context 'when multiple_approval_rules feature is not available' do + it 'does not protected branches' do + expect(hash.has_key?(:protected_branches)).to be_falsy + end + end +end diff --git a/ee/spec/requests/api/group_approval_rules_spec.rb b/ee/spec/requests/api/group_approval_rules_spec.rb index ecc03c03581f..ff6cea986138 100644 --- a/ee/spec/requests/api/group_approval_rules_spec.rb +++ b/ee/spec/requests/api/group_approval_rules_spec.rb @@ -4,93 +4,80 @@ RSpec.describe API::GroupApprovalRules, :aggregate_failures, feature_category: :source_code_management do let_it_be(:group) { create(:group_with_members) } - let_it_be(:group2) { create(:group_with_members) } - let_it_be(:admin) { create(:admin) } - let_it_be(:user) { create(:admin) } - let_it_be(:user2) { create(:user) } + let_it_be(:user_with_access) { create(:user) } let_it_be(:project) do - create(:project, :public, :repository, creator: user, group: group, + create(:project, :public, :repository, creator: user_with_access, group: group, only_allow_merge_if_pipeline_succeeds: false) end - let_it_be(:protected_branches) { create_list(:protected_branch, 2, project: project) } - let_it_be(:approver) { create(:user) } - let_it_be(:other_approver) { create(:user) } - - before_all do - group.add_maintainer(user2) - end - before do stub_licensed_features(merge_request_approvers: true) end + before_all do + group.add_owner(user_with_access) + end + describe 'POST /groups/:id/approval_rules' do let(:schema) { 'public_api/v4/group_approval_rule' } let(:url) { "/groups/#{group.id}/approval_rules" } - let(:current_user) { user } + let(:current_user) { user_with_access } let(:name) { 'name' } - let(:params) do + let(:required_params) do { name: name, approvals_required: 10 } end + let(:params) { required_params } + + subject(:request) { post api(url, current_user, admin_mode: current_user.admin?), params: params } + context 'when approval_group_rules flag is disabled' do before do stub_feature_flags(approval_group_rules: false) end it 'returns 404' do - post api(url, current_user, admin_mode: current_user.admin?), params: params + request expect(response).to have_gitlab_http_status(:not_found) end end it 'returns 201 status' do - post api(url, current_user, admin_mode: current_user.admin?), params: params + request expect(response).to have_gitlab_http_status(:created) expect(response).to match_response_schema(schema, dir: 'ee') end - context 'when multiple_approval_rules feature is available' do - before do - stub_licensed_features(multiple_approval_rules: true) - end - - it 'returns protected branches' do - post api(url, current_user, admin_mode: current_user.admin?), params: params - - expect(response).to have_gitlab_http_status(:created) - expect(json_response['protected_branches'].size).to be 2 - end - end + context 'when the user is an admin' do + let(:current_user) { create(:admin) } - context 'when multiple_approval_rules feature is not available' do - it 'does not return protected branches' do - post api(url, current_user, admin_mode: current_user.admin?), params: params + it 'returns 201 status' do + request expect(response).to have_gitlab_http_status(:created) - expect(json_response).not_to include('protected_branches') end end - context 'when a user is without access' do - let(:current_user) { user2 } + context 'when the user does not have access' do + let(:current_user) { create(:user) } it 'returns 403' do - post api(url, current_user, admin_mode: current_user.admin?), params: params + request expect(response).to have_gitlab_http_status(:forbidden) end end context 'when missing parameters' do + let(:params) { {} } + it 'returns 400 status' do - post api(url, current_user, admin_mode: current_user.admin?) + request expect(response).to have_gitlab_http_status(:bad_request) end @@ -100,7 +87,7 @@ let(:name) { '' } it 'returns 400 status' do - post api(url, current_user, admin_mode: current_user.admin?), params: params + request expect(response).to have_gitlab_http_status(:bad_request) expect(json_response['message']).to eq({ "name" => ["can't be blank"] }) @@ -108,24 +95,24 @@ end context 'with user_id or group_id params' do - before do - post api(url, current_user, admin_mode: current_user.admin?), params: params.merge!(extra_params) - end - context 'with user_ids' do - let(:extra_params) { { user_ids: [user2.id] } } + let(:params) { required_params.merge(user_ids: [user_with_access.id]) } it 'returns a user' do + request + expect(response).to have_gitlab_http_status(:created) expect(json_response['users'].size).to be 1 - expect(json_response.dig('users', 0, 'id')).to eq(user2.id) + expect(json_response.dig('users', 0, 'id')).to eq(user_with_access.id) end end context 'with group_ids' do - let(:extra_params) { { group_ids: [group.id] } } + let(:params) { required_params.merge(group_ids: [group.id]) } it 'returns a group' do + request + expect(response).to have_gitlab_http_status(:created) expect(json_response['groups'].size).to be 1 expect(json_response.dig('groups', 0, 'id')).to eq(group.id) -- GitLab