Skip to content

Commit

Permalink
CV2-3827 explicitly request callback (#1700)
Browse files Browse the repository at this point in the history
* CV2-3827 explicitly request callback

* CV2-3827 more tweaks to fix integration

* fix fixture

* fix fixtures'

* Update webhooks_controller_test.rb
  • Loading branch information
DGaffney authored Oct 23, 2023
1 parent df05a19 commit 8e5c935
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/models/concerns/alegre_similarity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def send_to_media_similarity_index(pm)
url: self.media_file_url(pm),
context: self.get_context(pm),
match_across_content_types: true,
requires_callback: true
}
self.request_api(
'post',
Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/alegre_webhooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ def valid_request?(request)

def webhook(request)
begin
doc_id = request.params.dig('requested', 'id')
doc_id = request.params.dig('data', 'requested', 'id')
raise 'Unexpected params format' if doc_id.blank?
redis = Redis.new(REDIS_CONFIG)
key = "alegre:webhook:#{doc_id}"
redis.lpush(key, request.params.to_json)
redis.expire(key, 1.day.to_i)
rescue StandardError => e
CheckSentry.notify(AlegreCallbackError.new(e.message), { alegre_response: request.params })
CheckSentry.notify(AlegreCallbackError.new(e.message), params: { alegre_response: request.params })
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions test/controllers/webhooks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,15 @@ def setup
CheckSentry.expects(:notify).never
redis = Redis.new(REDIS_CONFIG)
redis.del('foo')
payload = { 'action' => 'audio', 'requested' => { 'id' => 'foo', 'context' => { 'project_media_id' => random_number } } }
id = random_number
payload = { 'action' => 'audio', 'data' => {'requested' => { 'id' => 'foo', 'context' => { 'project_media_id' => id } }} }
assert_nil redis.lpop('alegre:webhook:foo')

post :index, params: { name: :alegre, token: CheckConfig.get('alegre_token') }.merge(payload)
response = JSON.parse(redis.lpop('alegre:webhook:foo'))
assert_equal 'foo', response.dig('requested', 'id')

travel_to Time.now.since(2.days)
assert_nil redis.lpop('alegre:webhook:foo')
assert_equal 'foo', response.dig('data', 'requested', 'id')
expectation = {"action"=>"index", "data"=>{"requested"=>{"context"=>{"project_media_id"=>id.to_s}, "id"=>"foo"}}, "token"=>"test", "name"=>"alegre", "controller"=>"api/v1/webhooks"}
assert_equal expectation, response
end

test "should report error if can't process Alegre webhook" do
Expand Down

0 comments on commit 8e5c935

Please sign in to comment.