user = User.create_with_omniauth(auth_hash['info'])
auth = user.authorizations.create_with_omniauth(auth_hash)
private
def auth_hash
# ensures availability but only retrieved once per cycle
@auth_hash ||= request.env['omniauth.auth']
end
schema:
ActiveRecord::Schema.define(version: 20210414024936) do
create_table "authorizations", force: :cascade do |t|
t.string "provider"
t.string "uid"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "authorizations", ["user_id"], name: "index_authorizations_on_user_id"
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
code in models:
class User < ActiveRecord::Base
has_many :authorizations
validates :name, :email, :presence => true
# save new user info
def self.create_with_omniauth info
create!(name: info['name'], email: info['email'])
end
end
class Authorization < ActiveRecord::Base
belongs_to :user
validates :provider, :uid, :presence => true
validates_uniqueness_of :uid, scope: :provider
# create new authorization
def self.create_with_omniauth auth
create!(uid: auth['uid'], provider: auth['provider'])
end
end
spec_helper.rb:
require 'omniauth'
OmniAuth.config.test_mode = true
omniauth_hash = { 'provider' => 'github',
'uid' => "12345",
'info' => {
'name' => "SUNY Tester",
}
}
OmniAuth.config.add_mock(:github, omniauth_hash)
rails_helper.rb
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:github]
sessions_controller_spec.rb:
require 'rails_helper'
RSpec.describe SessionsController, type: :controller do
describe "GET #create" do
context 'register with github' do
describe 'When signing up for first time' do
let(:authorization1) {instance_double('Authorization', provider: 'github', uid: '12345', user_id: '1')}
it "creates a User" do
expect(User).to receive(:create_with_omniauth).with(OmniAuth.config.mock_auth[:github]['info']).and_return(user1)
post :create, provider: :github
end
# none of the following work, just a couple examples of what I've tried
it "creates an Authorization" do
#allow(user1).to receive(:authorizations.create_with_omniauth).with(OmniAuth.config.mock_auth[:github]['info']).and_return(user1)
# expect(authorization1).to be_a_new(Authorization)
# expect(user1.authorizations).to receive(:create_with_omniauth).with(OmniAuth.config.mock_auth[:github]).and_return(authorization1)
post :create, provider: :github
end
The error messages are always something along the lines of:
SessionsController GET #create register with github When signing up for first time creates a User
Failure/Error: user.authorizations.create_with_omniauth(auth_hash)
#<InstanceDouble(User) (anonymous)> received unexpected message :authorizations with (no args)
If I try to create the authorization directly instead of with the proxy object, the user_id doesn't get set.
No matter how I go about it, I can't seem to find a way to mock a proxy object (the instance of user that is just created .authorizations )
Ironically, All my code as well as the students' code is working, but I can't seem to find a proper way to test it.