devise_certifiable 0.1.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.
Files changed (65) hide show
  1. data/.gitignore +3 -0
  2. data/Gemfile +10 -0
  3. data/LICENSE +19 -0
  4. data/README.rdoc +53 -0
  5. data/Rakefile +26 -0
  6. data/app/controllers/devise/certifications_controller.rb +34 -0
  7. data/app/views/devise/certifications/edit.html.erb +14 -0
  8. data/app/views/devise/mailer/certification_request.html.erb +8 -0
  9. data/config/locales/en.yml +17 -0
  10. data/devise_certifiable.gemspec +25 -0
  11. data/lib/devise_certifiable.rb +13 -0
  12. data/lib/devise_certifiable/controllers/helpers.rb +7 -0
  13. data/lib/devise_certifiable/controllers/url_helpers.rb +24 -0
  14. data/lib/devise_certifiable/mailer.rb +10 -0
  15. data/lib/devise_certifiable/model.rb +98 -0
  16. data/lib/devise_certifiable/rails.rb +12 -0
  17. data/lib/devise_certifiable/routes.rb +10 -0
  18. data/lib/devise_certifiable/schema.rb +12 -0
  19. data/lib/devise_certifiable/version.rb +3 -0
  20. data/lib/generators/active_record/devise_certifiable_generator.rb +13 -0
  21. data/lib/generators/active_record/templates/migration.rb +18 -0
  22. data/lib/generators/devise_certifiable/devise_certifiable_generator.rb +16 -0
  23. data/lib/generators/devise_certifiable/install_generator.rb +11 -0
  24. data/test/controllers/url_helpers_test.rb +34 -0
  25. data/test/generators/active_record_generator_test.rb +24 -0
  26. data/test/generators/devise_certifiable_generator_test.rb +27 -0
  27. data/test/generators/install_generator_test.rb +15 -0
  28. data/test/integration/certification_test.rb +80 -0
  29. data/test/mailers/certification_request_test.rb +63 -0
  30. data/test/models/certifiable_test.rb +172 -0
  31. data/test/models_test.rb +26 -0
  32. data/test/orm/active_record.rb +4 -0
  33. data/test/rails_app/Rakefile +10 -0
  34. data/test/rails_app/app/active_record/user.rb +7 -0
  35. data/test/rails_app/app/controllers/application_controller.rb +3 -0
  36. data/test/rails_app/app/controllers/home_controller.rb +4 -0
  37. data/test/rails_app/app/controllers/users_controller.rb +12 -0
  38. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  39. data/test/rails_app/app/models/monster.rb +5 -0
  40. data/test/rails_app/app/views/home/index.html.erb +1 -0
  41. data/test/rails_app/app/views/layouts/application.html.erb +20 -0
  42. data/test/rails_app/app/views/users/index.html.erb +1 -0
  43. data/test/rails_app/config.ru +4 -0
  44. data/test/rails_app/config/application.rb +29 -0
  45. data/test/rails_app/config/boot.rb +11 -0
  46. data/test/rails_app/config/database.yml +18 -0
  47. data/test/rails_app/config/environment.rb +5 -0
  48. data/test/rails_app/config/environments/development.rb +22 -0
  49. data/test/rails_app/config/environments/production.rb +33 -0
  50. data/test/rails_app/config/environments/test.rb +33 -0
  51. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  52. data/test/rails_app/config/initializers/devise.rb +179 -0
  53. data/test/rails_app/config/initializers/inflections.rb +2 -0
  54. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  55. data/test/rails_app/config/routes.rb +6 -0
  56. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +17 -0
  57. data/test/rails_app/lib/shared_user.rb +7 -0
  58. data/test/rails_app/script/rails +6 -0
  59. data/test/routes_test.rb +11 -0
  60. data/test/support/assertions.rb +24 -0
  61. data/test/support/helpers.rb +39 -0
  62. data/test/support/integration.rb +59 -0
  63. data/test/support/locale/en.yml +4 -0
  64. data/test/test_helper.rb +20 -0
  65. metadata +203 -0
