discourse/lib/discourse_dev/email_log.rb
Isaac Janzen 37942cb8bb
DEV: Convert admin-incoming-email modal to component-based API (#22701)
- Convert `admin-incoming-email` modal to component-based API
- Testing that the modal was working in local development was extremely challenging due to the need for `rejected` and `bounced` emails. Something that is not easy to stub in a local dev environment. To make this process more smooth for future developers I have added a new rake task:

```
desc "Creates sample email logs"
task "email_logs:populate" => ["db:load_config"] do |_, args|
  DiscourseDev::EmailLog.populate!
end
```

That will generate fully functional email logs in development to be toyed with.

<img width="787" alt="Screenshot 2023-07-20 at 3 27 04 PM" src="https://github.com/discourse/discourse/assets/50783505/47b3fe34-cd7e-49a5-8fe6-768c0fbd1aa2">
2023-07-20 16:31:20 -05:00

78 lines
1.9 KiB
Ruby

# frozen_string_literal: true
require "discourse_dev/record"
require "faker"
module DiscourseDev
class EmailLog < Record
def initialize
super(::EmailLog, DiscourseDev.config.email_logs[:count])
end
def create_sent!
::EmailLog.create!(email_log_data)
end
def create_bounced!
bounce_key = SecureRandom.hex
email_local_part, email_domain = SiteSetting.notification_email.split("@")
bounced_to_address = "#{email_local_part}+verp-#{bounce_key}@#{email_domain}"
bounce_data =
email_log_data.merge(
to_address: bounced_to_address,
bounced: true,
bounce_key: bounce_key,
bounce_error_code: "5.0.0",
)
# Bounced email logs require a matching incoming email record
::IncomingEmail.create!(
incoming_email_data.merge(to_addresses: bounced_to_address, is_bounce: true),
)
::EmailLog.create!(bounce_data)
end
def create_rejected!
::IncomingEmail.create!(incoming_email_data)
end
def email_log_data
{
to_address: User.random.email,
email_type: :digest,
user_id: User.random.id,
raw: Faker::Lorem.paragraph,
}
end
def incoming_email_data
user = User.random
subject = Faker::Lorem.sentence
email_content = <<-EMAIL
Return-Path: #{user.email}
From: #{user.email}
Date: #{Date.today}
Mime-Version: "1.0"
Content-Type: "text/plain"
Content-Transfer-Encoding: "7bit"
#{Faker::Lorem.paragraph}
EMAIL
{
user_id: user.id,
from_address: user.email,
raw: email_content,
error: Faker::Lorem.sentence,
rejection_message: I18n.t("emails.incoming.errors.bounced_email_error"),
}
end
def populate!
@count.times { create_sent! }
@count.times { create_bounced! }
@count.times { create_rejected! }
end
end
end