Omniauth-Facebook callback does not get initialised in Rails Mountable Engine - ruby-on-rails

I'm developing a mountable engine (called SimpleUser) which uses Devise, OmniAuth and OmniAuth-Facebook. First I made a test app with the gems about and every worked fine. Next, I started building the engine from scratch, using the code of the test app as an example.
Everything is almost done, except for the connection with Facebook (it uses the Javascript popup). When I click in "log in" the FB popup is displayed, I grant the app, it redirects to the route specified (see routes), but throws this error:
NoMethodError in SimpleUser::AuthController#create
undefined method `[]' for nil:NilClass
The error occurs in that action, in the line authentication = Authentication.find_by_provider_and_uid(auth['provider'], auth['uid']) where auth is nil (auth comes from auth = request.env["omniauth.auth"]).
One thing I check is that the Callback phase it's no initialised. This is the log of the test app:
Started GET "/auth/facebook/callback" for 127.0.0.1 at 2013-03-14
08:52:56 -0600 (facebook) Callback phase initiated. Processing by
AuthController#create as HTML Parameters: {"provider"=>"facebook"}
This is the log of the engine app:
Started GET "/simple_user/auth/facebook/callback" for 127.0.0.1 at 2013-03-14 08:51:19 -0600
Processing by SimpleUser::AuthController#create as HTML
Parameters: {"provider"=>"facebook"}
For manage OmniAuth, I added the gem to the .gemspec and to the Gemfile; also, I require the gems in the engine, and within a generator of the engine I move a template of omniauth.rb to config/initializers of the parent app during installation. This is what I have:
AuthController (located in app/controllers/simple_user/auth_controller.rb)
module SimpleUser
class AuthController < ApplicationController
def create
auth = request.env["omniauth.auth"]
authentication = Authentication.find_by_provider_and_uid(auth['provider'], auth['uid'])
if authentication
flash[:notice] = "Signed in successfully."
sign_in(:user, authentication.user)
redirect_to root_url
else
user = User.build_new_auth(auth)
#user.apply_omniauth(auth)
if user.save(:validate => false)
flash[:notice] = "Account created and signed in successfully."
sign_in(:user, user)
redirect_to root_url
else
flash[:error] = "Error while creating the user account. Please try again."
redirect_to root_url
end
end
end
end
end
Engine
module SimpleUser
require 'rubygems'
require 'devise'
require 'cancan'
require 'rolify'
require 'omniauth'
require 'omniauth-facebook'
require 'simple_form'
class Engine < ::Rails::Engine
isolate_namespace SimpleUser
config.before_configuration do
env_file = File.join(Rails.root, 'config', 'fb_config.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value.to_s
end if File.exists?(env_file)
env_file = File.join(Rails.root, 'config', 'devise_config.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value.to_s
end if File.exists?(env_file)
end
end
end
Generator
module SimpleUser
module Generators
class InstallGenerator < ::Rails::Generators::Base
source_root File.expand_path("../templates", __FILE__)
desc "Install SimpleUser"
def copy_config_file
copy_file "fb_config.yml", "config/fb_config.yml"
copy_file "devise_config.yml", "config/devise_config.yml"
copy_file "omniauth.rb", "config/initializers/omniauth.rb"
end
def copy_migrations
rake "simple_user:install:migrations"
SimpleUser::Engine.load_seed
end
end
end
end
Template of the omniauth.rb
require 'omniauth'
require 'omniauth-facebook'
OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], :scope => ENV['FACEBOOK_SCOPE']
end
Routes (on engine)
match 'auth/:provider/callback', to: 'auth#create'
match 'auth/failure', to: redirect('/')
Routes (on dummy app)
mount SimpleUser::Engine => "/simple_user", :as => "simple_user"
.gemspec dependencies
s.add_dependency "rails", "~> 3.2.12"
s.add_dependency "devise"
s.add_dependency "cancan"
s.add_dependency "rolify"
s.add_dependency "omniauth"
s.add_dependency "omniauth-facebook", "1.4.1"
s.add_dependency "simple_form"
#s.add_development_dependency "mysql2"
s.add_development_dependency "sqlite3"
s.add_development_dependency "jquery-rails"
s.add_development_dependency "debugger"
Gemfile
source "http://rubygems.org"
gemspec
gem 'devise'
gem 'cancan'
gem 'rolify'
gem 'omniauth'
gem 'omniauth-facebook', '1.4.1'
gem 'simple_form'
# Development
gem 'jquery-rails'
gem 'debugger'
I think the problem is the callback that is not initialised, and the reason may be that OmniAuth doesn't get loaded, but I don't know if it is and how to solve it.
You can check the project in https://github.com/pablomarti/simple_user, and if you want to clone it and test you can use the generator rails g simple_user:install, and you can see the code of test/dummy also to get the idea.
Thank you very much in advance.

The solution was to remove the omniauth.rb and include the middleware of OmniAuth in the engine, so the engine looks like this:
module SimpleUser
require 'rubygems'
require 'devise'
require 'cancan'
require 'rolify'
require 'omniauth'
require 'omniauth-facebook'
require 'simple_form'
class Engine < ::Rails::Engine
isolate_namespace SimpleUser
middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], :scope => ENV['FACEBOOK_SCOPE']
end
config.before_configuration do
env_file = File.join(Rails.root, 'config', 'fb_config.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value.to_s
end if File.exists?(env_file)
env_file = File.join(Rails.root, 'config', 'devise_config.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value.to_s
end if File.exists?(env_file)
end
end
end
Thanks to Dmitry Lihachev for his answer https://stackoverflow.com/a/8413724/347501 in a similar problem.

Related

Ruby on Rails - Rspec test with Devise is failing

I have a Problem with an rspec-request-test in my rails app. The tests, that need a user-login are failing. I have implemented the devise-tests according to this tutorial. Also I found many links, that are using a similar approach. e.g. here
Here is an extract of my gemfile:
gem 'devise', '~> 3.5'
gem 'devise-i18n'
gem 'devise-i18n-views'
gem 'cancancan', '~> 1.10'
gem 'rolify'
gem 'alchemy_cms', '~> 3.2.0'
gem 'rspec-core'
gem 'rspec-rails'
gem 'i18n-tasks', '~> 0.8.7'
gem 'factory_girl_rails'
Here is the rspec-test, that is failing: (spec/requests/campaigns_spec.rb)
require 'rails_helper'
RSpec.describe "Campaigns", type: :request do
describe "GET /campaigns" do
it "responds with 200" do
sign_in_as_a_valid_user_request
get campaigns_path
expect(response).to have_http_status(200)
end
end
end
You should only see this page, when you are logged in. So in the test I want to log in as a User (created with FactoryGirl), than go to the site, and get a 200-response. In the application itself this is working good.
spec/rails_helper.rb (extract)
...
require 'spec_helper'
require 'rspec/rails'
require 'devise'
require 'support/devise_support'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include Devise::TestHelpers, type: :controller
config.include Devise::TestHelpers, type: :view
config.include ValidUserRequestHelper
...
end
spec/support/devise_support.rb
module ValidUserRequestHelper
def sign_in_as_a_valid_user_request
#user ||= FactoryGirl.create :user
post_via_redirect user_session_path, 'user[email]' => #user.email, 'user[password]' => #user.password
end
end
here is the error, that the test is throwing:
1) Campaigns GET /campaigns responds with 200
Failure/Error: sign_in_as_a_valid_user_request
ActionController::RoutingError:
Alchemy::Page not found "/"
# ./spec/support/devise_support.rb:13:in `sign_in_as_a_valid_user_request'
# ./spec/requests/campaigns_spec.rb:6:in `block (3 levels) in <top (required)>'
According to the links that I found, this should work! but it doesn't... Can anyone tell me why? Why is there a routing error? The app itself is working fine.
The routing error occurs, because your host app does not know of the engines routes. That's why you have to use the routing proxy objects of rails engines in views outside the current controller scope. Please read more about rails engines and the characteristics of their routes in the official rails guides
Luckily, Alchemy comes with a handy test helper that solves your problems. Just add this to your spec_helper:
# spec/spec_helper.rb
require 'alchemy/test_support/controller_requests'
...
RSpec.configure do |config|
config.include Alchemy::TestSupport::ControllerRequests
...
end
Know you can use alchemy_get instead of get in your request specs.

