Add dummy webhook endpoint for LiteFarm

This commit is contained in:
Maikel Linke
2025-11-28 12:14:23 +11:00
parent f3efed7aeb
commit d811103a71
4 changed files with 121 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
# frozen_string_literal: true
# Webhook events
module DfcProvider
class EventsController < DfcProvider::ApplicationController
rescue_from JSON::ParserError, with: -> do
head :bad_request
end
# Trigger a webhook event.
#
# The only supported event is a `refresh` event of permissions.
# It means that our permissions to access data on another platform changed.
# We will need to pull the updated data.
def create
event = JSON.parse(request.body.read)
enterprises_url = event["enterpriseUrlid"]
if enterprises_url.blank?
render status: :bad_request, json: {
success: false,
message: "Missing parameter `enterpriseUrlid`",
}
return
end
render json: { success: true }
end
end
end

View File

@@ -12,6 +12,7 @@ DfcProvider::Engine.routes.draw do
resources :enterprise_groups, only: [:index, :show] do
resources :affiliated_by, only: [:create, :destroy], module: 'enterprise_groups'
end
resources :events, only: [:create]
resources :persons, only: [:show]
resources :supplied_products, only: [:index]
resources :product_groups, only: [:show]

View File

@@ -0,0 +1,59 @@
# frozen_string_literal: true
require_relative "../swagger_helper"
RSpec.describe "Events", swagger_doc: "dfc.yaml" do
let!(:user) { create(:oidc_user) }
before { login_as user }
path "/api/dfc/events" do
post "Create Event" do
consumes "application/json"
produces "application/json"
parameter name: :event, in: :body, schema: {
example: {
eventType: "refresh",
enterpriseUrlid: "https://api.beta.litefarm.org/dfc/enterprises/",
scope: "ReadEnterprise",
}
}
response "400", "bad request" do
describe "with missing request body" do
around do |example|
# Rswag expects all required parameters to be supplied with `let`
# but we want to send a request without the request body parameter.
parameters = example.metadata[:operation][:parameters]
example.metadata[:operation][:parameters] = []
example.run
example.metadata[:operation][:parameters] = parameters
end
run_test!
end
describe "with empty request body" do
let(:event) { nil }
run_test!
end
describe "with missing parameter" do
let(:event) { { eventType: "refresh" } }
run_test!
end
end
response "200", "success" do
let(:event) do |example|
example.metadata[:operation][:parameters].first[:schema][:example]
end
run_test! do
expect(json_response["success"]).to eq true
end
end
end
end
end

View File

@@ -579,6 +579,38 @@ paths:
dfc-b:URL: https://facebook.com/user
'404':
description: not found
"/api/dfc/events":
post:
summary: Create Event
parameters: []
tags:
- Events
responses:
'400':
description: bad request
content:
application/json:
examples:
test_example:
value:
success: false
message: Missing parameter `enterpriseUrlid`
'200':
description: success
content:
application/json:
examples:
test_example:
value:
success: true
requestBody:
content:
application/json:
schema:
example:
eventType: refresh
enterpriseUrlid: https://api.beta.litefarm.org/dfc/enterprises/
scope: ReadEnterprise
"/api/dfc/enterprises/{enterprise_id}/offers/{id}":
parameters:
- name: enterprise_id