Skip to content
Browse files

Finally, adds some RSpec basic tests for the strategy.

  • Loading branch information...
1 parent 4ef0d68 commit 6a2c03234e533bbb1e236710b1f30b48bd594ff1 @nicolasblanco nicolasblanco committed Sep 20, 2010
Showing with 128 additions and 22 deletions.
  1. +33 −22 Rakefile
  2. +81 −0 spec/devise_rpx_connectable_spec.rb
  3. +14 −0 spec/spec_helper.rb
View
55 Rakefile
@@ -1,30 +1,41 @@
-# encoding: utf-8
-require 'rubygems'
-require 'rake'
-require 'rake/rdoctask'
+require "rake"
+require "rake/rdoctask"
+require "rspec"
+require "rspec/core/rake_task"
require File.join(File.dirname(__FILE__), 'lib', 'devise_rpx_connectable', 'version')
-NAME = "devise_rpx_connectable"
-
begin
- gem 'jeweler'
require 'jeweler'
- Jeweler::Tasks.new do |spec|
- spec.name = NAME
- spec.version = ::Devise::RpxConnectable::VERSION
- spec.summary = %{Devise << RPX}
- spec.description = spec.summary
- spec.homepage = "http://github.com/slainer68/#{spec.name}"
- spec.authors = ["Nicolas Blanco"]
- spec.email = "slainer68@gmail.com"
-
- spec.files = FileList['[A-Z]*', File.join(*%w[{lib,rails} ** *]).to_s]
-
- spec.add_dependency 'devise', '>= 1.1.2'
- spec.add_dependency 'rpx_now', '>= 0.6.23'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "devise_rpx_connectable"
+ gem.version = ::Devise::RpxConnectable::VERSION
+ gem.summary = %Q{Authenticate your users with RPX in your Rails application using Devise.}
+ gem.description = %Q{Authenticate your users with RPX in your Rails application using Devise.}
+ gem.email = "slainer68@gmail.com"
+ gem.homepage = "http://github.com/slainer68/devise_rpx_connectable"
+ gem.authors = ["Nicolas Blanco"]
+
+ gem.add_dependency 'devise', '>= 1.1.2'
+ gem.add_dependency 'rpx_now', '>= 0.6.23'
+
+ gem.add_development_dependency "rspec", ">= 2.0.0.beta.22"
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
rescue LoadError
- puts "Jeweler - or one of its dependencies - is not available. " <<
- "Install it with: sudo gem install jeweler -s http://gemcutter.org"
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
+end
+
+Rspec::Core::RakeTask.new(:spec) do |spec|
+ spec.pattern = "spec/**/*_spec.rb"
end
+
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "devise_rpx_connectable #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
View
81 spec/devise_rpx_connectable_spec.rb
@@ -0,0 +1,81 @@
+require "spec_helper"
+require "active_support/all"
+
+require File.join(File.dirname(__FILE__), '..', 'lib', 'devise_rpx_connectable', 'strategy')
+
+class User
+ def self.rpx_extended_user_data; true; end
+ def self.rpx_additional_user_data; true; end
+end
+
+class RPXNow
+ @@api_key = "abcdefgh"
+ cattr_accessor :api_key
+end
+
+RPX_USER_DATA = { "identifier" => "superpipo_user" }
+PARAMS = { :token => "rpx_token" }
+
+describe 'DeviseRpxConnectable' do
+ before(:each) do
+ @strategy = Devise::RpxConnectable::Strategies::RpxConnectable.new
+ @mapping = mock(:mapping)
+ @mapping.should_receive(:to).and_return(User)
+ @strategy.should_receive(:mapping).and_return(@mapping)
+ @strategy.should_receive(:params).and_return(PARAMS)
+
+ @user = User.new
+ end
+
+ it "should fail if RPX returns no valid user" do
+ RPXNow.should_receive(:user_data).and_return(nil)
+
+ @strategy.should_receive(:"fail!").with(:rpx_invalid).and_return(true)
+
+ lambda { @strategy.authenticate! }.should_not raise_error
+ end
+
+ describe 'when the RPX user is valid' do
+ before(:each) do
+ RPXNow.should_receive(:user_data).and_return(RPX_USER_DATA)
+ end
+
+ it "should authenticate if a user exists in database" do
+ User.should_receive(:authenticate_with_rpx).with({ :identifier => RPX_USER_DATA["identifier"] }).and_return(@user)
+
+ @user.should_receive(:on_before_rpx_success).with(RPX_USER_DATA).and_return(true)
+
+ @strategy.should_receive(:"success!").with(@user).and_return(true)
+
+ lambda { @strategy.authenticate! }.should_not raise_error
+ end
+
+ describe 'when no user exists in database' do
+ before(:each) do
+ User.should_receive(:authenticate_with_rpx).with({ :identifier => RPX_USER_DATA["identifier"] }).and_return(nil)
+ end
+
+ it "should fail unless rpx_auto_create_account" do
+ User.should_receive(:"rpx_auto_create_account?").and_return(false)
+ @strategy.should_receive(:"fail!").with(:rpx_invalid).and_return(true)
+
+ lambda { @strategy.authenticate! }.should_not raise_error
+ end
+
+ it "should create a new user and success if rpx_auto_create_account" do
+ User.should_receive(:"rpx_auto_create_account?").and_return(true)
+
+ User.should_receive(:new).and_return(@user)
+ @user.should_receive(:"store_rpx_credentials!").with(RPX_USER_DATA).and_return(true)
+ @user.should_receive(:on_before_rpx_auto_create).with(RPX_USER_DATA).and_return(true)
+ @user.should_receive(:save).with({ :validate => false }).and_return(true)
+ @user.should_receive(:on_before_rpx_success).with(RPX_USER_DATA).and_return(true)
+
+ @strategy.should_receive(:"success!").with(@user).and_return(true)
+
+ lambda { @strategy.authenticate! }.should_not raise_error
+ end
+ end
+ end
+end
+
View
14 spec/spec_helper.rb
@@ -0,0 +1,14 @@
+require 'ruby-debug'
+require 'rspec'
+
+module Devise
+ class Strategies
+ class Base
+ end
+ end
+end
+
+RSpec.configure do |config|
+ config.mock_with :rspec
+end
+

0 comments on commit 6a2c032

Please sign in to comment.
Something went wrong with that request. Please try again.