Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing "Account Confirmation" with rails/rspec/capybara/devise

I'm using rspec/capybara/devise to conduct Integration testing in an app. One of the features of the app is the ever-populate "Account Registration" using a confirm feature (ie sign up - get an confirmation email - click on the link - account is validated).

require 'spec_helper'

describe "User Authentication" do
  describe "New user" do
    before(:each) do
      @user = Factory.build(:user)
    end

    it "can confirm account by clicking on confirmation link" do
      visit root_path
      click_link "Register"
      page.should have_content "Register for an account"
      fill_in "user_email", :with => @user.email
      fill_in "user_password", :with => @user.password
      fill_in "user_password_confirmation", :with => @user.password
      fill_in "user_first_name", :with => @user.first_name
      fill_in "user_last_name", :with => @user.last_name
      fill_in "user_city", :with => @user.city
      fill_in "user_province", :with => @user.province
      fill_in "user_country", :with => @user.country
      fill_in "user_expertise", :with => @user.expertise
      choose "user_experience_professional"
      click_button "Go!"
      last_email.to.should include(@user.email)
    end
  end
end

Here are my helpers:

module MailerMacros
  def last_email
    ActionMailer::Base.deliveries.last
  end
end

The confirmation link is in the HTML email generated. It would be lovely to be able to do something like this (assuming "Confirm My Account") is the link to the account validation.

last_email.body.find_link("Confirm My Account").click_link

Does anyone have any suggestions in being able to identify links in an email that could go into a request_spec?

Thanks

like image 859
Agazoom Avatar asked Jan 16 '12 21:01

Agazoom


1 Answers

I realize that this question is a year old, but it pointed me toward a solution that others may find helpful so I figured I would post it anyway.

I haven't fully explored the email_spec gem mentioned above so that may be a much more elegant approach, but this works quite well for my case. In my app we have devise modified to allow users to sign up with only an email address, then they have to confirm their account and are prompted for additional information. So I needed to test that the confirmations are working.

My feature file looks like this:

  Scenario: Confirm new account
    Given I have registered for an account as "[email protected]"
    When I follow the confirmation link in the confirmation email
    Then I should be able to set a password

First, I added the last_email helper to my cucumber support directory as mailer_helper.rb:

def last_email
  ActionMailer::Base.deliveries.last
end

Second, I wrote my step definitions like this:

Given(/^I have registered for an account as "(.*?)"$/) do |user_email|
  visit root_path
  click_link 'register'
  fill_in('Email', with: user_email)
  click_button 'Sign up'
  user = User.find_by_email(user_email)
  user.should_not be_nil
  user.confirmation_token.should_not be_nil
end

When(/^I follow the confirmation link in the confirmation email$/) do
  ctoken = last_email.body.match(/confirmation_token=\w*/)
  visit "/users/confirmation?#{ctoken}"
end

Then(/^I should be able to set a password$/) do
  fill_in('user[password]', with: 'passw0rd')
  fill_in('user[password_confirmation]', with: 'passw0rd')
  fill_in('user[first_name]', with: 'John')
  fill_in('user[last_name]', with: 'Doe')
  click_button 'Activate'
  page.should have_content('Your account was successfully confirmed. You are now signed in.')
  page.should have_css('div#flash_notice.alert.alert-success')
end
like image 138
DaKaZ Avatar answered Oct 08 '22 07:10

DaKaZ