Skip to content

Commit

Permalink
[BO-834] Add delete_association method to Factiva::Monitoring (#20)
Browse files Browse the repository at this point in the history
* monitoring: handle empty response body on make_request method

* monitoring: add delete_association implementation
  • Loading branch information
DaniPB authored Jan 16, 2025
1 parent ba2775f commit adaf530
Show file tree
Hide file tree
Showing 4 changed files with 862 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/factiva/monitoring.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def self.update_association(**args)
instance.update_association(**args)
end

def self.delete_association(**args)
instance.delete_association(**args)
end

def self.add_association_to_case(**args)
instance.add_association_to_case(**args)
end
Expand All @@ -51,6 +55,7 @@ def self.reset_auth
def self.stub!(create_case: {},
create_association: {},
update_association: {},
delete_association: {},
add_association_to_case: {},
remove_association_from_case: {},
get_matches: {},
Expand All @@ -60,6 +65,7 @@ def self.stub!(create_case: {},
create_case,
create_association,
update_association,
delete_association,
add_association_to_case,
remove_association_from_case,
get_matches,
Expand All @@ -77,6 +83,7 @@ class MockedRequest
attr_reader :stubbed_create_case,
:stubbed_create_association,
:stubbed_update_association,
:stubbed_delete_association,
:stubbed_add_association_to_case,
:stubbed_remove_association_from_case,
:stubbed_get_matches,
Expand All @@ -85,6 +92,7 @@ class MockedRequest
def initialize(stubbed_create_case,
stubbed_create_association,
stubbed_update_association,
stubbed_delete_association,
stubbed_add_association_to_case,
stubbed_remove_association_from_case,
stubbed_get_matches,
Expand All @@ -93,6 +101,7 @@ def initialize(stubbed_create_case,
@stubbed_create_case = stubbed_create_case
@stubbed_create_association = stubbed_create_association
@stubbed_update_association = stubbed_update_association
@stubbed_delete_association = stubbed_delete_association
@stubbed_add_association_to_case = stubbed_add_association_to_case
@stubbed_remove_association_from_case = stubbed_remove_association_from_case
@stubbed_get_matches = stubbed_get_matches
Expand All @@ -111,6 +120,10 @@ def update_association(**args)
stubbed_update_association
end

def delete_association(**args)
stubbed_delete_association
end

def add_association_to_case(**args)
stubbed_add_association_to_case
end
Expand Down Expand Up @@ -176,6 +189,15 @@ def update_association(association_id:, params:)
.value_or { |error| raise RequestError.new(error) }
end

def delete_association(association_id:)
url = association_url(association_id)

# If the request fails auth is reset and the request retried
delete(url)
.or { set_auth; delete(url) }
.value_or { |error| raise RequestError.new(error) }
end

def add_association_to_case(case_id:, association_id:)
params = { json: case_association_body(
association_id,
Expand Down Expand Up @@ -255,7 +277,7 @@ def make_request(http_method, url, params = nil)
.auth("Bearer #{auth.token}")
.send(*http_params)

response_body = JSON.parse(response.body.to_s)
response_body = response.body.to_s.empty? ? {} : JSON.parse(response.body.to_s)

if response.status.success?
Success(response_body)
Expand Down
17 changes: 17 additions & 0 deletions spec/factiva/monitoring_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ module Factiva
end
end

describe "#delete_association" do
context "when the association can be removed", vcr: "monitoring/delete_association" do
it "authenticates and delete the association" do
response = subject.delete_association(association_id: "10d89388-60a0-41c2-8497-74f4ed4ad4e5")
expect(response).to eq({})
end
end

context "when factiva returns an error", vcr: "monitoring/delete_association_invalid" do
it "authenticates and delete the association" do
expect {
subject.delete_association(association_id: "invalid_id")
}.to raise_error(Factiva::RequestError)
end
end
end

describe "#add_association_to_case", vcr: "monitoring/add_association_to_case" do
let(:sample_data1) {
{
Expand Down
Loading

0 comments on commit adaf530

Please sign in to comment.