From 872a96dc71cbf0c5ee6715ab65401aa76cacc7b6 Mon Sep 17 00:00:00 2001 From: Sarah Mei Date: Sun, 22 Jan 2012 08:28:23 -0800 Subject: [PATCH] Refactor services controller spec --- app/controllers/services_controller.rb | 7 +- spec/controllers/services_controller_spec.rb | 80 ++++++++++---------- 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/app/controllers/services_controller.rb b/app/controllers/services_controller.rb index d7adac787..25df66366 100644 --- a/app/controllers/services_controller.rb +++ b/app/controllers/services_controller.rb @@ -1,5 +1,6 @@ # Copyright (c) 2010-2011, Diaspora Inc. This file is -# licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. class ServicesController < ApplicationController before_filter :authenticate_user! @@ -38,8 +39,8 @@ class ServicesController < ApplicationController if existing_service = Service.where(:type => service.type.to_s, :uid => service.uid).first flash[:error] << I18n.t('services.create.already_authorized', - :diaspora_id => existing_service.user.person.profile.diaspora_handle, - :service_name => provider.camelize ) + :diaspora_id => existing_service.user.person.profile.diaspora_handle, + :service_name => provider.camelize ) end end diff --git a/spec/controllers/services_controller_spec.rb b/spec/controllers/services_controller_spec.rb index a4c9172d6..fd11b2150 100644 --- a/spec/controllers/services_controller_spec.rb +++ b/spec/controllers/services_controller_spec.rb @@ -36,50 +36,48 @@ describe ServicesController do end describe '#create' do - it 'creates a new OmniauthService' do - request.env['omniauth.auth'] = omniauth_auth - lambda{ + context 'when not fetching a photo' do + before do + request.env['omniauth.auth'] = omniauth_auth + end + + it 'creates a new OmniauthService' do + expect { + post :create, :provider => 'twitter' + }.to change(@user.services, :count).by(1) + end + + it 'redirects to getting started if the user is getting started' do + @user.getting_started = true post :create, :provider => 'twitter' - }.should change(@user.services, :count).by(1) + response.should redirect_to getting_started_path + end + + it 'redirects to services url if user is not getting started' do + @user.getting_started = false + post :create, :provider => 'twitter' + response.should redirect_to services_url + end + + it 'creates a twitter service' do + Service.delete_all + @user.getting_started = false + post :create, :provider => 'twitter' + @user.reload.services.first.class.name.should == "Services::Twitter" + end + + it 'returns error if the user already a service with that uid' do + Services::Twitter.create!(:nickname => omniauth_auth["info"]['nickname'], + :access_token => omniauth_auth['credentials']['token'], + :access_secret => omniauth_auth['credentials']['secret'], + :uid => omniauth_auth['uid'], + :user => bob) + post :create, :provider => 'twitter' + flash[:error].include?(bob.person.profile.diaspora_handle).should be_true + end end - it 'redirects to getting started if the user is getting started' do - @user.getting_started = true - request.env['omniauth.auth'] = omniauth_auth - post :create, :provider => 'twitter' - response.should redirect_to getting_started_path - end - - it 'redirects to services url' do - @user.getting_started = false - request.env['omniauth.auth'] = omniauth_auth - post :create, :provider => 'twitter' - response.should redirect_to services_url - end - - it 'creates a twitter service' do - Service.delete_all - @user.getting_started = false - request.env['omniauth.auth'] = omniauth_auth - post :create, :provider => 'twitter' - @user.reload.services.first.class.name.should == "Services::Twitter" - end - - it 'returns error if the service is connected with that uid' do - request.env['omniauth.auth'] = omniauth_auth - - Services::Twitter.create!(:nickname => omniauth_auth["info"]['nickname'], - :access_token => omniauth_auth['credentials']['token'], - :access_secret => omniauth_auth['credentials']['secret'], - :uid => omniauth_auth['uid'], - :user => bob) - - post :create, :provider => 'twitter' - - flash[:error].include?(bob.person.profile.diaspora_handle).should be_true - end - - context "photo fetching" do + context 'when fetching a photo' do before do omniauth_auth omniauth_auth["info"].merge!({"image" => "https://service.com/fallback_lowres.jpg"})