I've got a mailer sending through a GMail account, and I want to test that ActionMailer can actually log in to GMail's SMTP server with the credentials I've given it. What's the best way to test this?
When you create a new rails app a file called credentials.yml.enc is added to the config directory. This file will be decrypted in a production environment using a key stored either on a RAILS_MASTER_KEY environment variable or a master.key file I mentioned before.
The default pattern on most rails apps is a master config/credentials.yml.enc file. This acts as a global credentials file if no other environments are present with their own credentials file. The need for this comes when third-party services/APIS out there have "live" and "test" modes. Stripe is a great example of this.
I personally have found Rails credentials really hard to understand. I think there are three reasons why this is. The official Rails docs about the credentials feature are a little terse and not easily discoverable. The feature changed somewhat drastically from Rails 4 to Rails 5, even changing names from “secrets” to “credentials”.
According to this article, keys can be configured specific to e.g. production by running rails credentials:edit --environment production .) Since it’s encrypted, the config/credentials.yml.enc file can’t be edited directly. It can only be edited using the rails credentials:edit command.
smtp = Net::SMTP.new settings[:address], settings[:port]
smtp.enable_starttls_auto if settings[:enable_starttls_auto]
smtp.start(settings[:domain]) do
expect {
smtp.authenticate settings[:user_name], settings[:password], settings[:authentication]
}.to_not raise_error
end
Calling authenticate
will raise a Net::SMTPAuthenticationError
if the authentication fails.
Otherwise, it will return a Net::SMTP::Response
, and calling status
on the response will return "235"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With