-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separated the delete logic from data extraction result job, modified …
…tests
- Loading branch information
Showing
4 changed files
with
70 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'net/http' | ||
require 'uri' | ||
|
||
class Metais::ProjectDataExtractionDeleteJob < ApplicationJob | ||
queue_as :metais_data_extraction | ||
|
||
def perform(project_uuid) | ||
url = "#{ENV.fetch('API_URL')}/projects/#{project_uuid}" | ||
uri = URI(url) | ||
|
||
req = Net::HTTP::Delete.new(uri) | ||
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http| | ||
http.request(req) | ||
end | ||
|
||
unless res.is_a?(Net::HTTPSuccess) | ||
error_message = "Failed to delete project: #{res.code}, body: #{res.body}" | ||
raise RuntimeError, error_message | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
spec/jobs/metais/project_data_extraction_delete_job_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'rails_helper' | ||
require 'net/http' | ||
|
||
RSpec.describe Metais::ProjectDataExtractionDeleteJob, type: :job do | ||
include ActiveJob::TestHelper | ||
|
||
let(:project_uuid) { 'sample-uuid' } | ||
let(:api_url) { 'http://example.com/api' } | ||
let(:url) { "#{api_url}/projects/#{project_uuid}" } | ||
let(:uri) { URI(url) } | ||
let(:response) { instance_double('Net::HTTPResponse') } | ||
|
||
before do | ||
allow(ENV).to receive(:fetch).with('API_URL').and_return(api_url) | ||
allow(Net::HTTP).to receive(:start).and_return(response) | ||
end | ||
|
||
describe '#perform' do | ||
context 'when the delete request is successful' do | ||
before do | ||
allow(response).to receive(:is_a?).with(Net::HTTPSuccess).and_return(true) | ||
end | ||
|
||
it 'sends a delete request' do | ||
expect { | ||
described_class.perform_now(project_uuid) | ||
}.not_to raise_error | ||
|
||
expect(Net::HTTP).to have_received(:start).with(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') | ||
end | ||
end | ||
|
||
context 'when the delete request fails' do | ||
before do | ||
allow(response).to receive(:is_a?).with(Net::HTTPSuccess).and_return(false) | ||
allow(response).to receive(:code).and_return('500') | ||
allow(response).to receive(:body).and_return('Internal Server Error') | ||
end | ||
|
||
it 'raises a RuntimeError with the correct message' do | ||
expect { | ||
described_class.perform_now(project_uuid) | ||
}.to raise_error(RuntimeError, /Failed to delete project: 500, body: Internal Server Error/) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters