Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you test that Rails mailer credentials are valid?

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?

like image 530
XZVASFD Avatar asked Feb 23 '11 01:02

XZVASFD


People also ask

How do I get the credentials of a rails app?

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.

What is a global credentials file in rails?

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.

Why are Rails Credentials so hard to understand?

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”.

How do I edit a key in Rails 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.


1 Answers

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".

like image 140
JellicleCat Avatar answered Sep 18 '22 14:09

JellicleCat