mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
Query webhook to connect app
This commit is contained in:
15
app/jobs/connect_app_job.rb
Normal file
15
app/jobs/connect_app_job.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ConnectAppJob < ApplicationJob
|
||||
def perform(app, token)
|
||||
url = "https://n8n.openfoodnetwork.org.uk/webhook/regen/connect-enterprise"
|
||||
event = "connect-app"
|
||||
payload = {
|
||||
enterprise_id: app.enterprise_id,
|
||||
access_token: token,
|
||||
}
|
||||
|
||||
response = WebhookDeliveryJob.perform_now(url, event, payload)
|
||||
app.update!(data: JSON.parse(response))
|
||||
end
|
||||
end
|
||||
@@ -46,5 +46,7 @@ class WebhookDeliveryJob < ApplicationJob
|
||||
# Raise a failed request error and let job runner handle retrying.
|
||||
# In theory, only 5xx errors should be retried, but who knows.
|
||||
raise FailedWebhookRequestError, response.status.to_s unless response.success?
|
||||
|
||||
response.body
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,7 +5,8 @@ module Admin
|
||||
def create
|
||||
enterprise = Enterprise.find(element.dataset.enterprise_id)
|
||||
authorize! :admin, enterprise
|
||||
ConnectedApp.create!(enterprise_id: enterprise.id)
|
||||
app = ConnectedApp.create!(enterprise_id: enterprise.id)
|
||||
ConnectAppJob.perform_later(app, current_user.spree_api_key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
54
spec/fixtures/vcr_cassettes/ConnectAppJob/stores_connection_data_on_the_app.yml
vendored
Normal file
54
spec/fixtures/vcr_cassettes/ConnectAppJob/stores_connection_data_on_the_app.yml
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
---
|
||||
http_interactions:
|
||||
- request:
|
||||
method: post
|
||||
uri: https://n8n.openfoodnetwork.org.uk/webhook/regen/connect-enterprise
|
||||
body:
|
||||
encoding: UTF-8
|
||||
string: '{"id":"6efbbeea-4078-4bb5-88b1-c8dbf599c520","at":"2023-12-14 12:13:39
|
||||
+1100","event":"connect-app","data":{"enterprise_id":23,"access_token":"b5fa8c7fa1dd5331a2111fcc907040d842c5eb928c512e43"}}'
|
||||
headers:
|
||||
User-Agent:
|
||||
- openfoodnetwork_webhook/1.0
|
||||
Content-Type:
|
||||
- application/json
|
||||
Accept-Encoding:
|
||||
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
||||
Accept:
|
||||
- "*/*"
|
||||
response:
|
||||
status:
|
||||
code: 200
|
||||
message: OK
|
||||
headers:
|
||||
Server:
|
||||
- nginx
|
||||
Date:
|
||||
- Thu, 14 Dec 2023 01:13:41 GMT
|
||||
Content-Type:
|
||||
- text/html; charset=utf-8
|
||||
Content-Length:
|
||||
- '35'
|
||||
Connection:
|
||||
- keep-alive
|
||||
Etag:
|
||||
- W/"23-GW39X6dSljjgz4GPY7ICa+eNupE"
|
||||
Vary:
|
||||
- Accept-Encoding
|
||||
Strict-Transport-Security:
|
||||
- max-age=63072000
|
||||
X-Xss-Protection:
|
||||
- 1; mode=block
|
||||
X-Download-Options:
|
||||
- noopen
|
||||
X-Content-Type-Options:
|
||||
- nosniff
|
||||
X-Permitted-Cross-Domain-Policies:
|
||||
- none
|
||||
Referrer-Policy:
|
||||
- same-origin
|
||||
body:
|
||||
encoding: UTF-8
|
||||
string: '{"link":"https://example.net/edit"}'
|
||||
recorded_at: Thu, 14 Dec 2023 01:13:40 GMT
|
||||
recorded_with: VCR 6.2.0
|
||||
19
spec/jobs/connect_app_job_spec.rb
Normal file
19
spec/jobs/connect_app_job_spec.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
RSpec.describe ConnectAppJob, type: :job, vcr: true do
|
||||
subject { ConnectAppJob.new(app, token) }
|
||||
|
||||
let(:app) { ConnectedApp.create!(enterprise: ) }
|
||||
let(:enterprise) { create(:enterprise) }
|
||||
let(:token) { enterprise.owner.spree_api_key }
|
||||
|
||||
before { enterprise.owner.generate_api_key }
|
||||
|
||||
it "stores connection data on the app" do
|
||||
subject.perform_now
|
||||
|
||||
expect(app.data).to eq({ "link" => "https://example.net/edit" })
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user