rails rspec tests for lib class used by routes contains private methods

Simplecov detected that I was missing some tests on my lib/api_verson.rb class:
class ApiVersion
def initialize(version)
#version = version
end
def matches?(request)
versioned_accept_header?(request) || version_one?(request)
end
private
def versioned_accept_header?(request)
accept = request.headers['Accept']
accept && accept[/application\/vnd\.#{Rails.application.secrets.my_app_accept_header}-v#{#version}\+json/]
end
def unversioned_accept_header?(request)
accept = request.headers['Accept']
accept.blank? || accept[/application\/vnd\.#{Rails.application.secrets.my_app_accept_header}/].nil?
end
def version_one?(request)
#version == Rails.application.secrets.my_app_default_api_version && unversioned_accept_header?(request)
end
end
This class is used by the routes file to help setup api versions:
namespace :api, path: "", defaults: {format: :json} do
scope module: :v1, constraints: ApiVersion.new(1) do
get '/alive', to: 'api#alive'
end
scope module: :v2, constraints: ApiVersion.new(2) do
get '/alive', to: 'api#alive'
end
end
This setup was ported from versioning_your_ap_is.
I am trying to test the methods here that simplecov is reporting as failures:
require 'spec_helper'
describe ApiVersion do
before(:each) do
#apiversion = ApiVersion.new(1)
#request = ActionController::TestRequest.new(host: 'localhost')
#request.headers["Accept"] = "application/vnd.#{Rails.application.secrets.my_app_accept_header}-#{Rails.application.secrets.my_app_default_api_version}+json"
end
describe 'Method #versioned_accept_header =>' do
it 'Should return the correct accept header version' do
binding.pry
end
end
end
I am trying to build this first test to attempt??? to #apiversion.send(:unversioned_accept_header, #request) and I am getting the error:
#apiversion.send(:unversioned_accept_header, #request)
NoMethodError: undefined method `unversioned_accept_header' for #<ApiVersion:0x007fae009bdad8 #version=1>
from (pry):1:in `block (3 levels) in <top (required)>'
Basically the following methods are flagged: "matches?, versioned_accept_header?, unversioned_accept_header?, and version_one?"
I am not the rockstar at rspec and could use some pointers here. Thank you for your help.
Btw, this is a rails 4 application running:
group :development, :test do
gem 'pry'
gem 'pry-doc'
gem 'pry-debugger'
gem 'pry-rails'
gem 'pry-plus'
gem 'pry-rescue'
gem 'pry-stack_explorer'
gem 'pry-clipboard'
gem 'pry-nav'
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'faker'
gem 'seedbank'
gem 'capybara'
end
group :test do
gem 'simplecov', '~> 0.7.1'
gem 'shoulda-matchers'
gem 'spork-rails'
gem 'database_cleaner'
gem 'email_spec'
gem 'timecop'
gem 'json_spec'
end
You run this code
#apiversion.send(:unversioned_accept_header, #request)
but you have method:
def unversioned_accept_header?(request)
accept = request.headers['Accept']
accept.blank? || accept[/application\/vnd\.#{Rails.application.secrets.my_app_accept_header}/].nil?
end
try change
#apiversion.send(:unversioned_accept_header, #request) -> #apiversion.send(:unversioned_accept_header?, #request)
or change method name
def unversioned_accept_header? -> def unversioned_accept_header

Rails 4 mounted engine with rspec and factory_girl_rails

I am building a rails 4 mounted application with mongoid odm. Everything works fine but rspec tests does not work properly. When I run bundle exec rspec an error occurs saying that:
Factory not registered: cafcaf_user
My user model:
module Cafcaf
class User
include Mongoid::Document
field :username, type: String
field :email, type: String
field :full_name, type: String
field :last_name, type: String
end
end
my spec_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../spec/test_app/config/environment.rb", __FILE__)
require 'rspec/rails'
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end
my user_spec.rb
require 'spec_helper'
module Cafcaf
describe User do
it "has a valid factory" do
FactoryGirl.create(:cafcaf_user).should be_valid
end
it "is invalid without a username"
it "is invalid without an email"
end
end
my factories.rb
FactoryGirl.define do
factory :cafcaf_user, :class => 'User' do
username "MyString"
email "MyString"
full_name ""
last_name "MyString"
end
end
my lib/cafcaf/engine.rb
module Cafcaf
class Engine < ::Rails::Engine
isolate_namespace Cafcaf
config.generators do |g|
g.test_framework :rspec
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
end
end
end
my Gemfile
source "https://rubygems.org"
gem 'rails', "~> 4.0.4"
gem 'mongoid', github: 'mongoid/mongoid', tag: 'v4.0.0.beta1'
group :development, :test do
gem 'rspec-rails', '~> 3.0.0.beta2'
gem 'database_cleaner', '~> 1.2.0'
gem 'factory_girl_rails', '~> 4.4.1'
end
my gemspec
$:.push File.expand_path("../lib", __FILE__)
require "cafcaf/version"
# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "cafcaf"
s.version = Cafcaf::VERSION
s.authors = ["Your name"]
s.email = ["bla#bla.com"]
s.homepage = "http://ir.io"
s.summary = "Summary of Cafcaf."
s.description = " Description of Cafcaf."
s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]
s.add_dependency "rails", "~> 4.0.4"
end
Exactly I do not know ho to proceed. How to use rspec and factory_girs gem in a mounted rails engine app? I have done tons of test but did not find the solution.
I have found the solution by adding the following codes to spec_helper.rb
ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
Dir[File.join(ENGINE_RAILS_ROOT, "spec/factories/**/*.rb")].each {|f| require f }
Now it works like a charm.

minitest testing and code coverage

I use mini-test for testing framework. I use omniauth gem for authentication. I use simplecov for code coverage. I run my tests using "bundle exec rake" or "rake minitest:controllers". I give an example for controllers. When I run rake minitest:controllers, controllers code coverage becomes 100%. But, when I run bundle exec rake, controllers code coverage become 60%.
SessionsController.rb code:
class SessionsController < ApplicationController
def create
auth = request.env["omniauth.auth"]
person=Person.find_by_provider_and_uid(auth.provider,auth.uid) || Person.create_with_omniauth(auth)
redirect_to root_path
end
end
SessionsController_test.rb
require "minitest_helper"
describe SessionsController do
before do
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:identity]
#person = Fabricate.build(:person)
end
it "should create authentication" do
assert_difference('Person.count') do
post :create, :provider => "identity"
end
assert_redirected_to root_path #person
end
end
I wonder that if I miss one point on writing test. I wait your ideas. Thanks in advance.
EDIT
minitest_helper.rb
require 'simplecov'
Simplecov.start
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require "minitest/autorun"
require "minitest/rails"
require "minitest/pride"
require 'database_cleaner'
require "minitest/rails/capybara"
require "minitest-mongoid"
DatabaseCleaner[:mongoid].strategy = :truncation
#OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:identity, {
:uid => '12345'
})
class MiniTest::Spec
before :each do
DatabaseCleaner.start
end
after :each do
DatabaseCleaner.clean
end
end
According to Simplecov's documentation, you just have to add theses lines in top of your test/test_helper.rb:
# test/test_helper.rb
require 'simplecov'
SimpleCov.start
# ...
Also do not forget to install simplecov gem in test group:
# Gemfile
# ...
group :test do
gem 'simplecov'
end
And that's it.
Rails 6: I encountered some issues with Rails 6 and tests paralelization so you may deactivate it in test/test_helper.rb:
# test/test_helper.rb
# ...
class ActiveSupport::TestCase
# ...
# parallelize(workers: 2)
end
It's hard to tell with no more information.
First of all try rake minitest:all and update your question with the result.
Please try following in case the former test did not conclude positively:
namespace :test do
task :coverage do
require 'simplecov'
SimpleCov.start 'rails' # feel free to pass block
Rake::Task["test"].execute
end
end
Let us know and we can edit or update the answer.
Minitest is known to have had some issues with it. I believe it was still work in progress, not sure where they stand now. It is not you, it's minitest. That workaround helped in some cases, maybe it helps you too.

