I'm trying to test oauth in controller, but I got stuck. How can I test my create
method properly?
routes.rb
get '/auth/:provider/callback', to: 'socials#create'
resources :socials, only: :create
social.rb
def self.find_or_create_from_auth_hash(auth_hash)
#checking if the connected account exists (one user can have one acc/provider at the moment)
social_acc = where(provider: auth_hash.provider).first_or_create
social_acc.update(
uid: auth_hash.uid,
token: auth_hash.credentials.token,
secret: auth_hash.credentials.secret,
picture_url: auth_hash.info.image,
location: auth_hash.info.location,
description: auth_hash.info.description,
first_name: auth_hash.info.first_name || nil,
last_name: auth_hash.info.last_name || nil,
phone: auth_hash.info.phone || nil,
page_url: case social_acc.provider
when 'twitter' then auth_hash.info.urls.Twitter
when 'linkedin' then auth_hash.info.urls.public_profile
end
)
social_acc
end
socials_controller
def create
begin
@profile = current_user.profile
@social = @profile.socials.find_or_create_from_auth_hash(auth_hash)
flash[:success] = "#{@social.provider.camelize} account was successfully updated!"
rescue
flash[:alert] = "There was an error while trying to authenticate you!"
end
redirect_to request.env['omniauth.origin'] || edit_user_profile_path(current_user)
end
private
def auth_hash
request.env['omniauth.auth']
end
socials_controller_spec.rb
describe "when user is logged in" do
before(:each) do
login_user
end
describe "POST create" do
let!(:profile) { create(:profile, user: @user) }
let!(:social) { create(:social, profile: profile) }
#Not sure if I have to create auth_hash factory and if so then how.
it "creates or updates social" do
post :create
expect(assigns(:profile)).to eq(profile)
#########I don't know what I should put here:
#expect(assigns(:profile)).to eq(receive)
#expect(@profile).to receive(:find_or_create_from_auth_hash)
end
end
end
Aucun commentaire:
Enregistrer un commentaire