forked from yeti-switch/yeti-web
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs yeti-switch#362, removing cdrs exports
- Loading branch information
Showing
5 changed files
with
131 additions
and
1 deletion.
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,36 @@ | ||
require 'net/http' | ||
|
||
module Worker | ||
class RemoveCdrExportFileJob < ActiveJob::Base | ||
class TryAgainError < RuntimeError | ||
end | ||
|
||
queue_as 'cdr_export' | ||
|
||
def perform(cdr_export_id) | ||
cdr_export = CdrExport.find(cdr_export_id) | ||
raise 'status should be completed' unless cdr_export.completed? | ||
url = URI.parse(delete_url(cdr_export)) | ||
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 | ||
raise TryAgainError if (res.code.to_i != 200) && (res.code.to_i != 404) | ||
cdr_export.update!(status: CdrExport::STATUS_DELETED) | ||
end | ||
|
||
private | ||
|
||
def delete_url(cdr_export) | ||
[ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
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, status: status) | ||
end | ||
|
||
let(:delete_url) do | ||
[ | ||
Rails.configuration.yeti_web.fetch('cdr_export').fetch('delete_url').chomp('/'), | ||
"#{cdr_export.id}.csv" | ||
].join('/') | ||
end | ||
|
||
context 'when status completed' do | ||
let(:status) do | ||
CdrExport::STATUS_COMPLETED | ||
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 | ||
|
||
it 'status should be changed to Deleted' do | ||
expect { subject }.to change { cdr_export.reload.status }.from(CdrExport::STATUS_COMPLETED).to(CdrExport::STATUS_DELETED) | ||
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 'status should not be changed' do | ||
expect { subject rescue nil }.not_to change { cdr_export.reload.status } | ||
end | ||
|
||
it 'TryAgainError should be raised' do | ||
expect { subject }.to raise_error(Worker::RemoveCdrExportFileJob::TryAgainError) | ||
end | ||
end | ||
end | ||
|
||
context 'when status pending' do | ||
let(:status) do | ||
CdrExport::STATUS_PENDING | ||
end | ||
|
||
it 'should raise error' do | ||
expect { subject }.to raise_error(StandardError, 'status should be completed') | ||
end | ||
end | ||
end |