1
0
مراية لـ https://github.com/postalserver/postal.git تم المزامنة 2025-12-01 05:43:04 +00:00

test: add tests for message unqueueing

This adds a comprehensive set of tests for the message unqueueing service.

Additionally, it improves how message databases are used for testing environments.
هذا الالتزام موجود في:
Adam Cooke
2024-02-19 22:27:22 +00:00
ملتزم من قبل Adam Cooke
الأصل 465f4d8247
التزام b4016f6b49
21 ملفات معدلة مع 1658 إضافات و84 حذوفات

عرض الملف

@@ -12,9 +12,10 @@ module Postal
end
def initialize(organization_id, server_id)
def initialize(organization_id, server_id, database_name: nil)
@organization_id = organization_id
@server_id = server_id
@database_name = database_name
end
attr_reader :organization_id

عرض الملف

@@ -39,6 +39,10 @@ module Postal
@attributes = attributes
end
def reload
self.class.find_one(@database, @attributes["id"])
end
#
# Return the server for this message
#
@@ -200,9 +204,9 @@ module Postal
#
#  Save this message
#
def save
def save(queue_on_create: true)
save_raw_message
persisted? ? _update : _create
persisted? ? _update : _create(queue: queue_on_create)
self
end
@@ -346,8 +350,14 @@ module Postal
#
# Create a new item in the message queue for this message
#
def add_to_message_queue(options = {})
QueuedMessage.create!(message: self, server_id: @database.server_id, batch_key: batch_key, domain: recipient_domain, route_id: route_id, manual: options[:manual]).id
def add_to_message_queue(**options)
QueuedMessage.create!({
message: self,
server_id: @database.server_id,
batch_key: batch_key,
domain: recipient_domain,
route_id: route_id
}.merge(options))
end
#
@@ -572,7 +582,7 @@ module Postal
@database.update("messages", @attributes.except(:id), where: { id: @attributes["id"] })
end
def _create
def _create(queue: true)
self.timestamp = Time.now.to_f if timestamp.blank?
self.status = "Pending" if status.blank?
self.token = Nifty::Utils::RandomString.generate(length: 12) if token.blank?
@@ -581,7 +591,7 @@ module Postal
@database.statistics.increment_all(timestamp, scope)
Statistic.global.increment!(:total_messages)
Statistic.global.increment!("total_#{scope}".to_sym)
add_to_message_queue
add_to_message_queue if queue
end
def mail

عرض الملف

@@ -3,13 +3,6 @@
module Postal
module RspecHelpers
def with_global_server(&block)
server = Server.find(GLOBAL_SERVER.id)
block.call(server)
ensure
server.message_db.provisioner.clean
end
def create_plain_text_message(server, text, to = "test@example.com", override_attributes = {})
domain = create(:domain, owner: server)
attributes = { from: "test@#{domain.name}", subject: "Test Plain Text Message" }.merge(override_attributes)

عرض الملف

@@ -13,5 +13,10 @@ module Postal
attr_accessor :time
attr_accessor :suppress_bounce
def initialize
@details = ""
yield self if block_given?
end
end
end

47
lib/test_logger.rb Normal file
عرض الملف

@@ -0,0 +1,47 @@
# frozen_string_literal: true
class TestLogger
def initialize
@log_lines = []
@group_set = Klogger::GroupSet.new
@print = false
end
def print!
@print = true
end
def add(level, message, **tags)
@group_set.groups.each do |group|
tags = group[:tags].merge(tags)
end
@log_lines << { level: level, message: message, tags: tags }
puts message if @print
true
end
[:info, :debug, :warn, :error].each do |level|
define_method(level) do |message, **tags|
add(level, message, **tags)
end
end
def tagged(**tags, &block)
@group_set.call_without_id(**tags, &block)
end
def log_line(match)
@log_lines.reverse.each do |log_line|
return log_line if match.is_a?(String) && log_line[:message] == match
return log_line if match.is_a?(Regexp) && log_line[:message] =~ match
end
nil
end
def has_logged?(match)
!!log_line(match)
end
end