Skip to content

Instantly share code, notes, and snippets.

@sgruhier
Created February 22, 2011 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgruhier/839012 to your computer and use it in GitHub Desktop.
Save sgruhier/839012 to your computer and use it in GitHub Desktop.
Acceptance test with devise signup with stub for omniauth
require File.expand_path(File.dirname(__FILE__) + "/acceptance_helper")
feature "Registrations" do
context "classic sign up" do
before(:each) do
visit '/user/sign_up'
end
scenario "should have an email field" do
page.should have_css('#user_email')
end
scenario "should have a password field" do
page.should have_css('#user_password')
end
scenario "should have a password confirmation field" do
page.should have_css('#user_password_confirmation')
end
scenario "should be able to sign up" do
lambda {
fill_in('user_email', :with => "newuser@email.com")
fill_in('user_password', :with => 'secret')
fill_in('user_password_confirmation', :with => 'secret')
click_button(I18n.t(:"helpers.submit.signup"))
current_path.should == "/bookmarks"
page.should have_content(I18n.t(:"helpers.submit.signout"))
}.should change(User, :count)
end
scenario "should not be able to signup with an existing email" do
user = Factory(:user, :email => 'seb@email.com')
lambda {
fill_in('user_email', :with => user.email)
fill_in('user_password', :with => 'secret')
fill_in('user_password_confirmation', :with => 'secret')
click_button(I18n.t(:"helpers.submit.signup"))
current_path.should == "/user"
}.should_not change(User, :count)
end
end
context "amoniauth sign up with email" do
stub_omniauth!
scenario "should be able to sign up" do
lambda {
lambda {
visit "/user/sign_up"
click_link "facebook_connect"
page.should have_content(I18n.t(:"devise.omniauth_callbacks.success", :kind => 'facebook'))
}.should change(User, :count)
}.should change(UserToken, :count)
end
end
context "amoniauth sign up without email" do
stub_omniauth!(nil)
before(:each) do
visit "/user/sign_up"
click_link "facebook_connect"
current_path.should == "/user/sign_up/facebook/finalize"
page.should have_css('#user_email')
page.should_not have_css('#user_password')
end
scenario "should be able to sign up with facebook account if enter a valid email" do
lambda {
lambda {
fill_in('user_email', :with => "user@email.com")
click_button(I18n.t(:"helpers.submit.signup"))
page.should have_content(I18n.t(:"devise.omniauth_callbacks.success", :kind => 'facebook'))
}.should change(User, :count)
}.should change(UserToken, :count)
end
scenario "should not be able to sign up with facebook account with an invalid email" do
lambda {
lambda {
fill_in('user_email', :with => "foo")
click_button(I18n.t(:"helpers.submit.signup"))
current_path.should == "/user/sign_up/facebook/finalize"
}.should_not change(User, :count)
}.should_not change(UserToken, :count)
end
end
end
module DeviseMacros
def stub_omniauth!(email = "seb@email.com")
before(:each) do
@access_token = {
:access_token => "xilinus"
}
@facebook_info = {
:id => '12345',
:link => 'http://facebook.com/user_example',
:email => email,
:first_name => 'User',
:last_name => 'Example',
:website => 'http://xilinus.com'
}
Devise::OmniAuth.short_circuit_authorizers!
Devise::OmniAuth.stub!(:facebook) do |b|
b.post('/oauth/access_token') { [200, {}, @access_token.to_json] }
b.get('/me?access_token=xilinus') { [200, {}, @facebook_info.to_json] }
end
end
after do
Devise::OmniAuth.unshort_circuit_authorizers!
Devise::OmniAuth.reset_stubs!
end
end
end
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :acceptance
config.extend DeviseMacros, :type => :acceptance
end
Devise::OmniAuth.test_mode!
Fixture for a delicious import method
it "should import bookmarks", :wip => true do
lambda {
@delicious.should_receive(:read_file).and_return(File.read(Rails.root.join("spec/fixtures/delicious-seb.html")))
@user.import_delicious!
@user.tags.count.should == 15
}.should change(@user.bookmarks, :count).by(15)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment