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 191 192 193 194 195 196
|
# frozen_string_literal: true
RSpec.shared_examples 'set up an integration' do |endpoint:, integration:|
include_context 'with integration'
let(:integration_attrs) { attributes_for(integration_factory).without(:active, :type) }
let(:url) { api("/projects/#{project.id}/#{endpoint}/#{dashed_integration}", user) }
subject(:request) { put url, params: integration_attrs }
it "updates #{integration} settings and returns the correct fields" do
request
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['slug']).to eq(dashed_integration)
current_integration = project.integrations.by_name(integration).first
expect(current_integration).to have_attributes(integration_attrs)
expect(json_response['properties'].keys).to match_array(current_integration.api_field_names)
unless current_integration.secret_fields.empty?
expect(json_response['properties'].keys).not_to include(*current_integration.secret_fields)
end
end
context 'when all booleans are flipped' do
it "updates #{integration} settings and returns the correct fields" do
flipped_attrs = integration_attrs.transform_values do |value|
[true, false].include?(value) ? !value : value
end
put url, params: flipped_attrs
expect(response).to have_gitlab_http_status(:ok)
expect(project.integrations.by_name(integration).first).to have_attributes(flipped_attrs)
end
end
it 'returns if required fields missing' do
required_attributes = integration_attrs_list.select do |attr|
integration_klass.validators_on(attr).any? do |v|
v.instance_of?(ActiveRecord::Validations::PresenceValidator) &&
# exclude presence validators with conditional since those are not really required
v.options.exclude?(:if) && v.options.exclude?(:unless)
end
end
if required_attributes.empty?
expected_code = :ok
else
integration_attrs.delete(required_attributes.sample)
expected_code = :bad_request
end
put url, params: integration_attrs
expect(response).to have_gitlab_http_status(expected_code)
end
context 'when an integration is disabled' do
before do
allow(Integration).to receive(:disabled_integration_names).and_return([integration.to_param])
end
it 'returns bad request' do
put url, params: integration_attrs
expect(response).to have_gitlab_http_status(:bad_request)
end
end
context 'when an integration is disabled at the project-level' do
before do
allow_next_found_instance_of(Project) do |project|
allow(project).to receive(:disabled_integrations).and_return([integration])
end
end
it 'returns bad request' do
put url, params: integration_attrs
expect(response).to have_gitlab_http_status(:bad_request)
end
end
end
RSpec.shared_examples 'disable an integration' do |endpoint:, integration:|
include_context 'with integration'
before do
project_integrations_map[integration].activate!
end
it "deletes #{integration}" do
delete api("/projects/#{project.id}/#{endpoint}/#{dashed_integration}", user)
expect(response).to have_gitlab_http_status(:no_content)
project.send(integration_method).reload
expect(project.send(integration_method).activated?).to be_falsey
end
it 'returns not found if integration does not exist' do
delete api("/projects/#{project2.id}/#{endpoint}/#{dashed_integration}", user)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('404 Integration Not Found')
end
end
RSpec.shared_examples 'get an integration settings' do |endpoint:, integration:|
include_context 'with integration'
let(:initialized_integration) { project_integrations_map[integration] }
def deactive_integration!
return initialized_integration.deactivate! unless initialized_integration.is_a?(::Integrations::Prometheus)
# Integrations::Prometheus sets `#active` itself within a `before_save`:
initialized_integration.manual_configuration = false
initialized_integration.save!
end
def activate_integration!
return initialized_integration.activate! unless initialized_integration.is_a?(::Integrations::Prometheus)
# Integrations::Prometheus sets `#active` itself within a `before_save`:
initialized_integration.manual_configuration = true
initialized_integration.save!
end
context 'when the integration is not active' do
before do
deactive_integration!
end
it "returns all properties of inactive integration #{integration}, except password fields" do
get api("/projects/#{project.id}/#{endpoint}/#{dashed_integration}", user)
expect(initialized_integration).not_to be_active
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['properties'].keys).to match_array(integration_instance.api_field_names)
unless integration_instance.secret_fields.empty?
expect(json_response['properties'].keys).not_to include(*integration_instance.secret_fields)
end
end
end
context 'when the integration is active' do
before do
activate_integration!
end
it "returns all properties of active integration #{integration}, except password fields" do
get api("/projects/#{project.id}/#{endpoint}/#{dashed_integration}", user)
expect(initialized_integration).to be_active
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['properties'].keys).to match_array(integration_instance.api_field_names)
unless integration_instance.secret_fields.empty?
expect(json_response['properties'].keys).not_to include(*integration_instance.secret_fields)
end
end
end
it 'returns authentication error when unauthenticated' do
get api("/projects/#{project.id}/#{endpoint}/#{dashed_integration}")
expect(response).to have_gitlab_http_status(:unauthorized)
end
it "returns not found if integration does not exist" do
get api("/projects/#{project2.id}/#{endpoint}/#{dashed_integration}", user)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('404 Integration Not Found')
end
it "returns not found if integration exists but is in `Project#disabled_integrations`" do
allow_next_found_instance_of(Project) do |project|
allow(project).to receive(:disabled_integrations).and_return([integration])
end
get api("/projects/#{project.id}/#{endpoint}/#{dashed_integration}", user)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('404 Integration Not Found')
end
it "returns error when authenticated but not a project owner" do
project.add_developer(user2)
get api("/projects/#{project.id}/#{endpoint}/#{dashed_integration}", user2)
expect(response).to have_gitlab_http_status(:forbidden)
end
end
|