test(webhook): delivered stream event

Phillip Davis created

Change summary

sgx-bwmsgsv2.rb                       | 16 ++++++++++++++++
test/property/test_webhook_handler.rb | 28 ++++++++++++++++++++++++++++
2 files changed, 44 insertions(+)

Detailed changes

sgx-bwmsgsv2.rb 🔗

@@ -1135,6 +1135,22 @@ class WebhookHandler < Goliath::API
 
 			# TODO: remove this hack
 			if jparams['to'].length > 1
+				case type
+				when 'message-failed'
+					MessageEvent::Failed.new(
+						timestamp: jparams['time'],
+						stanza_id: id,
+						bandwidth_id: jparams['id'],
+						error_code: jparams['errorCode'].to_s,
+						error_description: jparams['description'].to_s
+					).emit(REDIS)
+				when 'message-delivered'
+					MessageEvent::Delivered.new(
+						timestamp: jparams['time'],
+						stanza_id: id,
+						bandwidth_id: jparams['id']
+					).emit(REDIS)
+				end
 				puts "WARN! group no rcpt: #{users_num}"
 				return [200, {}, "OK"]
 			end

test/property/test_webhook_handler.rb 🔗

@@ -181,4 +181,32 @@ class WebhookPropertyTest < Minitest::Test
 		}
 	end
 	em :test_unknown_direction_returns_400_missing_params_when_message_failed
+
+	def test_delivered_emits_correct_stream_event
+		property_of {
+			Webhook
+				.new(REDIS)
+				.type { "message-delivered" }
+				.generate
+		}.check { |metadata, example|
+			invoke_webhook(example)
+
+			entries = REDIS.stream_entries("messages").sync
+			assert_equal 1, entries.length
+
+			fields = entries.first[:fields]
+			expected_keys = %w[
+				event source timestamp stanza_id bandwidth_id
+			].sort
+			assert_equal expected_keys, fields.keys.sort
+
+			assert_equal "delivered", fields["event"]
+			assert_equal "bwmsgsv2", fields["source"]
+
+			assert_equal metadata["stanza_id"], fields["stanza_id"]
+			assert_equal example["message"]["id"], fields["bandwidth_id"]
+			assert_equal example["message"]["time"], fields["timestamp"]
+		}
+	end
+	em :test_delivered_emits_correct_stream_event
 end