lperichon-devise_invitable 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/.document +5 -0
  2. data/.gitignore +22 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +92 -0
  5. data/Rakefile +54 -0
  6. data/VERSION +1 -0
  7. data/app/controllers/devise/invitations_controller.rb +52 -0
  8. data/app/views/devise/invitations/edit.html.erb +14 -0
  9. data/app/views/devise/invitations/new.html.erb +12 -0
  10. data/app/views/devise/mailer/invitation.html.erb +8 -0
  11. data/devise_invitable.gemspec +123 -0
  12. data/init.rb +1 -0
  13. data/lib/devise_invitable.rb +21 -0
  14. data/lib/devise_invitable/controllers/helpers.rb +6 -0
  15. data/lib/devise_invitable/controllers/url_helpers.rb +24 -0
  16. data/lib/devise_invitable/locales/en.yml +5 -0
  17. data/lib/devise_invitable/mailer.rb +8 -0
  18. data/lib/devise_invitable/model.rb +139 -0
  19. data/lib/devise_invitable/rails.rb +15 -0
  20. data/lib/devise_invitable/routes.rb +11 -0
  21. data/lib/devise_invitable/schema.rb +11 -0
  22. data/test/integration/invitable_test.rb +122 -0
  23. data/test/integration_tests_helper.rb +39 -0
  24. data/test/mailers/invitation_test.rb +62 -0
  25. data/test/model_tests_helper.rb +41 -0
  26. data/test/models/invitable_test.rb +172 -0
  27. data/test/models_test.rb +35 -0
  28. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  29. data/test/rails_app/app/controllers/application_controller.rb +10 -0
  30. data/test/rails_app/app/controllers/home_controller.rb +4 -0
  31. data/test/rails_app/app/controllers/users_controller.rb +12 -0
  32. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  33. data/test/rails_app/app/models/user.rb +4 -0
  34. data/test/rails_app/app/views/home/index.html.erb +0 -0
  35. data/test/rails_app/config/boot.rb +110 -0
  36. data/test/rails_app/config/database.yml +22 -0
  37. data/test/rails_app/config/environment.rb +44 -0
  38. data/test/rails_app/config/environments/development.rb +17 -0
  39. data/test/rails_app/config/environments/production.rb +28 -0
  40. data/test/rails_app/config/environments/test.rb +28 -0
  41. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  42. data/test/rails_app/config/initializers/devise.rb +105 -0
  43. data/test/rails_app/config/initializers/inflections.rb +2 -0
  44. data/test/rails_app/config/initializers/new_rails_defaults.rb +21 -0
  45. data/test/rails_app/config/initializers/session_store.rb +15 -0
  46. data/test/rails_app/config/routes.rb +3 -0
  47. data/test/rails_app/vendor/plugins/devise_invitable/init.rb +1 -0
  48. data/test/routes_test.rb +20 -0
  49. data/test/test_helper.rb +58 -0
  50. metadata +173 -0
