josevalim-auth_helpers 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ module AuthHelpers
2
+ module Model
3
+
4
+ # Hacks into update attributes to dael with email, email confirmation,
5
+ # password and password confirmation. If the e-mail changes, it resends
6
+ # the confirmation instructions if the confirmable module is also included.
7
+ #
8
+ module Updatable
9
+ def self.included(base)
10
+ base.send :attr_accessor, :email_confirmation, :password_confirmation
11
+ base.send :attr_accessible, :email, :email_confirmation, :password, :password_confirmation
12
+ base.send :validates_confirmation_of, :email, :password
13
+ end
14
+
15
+ def update_attributes(options)
16
+ options.delete(:email) if options[:email] == self.email
17
+ options.delete(:email_confirmation) if options[:email_confirmation].blank?
18
+
19
+ options.delete(:password) if options[:password].blank? || self.valid_password?(options[:password])
20
+ options.delete(:password_confirmation) if options[:password_confirmation].blank?
21
+
22
+ # Force confirmations (if confirmation is nil, it won't validate, it has to be at least blank)
23
+ options[:email_confirmation] ||= '' if options[:email]
24
+ options[:password_confirmation] ||= '' if options[:password]
25
+
26
+ if super(options)
27
+ if options[:email] && self.respond_to?(:send_confirmation_instructions)
28
+ self.send_confirmation_instructions(:update)
29
+ end
30
+
31
+ return true
32
+ end
33
+
34
+ return false
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -16,39 +16,40 @@ module AuthHelpers
16
16
  class Notifier < ActionMailer::Base
17
17
  class << self; attr_accessor :sender, :content_type end
18
18
 
19
- def new_account(record)
20
- @subject = I18n.t 'actionmailer.auth_helpers.new_account', :default => 'New account'
21
- set_ivars!(:confirmable, record)
19
+ def create_confirmation(record)
20
+ @subject = I18n.t 'actionmailer.auth_helpers.create_confirmation', :default => 'Create confirmation'
21
+ set_ivars!(record)
22
22
  end
23
23
 
24
- def email_changed(record)
25
- @subject = I18n.t 'actionmailer.auth_helpers.email_changed', :default => 'You changed your e-mail'
26
- set_ivars!(:confirmable, record)
24
+ def update_confirmation(record)
25
+ @subject = I18n.t 'actionmailer.auth_helpers.update_confirmation', :default => 'Update e-mail confirmation'
26
+ set_ivars!(record)
27
27
  end
28
28
 
29
29
  def reset_password(record)
30
30
  @subject = I18n.t 'actionmailer.auth_helpers.reset_password', :default => 'Reset password'
31
- set_ivars!(:recoverable, record)
31
+ set_ivars!(record)
32
32
  end
33
33
 
34
- def confirmation_code(record)
35
- @subject = I18n.t 'actionmailer.auth_helpers.confirmation_code', :default => 'Confirmation code'
36
- set_ivars!(:confirmable, record)
34
+ def resend_confirmation(record)
35
+ @subject = I18n.t 'actionmailer.auth_helpers.resend_confirmation', :default => 'Confirmation code'
36
+ set_ivars!(record)
37
37
  end
38
38
 
39
39
  protected
40
40
 
41
- def set_ivars!(assign, record)
42
- @from = self.class.sender
43
- @content_type = self.class.content_type
44
- @body[assign] = record
45
- @recipients = record.email
46
- @sent_on = Time.now.utc
47
- @headers = {}
41
+ def set_ivars!(record)
42
+ @from = self.class.sender
43
+ @content_type = self.class.content_type
44
+ @recipients = record.email
45
+ @sent_on = Time.now.utc
46
+ @headers = {}
47
+ @body[:record] = record
48
+ @body[record.class.name.downcase] = record
48
49
  end
49
50
 
50
51
  end
51
52
  end
52
53
 
53
- AuthHelpers::Notifier.content_type = 'text/html'
54
- AuthHelpers::Notifier.template_root = File.join(File.dirname(__FILE__), '..', '..', 'views')
54
+ AuthHelpers::Notifier.content_type ||= 'text/html'
55
+ AuthHelpers::Notifier.template_root ||= File.join(File.dirname(__FILE__), '..', '..', 'views')
@@ -10,14 +10,7 @@ module AuthHelpers
10
10
  @confirmable = base.described_class.create!(@valid_attributes)
11
11
  end