omniauth OAuthException & OAuth::Unauthorized

I have installed omniauth 1.0. Also I have oauth-0.4.5, oauth2-0.5.1, omniauth-facebook-1.0.0, omniauth-twitter-0.0.6.
omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :developer unless Rails.env.production?
provider :facebook, ENV['167257285348131'], ENV['c8c722f697scb2afcf1600286c6212a9'], :scope => 'email,offline_access,read_stream', :display => 'popup'
provider :twitter, ENV['fma2L22ObJCW52QrL7uew'], ENV['4aZfhCAOdiS7ap8pHJ7I1OZslFwVWWLiAMVpYUI']
end
session_controller.rb
class SessionsController < ApplicationController
require 'omniauth-facebook'
require 'omniauth-twitter'
require 'omniauth'
def create
#user = User.find_or_create_from_auth_hash(auth_hash)
self.current_user = #user
redirect_to '/'
end
def auth_hash
request.env['omniauth.auth']
end
end
Also I add
'omniauth'
'omniauth-facebook'
'omniauth-twitter' gems to gemfile
There are two problems:
When I go http://localhost:3000/auth/facebook I get
{
"error": {
"message": "Missing client_id parameter.",
"type": "OAuthException"
}
}
And the link graph.facebook.com/oauth/authorize?response_type=code&client_id=&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Ffacebook%2Fcallback&parse=query&scope=email%2Coffline_access%2Cread_stream&display=popup
And there is no client_id!!!
When I go to http://localhost:3000/auth/twitter I get OAuth::Unauthorized
401 Unauthorized
Any ideas?
Alex D. is correct in that the ENV[] breaks it. To create omniauth.rb so that it uses different keys in different environments just put:
provider :twitter, TWITTER_KEY, TWITTER_SECRET
in omniauth.rb
and then in your environment config files (config/environments/development.rb, etc.) put the key you want to use for that environment.
config/environments/development.rb:
TWITTER_KEY = 'aaaaaaa'
TWITTER_SECRET = 'aaaabbbbbb'
config/environments/production.rb:
TWITTER_KEY = 'ccccccc'
TWITTER_SECRET = 'ccccdddddd'
ENV['something']
looks into your environment vars for "something", so it would expect
something='12345'
so you should do it like that
export AUTH_FB_KEY='....'
export AUTH_FB_SECRET='...'
check with
env
and update your config
provider :facebook, ENV['AUTH_FB_KEY'], ENV['AUTH_FB_SECRET']
if you use heroku
heroku config:add AUTH_FB_KEY='....'
There have been breaking changes made in omniauth 1.0 - https://github.com/intridea/omniauth
OmniAuth 1.0 has several breaking changes from version 0.x. You can
set the dependency to ~> 0.3.2 if you do not wish to make the more
difficult upgrade. See the wiki for more information.
I would try reverting omniauth to 0.3.2:
gem install omniauth --version '~> 0.3.2'
or if you're using bundler, in your Gemfile:
gem omniauth, '~> 0.3.2'

Resources