@@ -0,0 +1,41 @@
1
+ class ActiveSupport::TestCase
2
+ def setup_mailer
3
+ ActionMailer::Base.deliveries = []
4
+ end
5
+
6
+ def store_translations(locale, translations, &block)
7
+ begin
8
+ I18n.backend.store_translations locale, translations
9
+ yield
10
+ ensure
11
+ I18n.reload!
12
+ end
13
+ end
14
+
15
+ # Helpers for creating new users
16
+ #
17
+ def generate_unique_email
18
+ @@email_count ||= 0
19
+ @@email_count += 1
20
+ "test#{@@email_count}@email.com"
21
+ end
22
+
23
+ def valid_attributes(attributes={})
24
+ { :email => generate_unique_email,
25
+ :password => '123456',
26
+ :password_confirmation => '123456' }.update(attributes)
27
+ end
28
+
29
+ def new_user(attributes={})
30
+ User.new(valid_attributes(attributes))
31
+ end
32
+
33
+ def create_user_with_invitation(invitation_token, attributes={})
34
+ user = new_user({:password => nil, :password_confirmation => nil}.update(attributes))
35
+ user.skip_confirmation!
36
+ user.invitation_token = invitation_token
37
+ user.invitation_sent_at = Time.now.utc
38
+ user.save(false)
39
+ user
40
+ end
41
+ end
@@ -0,0 +1,172 @@
1
+ require 'test/test_helper'
2
+ require 'test/model_tests_helper'
3
+
4
+ class InvitableTest < ActiveSupport::TestCase
5
+
6
+ def setup
7
+ setup_mailer
8
+ end
9
+
10
+ test 'should not generate invitation token after creating a record' do
11
+ assert_nil new_user.invitation_token
12
+ end
13
+
14
+ test 'should regenerate invitation token each time' do
15
+ user = new_user
16
+ 3.times do
17
+ token = user.invitation_token
18
+ user.resend_invitation!
19
+ assert_not_equal token, user.invitation_token
20
+ end
21
+ end
22
+
23
+ test 'should test invitation sent at with invite_for configuration value' do
24
+ user = create_user_with_invitation('')
25
+
26
+ User.stubs(:invite_for).returns(nil)
27
+ user.invitation_sent_at = Time.now.utc
28
+ assert user.valid_invitation?
29
+
30
+ User.stubs(:invite_for).returns(nil)
31
+ user.invitation_sent_at = 1.year.ago
32
+ assert user.valid_invitation?
33
+
34
+ User.stubs(:invite_for).returns(0)
35
+ user.invitation_sent_at = Time.now.utc
36
+ assert user.valid_invitation?
37
+
38
+ User.stubs(:invite_for).returns(0)
39
+ user.invitation_sent_at = 1.day.ago
40
+ assert user.valid_invitation?
41
+
42
+ User.stubs(:invite_for).returns(1.day)
43
+ user.invitation_sent_at = Time.now.utc
44
+ assert user.valid_invitation?
45
+
46
+ User.stubs(:invite_for).returns(1.day)
47
+ user.invitation_sent_at = 1.day.ago
48
+ assert_not user.valid_invitation?
49
+ end
50
+
51
+ test 'should never generate the same invitation token for different users' do
52
+ invitation_tokens = []
53
+ 3.times do
54
+ user = new_user
55
+ user.resend_invitation!
56
+ token = user.invitation_token
57
+ assert !invitation_tokens.include?(token)
58
+ invitation_tokens << token
59
+ end
60
+ end
61
+
62
+ test 'should set password and password confirmation from params' do
63
+ create_user_with_invitation('valid_token', :password => nil, :password_confirmation => nil)
64
+ user = User.accept_invitation!(:invitation_token => 'valid_token', :password => '123456789', :password_confirmation => '123456789')
65
+ assert user.valid_password?('123456789')
66
+ end
67
+
68
+ test 'should set password and save the record' do
69
+ user = create_user_with_invitation('valid_token', :password => nil, :password_confirmation => nil)
70
+ old_encrypted_password = user.encrypted_password
71
+ user = User.accept_invitation!(:invitation_token => 'valid_token', :password => '123456789', :password_confirmation => '123456789')
72
+ assert_not_equal old_encrypted_password, user.encrypted_password
73
+ end
74
+
75
+ test 'should clear invitation token while setting the password' do
76
+ user = new_user
77
+ user.skip_confirmation!
78
+ user.update_attribute(:invitation_token, 'valid_token')
79
+ assert_present user.invitation_token
80
+ assert user.accept_invitation!
81
+ assert_nil user.invitation_token
82
+ end
83
+
84
+ test 'should not clear invitation token if record is invalid' do
85
+ user = new_user
86
+ user.skip_confirmation!
87
+ user.update_attribute(:invitation_token, 'valid_token')
88
+ assert_present user.invitation_token
89
+ User.any_instance.stubs(:valid?).returns(false)
90
+ User.accept_invitation!(:invitation_token => 'valid_token', :password => '123456789', :password_confirmation => '987654321')
91
+ user.reload
92
+ assert_present user.invitation_token
93
+ end
94
+
95
+ test 'should reset invitation token and send invitation by email' do
96
+ user = new_user
97
+ assert_difference('ActionMailer::Base.deliveries.size') do
98
+ token = user.invitation_token
99
+ user.resend_invitation!
100
+ assert_not_equal token, user.invitation_token
101
+ end
102
+ end
103
+
104
+ test 'should return a record with invitation token and no errors to send invitation by email' do
105
+ invited_user = User.send_invitation(:email => "valid@email.com")
106
+ assert invited_user.errors.blank?
107
+ assert_present invited_user.invitation_token
108
+ end
109
+
110
+ test 'should return a record with errors if user was found by e-mail' do
111
+ user = create_user_with_invitation('')
112
+ user.update_attribute(:invitation_token, nil)
113
+ invited_user = User.send_invitation(:email => user.email)
114
+ assert_equal invited_user, user
115
+ assert_equal 'has already been taken', invited_user.errors[:email]
116
+ end
117
+
118
+ test 'should return a new record with errors if e-mail is blank' do
119
+ invited_user = User.send_invitation(:email => '')
120
+ assert invited_user.new_record?
121
+ assert_equal ["can't be blank", "is invalid"], invited_user.errors[:email]
122
+ end
123
+
124
+ test 'should return a new record with errors if e-mail is invalid' do
125
+ invited_user = User.send_invitation(:email => 'invalid_email')
126
+ assert invited_user.new_record?
127
+ assert_equal "is invalid", invited_user.errors[:email]
128
+ end
129
+
130
+ test 'should find a user to set his password based on invitation_token' do
131
+ user = new_user
132
+ user.resend_invitation!
133
+
134
+ invited_user = User.accept_invitation!(:invitation_token => user.invitation_token)
135
+ assert_equal invited_user, user
136
+ end
137
+
138
+ test 'should return a new record with errors if no invitation_token is found' do
139
+ invited_user = User.accept_invitation!(:invitation_token => 'invalid_token')
140
+ assert invited_user.new_record?
141
+ assert_equal 'is invalid', invited_user.errors[:invitation_token]
142
+ end
143
+
144
+ test 'should return a new record with errors if invitation_token is blank' do
145
+ invited_user = User.accept_invitation!(:invitation_token => '')
146
+ assert invited_user.new_record?
147
+ assert_equal "can't be blank", invited_user.errors[:invitation_token]
148
+ end
149
+
150
+ test 'should return record with errors if invitation_token has expired' do
151
+ user = create_user_with_invitation('valid_token')
152
+ user.update_attribute(:invitation_sent_at, 1.day.ago)
153
+ User.stubs(:invite_for).returns(10.hours)
154
+ invited_user = User.accept_invitation!(:invitation_token => 'valid_token')
155
+ assert_equal user, invited_user
156
+ assert_equal "is invalid", invited_user.errors[:invitation_token]
157
+ end
158
+
159
+ test 'should set successfully user password given the new password and confirmation' do
160
+ user = new_user(:password => nil, :password_confirmation => nil)
161
+ user.resend_invitation!
162
+
163
+ invited_user = User.accept_invitation!(
164
+ :invitation_token => user.invitation_token,
165
+ :password => 'new_password',
166
+ :password_confirmation => 'new_password'
167
+ )
168
+ user.reload
169
+
170
+ assert user.valid_password?('new_password')
171
+ end
172
+ end
@@ -0,0 +1,35 @@
1
+ require 'test/test_helper'
2
+
3
+ class Invitable < User
4
+ devise :database_authenticatable, :invitable, :invite_for => 5.days
5
+ end
6
+
7
+ class ActiveRecordTest < ActiveSupport::TestCase
8
+ def include_module?(klass, mod)
9
+ klass.devise_modules.include?(mod) &&
10
+ klass.included_modules.include?(Devise::Models::const_get(mod.to_s.classify))
11
+ end
12
+
13
+ def assert_include_modules(klass, *modules)
14
+ modules.each do |mod|
15
+ assert include_module?(klass, mod), "#{klass} not include #{mod}"
16
+ end
17
+
18
+ (Devise::ALL - modules).each do |mod|
19
+ assert_not include_module?(klass, mod), "#{klass} include #{mod}"
20
+ end
21
+ end
22
+
23
+ test 'add invitable module only' do
24
+ assert_include_modules Invitable, :database_authenticatable, :invitable
25
+ end
26
+
27
+ test 'set a default value for invite_for' do
28
+ assert_equal 5.days, Invitable.invite_for
29
+ end
30
+
31
+ test 'invitable attributes' do
32
+ assert_not_nil Invitable.columns_hash['invitation_token']
33
+ assert_not_nil Invitable.columns_hash['invitation_sent_at']
34
+ end
35
+ end
@@ -0,0 +1,6 @@
1
+ class AdminsController < ApplicationController
2
+ before_filter :authenticate_admin!
3
+
4
+ def index
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ # Filters added to this controller apply to all controllers in the application.
2
+ # Likewise, all the methods added will be available for all controllers.
3
+
4
+ class ApplicationController < ActionController::Base
5
+ helper :all # include all helpers, all the time
6
+ protect_from_forgery # See ActionController::RequestForgeryProtection for details
7
+
8
+ # Scrub sensitive parameters from your log
9
+ filter_parameter_logging :password
10
+ end
@@ -0,0 +1,4 @@
1
+ class HomeController < ApplicationController
2
+ def index
3
+ end
4
+ end
@@ -0,0 +1,12 @@
1
+ class UsersController < ApplicationController
2
+ before_filter :authenticate_user!
3
+
4
+ def index
5
+ user_session[:cart] = "Cart"
6
+ end
7
+
8
+ def expire
9
+ user_session['last_request_at'] = 31.minutes.ago.utc
10
+ render :text => 'User will be expired on next request'
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ # Methods added to this helper will be available to all templates in the application.
2
+ module ApplicationHelper
3
+ end
@@ -0,0 +1,4 @@
1
+ class User < ActiveRecord::Base
2
+ devise :database_authenticatable, :confirmable, :recoverable, :rememberable, :validatable
3
+ attr_accessible :username, :email, :password, :password_confirmation
4
+ end
@@ -0,0 +1,110 @@
1
+ # Don't change this file!
2
+ # Configure your app in config/environment.rb and config/environments/*.rb
3
+
4
+ RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5
+
6
+ module Rails
7
+ class << self
8
+ def boot!
9
+ unless booted?
10
+ preinitialize
11
+ pick_boot.run
12
+ end
13
+ end
14
+
15
+ def booted?
16
+ defined? Rails::Initializer
17
+ end
18
+
19
+ def pick_boot
20
+ (vendor_rails? ? VendorBoot : GemBoot).new
21
+ end
22
+
23
+ def vendor_rails?
24
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
25
+ end
26
+
27
+ def preinitialize
28
+ load(preinitializer_path) if File.exist?(preinitializer_path)
29
+ end
30
+
31
+ def preinitializer_path
32
+ "#{RAILS_ROOT}/config/preinitializer.rb"
33
+ end
34
+ end
35
+
36
+ class Boot
37
+ def run
38
+ load_initializer
39
+ Rails::Initializer.run(:set_load_path)
40
+ end
41
+ end
42
+
43
+ class VendorBoot < Boot
44
+ def load_initializer
45
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46
+ Rails::Initializer.run(:install_gem_spec_stubs)
47
+ Rails::GemDependency.add_frozen_gem_path
48
+ end
49
+ end
50
+
51
+ class GemBoot < Boot
52
+ def load_initializer
53
+ self.class.load_rubygems
54
+ load_rails_gem
55
+ require 'initializer'
56
+ end
57
+
58
+ def load_rails_gem
59
+ if version = self.class.gem_version
60
+ gem 'rails', version
61
+ else
62
+ gem 'rails'
63
+ end
64
+ rescue Gem::LoadError => load_error
65
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
66
+ exit 1
67
+ end
68
+
69
+ class << self
70
+ def rubygems_version
71
+ Gem::RubyGemsVersion rescue nil
72
+ end
73
+
74
+ def gem_version
75
+ if defined? RAILS_GEM_VERSION
76
+ RAILS_GEM_VERSION
77
+ elsif ENV.include?('RAILS_GEM_VERSION')
78
+ ENV['RAILS_GEM_VERSION']
79
+ else
80
+ parse_gem_version(read_environment_rb)
81
+ end
82
+ end
83
+
84
+ def load_rubygems
85
+ min_version = '1.3.2'
86
+ require 'rubygems'
87
+ unless rubygems_version >= min_version
88
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
89
+ exit 1
90
+ end
91
+
92
+ rescue LoadError
93
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
94
+ exit 1
95
+ end
96
+
97
+ def parse_gem_version(text)
98
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
99
+ end
100
+
101
+ private
102
+ def read_environment_rb
103
+ File.read("#{RAILS_ROOT}/config/environment.rb")
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ # All that for this:
110
+ Rails.boot!
@@ -0,0 +1,22 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3-ruby (not necessary on OS X Leopard)
3
+ development:
4
+ adapter: sqlite3
5
+ database: ":memory:"
6
+ pool: 5
7
+ timeout: 5000
8
+
9
+ # Warning: The database defined as "test" will be erased and
10
+ # re-generated from your development database when you run "rake".
11
+ # Do not set this db to the same as development or production.
12
+ test:
13
+ adapter: sqlite3
14
+ database: ":memory:"
15
+ pool: 5
16
+ timeout: 5000
17
+
18
+ production:
19
+ adapter: sqlite3
20
+ database: db/production.sqlite3
21
+ pool: 5
22
+ timeout: 5000
@@ -0,0 +1,44 @@
1
+ # Be sure to restart your server when you modify this file
2
+
3
+ # Specifies gem version of Rails to use when vendor/rails is not present
4
+ RAILS_GEM_VERSION = '~> 2.3.4' unless defined? RAILS_GEM_VERSION
5
+
6
+ # Bootstrap the Rails environment, frameworks, and default configuration
7
+ require File.join(File.dirname(__FILE__), 'boot')
8
+
9
+ Rails::Initializer.run do |config|
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Add additional load paths for your own custom dirs
15
+ # config.load_paths += %W( #{RAILS_ROOT}/extras )
16
+
17
+ # Specify gems that this application depends on and have them installed with rake gems:install
18
+ # config.gem "bj"
19
+ # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
20
+ # config.gem "sqlite3-ruby", :lib => "sqlite3"
21
+ # config.gem "aws-s3", :lib => "aws/s3"
22
+ # config.gem 'devise'
23
+
24
+ # Only load the plugins named here, in the order given (default is alphabetical).
25
+ # :all can be used as a placeholder for all plugins not explicitly named
26
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
27
+ #config.plugin_paths += %W( #{RAILS_ROOT}/../../.. )
28
+ #config.plugins = [:devise_invitable]
29
+
30
+ # Skip frameworks you're not going to use. To use Rails without a database,
31
+ # you must remove the Active Record framework.
32
+ # config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
33
+
34
+ # Activate observers that should always be running
35
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
36
+
37
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
38
+ # Run "rake -D time" for a list of tasks for finding time zone names.
39
+ config.time_zone = 'UTC'
40
+
41
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
42
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
43
+ # config.i18n.default_locale = :en
44
+ end