@@ -0,0 +1,16 @@
1
+ module DeviseCertifiable
2
+ module Generators
3
+ class DeviseCertifiableGenerator < Rails::Generators::NamedBase
4
+ namespace "devise_certifiable"
5
+
6
+ desc "Add :certifiable directive in the given model and generate migration for ActiveRecord"
7
+
8
+ def inject_devise_certifiable_content
9
+ path = File.join("app", "models", "#{file_path}.rb")
10
+ inject_into_file(path, "certifiable, :", :after => "devise :") if File.exists?(path)
11
+ end
12
+
13
+ hook_for :orm
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ module DeviseCertifiable
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+
6
+ def copy_locale
7
+ copy_file "../../../config/locales/en.yml", "config/locales/devise_certifiable.en.yml"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+
3
+ class RoutesTest < ActionController::TestCase
4
+ tests ApplicationController
5
+
6
+ def assert_path_and_url(name, prepend_path=nil)
7
+ @request.path = '/users/session'
8
+ prepend_path = "#{prepend_path}_" if prepend_path
9
+
10
+ # Resource param
11
+ assert_equal @controller.send(:"#{prepend_path}#{name}_path", :user),
12
+ send(:"#{prepend_path}user_#{name}_path")
13
+ assert_equal @controller.send(:"#{prepend_path}#{name}_url", :user),
14
+ send(:"#{prepend_path}user_#{name}_url")
15
+
16
+ # Default url params
17
+ assert_equal @controller.send(:"#{prepend_path}#{name}_path", :user, :param => 123),
18
+ send(:"#{prepend_path}user_#{name}_path", :param => 123)
19
+ assert_equal @controller.send(:"#{prepend_path}#{name}_url", :user, :param => 123),
20
+ send(:"#{prepend_path}user_#{name}_url", :param => 123)
21
+
22
+ @request.path = nil
23
+ # With an object
24
+ assert_equal @controller.send(:"#{prepend_path}#{name}_path", User.new),
25
+ send(:"#{prepend_path}user_#{name}_path")
26
+ assert_equal @controller.send(:"#{prepend_path}#{name}_url", User.new),
27
+ send(:"#{prepend_path}user_#{name}_url")
28
+ end
29
+
30
+ test 'should alias certification to mapped user certification' do
31
+ assert_path_and_url :certification
32
+ assert_path_and_url :certification, :edit
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ require "test_helper"
2
+ require "rails/generators/test_case"
3
+
4
+ if DEVISE_ORM == :active_record
5
+ require "generators/active_record/devise_certifiable_generator"
6
+
7
+ class ActiveRecordGeneratorTest < Rails::Generators::TestCase
8
+ tests ActiveRecord::Generators::DeviseCertifiableGenerator
9
+
10
+ destination File.expand_path("../../tmp", __FILE__)
11
+ setup :prepare_destination
12
+
13
+ test "migration is properly created" do
14
+ run_generator %w(monster)
15
+ assert_migration "db/migrate/devise_certifiable_add_to_monsters.rb"
16
+ end
17
+
18
+ test "all files are properly deleted" do
19
+ run_generator %w(monster)
20
+ run_generator %w(monster), :behavior => :revoke
21
+ assert_no_migration "db/migrate/devise_create_monsters.rb"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+ require 'rails/generators'
3
+ require "generators/devise_certifiable/devise_certifiable_generator"
4
+
5
+ class DeviseCertifiableGeneratorTest < Rails::Generators::TestCase
6
+ tests DeviseCertifiable::Generators::DeviseCertifiableGenerator
7
+
8
+ destination File.expand_path("../../tmp", __FILE__)
9
+
10
+ setup do
11
+ prepare_destination
12
+ copy_model
13
+ end
14
+
15
+ test "add :certifiable to model" do
16
+ # run_generator %w(monster)
17
+ # assert_file "app/models/monster.rb", /devise :certifiable/
18
+ flunk 'TODO'
19
+ end
20
+
21
+ def copy_model
22
+ model = File.expand_path("../../rails_app/app/models/monster.rb", __FILE__)
23
+ destination = File.join(destination_root, 'app/models')
24
+ FileUtils.mkdir_p(destination)
25
+ FileUtils.cp model, destination
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ require "test_helper"
2
+ require 'rails/generators'
3
+ require "generators/devise_certifiable/install_generator"
4
+
5
+ class InstallGeneratorTest < Rails::Generators::TestCase
6
+ tests DeviseCertifiable::Generators::InstallGenerator
7
+
8
+ destination File.expand_path("../../tmp", __FILE__)
9
+ setup :prepare_destination
10
+
11
+ test "Assert all files are properly created" do
12
+ run_generator
13
+ assert_file "config/locales/devise_certifiable.en.yml"
14
+ end
15
+ end
@@ -0,0 +1,80 @@
1
+ require 'test_helper'
2
+
3
+ class CertificationTest < ActionController::IntegrationTest
4
+ def teardown
5
+ Capybara.reset_sessions!
6
+ end
7
+
8
+ def visit_certify_with_token(token)
9
+ visit edit_user_certification_path(:certification_token => token)
10
+ end
11
+
12
+ test 'new user need to be certified and confirmed before signing in' do
13
+ admin = create_user
14
+ user = sign_in_as_user
15
+ assert page.has_selector? 'div#flash_alert', :text => 'Your account needs to be certified.'
16
+ user.certify!(admin)
17
+ sign_in_as_user(user)
18
+ assert page.has_selector? 'div#flash_alert', :text => 'You have to confirm your account before continuing.'
19
+ user.confirm!
20
+ sign_in_as_user(user)
21
+ assert page.has_selector? 'div#flash_notice', :text => 'Signed in successfully.'
22
+ assert page.has_content? "Hello User #{user.email}!"
23
+ assert page.has_content? 'Home!'
24
+ end
25
+
26
+ test 'certifier should login before certifying user' do
27
+ user = create_user
28
+ visit_certify_with_token user.certification_token
29
+ assert page.has_selector? 'div#flash_alert', :text => 'You need to sign in or sign up before continuing.'
30
+
31
+ admin = sign_in_as_admin
32
+ visit_certify_with_token user.certification_token
33
+ assert page.has_selector? 'h2', :text => 'User Certification'
34
+ assert page.has_selector? 'label', :text => user.email
35
+ assert page.has_button? 'Certify'
36
+ end
37
+
38
+ test 'complete certifications proces' do
39
+ user = create_user
40
+ admin = sign_in_as_admin
41
+ visit_certify_with_token user.certification_token
42
+ #click_button 'user_submit'
43
+ flunk 'TODO'
44
+ end
45
+
46
+
47
+ test 'invalid certification token should produce an error' do
48
+ admin = sign_in_as_admin
49
+ visit_certify_with_token 'invalid_confirmation'
50
+ assert page.has_selector? 'div#flash_alert', :text => 'The certification token provided is not valid!'
51
+ end
52
+
53
+ test 'new sign up should provide a message and redirect to root url' do
54
+ visit new_user_registration_path
55
+ fill_in 'user_email', :with => 'test1@email.com'
56
+ fill_in 'user_password', :with => '123456'
57
+ fill_in 'user_password_confirmation', :with => '123456'
58
+ click_button 'Sign up'
59
+ # save_and_open_page
60
+
61
+ # assert page.has_selector? 'div#flash_notice', :text => 'You have signed up successfully. However, we could not sign you in because your account is uncertified.'
62
+ # assert_current_url "/"
63
+ # assert_not warden.authenticated(:user)
64
+ # assert page.has_button? 'Sign in'
65
+ flunk 'TODO'
66
+ end
67
+
68
+ test 'already certified user should be presented with a message instead' do
69
+ user = create_user
70
+ user.certified_at = Time.now
71
+ user.save
72
+
73
+ admin = sign_in_as_admin
74
+ visit_certify_with_token user.certification_token
75
+ #save_and_open_page
76
+ #assert page.has_selector? 'div#flash_notice', :text => 'Already certified'
77
+ flunk 'TODO'
78
+
79
+ end
80
+ end
@@ -0,0 +1,63 @@
1
+ require 'test_helper'
2
+
3
+ class CertificationRequestTest < ActionMailer::TestCase
4
+
5
+ def setup
6
+ setup_mailer
7
+ Devise.mailer_sender = 'test@example.com'
8
+ end
9
+
10
+ def user
11
+ @user ||= create_user
12
+ end
13
+
14
+ def mail
15
+ @mail ||= begin
16
+ user
17
+ ActionMailer::Base.deliveries.first
18
+ end
19
+ end
20
+
21
+ test 'email sent after creating the user' do
22
+ assert_not_nil mail
23
+ end
24
+
25
+ test 'content type should be set to html' do
26
+ assert mail.content_type.include?('text/html')
27
+ end
28
+
29
+ test 'send confirmation instructions to the user email' do
30
+ mail
31
+ assert_equal ['test@example.com'], mail.to
32
+ end
33
+
34
+ test 'setup sender from configuration' do
35
+ assert_equal ['test@example.com'], mail.from
36
+ end
37
+
38
+ test 'setup reply to as copy from sender' do
39
+ assert_equal ['test@example.com'], mail.reply_to
40
+ end
41
+
42
+ test 'setup subject from I18n' do
43
+ store_translations :en, :devise => { :mailer => { :certification_request => { :subject => 'New Certification Request' } } } do
44
+ assert_equal 'New Certification Request', mail.subject
45
+ end
46
+ end
47
+
48
+ test 'subject namespaced by model' do
49
+ store_translations :en, :devise => { :mailer => { :certification_request => { :user_subject => 'User Certification Request' } } } do
50
+ assert_equal 'User Certification Request', mail.subject
51
+ end
52
+ end
53
+
54
+ test 'body should have user info' do
55
+ assert_match /#{user.email}/, mail.body.encoded
56
+ end
57
+
58
+ test 'body should have link to certify the account' do
59
+ host = ActionMailer::Base.default_url_options[:host]
60
+ edit_certification_url_regexp = %r{<a href=\"http://#{host}/users/certification/edit\?certification_token=#{user.certification_token}">}
61
+ assert_match edit_certification_url_regexp, mail.body.encoded
62
+ end
63
+ end
@@ -0,0 +1,172 @@
1
+ require 'test_helper'
2
+
3
+ class CertifiableTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ setup_mailer
7
+ end
8
+
9
+ test 'should generate certification token after creating a record' do
10
+ assert_nil new_user.certification_token
11
+ assert_not_nil create_user.certification_token
12
+ end
13
+
14
+ test 'should not generate certification token if already certified' do
15
+ flunk 'TODO'
16
+ end
17
+
18
+ test 'should never generate the same certification token for different users' do
19
+ certification_tokens = []
20
+ 3.times do
21
+ token = create_user.certification_token
22
+ assert !certification_tokens.include?(token)
23
+ certification_tokens << token
24
+ end
25
+ end
26
+
27
+ test 'should certify a user by updating certfied at and certified by' do
28
+ admin = create_user
29
+ user = create_user
30
+ assert_nil user.certified_at
31
+ assert_nil user.certified_by
32
+ assert user.certify!(admin)
33
+ assert_not_nil user.certified_at
34
+ assert_not_nil user.certified_by
35
+ assert_equal user.certified_by, admin
36
+ end
37
+
38
+ test 'should clear certification token while certifiying a user' do
39
+ admin = create_user
40
+ user = create_user
41
+ assert_present user.certification_token
42
+ user.certify!(admin)
43
+ assert_nil user.certification_token
44
+ end
45
+
46
+ test 'should not certify without a certification_authority' do
47
+ user = create_user
48
+ assert_not user.certify!(nil)
49
+ assert_equal "(certification authority) not specified", user.errors[:certified_by].join
50
+
51
+ user2 = create_user
52
+ assert_not user2.certify!('')
53
+ assert_equal "(certification authority) not specified", user2.errors[:certified_by].join
54
+ end
55
+
56
+ test 'should verify whether a user is certified' do
57
+ admin = create_user
58
+ assert_not new_user.certified?
59
+ user = create_user
60
+ assert_not user.certified?
61
+ user.certify!(admin)
62
+ assert user.certified?
63
+ end
64
+
65
+ test 'should not certify a user already certified' do
66
+ admin = create_user
67
+ user = create_user
68
+ assert user.certify!(admin)
69
+ assert_blank user.errors[:email]
70
+ assert_not user.certify!(admin)
71
+ assert_equal "was already certified!", user.errors[:email].join
72
+ end
73
+
74
+ test 'should find a user by token, but not certify it' do
75
+ user = create_user
76
+ certifiable_user = User.find_resource_by_token(user.certification_token)
77
+ assert_equal certifiable_user, user
78
+ assert_not user.reload.certified?
79
+ end
80
+
81
+ test 'should find a user by token and certify it' do
82
+ admin = create_user
83
+ user = create_user
84
+ certifiable_user = User.certify_by_token(user.certification_token, admin)
85
+ assert_equal certifiable_user, user
86
+ assert user.reload.certified?
87
+ end
88
+
89
+ test 'should return a new record with errors when a invalid token is given' do
90
+ certifiable_user = User.find_resource_by_token('invalid_certification_token')
91
+ assert_not certifiable_user.persisted?
92
+ assert_equal "is invalid", certifiable_user.errors[:certification_token].join
93
+ end
94
+
95
+ test 'should return a new record with errors when a blank or nil token is given' do
96
+ certifiable_user = User.find_resource_by_token('')
97
+ assert_not certifiable_user.persisted?
98
+ assert_equal "can't be blank", certifiable_user.errors[:certification_token].join
99
+ certifiable_user = User.find_resource_by_token(nil)
100
+ assert_not certifiable_user.persisted?
101
+ assert_equal "can't be blank", certifiable_user.errors[:certification_token].join
102
+ end
103
+
104
+ test 'should send certification instructions by email only after creating user' do
105
+ assert_email_sent do
106
+ create_user
107
+ end
108
+ end
109
+
110
+ test 'should not send certification instructions by email before creating user' do
111
+ assert_email_not_sent do
112
+ new_user
113
+ end
114
+ end
115
+
116
+ test 'should not send certification when trying to save an invalid user' do
117
+ # assert_email_not_sent do
118
+ # user = new_user
119
+ # user.stubs(:valid?).returns(false)
120
+ # user.save
121
+ # end
122
+ flunk 'TODO'
123
+ end
124
+
125
+ test 'should always have certification token when email is sent' do
126
+ user = new_user
127
+ user.save
128
+ user.request_certification
129
+ assert_not_nil user.reload.certification_token
130
+ end
131
+
132
+ test 'should automatically certify new invited user with inviter' do
133
+ # admin = create_user
134
+ # user = User.invite!({ :username => "usertest", :email => generate_unique_email}, admin)
135
+ # assert_nil user.reload.certification_token
136
+ # assert_not_nil user.certified_at
137
+ # assert_equal user.certified_by, admin
138
+ flunk 'TODO'
139
+ end
140
+
141
+ test 'should not send instructions if the user is already certified' do
142
+ admin = create_user
143
+ user = create_user
144
+ user.certify!(admin)
145
+ assert_email_not_sent do
146
+ user.certify!(admin)
147
+ end
148
+ end
149
+
150
+ test 'should not send confirmation instructions before being certified' do
151
+ admin = create_user
152
+ assert_equal 1, ActionMailer::Base.deliveries.size
153
+ user = create_user
154
+ assert_equal 2, ActionMailer::Base.deliveries.size
155
+ assert_equal 'Certification request', ActionMailer::Base.deliveries.last.subject
156
+ user.certify!(admin)
157
+ assert_equal 3, ActionMailer::Base.deliveries.size
158
+ assert_equal 'Confirmation instructions', ActionMailer::Base.deliveries.last.subject
159
+ end
160
+
161
+ test 'should be active after being certified and confirmed' do
162
+ admin = create_user
163
+ user = create_user
164
+ assert_not user.active_for_authentication?
165
+ assert_equal :uncertified, user.inactive_message
166
+ user.certify!(admin)
167
+ assert_not user.active_for_authentication?
168
+ assert_equal :unconfirmed, user.inactive_message
169
+ user.confirm!
170
+ assert user.reload.active_for_authentication?
171
+ end
172
+ end