-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SessionsController specs written and passing
- Loading branch information
Showing
11 changed files
with
194 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Place all the behaviors and hooks related to the matching controller here. | ||
# All this logic will automatically be available in application.js. | ||
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Place all the styles related to the sessions controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: http://sass-lang.com/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
class ApplicationController < ActionController::Base | ||
protect_from_forgery | ||
|
||
def authorized? | ||
@user = User.find(session[:user_id]) if session[:user_id] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
class SessionsController < ApplicationController | ||
before_filter :check_for_user, :except => :destroy | ||
|
||
def index | ||
|
||
end | ||
|
||
def new | ||
session[:oauth] ||= {} | ||
consumer = OAuth::Consumer.new( | ||
'anonymous', 'anonymous', :site => 'https://www.google.com', | ||
:request_token_path => '/accounts/OAuthGetRequestToken?scope=https://mail.google.com/%20https://www.googleapis.com/auth/userinfo%23email', | ||
:access_token_path => '/accounts/OAuthGetAccessToken', | ||
:authorize_path => '/account/OAuthAuthorizeToken' | ||
) | ||
|
||
if session[:oauth][:request_token] | ||
request_token = OAuth::RequestToken.new(consumer, session[:oauth][:request_token], session[:oauth][:request_secret]) | ||
reset_session | ||
|
||
begin | ||
access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier]) | ||
rescue OAuth::Unauthorized | ||
flash[:error] = 'Oops, we were not granted permission. You may try again if you like.' | ||
redirect_to root_url and return | ||
end | ||
|
||
response = access_token.get('https://www.googleapis.com/userinfo/email?alt=json') | ||
email = JSON.parse(response.body)['data']['email'] | ||
user = User.find_or_initialize_by_email(email) | ||
user.attributes = { :token => access_token.token, :secret => access_token.secret } | ||
user.save | ||
session[:user_id] = user.id | ||
redirect_to brooms_url | ||
else | ||
request_token = consumer.get_request_token(:oauth_callback => "#{request.scheme}://#{request.host}:#{request.port}/login") | ||
session[:oauth][:request_token] = request_token.token | ||
session[:oauth][:request_secret] = request_token.secret | ||
redirect_to '/accounts/OAuthAuthorizeToken' | ||
end | ||
end | ||
|
||
def destroy | ||
reset_session | ||
redirect_to root_url | ||
end | ||
|
||
private | ||
|
||
def check_for_user | ||
redirect_to brooms_url if authorized? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module SessionsHelper | ||
end |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
require 'spec_helper' | ||
|
||
describe SessionsController, 'when authorized' do | ||
before :each do | ||
controller.stub(:authorized?).and_return(true) | ||
end | ||
|
||
it 'should redirect to brooms_url' do | ||
get :index | ||
response.should redirect_to(brooms_url) | ||
get :new | ||
response.should redirect_to(brooms_url) | ||
end | ||
|
||
describe 'on get :destroy' do | ||
before :each do | ||
session[:user_id] = 1 | ||
get :destroy | ||
end | ||
|
||
it 'should clear the user session' do | ||
session.should be_blank | ||
end | ||
|
||
it 'should redirect to root_url' do | ||
response.should redirect_to(root_url) | ||
end | ||
end | ||
end | ||
|
||
describe SessionsController, 'when unauthorized' do | ||
it 'should respond with success on GET :index' do | ||
get :index | ||
response.should be_success | ||
end | ||
|
||
describe 'on GET :new with empty session' do | ||
before :each do | ||
request_token = mock(OAuth::RequestToken, :token => 'token', :secret => 'secret') | ||
OAuth::Consumer.any_instance.stub(:get_request_token).and_return(request_token) | ||
get :new | ||
end | ||
|
||
it 'should redirect to /accounts/OAuthAuthorizeToken' do | ||
response.should redirect_to('/accounts/OAuthAuthorizeToken') | ||
end | ||
|
||
it 'should set session[:oauth][:request_token]' do | ||
session[:oauth][:request_token].should eq('token') | ||
end | ||
|
||
it 'should set session[:oauth][:request_secret]' do | ||
session[:oauth][:request_secret].should eq('secret') | ||
end | ||
end | ||
|
||
describe 'on GET :new with session[:oauth][:request_token]' do | ||
before :each do | ||
session[:oauth] = {} | ||
session[:oauth][:request_token] = 'token' | ||
session[:oauth][:request_secret] = 'secret' | ||
response = mock(Net::HTTPSuccess, :body => '{"data":{"email": "push@broom.com"}}') | ||
access_token = mock(OAuth::AccessToken, :get => response, :token => 'token', :secret => 'secret') | ||
OAuth::RequestToken.any_instance.stub(:get_access_token).and_return(access_token) | ||
User.stub(:find_or_initialize_by_email).and_return(stub_model(User)) | ||
end | ||
|
||
it 'should redirect to brooms_url' do | ||
get :new | ||
response.should redirect_to(brooms_url) | ||
end | ||
|
||
it 'should find or setup and save a user' do | ||
User.should_receive(:find_or_initialize_by_email).with('push@broom.com') | ||
User.any_instance.should_receive(:attributes=).with({ :token => 'token', :secret => 'secret' }) | ||
get :new | ||
end | ||
|
||
it 'should set session[:user_id]' do | ||
get :new | ||
session[:user_id].should_not be_blank | ||
end | ||
|
||
it 'should blank session[:oauth]' do | ||
get :new | ||
session[:oauth].should be_blank | ||
end | ||
|
||
describe 'but the user denies access on Google page' do | ||
before :each do | ||
OAuth::RequestToken.any_instance.stub(:get_access_token).and_raise(OAuth::Unauthorized) | ||
end | ||
|
||
it 'should redirect to root_url' do | ||
get :new | ||
response.should redirect_to(root_url) | ||
end | ||
|
||
it 'should set flash[:error] with info of the mishap' do | ||
get :new | ||
flash[:error].should eq("Oops, we were not granted permission. You may try again if you like.") | ||
end | ||
|
||
it 'should reset the session' do | ||
get :new | ||
session.should be_blank | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'spec_helper' | ||
|
||
# Specs in this file have access to a helper object that includes | ||
# the SessionsHelper. For example: | ||
# | ||
# describe SessionsHelper do | ||
# describe "string concat" do | ||
# it "concats two strings with spaces" do | ||
# helper.concat_strings("this","that").should == "this that" | ||
# end | ||
# end | ||
# end | ||
describe SessionsHelper do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |