Skip to content

Commit

Permalink
Add member submission logic
Browse files Browse the repository at this point in the history
Commented for now until webhook parsing is fully debbuged.
  • Loading branch information
thibaudgg committed Sep 13, 2024
1 parent dac9fa6 commit c15e920
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ gem "json"
group :test do
gem "minitest"
gem "rack-test"
gem "webmock", require: "webmock/minitest"
end

13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ GEM
minitest (>= 5.1)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
base64 (0.2.0)
bigdecimal (3.1.8)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
crack (1.0.0)
bigdecimal
rexml
drb (2.2.1)
hashdiff (1.1.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
json (2.7.2)
Expand All @@ -41,6 +47,7 @@ GEM
mustermann (3.0.3)
ruby2_keywords (~> 0.0.1)
nio4r (2.7.3)
public_suffix (6.0.1)
puma (6.4.2)
nio4r (~> 2.0)
rack (3.1.7)
Expand All @@ -51,11 +58,16 @@ GEM
rackup (2.1.0)
rack (>= 3)
webrick (~> 1.8)
rexml (3.3.7)
ruby2_keywords (0.0.5)
securerandom (0.3.1)
tilt (2.4.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
webmock (3.23.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
webrick (1.8.1)

PLATFORMS
Expand All @@ -71,6 +83,7 @@ DEPENDENCIES
rack-test
rackup
sinatra!
webmock

RUBY VERSION
ruby 3.3.5p100
Expand Down
5 changes: 3 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class App < Sinatra::Base
begin
member_params = Webhook.handle!(payload)
logger.info member_params
rescue ArgumentError => e
logger.info e.message
rescue => e
logger.info "#{e.class} - #{e.message}"
end

status 204
Expand All @@ -35,6 +35,7 @@ def verify_signature!

computed_hmac = Base64.strict_encode64(
OpenSSL::HMAC.digest('sha256', secret, @request_body))
# TODO: Remove debug logging
logger.info "Header HMAC: #{signature}"
logger.info "Computed HMAC: #{computed_hmac}"

Expand Down
35 changes: 33 additions & 2 deletions lib/webhook.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require 'yaml'
require 'net/http'
require 'json'
require 'uri'

class Webhook
attr_reader :payload
Expand All @@ -14,6 +17,7 @@ def initialize(payload)
def handle!
ensure_mapping!

# submit_member!(member_params)
member_params
end

Expand All @@ -23,12 +27,29 @@ def ensure_mapping!
return if mapping

store_name = @payload.dig("store", "name")
raise ArgumentError, "No mapping found for store: #{store_id} (#{store_name})"
raise "No mapping found for store: #{store_id} (#{store_name})"
end

def submit_member!(params)
http = Net::HTTP.new(api_uri.host, api_uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if ENV["RACK_ENV"] == "test"

headers = {
"Content-Type" => "application/json",
"Authorization" => "Token token=#{api_token}"
}
request = Net::HTTP::Post.new(api_uri.path, headers)
request.body = params.to_json

response = http.request(request)
unless response.code == "201"
raise "Failed to create member: #{response.code}"
end
end

def member_params
{
organization: organization,
name: "#{billing["last_name"]} #{billing["first_name"]}",
emails: billing["email"],
phones: billing["phone"],
Expand All @@ -52,6 +73,16 @@ def organization
mapping.first
end

def api_token
ENV["#{organization.upcase}_API_TOKEN"]
end

def api_uri
url = mapping.last["api_endpoint"]
url.gsub!(/\.ch/, ".test") if ENV["RACK_ENV"] == "test"
URI.parse(url)
end

def basket_complements
mapping_ids_for("basket_complements").map { |id|
{ basket_complement_id: id, quantity: 1 }
Expand Down
25 changes: 25 additions & 0 deletions test/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_relative '../app'
require "minitest/autorun"


class AppTest < Minitest::Test
include Rack::Test::Methods

Expand All @@ -28,12 +29,36 @@ def request(payload, secret: nil)
end

def test_valid_webhook_request
ENV["COCAGNE_API_TOKEN"] = "api-token-cocagne"
payload = File.read('test/fixtures/order_created.json')
stub_request(:any, "https://admin.cocagne.test/api/v1/members")
.to_return(status: 201)

request(payload)

assert_equal 204, last_response.status
assert_empty last_response.body

# assert_requested :post, "https://admin.cocagne.test/api/v1/members",
# times: 1,
# headers: {
# "Content-Type" => "application/json",
# "Authorization" => "Token token=api-token-cocagne"
# },
# body: {
# name: "Doe John",
# emails: "[email protected]",
# phones: "079 123 45 67",
# address: "Chemin de la Mairie, 1",
# city: "Troinex",
# zip: "1256",
# country_code: "CH",
# waiting_basket_size_id: 1,
# waiting_depot_id: 22,
# members_basket_complements_attributes: [
# { basket_complement_id:10, quantity:1 }
# ]
# }.to_json
end

def test_unknown_store
Expand Down

0 comments on commit c15e920

Please sign in to comment.