-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
154 additions
and
4 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
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,44 @@ | ||
require 'net/http' | ||
|
||
module Worker | ||
class RemoveCdrExportFileJob < ActiveJob::Base | ||
class FileNotDeletedError < RuntimeError | ||
def initialize(http_code) | ||
@http_code = http_code | ||
end | ||
|
||
def message | ||
"File was not deleted! http code: #{@http_code}" | ||
end | ||
end | ||
|
||
queue_as 'cdr_export' | ||
ALLOWED_HTTP_CODES = [200, 404].freeze | ||
|
||
def perform(cdr_export_id) | ||
url = URI.parse(delete_url(cdr_export_id)) | ||
req = Net::HTTP::Delete.new(url.to_s) | ||
res = Net::HTTP.start( | ||
url.host, | ||
url.port, | ||
use_ssl: url.scheme == 'https', | ||
verify_mode: OpenSSL::SSL::VERIFY_NONE | ||
) do |http| | ||
http.request(req) | ||
end | ||
http_code = res.code.to_i | ||
unless ALLOWED_HTTP_CODES.include?(http_code) | ||
raise FileNotDeletedError.new(http_code) | ||
end | ||
end | ||
|
||
private | ||
|
||
def delete_url(cdr_export_id) | ||
[ | ||
Rails.configuration.yeti_web.fetch('cdr_export').fetch('delete_url').chomp('/'), | ||
"#{cdr_export_id}.csv" | ||
].join('/') | ||
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
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
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
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
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
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
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,59 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Worker::RemoveCdrExportFileJob, type: :job do | ||
subject do | ||
described_class.perform_now(cdr_export.id) | ||
end | ||
|
||
let(:cdr_export) do | ||
FactoryGirl.create(:cdr_export, :completed) | ||
end | ||
|
||
let(:delete_url) do | ||
[ | ||
Rails.configuration.yeti_web.fetch('cdr_export').fetch('delete_url').chomp('/'), | ||
"#{cdr_export.id}.csv" | ||
].join('/') | ||
end | ||
|
||
let!(:http_mock) do | ||
stub_request(:delete, delete_url).to_return(status: http_code) | ||
end | ||
|
||
shared_examples :valid_deleting do | ||
it 'http mock should be requested' do | ||
subject | ||
expect(http_mock).to have_been_requested.once | ||
end | ||
|
||
it 'nothing should be raised' do | ||
expect { subject }.not_to raise_error | ||
end | ||
end | ||
|
||
context 'when http code 200' do | ||
let(:http_code) do | ||
200 | ||
end | ||
|
||
include_examples :valid_deleting | ||
end | ||
|
||
context 'when http code 404' do | ||
let(:http_code) do | ||
404 | ||
end | ||
|
||
include_examples :valid_deleting | ||
end | ||
|
||
context 'when http code 400' do | ||
let(:http_code) do | ||
400 | ||
end | ||
|
||
it 'error should be raised' do | ||
expect { subject }.to raise_error(Worker::RemoveCdrExportFileJob::FileNotDeletedError, 'File was not deleted! http code: 400') | ||
end | ||
end | ||
end |