diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 05e22ca89..f9341f8a4 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -53,8 +53,10 @@ class SessionsController < ApplicationController # Users who sign up using oauth are presumed to have valid email adddresses. user.confirm_email! - auth = user.user_authorizations.build :provider => auth_hash[:provider], :uid => auth_hash[:uid], - :token => auth_hash[:credentials][:token], :token_expiration => Time.at(auth_hash[:credentials][:expires_at]) + auth = user.user_authorizations.build :provider => auth_hash[:provider], + :uid => auth_hash[:uid], + :token => auth_hash[:credentials][:token], + :token_expiration => Time.at(auth_hash[:credentials][:expires_at]) user.save complete_sign_in user end diff --git a/config/environments/test.rb b/config/environments/test.rb index 277306ac0..5fddbed0e 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -39,4 +39,8 @@ SampleApp::Application.configure do silence_warnings do BCrypt::Engine::DEFAULT_COST = BCrypt::Engine::MIN_COST end + + # For testing omniauth + OmniAuth.config.test_mode = true end + diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index 7f45aee3b..6abfcdbe7 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -28,12 +28,58 @@ describe SessionsController do controller.signed_in?.should == true end - it "should redirect the user to the proper page" do post :create, :session => @attr response.should redirect_to(client_url) end + + end + + describe "create_oauth" do + before(:each) do + OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({ + 'uid' => '100', + 'provider' => 'facebook', + 'info' => { + 'first_name' => 'FirstName', + 'last_name' => 'LastName', + 'email' => 'test_oauth@example.com', + 'location' => 'mylocation' + }, + 'credentials' => { + 'token' => 'facebooktoken', + 'expires_at' => 1000000000 + } + }) + end + + it "should create a user when oauth comes in with a non-currently existing user" do + request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook] + lambda do + visit '/auth/facebook' + end.should change(User, :count).by(1) + user = User.find_by_email('test_oauth@example.com') + user.should_not be_nil + user.first_name.should == "FirstName" + response.should be_success + + # also verify that a second visit does *not* create another new user + lambda do + visit '/auth/facebook' + end.should change(User, :count).by(0) + end + + + it "should not create a user when oauth comes in with a currently existing user" do + user = FactoryGirl.create(:user) # in the jam session + OmniAuth.config.mock_auth[:facebook][:info][:email] = user.email + OmniAuth.config.mock_auth[:facebook] = OmniAuth.config.mock_auth[:facebook] + + lambda do + visit '/auth/facebook' + end.should change(User, :count).by(0) + end end diff --git a/spec/requests/static_pages_spec.rb b/spec/requests/static_pages_spec.rb index 85e5ffc67..f81261f83 100644 --- a/spec/requests/static_pages_spec.rb +++ b/spec/requests/static_pages_spec.rb @@ -4,6 +4,7 @@ describe "Static pages" do subject { page } + describe "Home page" do before { visit '/oldhome' }