Commit b1044aeb authored by Sean Arnold's avatar Sean Arnold

Stop using reference to feature

- Fix factory
- Fix some specs
parent 16007fcd
......@@ -8,7 +8,7 @@ module AlertManagement
belongs_to :alert, class_name: 'AlertManagement::Alert', foreign_key: 'alert_id', inverse_of: :metric_images
def self.available_for?(project)
project&.feature_available?(:alert_metric_upload)
true
end
private
......
......@@ -15,7 +15,12 @@ module AlertManagement
end
def execute
return ServiceResponse.error(message: _("You are not authorized to upload metric images"), http_status: :forbidden) unless can_upload_metrics?
unless can_upload_metrics?
return ServiceResponse.error(
message: _("You are not authorized to upload metric images"),
http_status: :forbidden
)
end
metric = AlertManagement::MetricImage.new(
alert: alert,
......
......@@ -72,7 +72,6 @@ module GitlabSubscriptions
PREMIUM_FEATURES = %i[
adjourned_deletion_for_projects_and_groups
admin_audit_log
alert_metric_upload
auditor_user
blocking_merge_requests
board_assignee_lists
......
......@@ -50,7 +50,10 @@ module API
).execute
if upload.success?
present upload.payload[:metric], with: Entities::MetricImage, current_user: current_user, project: user_project
present upload.payload[:metric],
with: Entities::MetricImage,
current_user: current_user,
project: user_project
else
render_api_error!(upload.message, upload.http_status)
end
......
......@@ -19,22 +19,6 @@ RSpec.describe AlertManagement::MetricImage do
describe '.available_for?' do
subject { described_class.available_for?(issue.project) }
before do
stub_licensed_features(alert_metric_upload: true)
end
let_it_be_with_refind(:issue) { create(:issue) }
context 'license enabled' do
it { is_expected.to eq(true) }
end
context 'license disabled' do
before do
stub_licensed_features(alert_metric_upload: false)
end
it { is_expected.to eq(false) }
end
it { is_expected.to eq(true) }
end
end
......@@ -18,7 +18,10 @@ RSpec.describe API::AlertManagementAlerts do
project.add_developer(user)
end
subject { post api("/projects/#{project.id}/alert_management_alerts/#{alert.iid}/metric_images/authorize", user), headers: workhorse_headers }
subject do
post api("/projects/#{project.id}/alert_management_alerts/#{alert.iid}/metric_images/authorize", user),
headers: workhorse_headers
end
it 'authorizes uploading with workhorse header' do
subject
......@@ -104,9 +107,10 @@ RSpec.describe API::AlertManagementAlerts do
expect(json_response['filename']).to eq(file_name)
expect(json_response['url']).to eq(url)
expect(json_response['url_text']).to eq(url_text)
expect(json_response['file_path']).to match(%r{/uploads/-/system/alert_management_metric_image/file/\d+/#{file_name}})
expect(json_response['created_at']).not_to be_nil
expect(json_response['id']).not_to be_nil
file_path_regex = %r{/uploads/-/system/alert_management_metric_image/file/\d+/#{file_name}}
expect(json_response['file_path']).to match(file_path_regex)
end
end
......@@ -133,7 +137,6 @@ RSpec.describe API::AlertManagementAlerts do
allow(uploader).to receive(:file_storage?).and_return(true)
end
stub_licensed_features(alert_metric_upload: true)
project.send("add_#{user_role}", user)
end
......@@ -142,7 +145,6 @@ RSpec.describe API::AlertManagementAlerts do
context 'file size too large' do
before do
stub_licensed_features(alert_metric_upload: true)
allow_next_instance_of(UploadedFile) do |upload_file|
allow(upload_file).to receive(:size).and_return(AlertManagement::MetricImage::MAX_FILE_SIZE + 1)
end
......@@ -161,7 +163,7 @@ RSpec.describe API::AlertManagementAlerts do
project.add_developer(user)
allow_next_instance_of(::AlertManagement::MetricImages::UploadService) do |service|
error = double(success?: false, message: 'some error', http_status: :bad_request)
error = instance_double(ServiceResponse, success?: false, message: 'some error', http_status: :bad_request)
allow(service).to receive(:execute).and_return(error)
end
end
......@@ -177,7 +179,6 @@ RSpec.describe API::AlertManagementAlerts do
context 'object storage enabled' do
before do
# Object storage
stub_licensed_features(alert_metric_upload: true)
stub_uploads_object_storage(MetricImageUploader)
allow_next_instance_of(MetricImageUploader) do |uploader|
......@@ -240,7 +241,6 @@ RSpec.describe API::AlertManagementAlerts do
with_them do
before do
stub_licensed_features(alert_metric_upload: true)
project.send("add_#{user_role}", user) unless user_role == :not_member
project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE) unless public_project
end
......@@ -255,7 +255,10 @@ RSpec.describe API::AlertManagementAlerts do
let!(:image) { create(:alert_metric_image, alert: alert) }
let(:params) { { url: 'http://test.example.com', url_text: 'Example website 123' } }
subject { put api("/projects/#{project.id}/alert_management_alerts/#{alert.iid}/metric_images/#{image.id}", user), params: params }
subject do
put api("/projects/#{project.id}/alert_management_alerts/#{alert.iid}/metric_images/#{image.id}", user),
params: params
end
shared_examples 'can_update_metric_image' do
it 'can update the metric images' do
......@@ -286,7 +289,6 @@ RSpec.describe API::AlertManagementAlerts do
with_them do
before do
stub_licensed_features(alert_metric_upload: true)
project.send("add_#{user_role}", user) unless user_role == :not_member
project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE) unless public_project
end
......@@ -299,44 +301,27 @@ RSpec.describe API::AlertManagementAlerts do
project.add_developer(user)
end
context 'feature is enabled' do
before do
stub_licensed_features(alert_metric_upload: true)
end
context 'and metric image not found' do
subject { put api("/projects/#{project.id}/alert_management_alerts/#{alert.iid}/metric_images/#{non_existing_record_id}", user) }
it 'returns an error' do
subject
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('Metric image not found')
end
context 'and metric image not found' do
subject do
put api("/projects/#{project.id}/alert_management_alerts/#{alert.iid}/metric_images/#{non_existing_record_id}", user) # rubocop: disable Layout/LineLength
end
context 'metric image cannot be updated' do
let(:params) { { url_text: 'something_long' * 100 } }
it 'returns an error' do
subject
it 'returns an error' do
subject
expect(response).to have_gitlab_http_status(:unprocessable_entity)
expect(json_response['message']).to eq('Metric image could not be updated')
end
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('Metric image not found')
end
end
context 'feature not enabled' do
before do
stub_licensed_features(alert_metric_upload: false)
end
context 'metric image cannot be updated' do
let(:params) { { url_text: 'something_long' * 100 } }
it 'returns an error' do
subject
expect(response).to have_gitlab_http_status(:forbidden)
expect(json_response['message']).to eq('Feature not available')
expect(response).to have_gitlab_http_status(:unprocessable_entity)
expect(json_response['message']).to eq('Metric image could not be updated')
end
end
end
......@@ -347,7 +332,9 @@ RSpec.describe API::AlertManagementAlerts do
let!(:image) { create(:alert_metric_image, alert: alert) }
subject { delete api("/projects/#{project.id}/alert_management_alerts/#{alert.iid}/metric_images/#{image.id}", user) }
subject do
delete api("/projects/#{project.id}/alert_management_alerts/#{alert.iid}/metric_images/#{image.id}", user)
end
shared_examples 'can delete metric image successfully' do
it 'can delete the metric images' do
......@@ -377,7 +364,6 @@ RSpec.describe API::AlertManagementAlerts do
with_them do
before do
stub_licensed_features(alert_metric_upload: true)
project.send("add_#{user_role}", user) unless user_role == :not_member
project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE) unless public_project
end
......@@ -387,12 +373,13 @@ RSpec.describe API::AlertManagementAlerts do
context 'when user has access' do
before do
stub_licensed_features(alert_metric_upload: true)
project.add_developer(user)
end
context 'when metric image not found' do
subject { delete api("/projects/#{project.id}/alert_management_alerts/#{alert.iid}/metric_images/#{non_existing_record_id}", user) }
subject do
delete api("/projects/#{project.id}/alert_management_alerts/#{alert.iid}/metric_images/#{non_existing_record_id}", user) # rubocop: disable Layout/LineLength
end
it 'returns an error' do
subject
......@@ -419,19 +406,6 @@ RSpec.describe API::AlertManagementAlerts do
expect(json_response['message']).to eq('Metric image could not be deleted')
end
end
context 'when feature not enabled' do
before do
stub_licensed_features(alert_metric_upload: false)
end
it 'returns an error' do
subject
expect(response).to have_gitlab_http_status(:forbidden)
expect(json_response['message']).to eq('Feature not available')
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment