Skip to content

Commit

Permalink
add no registration done tests for http, pubsub, eventbridge
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-liuu committed Oct 30, 2024
1 parent fc2103c commit 94e9c93
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 3 deletions.
1 change: 0 additions & 1 deletion lib/shopify_api/webhooks/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ def webhook_registration_needed?(client, registration)
check_response = client.query(query: registration.build_check_query, response_as_struct: false)
raise Errors::WebhookRegistrationError,
"Failed to check if webhook was already registered" unless check_response.ok?

parsed_check_result = registration.parse_check_result(T.cast(check_response.body, T::Hash[String, T.untyped]))
must_register = parsed_check_result[:current_address] != registration.callback_address ||
parsed_check_result[:fields] != registration.fields ||
Expand Down
67 changes: 65 additions & 2 deletions test/webhooks/registry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ def test_process_no_handler
end
end

def test_http_registraion_is_not_needed_if_identical_webhook_exists
do_no_registration_needed_test(
queries[:http][:check_existing_response_with_attributes],
:http,
"test-webhooks",
fields: "field1, field2",
metafield_namespaces: ["namespace1", "namespace2"],
)
end

def test_http_registration_add_and_update
# add webhook
do_registration_test(
Expand Down Expand Up @@ -255,6 +265,16 @@ def test_raises_on_http_registration_check_error
do_registration_check_error_test(:http, "test-webhooks")
end

def test_pubsub_registration_is_not_needed_if_identical_webhook_exists
do_no_registration_needed_test(
queries[:pub_sub][:check_existing_response_with_attributes],
:pub_sub,
"pubsub://my-project-id:my-topic-id",
fields: "field1, field2",
metafield_namespaces: ["namespace1", "namespace2"],
)
end

def test_pubsub_registration_add_and_update
# add webhook
do_registration_test(
Expand Down Expand Up @@ -345,6 +365,16 @@ def test_raises_on_pubsub_registration_check_error
do_registration_check_error_test(:pub_sub, "pubsub://my-project-id:my-topic-id")
end

def test_eventbridge_registration_is_not_needed_if_identical_webhook_exists
do_no_registration_needed_test(
queries[:event_bridge][:check_existing_response_with_attributes],
:event_bridge,
"test-webhooks",
fields: "field1, field2",
metafield_namespaces: ["namespace1", "namespace2"],
)
end

def test_eventbridge_registration_add_and_update
# add webhook
do_registration_test(
Expand Down Expand Up @@ -587,10 +617,9 @@ def do_registration_test(
)
# Given
ShopifyAPI::Webhooks::Registry.clear
check_query_body = { query: queries[delivery_method][:check_query], variables: nil }

stub_request(:post, @url)
.with(body: JSON.dump(check_query_body))
.with(body: JSON.dump({ query: queries[delivery_method][:check_query], variables: nil }))
.to_return({ status: 200, body: JSON.dump(expected_check_response) })

stub_request(:post, @url)
Expand Down Expand Up @@ -642,6 +671,40 @@ def do_registration_check_error_test(delivery_method, path)
)
end
end

def do_no_registration_needed_test(
expected_check_response,
delivery_method,
path,
fields: nil,
metafield_namespaces: nil
)
# Given
ShopifyAPI::Webhooks::Registry.clear

stub_request(:post, @url)
.with(body: JSON.dump({ query: queries[delivery_method][:check_query], variables: nil }))
.to_return({ status: 200, body: JSON.dump(expected_check_response) })

# When
ShopifyAPI::Webhooks::Registry.add_registration(
topic: @topic,
delivery_method: delivery_method,
path: path,
handler: TestHelpers::FakeWebhookHandler.new(
lambda do |topic, shop, body|
end,
),
fields: fields,
metafield_namespaces: metafield_namespaces,
)
update_registration_response = ShopifyAPI::Webhooks::Registry.register_all(
session: @session,
)[0]

# Then
assert_nil(update_registration_response.body)
end
end
end
end
52 changes: 52 additions & 0 deletions test/webhooks/webhook_registration_queries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ def queries
},
},
},
check_existing_response_with_attributes: {
"data" => {
"webhookSubscriptions" => {
"edges" => [
"node" => {
"id" => "gid://shopify/WebhookSubscription/12345",
"includeFields" => ["field1", "field2"],
"metafieldNamespaces" => ["namespace1", "namespace2"],
"endpoint" => {
"typename" => "WebhookHttpEndpoint",
"callbackUrl" => "https://app-address.com/test-webhooks",
},
},
],
},
},
},
register_update_query:
<<~QUERY,
mutation webhookSubscription {
Expand Down Expand Up @@ -318,6 +335,23 @@ def queries
},
},
},
check_existing_response_with_attributes: {
"data" => {
"webhookSubscriptions" => {
"edges" => [
"node" => {
"id" => "gid://shopify/WebhookSubscription/12345",
"endpoint" => {
"typename" => "WebhookEventBridgeEndpoint",
"arn" => "test-webhooks",
},
"includeFields" => ["field1", "field2"],
"metafieldNamespaces" => ["namespace1", "namespace2"],
},
],
},
},
},
register_update_query:
<<~QUERY,
mutation webhookSubscription {
Expand Down Expand Up @@ -512,6 +546,24 @@ def queries
},
},
},
check_existing_response_with_attributes: {
"data" => {
"webhookSubscriptions" => {
"edges" => [
"node" => {
"id" => "gid://shopify/WebhookSubscription/12345",
"endpoint" => {
"typename" => "WebhookPubSubEndpoint",
"pubSubProject" => "my-project-id",
"pubSubTopic" => "my-topic-id",
},
"includeFields" => ["field1", "field2"],
"metafieldNamespaces" => ["namespace1", "namespace2"],
},
],
},
},
},
register_update_query:
<<~QUERY,
mutation webhookSubscription {
Expand Down

0 comments on commit 94e9c93

Please sign in to comment.