12
12
 
13
- it { should_not allow_mass_assignment_of(:confirmation_code) }
14
-
15
- it 'should remove confirmation code' do
16
- @confirmable.confirm!
17
- @confirmable.confirmation_code.should be_nil
18
- end
19
-
20
- it 'should set the date account was confirmed' do
13
+ it 'should set the confirmation date on #confirm!' do
21
14
  @confirmable.confirmed_at.should be_nil
22
15
  @confirmable.confirm!
23
16
  @confirmable.confirmed_at.should_not be_nil
@@ -32,62 +25,63 @@ module AuthHelpers
32
25
  end
33
26
 
34
27
  describe 'on create' do
35
- it "should set confirmation_code" do
36
- @confirmable.confirmation_code.length.should == 40
28
+ it "should set confirmed_at to nil" do
29
+ @confirmable.confirmed_at.should be_nil
37
30
  end
38
31
 
39
32
  it "should set confirmation_sent_at" do
40
33
  @confirmable.confirmation_sent_at.should_not be_blank
41
34
  end
42
35
 
43
- it "should send a new account notification" do
36
+ it "should send create confirmation notification" do
44
37
  ActionMailer::Base.deliveries.length.should == 1
45
38
  end
46
39
  end
47
40
 
48
- describe 'with a valid confirmation code' do
41
+ describe 'with a valid perishable token' do
49
42
  it "should confirm his account" do
50
- record = base.described_class.find_and_confirm(@confirmable.confirmation_code)
43
+ record = base.described_class.find_and_confirm(:perishable_token => @confirmable.perishable_token)
51
44
  record.errors.should be_empty
52
45
  end
53
46
 
54
- it "should clean confirmation code" do
55
- base.described_class.find_and_confirm(@confirmable.confirmation_code)
47
+ it "should set confirmation date" do
48
+ record = base.described_class.find_and_confirm(:perishable_token => @confirmable.perishable_token)
56
49
  @confirmable.reload
57
- @confirmable.confirmation_code.should be_nil
58
- end
59
-
60
- it "should set confirmed_at date" do
61
- record = base.described_class.find_and_confirm(@confirmable.confirmation_code)
62
- record.confirmed_at.should_not be_nil
50
+ @confirmable.confirmed_at.should_not be_nil
63
51
  end
64
52
  end
65
53
 
66
- describe 'with an invalid confirmation code' do
54
+ describe 'with an invalid perishable token' do
67
55
  it "should set an error message" do
68
- record = base.described_class.find_and_confirm('invalid_code')
69
- record.errors.on(:confirmation_code).should == record.errors.generate_message(:confirmation_code, :invalid)
56
+ record = base.described_class.find_and_confirm(:perishable_token => "invalid token")
57
+ record.errors.on(:perishable_token).should == record.errors.generate_message(:perishable_token, :invalid_confirmation, :default => :invalid)
58
+ end
59
+
60
+ it "should return a new record with the perishable token set" do
61
+ record = base.described_class.find_and_confirm(:perishable_token => "invalid token")
62
+ record.should be_new_record
63
+ record.perishable_token.should == "invalid token"
70
64
  end
71
65
  end
72
66
 
73
67
  describe 'when lost confirmation code' do
74
68
  before(:each){ ActionMailer::Base.deliveries = [] }
75
69
 
76
- it "should resend confirmation code if account is not confirmed" do
77
- record = base.described_class.find_and_resend_confirmation_code(:email => @confirmable.email)
70
+ it "should resend confirmation instructions if account is not confirmed" do
71
+ record = base.described_class.find_and_resend_confirmation_instructions(:email => @confirmable.email)
78
72
  record.errors.should be_empty
79
73
  ActionMailer::Base.deliveries.length.should == 1
80
74
  end
81
75
 
82
- it "should not resend confirmation code if account is confirmed" do
76
+ it "should not resend confirmation instructions if account is confirmed" do
83
77
  @confirmable.confirm!
84
- record = base.described_class.find_and_resend_confirmation_code(:email => @confirmable.email)
78
+ record = base.described_class.find_and_resend_confirmation_instructions(:email => @confirmable.email)
85
79
  record.errors.on(:email).should == record.errors.generate_message(:email, :already_confirmed)
86
80
  ActionMailer::Base.deliveries.length.should == 0
87
81
  end
88
82
 
