27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

122 CHAPTER 6 Authentication and basic authorization<br />

14 scenarios (14 passed)<br />

115 steps (115 passed)<br />

# and<br />

6 examples, 0 failures, 5 pending<br />

Great! Commit that!<br />

git add .<br />

git commit -m "Added feature to ensure Devise signup is always working"<br />

git push<br />

In this section, you added a feature to make sure Devise is set up correctly for your<br />

application. When users sign up to your site, they’ll receive an email as long as you<br />

configured your Action Mailer settings correctly. The next section covers how Devise<br />

automatically signs in users who click the confirmation link provided in the email<br />

they’ve been sent.<br />

6.3 Confirmation link sign-in<br />

With users now able to sign up to your site, you should make sure they’re also able to<br />

sign in. When users are created, they should be sent a confirmation email in which<br />

they have to click a link to confirm their email address. You don’t want users signing<br />

up with fake email addresses! Once confirmed, the user is automatically signed in by<br />

Devise.<br />

6.3.1 Testing email<br />

First, you enable the confirmable module for Devise because (as you saw earlier) it’s<br />

one of the optional modules. With this module turned on, users will receive a confirmation<br />

email that contains a link for them to activate their account. You need to write<br />

a test that checks whether users receive a confirmation email when they sign up and<br />

can confirm their account by clicking a link inside that email.<br />

For this test, you use another gem called email_spec. To install this gem, add the<br />

following line to your Gemfile inside the test group:<br />

gem 'email_spec'<br />

Now run bundle to install it.<br />

Next, run the generator to get the steps for email_spec with the following<br />

command:<br />

rails g email_spec:steps<br />

This command generates steps in a file at features/step_definitions/email_steps.rb that<br />

you can use in your features to check whether a user received a specific email and<br />

more. This is precisely what you need to help you craft the next feature: signing in,<br />

receiving an email, and clicking the confirmation link inside it.<br />

One additional piece you must set up is to require the specific files from the<br />

email_spec library. Create a new file at features/support/email.rb, which you use for<br />

requiring the email_spec files. Inside this file, put these lines:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!