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

test: add tests for Server model

هذا الالتزام موجود في:
Adam Cooke
2024-02-20 17:08:02 +00:00
ملتزم من قبل Adam Cooke
الأصل 8b61100082
التزام 2023200d91
19 ملفات معدلة مع 707 إضافات و64 حذوفات

عرض الملف

@@ -192,15 +192,22 @@ class Server < ApplicationRecord
end
def send_limit_approaching?
send_limit && (send_volume >= send_limit * 0.90)
return false unless send_limit
(send_volume >= send_limit * 0.90)
end
def send_limit_exceeded?
send_limit && send_volume >= send_limit
return false unless send_limit
send_volume >= send_limit
end
def send_limit_warning(type)
AppMailer.send("server_send_limit_#{type}", self).deliver
if organization.notification_addresses.present?
AppMailer.send("server_send_limit_#{type}", self).deliver
end
update_column("send_limit_#{type}_notified_at", Time.now)
WebhookRequest.trigger(self, "SendLimit#{type.to_s.capitalize}", server: webhook_hash, volume: send_volume, limit: send_limit)
end
@@ -209,17 +216,6 @@ class Server < ApplicationRecord
@queue_size ||= queued_messages.ready.count
end
def stats
{
queue: queue_size,
held: held_messages,
bounce_rate: bounce_rate,
message_rate: message_rate,
throughput: throughput_stats,
size: message_db.total_size
}
end
# Return the domain which can be used to authenticate emails sent from the given e-mail address.
#
# @param address [String] an e-mail address
@@ -274,7 +270,10 @@ class Server < ApplicationRecord
self.suspended_at = Time.now
self.suspension_reason = reason
save!
AppMailer.server_suspended(self).deliver
if organization.notification_addresses.present?
AppMailer.server_suspended(self).deliver
end
true
end
def unsuspend
@@ -283,12 +282,6 @@ class Server < ApplicationRecord
save!
end
def validate_ip_pool_belongs_to_organization
return unless ip_pool && ip_pool_id_changed? && !organization.ip_pools.include?(ip_pool)
errors.add :ip_pool_id, "must belong to the organization"
end
def ip_pool_for_message(message)
return unless message.scope == "outgoing"
@@ -300,46 +293,48 @@ class Server < ApplicationRecord
end
end
end
ip_pool
end
def self.triggered_send_limit(type)
servers = where("send_limit_#{type}_at IS NOT NULL AND send_limit_#{type}_at > ?", 3.minutes.ago)
servers.where("send_limit_#{type}_notified_at IS NULL OR send_limit_#{type}_notified_at < ?", 1.hour.ago)
private
def validate_ip_pool_belongs_to_organization
return unless ip_pool && ip_pool_id_changed? && !organization.ip_pools.include?(ip_pool)
errors.add :ip_pool_id, "must belong to the organization"
end
def self.send_send_limit_notifications
[:approaching, :exceeded].each_with_object({}) do |type, hash|
hash[type] = 0
servers = triggered_send_limit(type)
next if servers.empty?
class << self
servers.each do |server|
hash[type] += 1
server.send_limit_warning(type)
end
end
end
def self.[](id, extra = nil)
server = nil
if id.is_a?(String)
if id =~ /\A(\w+)\/(\w+)\z/
server = includes(:organization).where(organizations: { permalink: ::Regexp.last_match(1) }, permalink: ::Regexp.last_match(2)).first
end
else
server = where(id: id).first
def triggered_send_limit(type)
servers = where("send_limit_#{type}_at IS NOT NULL AND send_limit_#{type}_at > ?", 3.minutes.ago)
servers.where("send_limit_#{type}_notified_at IS NULL OR send_limit_#{type}_notified_at < ?", 1.hour.ago)
end
if extra
if extra.is_a?(String)
server.domains.where(name: extra.to_s).first
def send_send_limit_notifications
[:approaching, :exceeded].each_with_object({}) do |type, hash|
hash[type] = 0
servers = triggered_send_limit(type)
next if servers.empty?
servers.each do |server|
hash[type] += 1
server.send_limit_warning(type)
end
end
end
def [](id, extra = nil)
if id.is_a?(String) && id =~ /\A(\w+)\/(\w+)\z/
joins(:organization).where(
organizations: { permalink: ::Regexp.last_match(1) }, permalink: ::Regexp.last_match(2)
).first
else
server.message(extra.to_i)
find_by(id: id.to_i)
end
else
server
end
end
end