89
- it "should show a error message on resend confirmation code if e-mail is not valid" do
90
- record = base.described_class.find_and_resend_confirmation_code(:email => 'invalid')
83
+ it "should show a error message on resend confirmation instructions if e-mail is not valid" do
84
+ record = base.described_class.find_and_resend_confirmation_instructions(:email => 'invalid')
91
85
  record.errors.on(:email).should == record.errors.generate_message(:email, :not_found)
92
86
  ActionMailer::Base.deliveries.length.should == 0
93
87
  end
@@ -6,33 +6,32 @@ module AuthHelpers
6
6
  def self.included(base)
7
7
  base.class_eval do
8
8
  before(:each) do
9
- @member = OpenStruct.new(:email => 'recipient@email.com',
10
- :confirmation_code => '0123456789',
11
- :reset_password_code => 'abcdefghij')
9
+ @record = OpenStruct.new(:email => 'recipient@email.com',
10
+ :perishable_token => '0123456789')
12
11
  end
13
12
 
14
13
  it "should deliver new account notification" do
15
- email = ::AuthHelpers::Notifier.create_new_account(@member)
14
+ email = ::AuthHelpers::Notifier.create_create_confirmation(@record)
16
15
  email.to.should == [ 'recipient@email.com' ]
17
- email.body.should match(/#{@member.confirmation_code}/)
16
+ email.body.should match(/#{@record.perishable_token}/)
18
17
  end
19
18
 
20
19
  it "should deliver email changed notification" do
21
- email = ::AuthHelpers::Notifier.create_email_changed(@member)
20
+ email = ::AuthHelpers::Notifier.create_update_confirmation(@record)
22
21
  email.to.should == [ 'recipient@email.com' ]
23
- email.body.should match(/#{@member.confirmation_code}/)
22
+ email.body.should match(/#{@record.perishable_token}/)
24
23
  end
25
24
 
26
25
  it "should deliver reset password code" do
27
- email = ::AuthHelpers::Notifier.create_reset_password(@member)
26
+ email = ::AuthHelpers::Notifier.create_reset_password(@record)
28
27
  email.to.should == [ 'recipient@email.com' ]
29
- email.body.should match(/#{@member.reset_password_code}/)
28
+ email.body.should match(/#{@record.perishable_token}/)
30
29
  end
31
30
 
32
31
  it "should resend confirmation code" do
33
- email = ::AuthHelpers::Notifier.create_confirmation_code(@member)
32
+ email = ::AuthHelpers::Notifier.create_resend_confirmation(@record)
34
33
  email.to.should == [ 'recipient@email.com' ]
35
- email.body.should match(/#{@member.confirmation_code}/)
34
+ email.body.should match(/#{@record.perishable_token}/)
36
35
  end
37
36
  end
38
37
  end
@@ -10,48 +10,41 @@ module AuthHelpers
10
10
  ActionMailer::Base.deliveries = []
11
11
  end
12
12
 
13
- it "should send a reset password code to the user" do
14
- record = base.described_class.find_and_send_reset_password_code(:email => @recoverable.email)
13
+ it "should send a reset password instructions to the user" do
14
+ record = base.described_class.find_and_send_reset_password_instructions(:email => @recoverable.email)
15
15
  record.errors.should be_empty
16
- record.reset_password_code.should_not be_blank
17
- ActionMailer::Base.deliveries.length.should == 1
16
+ ActionMailer::Base.deliveries.length.should == 1
18
17
  end
19
18
 
20
19
  describe 'and reset password code is sent' do
21
20
  before(:each) do
22
- base.described_class.find_and_send_reset_password_code(:email => @recoverable.email)
21
+ base.described_class.find_and_send_reset_password_instructions(:email => @recoverable.email)
23
22
  @recoverable.reload
24
23
  end
25
24
 
26
25
  it "should reset password if reset password code is valid" do
27
- record = base.described_class.find_and_reset_password(:reset_password_code => @recoverable.reset_password_code, :password => '654321', :password_confirmation => '654321')
28
- record.errors.should be_empty
29
-
30
- record = base.described_class.find_and_authenticate(:email => @recoverable.email, :password => '654321')
31
- record.errors.should be_empty
32
- end
33
-
34
- it "should not reset password if the given reset password code is invalid" do
35
- record = base.described_class.find_and_reset_password(:reset_password_code => 'invalid_pass_code', :password => '654321', :password_confirmation => '654321')
36
- record.errors.on(:reset_password_code).should == record.errors.generate_message(:reset_password_code, :invalid)
37
- record = base.described_class.find_and_authenticate(:email => @recoverable.email, :password => '654321')
38
- record.errors.should_not be_empty
39
- end
40
-
41
- it "should not reset password if password doesn't match confirmation" do
42
- record = base.described_class.find_and_reset_password(:reset_password_code => @recoverable.reset_password_code, :password => '654321', :password_confirmation => '123456')
43
- record.errors.on(:password).should == record.errors.generate_message(:password, :confirmation)
44
-
45
- record = base.described_class.find_and_authenticate(:email => @recoverable.email, :password => '654321')
46
- record.errors.should_not be_empty
47
- end
48
-
49
- it "should clean reset_password_code when password is successfully reset" do
50
- base.described_class.find_and_reset_password(:reset_password_code => @recoverable.reset_password_code, :password => '654321', :password_confirmation => '654321')
51
- @recoverable.reload
52
- @recoverable.reset_password_code.should be_nil
26
+ record = base.described_class.find_and_reset_password(:perishable_token => @recoverable.perishable_token, :password => '654321', :password_confirmation => '654321')
27
+ record.errors.should be_empty
28
+
29
+ @recoverable.reload
30
+ @recoverable.valid_password?('654321').should be_true
31
+ end
32
+
33
+ it "should not reset password if the given reset password code is invalid" do
34
+ record = base.described_class.find_and_reset_password(:perishable_token => 'invalid_token', :password => '654321', :password_confirmation => '654321')
35
+ record.errors.on(:perishable_token).should == record.errors.generate_message(:perishable_token, :invalid_reset_password, :default => :invalid)
36
+ @recoverable.reload
37
+ @recoverable.valid_password?('654321').should be_false
53
38
  end
54
- end
39
+
40
+ it "should not reset password if password doesn't match confirmation" do
41
+ record = base.described_class.find_and_reset_password(:perishable_token => @recoverable.perishable_token, :password => '654321', :password_confirmation => '123456')
42
+ record.errors.on(:password).should == record.errors.generate_message(:password, :confirmation)
43
+
44
+ @recoverable.reload
45
+ @recoverable.valid_password?('654321').should be_false
46
+ end
47
+ end
55
48
  end
56
49
  end
57
50
  end
@@ -0,0 +1,37 @@
1
+ module AuthHelpers
2
+ module Spec
3
+
4
+ module Updatable
5
+ def self.included(base)
6
+ base.class_eval do
7
+ describe "on update" do
8
+ before(:each) do
9
+ @authenticable = base.described_class.create!(@valid_attributes)
10
+ ActionMailer::Base.deliveries = []
11
+ end
12
+
13
+ it "should ignore e-mail confirmation if e-mail has not changed" do
14
+ attributes = { :email => @authenticable.email, :email_confirmation => '' }
15
+ @authenticable.update_attributes(attributes).should be_true
16
+ end
17
+
18
+ it "should ignore password confirmation if password has not changed" do
19
+ attributes = { :password => @valid_attributes[:password], :password_confirmation => '' }
20
+ @authenticable.update_attributes(attributes).should be_true
21
+ end
22
+
23
+ if base.described_class.new.respond_to?(:set_confirmation_code, true)
24
+ it "should send an e-mail if e-mail changes" do
25
+ attributes = { :email => @valid_attributes[:email].to_s.next, :email_confirmation => @valid_attributes[:email].to_s.next }
26
+ @authenticable.update_attributes(attributes).should be_true
27
+ @authenticable.email.should == @valid_attributes[:email].to_s.next
28
+ ActionMailer::Base.deliveries.length.should == 1
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1 @@
1
+ Welcome! Your confirmation code is <%= @record.perishable_token %>.
@@ -0,0 +1 @@
1
+ Your confirmation code is <%= @record.perishable_token %>.
@@ -1 +1 @@
1
- Your reset password code is <%= @recoverable.reset_password_code %>.
1
+ Your reset password code is <%= @record.perishable_token %>.
@@ -0,0 +1 @@
1
+ Your new confirmation code is <%= @record.perishable_token %>.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: josevalim-auth_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jos\xC3\xA9 Valim"
@@ -13,16 +13,16 @@ date: 2009-04-24 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: remarkable_rails
16
+ name: authlogic
17
17
  type: :runtime
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 3.0.7
23
+ version: "0"
24
24
  version:
25
- description: AuthHelpers is a collection of modules to include in your model to deal with authentication.
25
+ description: AuthHelpers is a collection of modules to improve your Authlogic models.
26
26
  email: jose.valim@gmail.com
27
27
  executables: []
28
28
 
@@ -36,25 +36,19 @@ files:
36
36
  - README
37
37
  - init.rb
38
38
  - lib/auth_helpers.rb
39
- - lib/auth_helpers/migration.rb
40
39
  - lib/auth_helpers/notifier.rb
41
40
  - lib/auth_helpers/model/associatable.rb
42
- - lib/auth_helpers/model/authenticable.rb
43
41
  - lib/auth_helpers/model/confirmable.rb
44
42
  - lib/auth_helpers/model/recoverable.rb
45
- - lib/auth_helpers/model/rememberable.rb
46
- - lib/auth_helpers/model/validatable.rb
47
- - lib/auth_helpers/spec/associatable.rb
48
- - lib/auth_helpers/spec/authenticable.rb
43
+ - lib/auth_helpers/model/updatable.rb
49
44
  - lib/auth_helpers/spec/confirmable.rb
50
45
  - lib/auth_helpers/spec/notifier.rb
51
46
  - lib/auth_helpers/spec/recoverable.rb
52
- - lib/auth_helpers/spec/rememberable.rb
53
- - lib/auth_helpers/spec/validatable.rb
54
- - views/auth_helpers/notifier/confirmation_code.erb
55
- - views/auth_helpers/notifier/email_changed.erb
56
- - views/auth_helpers/notifier/new_account.erb
47
+ - lib/auth_helpers/spec/updatable.rb
48
+ - views/auth_helpers/notifier/create_confirmation.erb
49
+ - views/auth_helpers/notifier/resend_confirmation.erb
57
50
  - views/auth_helpers/notifier/reset_password.erb
51
+ - views/auth_helpers/notifier/update_confirmation.erb
58
52
  has_rdoc: true
59
53
  homepage: http://github.com/josevalim/auth_helpers
60
54
  post_install_message:
@@ -81,6 +75,6 @@ rubyforge_project:
81
75
  rubygems_version: 1.2.0
82
76
  signing_key:
83
77
  specification_version: 2
84
- summary: AuthHelpers is a collection of modules to include in your model to deal with authentication.
78
+ summary: AuthHelpers is a collection of modules to improve your Authlogic models.
85
79
  test_files: []
86
80
 
@@ -1,56 +0,0 @@
1
- module AuthHelpers
2
- # Helpers to migration:
3
- #
4
- # create_table :accounts do |t|
5
- # t.extend AuthHelpers::Migration
6
- #
7
- # t.authenticable
8
- # t.confirmable
9
- # t.recoverable
10
- # t.rememberable
11
- # t.timestamps
12
- # end
13
- #
14
- # However this method does not add indexes. If you need them, here is the declaration:
15
- #
16
- # add_index "accounts", ["email"], :name => "email", :unique => true
17
- # add_index "accounts", ["token"], :name => "token", :unique => true
18
- # add_index "accounts", ["confirmation_code"], :name => "confirmation_code", :unique => true
19
- # add_index "accounts", ["reset_password_code"], :name => "reset_password_code", :unique => true
20
- #
21
- # E-mail index should be slightly changed with you are working with polymorphic
22
- # associations.
23
- #
24
- module Migration
25
-
26
- # Creates email, hashed_password and salt.
27
- #
28
- def authenticable
29
- self.string :email, :limit => 100, :null => false, :default => ''
30
- self.string :hashed_password, :limit => 40, :null => false, :default => ''
31
- self.string :salt, :limit => 10, :null => false, :default => ''
32
- end
33
-
34
- # Creates confirmation_code, confirmed_at and confirmation_sent_at.
35
- #
36
- def confirmable
37
- self.string :confirmation_code, :limit => 40, :null => true
38
- self.datetime :confirmed_at
39
- self.datetime :confirmation_sent_at
40
- end
41
-
42
- # Creates reset_password_code.
43
- #
44
- def recoverable
45
- self.string :reset_password_code, :limit => 40, :null => true
46
- end
47
-
48
- # Creates token and token_created_at.
49
- #
50
- def rememberable
51
- self.string :token, :limit => 40, :null => true
52
- self.datetime :token_expires_at
53
- end
54
-
55
- end
56
- end