solidus_webhooks 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d71609325f0e59694111c1efee4a131a283af6dfac87d9102a9e405dfca918ea
|
4
|
+
data.tar.gz: b9f80ecd1f6652d83915b2a49c083285df3e7e17c63b01414c273857daeadd44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 826c8abe905a3a69d7ea642b72a047534282db7bb60c3cdced93f897c2d62f887d72807aff7cb57cc5ec1082966b9602908c97d7ba2d866ad3ffb8aeb0f09ce1
|
7
|
+
data.tar.gz: 8fab3c87b593ca5743818ed8551296b85709813341fa5bba3ddd2561992138fc92ba984525b3080a5c30fd9d2b64f35f99d610cdcd885115f1732eadccbe4780
|
data/README.md
CHANGED
@@ -108,6 +108,20 @@ Spree::RoleConfiguration.configure do |config|
|
|
108
108
|
end
|
109
109
|
```
|
110
110
|
|
111
|
+
### Accessing the API User
|
112
|
+
|
113
|
+
If you need to access the API user that is making the call to the webhook, you can just accept an additional argument in the callable handler.
|
114
|
+
|
115
|
+
Example:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
SolidusWebhooks.config.register_webhook_handler :tracking_number, -> payload, user {
|
119
|
+
Rails.logger.info "Received payload from user #{user.email}: #{payload.to_json}"
|
120
|
+
# …
|
121
|
+
}
|
122
|
+
```
|
123
|
+
|
124
|
+
|
111
125
|
Installation
|
112
126
|
------------
|
113
127
|
|
@@ -4,10 +4,11 @@ class Spree::WebhooksController < Spree::Api::BaseController
|
|
4
4
|
def receive
|
5
5
|
webhook = Spree::Webhook.find(params[:id])
|
6
6
|
payload = request.request_parameters["webhook"]
|
7
|
+
user = current_api_user
|
7
8
|
|
8
9
|
authorize! :receive, webhook
|
9
10
|
|
10
|
-
webhook.receive(payload)
|
11
|
+
webhook.receive(payload, user)
|
11
12
|
|
12
13
|
head :ok
|
13
14
|
end
|
data/app/models/spree/webhook.rb
CHANGED
@@ -4,8 +4,12 @@ class Spree::Webhook
|
|
4
4
|
|
5
5
|
WebhookNotFound = Class.new(StandardError)
|
6
6
|
|
7
|
-
def receive(payload)
|
8
|
-
|
7
|
+
def receive(payload, user)
|
8
|
+
if handler_arity == 1
|
9
|
+
handler.call(payload)
|
10
|
+
else
|
11
|
+
handler.call(payload, user)
|
12
|
+
end
|
9
13
|
end
|
10
14
|
|
11
15
|
def self.find(id)
|
@@ -16,4 +20,14 @@ class Spree::Webhook
|
|
16
20
|
|
17
21
|
new(id: id, handler: handler)
|
18
22
|
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def handler_arity
|
27
|
+
if handler.respond_to?(:arity)
|
28
|
+
handler.arity
|
29
|
+
else
|
30
|
+
handler.method(:call).arity
|
31
|
+
end
|
32
|
+
end
|
19
33
|
end
|
@@ -3,45 +3,73 @@ require 'spec_helper'
|
|
3
3
|
RSpec.feature "Can register a handler and receive Webhooks", type: :request do
|
4
4
|
background do
|
5
5
|
SolidusWebhooks.reset_config!
|
6
|
-
SolidusWebhooks.config.register_webhook_handler :
|
7
|
-
SolidusWebhooks.config.register_webhook_handler :
|
6
|
+
SolidusWebhooks.config.register_webhook_handler :proc, proc_handler
|
7
|
+
SolidusWebhooks.config.register_webhook_handler :method, method_handler
|
8
|
+
SolidusWebhooks.config.register_webhook_handler :user, user_handler
|
9
|
+
SolidusWebhooks.config.register_webhook_handler :splat, splat_handler
|
10
|
+
SolidusWebhooks.config.register_webhook_handler :method_and_user, method_and_user_handler
|
8
11
|
end
|
9
12
|
|
10
|
-
let(:
|
11
|
-
let(:
|
13
|
+
let(:proc_payloads) { [] }
|
14
|
+
let(:method_payloads) { [] }
|
15
|
+
let(:user_payloads) { [] }
|
16
|
+
let(:splat_payloads) { [] }
|
17
|
+
let(:method_and_user_payloads) { [] }
|
12
18
|
|
13
|
-
let(:
|
14
|
-
let(:
|
19
|
+
let(:proc_handler) { ->(payload) { proc_payloads << payload } }
|
20
|
+
let(:method_handler) { Struct.new(:payloads) do
|
21
|
+
def call(payload) payloads << payload end
|
22
|
+
end.new(method_payloads) }
|
23
|
+
let(:user_handler) { ->(payload, user) { user_payloads << [payload, user] } }
|
24
|
+
let(:splat_handler) { ->(*args) { splat_payloads << args } }
|
25
|
+
let(:method_and_user_handler) { Struct.new(:payloads) do
|
26
|
+
def call(payload, user) payloads << payload end
|
27
|
+
end.new(method_and_user_payloads) }
|
15
28
|
|
16
|
-
let(:
|
17
|
-
let(:
|
29
|
+
let(:authorized_user) { create(:admin_user, spree_api_key: "123") }
|
30
|
+
let(:authorized_token) { authorized_user.spree_api_key }
|
31
|
+
|
32
|
+
let(:unauthorized_user) { create(:user, spree_api_key: "456") }
|
33
|
+
let(:unauthorized_token) { unauthorized_user.spree_api_key }
|
18
34
|
|
19
35
|
scenario "calls the handler passing the payload" do
|
20
|
-
post "/webhooks/
|
36
|
+
post "/webhooks/proc?token=#{authorized_token}", as: :json, params: {a: 123}
|
37
|
+
expect(response).to have_http_status(:ok)
|
38
|
+
|
39
|
+
post "/webhooks/proc?token=#{authorized_token}", as: :json, params: {b: 456}
|
40
|
+
expect(response).to have_http_status(:ok)
|
41
|
+
|
42
|
+
post "/webhooks/method?token=#{authorized_token}", as: :json, params: {c: 789}
|
43
|
+
expect(response).to have_http_status(:ok)
|
44
|
+
|
45
|
+
post "/webhooks/user?token=#{authorized_token}", as: :json, params: {d: 012}
|
21
46
|
expect(response).to have_http_status(:ok)
|
22
47
|
|
23
|
-
post "/webhooks/
|
48
|
+
post "/webhooks/splat?token=#{authorized_token}", as: :json, params: {e: 345}
|
24
49
|
expect(response).to have_http_status(:ok)
|
25
50
|
|
26
|
-
post "/webhooks/
|
51
|
+
post "/webhooks/method_and_user?token=#{authorized_token}", as: :json, params: {f: 678}
|
27
52
|
expect(response).to have_http_status(:ok)
|
28
53
|
|
29
|
-
expect(
|
30
|
-
expect(
|
54
|
+
expect(proc_payloads).to eq([{'a' => 123}, {'b' => 456}])
|
55
|
+
expect(method_payloads).to eq([{'c' => 789}])
|
56
|
+
expect(user_payloads).to eq([[{'d' => 012}, authorized_user]])
|
57
|
+
expect(splat_payloads).to eq([[{'e' => 345}, authorized_user]])
|
58
|
+
expect(method_and_user_payloads).to eq([{'f' => 678}])
|
31
59
|
end
|
32
60
|
|
33
61
|
scenario "receives a bad handler id" do
|
34
|
-
post "/webhooks/
|
62
|
+
post "/webhooks/abc?token=#{authorized_token}", as: :json, params: {a: 123}
|
35
63
|
expect(response).to have_http_status(:not_found)
|
36
64
|
end
|
37
65
|
|
38
66
|
scenario "refuses a bad token" do
|
39
|
-
post "/webhooks/
|
67
|
+
post "/webhooks/user?token=b4d-t0k3n", as: :json, params: {a: 123}
|
40
68
|
expect(response).to have_http_status(:unauthorized)
|
41
69
|
end
|
42
70
|
|
43
71
|
scenario "refuses a token without permissions" do
|
44
|
-
post "/webhooks/
|
72
|
+
post "/webhooks/proc?token=#{unauthorized_token}", as: :json, params: {a: 123}
|
45
73
|
expect(response).to have_http_status(:unauthorized)
|
46
74
|
end
|
47
75
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: solidus_webhooks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Elia Schito
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: solidus_core
|