quo_vadis 1.4.2 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (191) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +11 -7
  3. data/CHANGELOG.md +43 -0
  4. data/Gemfile +11 -1
  5. data/LICENSE.txt +21 -0
  6. data/README.md +460 -127
  7. data/Rakefile +15 -9
  8. data/app/controllers/quo_vadis/confirmations_controller.rb +102 -0
  9. data/app/controllers/quo_vadis/logs_controller.rb +20 -0
  10. data/app/controllers/quo_vadis/password_resets_controller.rb +68 -0
  11. data/app/controllers/quo_vadis/passwords_controller.rb +28 -0
  12. data/app/controllers/quo_vadis/recovery_codes_controller.rb +54 -0
  13. data/app/controllers/quo_vadis/sessions_controller.rb +50 -132
  14. data/app/controllers/quo_vadis/totps_controller.rb +72 -0
  15. data/app/controllers/quo_vadis/twofas_controller.rb +26 -0
  16. data/app/mailers/quo_vadis/mailer.rb +73 -0
  17. data/app/models/quo_vadis/account.rb +72 -0
  18. data/app/models/quo_vadis/account_confirmation_token.rb +17 -0
  19. data/app/models/quo_vadis/log.rb +59 -0
  20. data/app/models/quo_vadis/password.rb +68 -0
  21. data/app/models/quo_vadis/password_reset_token.rb +17 -0
  22. data/app/models/quo_vadis/recovery_code.rb +26 -0
  23. data/app/models/quo_vadis/session.rb +55 -0
  24. data/app/models/quo_vadis/token.rb +42 -0
  25. data/app/models/quo_vadis/totp.rb +56 -0
  26. data/app/views/quo_vadis/confirmations/edit.html.erb +10 -0
  27. data/app/views/quo_vadis/confirmations/edit_email.html.erb +14 -0
  28. data/app/views/quo_vadis/confirmations/index.html.erb +14 -0
  29. data/app/views/quo_vadis/confirmations/new.html.erb +16 -0
  30. data/app/views/quo_vadis/logs/index.html.erb +30 -0
  31. data/app/views/quo_vadis/mailer/account_confirmation.text.erb +4 -0
  32. data/app/views/quo_vadis/mailer/email_change_notification.text.erb +8 -0
  33. data/app/views/quo_vadis/mailer/identifier_change_notification.text.erb +8 -0
  34. data/app/views/quo_vadis/mailer/password_change_notification.text.erb +8 -0
  35. data/app/views/quo_vadis/mailer/password_reset_notification.text.erb +8 -0
  36. data/app/views/quo_vadis/mailer/recovery_codes_generation_notification.text.erb +8 -0
  37. data/app/views/quo_vadis/mailer/reset_password.text.erb +4 -0
  38. data/app/views/quo_vadis/mailer/totp_reuse_notification.text.erb +6 -0
  39. data/app/views/quo_vadis/mailer/totp_setup_notification.text.erb +8 -0
  40. data/app/views/quo_vadis/mailer/twofa_deactivated_notification.text.erb +8 -0
  41. data/app/views/quo_vadis/password_resets/edit.html.erb +17 -0
  42. data/app/views/quo_vadis/password_resets/index.html.erb +5 -0
  43. data/app/views/quo_vadis/password_resets/new.html.erb +12 -0
  44. data/app/views/quo_vadis/passwords/edit.html.erb +22 -0
  45. data/app/views/quo_vadis/recovery_codes/challenge.html.erb +11 -0
  46. data/app/views/quo_vadis/recovery_codes/index.html.erb +25 -0
  47. data/app/views/quo_vadis/sessions/index.html.erb +26 -0
  48. data/app/views/quo_vadis/sessions/new.html.erb +24 -0
  49. data/app/views/quo_vadis/totps/challenge.html.erb +11 -0
  50. data/app/views/quo_vadis/totps/new.html.erb +17 -0
  51. data/app/views/quo_vadis/twofas/show.html.erb +20 -0
  52. data/bin/console +15 -0
  53. data/bin/rails +21 -0
  54. data/bin/setup +8 -0
  55. data/config/locales/quo_vadis.en.yml +80 -24
  56. data/config/routes.rb +46 -12
  57. data/db/migrate/202102150904_setup.rb +48 -0
  58. data/lib/generators/quo_vadis/install_generator.rb +4 -23
  59. data/lib/quo_vadis.rb +103 -97
  60. data/lib/quo_vadis/controller.rb +227 -0
  61. data/lib/quo_vadis/crypt.rb +43 -0
  62. data/lib/quo_vadis/current_request_details.rb +11 -0
  63. data/lib/quo_vadis/defaults.rb +18 -0
  64. data/lib/quo_vadis/encrypted_type.rb +17 -0
  65. data/lib/quo_vadis/engine.rb +9 -11
  66. data/lib/quo_vadis/hmacable.rb +26 -0
  67. data/lib/quo_vadis/ip_masking.rb +31 -0
  68. data/lib/quo_vadis/model.rb +83 -0
  69. data/lib/quo_vadis/version.rb +3 -1
  70. data/quo_vadis.gemspec +20 -25
  71. data/test/dummy/README.markdown +1 -0
  72. data/test/dummy/Rakefile +2 -6
  73. data/test/dummy/app/controllers/application_controller.rb +0 -1
  74. data/test/dummy/app/controllers/articles_controller.rb +8 -11
  75. data/test/dummy/app/controllers/sign_ups_controller.rb +42 -0
  76. data/test/dummy/app/controllers/users_controller.rb +13 -5
  77. data/test/dummy/app/models/application_record.rb +3 -0
  78. data/test/dummy/app/models/article.rb +2 -1
  79. data/test/dummy/app/models/person.rb +5 -2
  80. data/test/dummy/app/models/user.rb +4 -1
  81. data/test/dummy/app/views/articles/also_secret.html.erb +1 -0
  82. data/test/dummy/app/views/articles/index.html.erb +1 -1
  83. data/test/dummy/app/views/articles/secret.html.erb +1 -0
  84. data/test/dummy/app/views/articles/very_secret.html.erb +2 -0
  85. data/test/dummy/app/views/layouts/application.html.erb +41 -25
  86. data/test/dummy/app/views/sign_ups/new.html.erb +37 -0
  87. data/test/dummy/app/views/sign_ups/show.html.erb +5 -0
  88. data/test/dummy/app/views/users/new.html.erb +32 -9
  89. data/test/dummy/config.ru +6 -3
  90. data/test/dummy/config/application.rb +18 -9
  91. data/test/dummy/config/boot.rb +3 -9
  92. data/test/dummy/config/database.yml +2 -14
  93. data/test/dummy/config/environment.rb +2 -3
  94. data/test/dummy/config/initializers/quo_vadis.rb +2 -76
  95. data/test/dummy/config/routes.rb +11 -3
  96. data/test/dummy/db/migrate/202102121932_create_users.rb +10 -0
  97. data/test/dummy/db/migrate/202102121935_create_people.rb +10 -0
  98. data/test/dummy/db/schema.rb +80 -21
  99. data/test/fixtures/quo_vadis/mailer/account_confirmation.text +4 -0
  100. data/test/fixtures/quo_vadis/mailer/email_change_notification.text +8 -0
  101. data/test/fixtures/quo_vadis/mailer/identifier_change_notification.text +8 -0
  102. data/test/fixtures/quo_vadis/mailer/password_change_notification.text +8 -0
  103. data/test/fixtures/quo_vadis/mailer/password_reset_notification.text +8 -0
  104. data/test/fixtures/quo_vadis/mailer/recovery_codes_generation_notification.text +8 -0
  105. data/test/fixtures/quo_vadis/mailer/reset_password.text +4 -0
  106. data/test/fixtures/quo_vadis/mailer/totp_reuse_notification.text +6 -0
  107. data/test/fixtures/quo_vadis/mailer/totp_setup_notification.text +8 -0
  108. data/test/fixtures/quo_vadis/mailer/twofa_deactivated_notification.text +8 -0
  109. data/test/integration/account_confirmation_test.rb +145 -0
  110. data/test/integration/controller_test.rb +280 -0
  111. data/test/integration/logging_test.rb +244 -0
  112. data/test/integration/password_change_test.rb +99 -0
  113. data/test/integration/password_login_test.rb +137 -0
  114. data/test/integration/password_reset_test.rb +136 -0
  115. data/test/integration/recovery_codes_test.rb +48 -0
  116. data/test/integration/sessions_test.rb +86 -0
  117. data/test/integration/sign_up_test.rb +26 -12
  118. data/test/integration/totps_test.rb +96 -0
  119. data/test/integration/twofa_test.rb +82 -0
  120. data/test/mailers/mailer_test.rb +200 -0
  121. data/test/models/account_test.rb +50 -0
  122. data/test/models/crypt_test.rb +22 -0
  123. data/test/models/log_test.rb +16 -0
  124. data/test/models/mask_ip_test.rb +27 -0
  125. data/test/models/model_test.rb +66 -0
  126. data/test/models/password_test.rb +179 -0
  127. data/test/models/recovery_code_test.rb +54 -0
  128. data/test/models/session_test.rb +67 -0
  129. data/test/models/token_test.rb +70 -0
  130. data/test/models/totp_test.rb +68 -0
  131. data/test/quo_vadis_test.rb +39 -3
  132. data/test/test_helper.rb +42 -70
  133. metadata +120 -204
  134. data/app/controllers/controller_mixin.rb +0 -109
  135. data/app/mailers/quo_vadis/notifier.rb +0 -30
  136. data/app/models/model_mixin.rb +0 -128
  137. data/lib/generators/quo_vadis/templates/migration.rb.erb +0 -18
  138. data/lib/generators/quo_vadis/templates/quo_vadis.rb.erb +0 -96
  139. data/test/dummy/.gitignore +0 -2
  140. data/test/dummy/app/helpers/application_helper.rb +0 -2
  141. data/test/dummy/app/helpers/articles_helper.rb +0 -2
  142. data/test/dummy/app/views/articles/new.html.erb +0 -11
  143. data/test/dummy/app/views/layouts/sessions.html.erb +0 -3
  144. data/test/dummy/app/views/quo_vadis/notifier/change_password.text.erb +0 -9
  145. data/test/dummy/app/views/quo_vadis/notifier/invite.text.erb +0 -8
  146. data/test/dummy/app/views/sessions/edit.html.erb +0 -11
  147. data/test/dummy/app/views/sessions/forgotten.html.erb +0 -13
  148. data/test/dummy/app/views/sessions/invite.html.erb +0 -31
  149. data/test/dummy/app/views/sessions/new.html.erb +0 -15
  150. data/test/dummy/config/environments/development.rb +0 -26
  151. data/test/dummy/config/environments/production.rb +0 -49
  152. data/test/dummy/config/environments/test.rb +0 -37
  153. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  154. data/test/dummy/config/initializers/inflections.rb +0 -10
  155. data/test/dummy/config/initializers/mime_types.rb +0 -5
  156. data/test/dummy/config/initializers/rack_patch.rb +0 -16
  157. data/test/dummy/config/initializers/secret_token.rb +0 -7
  158. data/test/dummy/config/initializers/session_store.rb +0 -8
  159. data/test/dummy/config/locales/en.yml +0 -5
  160. data/test/dummy/config/locales/quo_vadis.en.yml +0 -21
  161. data/test/dummy/db/migrate/20110124125037_create_users.rb +0 -13
  162. data/test/dummy/db/migrate/20110124131535_create_articles.rb +0 -14
  163. data/test/dummy/db/migrate/20110127094709_add_authentication_to_users.rb +0 -18
  164. data/test/dummy/db/migrate/20111004112209_create_people.rb +0 -13
  165. data/test/dummy/db/migrate/20111004132342_add_authentication_to_people.rb +0 -18
  166. data/test/dummy/public/404.html +0 -26
  167. data/test/dummy/public/422.html +0 -26
  168. data/test/dummy/public/500.html +0 -26
  169. data/test/dummy/public/javascripts/application.js +0 -2
  170. data/test/dummy/public/javascripts/controls.js +0 -965
  171. data/test/dummy/public/javascripts/dragdrop.js +0 -974
  172. data/test/dummy/public/javascripts/effects.js +0 -1123
  173. data/test/dummy/public/javascripts/prototype.js +0 -6001
  174. data/test/dummy/public/javascripts/rails.js +0 -175
  175. data/test/dummy/public/stylesheets/.gitkeep +0 -0
  176. data/test/dummy/script/rails +0 -6
  177. data/test/integration/activation_test.rb +0 -108
  178. data/test/integration/authenticate_test.rb +0 -39
  179. data/test/integration/blocked_test.rb +0 -23
  180. data/test/integration/config_test.rb +0 -118
  181. data/test/integration/cookie_test.rb +0 -67
  182. data/test/integration/csrf_test.rb +0 -41
  183. data/test/integration/forgotten_test.rb +0 -93
  184. data/test/integration/helper_test.rb +0 -18
  185. data/test/integration/locale_test.rb +0 -197
  186. data/test/integration/navigation_test.rb +0 -7
  187. data/test/integration/sign_in_person_test.rb +0 -26
  188. data/test/integration/sign_in_test.rb +0 -24
  189. data/test/integration/sign_out_test.rb +0 -20
  190. data/test/support/integration_case.rb +0 -11
  191. data/test/unit/user_test.rb +0 -75
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ class CryptTest < ActiveSupport::TestCase
4
+
5
+ setup do
6
+ @crypt = QuoVadis::Crypt
7
+ end
8
+
9
+ test 'round trip' do
10
+ plaintext = 'the quick brown fox'
11
+ ciphertext = @crypt.encrypt plaintext
12
+ refute_equal plaintext, ciphertext
13
+ assert_equal plaintext, @crypt.decrypt(ciphertext)
14
+ end
15
+
16
+ test 'same plaintext encrypts to different ciphertexts' do
17
+ plaintext = 'the quick brown fox'
18
+ ciphertext = @crypt.encrypt plaintext
19
+ refute_equal ciphertext, @crypt.encrypt(plaintext)
20
+ end
21
+
22
+ end
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ class LogTest < ActiveSupport::TestCase
4
+
5
+ test 'smoke' do
6
+ user = User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
7
+ account = user.qv_account
8
+
9
+ log = account.logs.create! action: QuoVadis::Log::LOGIN_SUCCESS, ip: '1.2.3.4', metadata: {foo: 'bar'}
10
+
11
+ assert_equal QuoVadis::Log::LOGIN_SUCCESS, log.action
12
+ assert_equal '1.2.3.4', log.ip
13
+ assert_equal 'bar', log.metadata['foo']
14
+ end
15
+
16
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ class MaskIpTest < ActiveSupport::TestCase
4
+
5
+ setup do
6
+ @masking = QuoVadis.mask_ips
7
+ end
8
+
9
+ teardown do
10
+ QuoVadis.mask_ips @masking
11
+ end
12
+
13
+
14
+ test 'mask ips' do
15
+ [ QuoVadis::Log, QuoVadis::Session ].each do |klass|
16
+ QuoVadis.mask_ips false
17
+ instance = klass.new(ip: '1.2.3.4')
18
+ instance.valid?
19
+ assert_equal '1.2.3.4', instance.ip
20
+
21
+ QuoVadis.mask_ips true
22
+ instance.valid?
23
+ assert_equal '1.2.3.0', instance.ip
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,66 @@
1
+ require 'test_helper'
2
+
3
+ class ModelTest < ActiveSupport::TestCase
4
+ include ActionMailer::TestHelper
5
+
6
+ test 'responds to' do
7
+ assert_respond_to User, :authenticates
8
+ u = User.new
9
+ assert_respond_to u, :password
10
+ assert_respond_to u, :password=
11
+ assert_respond_to u, :password_confirmation
12
+ assert_respond_to u, :password_confirmation=
13
+ end
14
+
15
+
16
+ test 'creates account' do
17
+ u = User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
18
+ assert u.persisted?
19
+ assert u.qv_account.persisted?
20
+ end
21
+
22
+
23
+ test 'destroys account' do
24
+ u = User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
25
+ ac = u.qv_account
26
+ u.destroy
27
+ assert ac.destroyed?
28
+ end
29
+
30
+
31
+ test 'copies model identifier to account' do
32
+ email = 'bob@example.com'
33
+ u = User.create! name: 'bob', email: email, password: '123456789abc'
34
+ assert_equal email, u.qv_account.identifier
35
+
36
+ email = 'b@foo.com'
37
+ u.update email: email
38
+ u.qv_account.reload
39
+ assert_equal email, u.qv_account.identifier
40
+
41
+ u.update name: nil, email: 'xyz' # nil name is invalid
42
+ u.qv_account.reload
43
+ refute_equal 'xyz', u.qv_account.identifier
44
+ end
45
+
46
+
47
+ test 'ensures uniqueness validation on identifier' do
48
+ Foo = Class.new ActiveRecord::Base
49
+ assert_raises NotImplementedError, 'missing uniqueness validation on ModelTest::Foo#email. Try adding: `validates :email, uniqueness: true`' do
50
+ Foo.instance_eval 'authenticates'
51
+ end
52
+
53
+ Bar = Class.new ActiveRecord::Base
54
+ Bar.instance_eval 'validates :email, uniqueness: true'
55
+ Bar.instance_eval 'authenticates'
56
+ # no error raised
57
+ end
58
+
59
+
60
+ test 'notifies on email change' do
61
+ u = User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
62
+ assert_enqueued_email_with QuoVadis::Mailer, :email_change_notification, args: {email: 'bob@example.com'} do
63
+ u.update email: 'robert@example.com'
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,179 @@
1
+ require 'test_helper'
2
+
3
+ class PasswordTest < ActiveSupport::TestCase
4
+
5
+ VALID_PASSWORD = '123456789abc'
6
+ SIXTY_FOUR_CHARS = '1234567890123456789012345678901234567890123456789012345678901234'
7
+
8
+ test 'validations' do
9
+ pw = QuoVadis::Password.new
10
+ pw.valid?
11
+ refute_empty pw.errors[:password]
12
+
13
+ pw.password = nil
14
+ pw.valid?
15
+ refute_empty pw.errors[:password]
16
+
17
+ pw.password = ''
18
+ pw.valid?
19
+ refute_empty pw.errors[:password]
20
+
21
+ pw.password = 'x'
22
+ pw.valid?
23
+ refute_empty pw.errors[:password]
24
+
25
+ pw.password = VALID_PASSWORD
26
+ pw.valid?
27
+ assert_empty pw.errors[:password]
28
+ end
29
+
30
+
31
+ test 'confirmation' do
32
+ pw = QuoVadis::Password.new password: VALID_PASSWORD, password_confirmation: nil
33
+ pw.valid?
34
+ assert_empty pw.errors[:password_confirmation]
35
+
36
+ pw = QuoVadis::Password.new password: VALID_PASSWORD, password_confirmation: ''
37
+ pw.valid?
38
+ refute_empty pw.errors[:password_confirmation]
39
+
40
+ pw.password_confirmation = VALID_PASSWORD
41
+ pw.valid?
42
+ assert_empty pw.errors[:password_confirmation]
43
+ assert_empty pw.errors[:password]
44
+ end
45
+
46
+
47
+ test 'model passes through password to quo_vadis' do
48
+ user = User.new name: 'bob', email: 'bob@example.com'
49
+ assert user.valid?
50
+
51
+ user = User.new name: 'bob', email: 'bob@example.com', password: ''
52
+ refute user.valid?
53
+ refute_empty user.errors[:password]
54
+
55
+ user.password = 'x'
56
+ refute user.valid?
57
+ refute_empty user.errors[:password]
58
+
59
+ user.password = VALID_PASSWORD
60
+ assert user.save
61
+
62
+ _user = User.last
63
+ assert _user.valid?
64
+
65
+ _pw = _user.qv_account.password
66
+ assert _pw.valid?
67
+ end
68
+
69
+
70
+ test 'model passes through password confirmation to quo_vadis' do
71
+ user = User.new name: 'bob', email: 'bob@example.com', password: VALID_PASSWORD, password_confirmation: 'x'
72
+ refute user.valid?
73
+ refute_empty user.errors[:password_confirmation]
74
+
75
+ user.password_confirmation = VALID_PASSWORD
76
+ assert user.valid?
77
+ end
78
+
79
+
80
+ test 'change' do
81
+ user = User.create! name: 'bob', email: 'bob@example.com', password: VALID_PASSWORD
82
+ pw = user.qv_account.password
83
+
84
+ refute pw.change('wrong', '', '')
85
+ assert_equal ['is incorrect'], pw.errors[:password]
86
+
87
+ pw = QuoVadis::Password.find pw.id
88
+ refute pw.change(VALID_PASSWORD, '', '')
89
+ assert_equal ["can't be blank"], pw.errors[:new_password]
90
+
91
+ pw = QuoVadis::Password.find pw.id
92
+ refute pw.change(VALID_PASSWORD, 'x', 'x')
93
+ assert_equal ["is too short (minimum is #{QuoVadis.password_minimum_length} characters)"], pw.errors[:new_password]
94
+
95
+ pw = QuoVadis::Password.find pw.id
96
+ refute pw.change(VALID_PASSWORD, 'xxxxxxxxxxxx', 'yyyyyyyyyyyy')
97
+ assert_equal ["doesn't match Password"], pw.errors[:new_password_confirmation]
98
+
99
+ pw = QuoVadis::Password.find pw.id
100
+ assert pw.change(VALID_PASSWORD, 'xxxxxxxxxxxx', 'xxxxxxxxxxxx')
101
+ assert pw.authenticate 'xxxxxxxxxxxx'
102
+ end
103
+
104
+
105
+ test 'reset' do
106
+ user = User.create! name: 'bob', email: 'bob@example.com', password: VALID_PASSWORD
107
+ pw = user.qv_account.password
108
+
109
+ refute pw.reset('', '')
110
+ assert_equal ["can't be blank"], pw.errors[:password]
111
+
112
+ refute pw.reset('x', 'x')
113
+ assert_equal ["is too short (minimum is #{QuoVadis.password_minimum_length} characters)"], pw.errors[:password]
114
+
115
+ refute pw.reset('xxxxxxxxxxxx', 'yyyyyyyyyyyy')
116
+ assert_equal ["doesn't match Password"], pw.errors[:password_confirmation]
117
+
118
+ assert pw.reset('xxxxxxxxxxxx', 'xxxxxxxxxxxx')
119
+ assert pw.authenticate 'xxxxxxxxxxxx'
120
+ end
121
+
122
+
123
+ test 'passwords can only be updated via #change and #reset' do
124
+ user = User.create! name: 'bob', email: 'bob@example.com', password: VALID_PASSWORD
125
+ pw = user.qv_account.password
126
+
127
+ refute pw.update password: 'secretsecret'
128
+ assert_equal ["must be updated via #change or #reset"], pw.errors[:password]
129
+
130
+ pw.password = VALID_PASSWORD
131
+ refute pw.save
132
+ assert_equal ["must be updated via #change or #reset"], pw.errors[:password]
133
+ end
134
+
135
+
136
+ test 'cascade destroy' do
137
+ user = User.create! name: 'bob', email: 'bob@example.com', password: VALID_PASSWORD
138
+ assert user.qv_account.persisted?
139
+ assert user.qv_account.password.persisted?
140
+
141
+ user.destroy
142
+ assert user.qv_account.destroyed?
143
+ assert user.qv_account.password.destroyed?
144
+ end
145
+
146
+
147
+ test 'cannot override existing password' do
148
+ user = User.create! name: 'bob', email: 'bob@example.com', password: VALID_PASSWORD
149
+
150
+ assert_raises QuoVadis::PasswordExistsError do
151
+ user.password = 'cba987654321'
152
+ end
153
+ end
154
+
155
+
156
+ test 'passwords may be 64 characters or longer' do
157
+ pw = QuoVadis::Password.new password: SIXTY_FOUR_CHARS
158
+ pw.valid?
159
+ assert_empty pw.errors[:password]
160
+
161
+ pw.password = "#{SIXTY_FOUR_CHARS}abc"
162
+ pw.valid?
163
+ assert_empty pw.errors[:password]
164
+ end
165
+
166
+ test 'passwords may contain spaces, no truncation' do
167
+ pw = QuoVadis::Password.new password: ' '
168
+ pw.valid?
169
+ assert_empty pw.errors[:password]
170
+ end
171
+
172
+ test 'passwords may contain unicode characters' do
173
+ pw = QuoVadis::Password.new password: '★ ★ ★ ★ ★ ★ '
174
+ pw.valid?
175
+ assert_empty pw.errors[:password]
176
+ end
177
+
178
+
179
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class RecoveryCodeTest < ActiveSupport::TestCase
4
+
5
+ setup do
6
+ @user = User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
7
+ @rc = QuoVadis::RecoveryCode.new(account: @user.qv_account).tap &:save!
8
+ end
9
+
10
+
11
+ test 'code can be retrieved initially' do
12
+ assert_equal 11, @rc.code.length
13
+ end
14
+
15
+
16
+ test 'code does not change' do
17
+ code = @rc.code
18
+ @rc.valid?
19
+ assert_equal code, @rc.code
20
+ end
21
+
22
+
23
+ test 'code not available after finding' do
24
+ rc = QuoVadis::RecoveryCode.find @rc.id
25
+ assert_nil rc.code
26
+ end
27
+
28
+
29
+ test 'authenticate' do
30
+ code = @rc.code
31
+ refute @rc.authenticate_code 'wrong'
32
+ assert @rc.authenticate_code code
33
+ end
34
+
35
+
36
+ test 'recovery code is destroyed after successful use' do
37
+ code = @rc.code
38
+ assert @rc.authenticate_code code
39
+ assert @rc.destroyed?
40
+ end
41
+
42
+ test 'generate a fresh set of codes' do
43
+ account = @user.qv_account
44
+ codes = []
45
+ assert_difference 'QuoVadis::RecoveryCode.count', 5 do
46
+ codes = account.generate_recovery_codes
47
+ end
48
+ assert_equal 5, codes.length
49
+ codes.each do |code|
50
+ assert_instance_of String, code
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,67 @@
1
+ require 'test_helper'
2
+
3
+ class SessionTest < ActiveSupport::TestCase
4
+
5
+ test 'expired?' do
6
+ refute QuoVadis::Session.new.expired?
7
+ assert QuoVadis::Session.new(lifetime_expires_at: 1.day.ago).expired?
8
+ refute QuoVadis::Session.new(lifetime_expires_at: 1.day.from_now).expired?
9
+
10
+ QuoVadis.session_idle_timeout 5.minutes
11
+ refute QuoVadis::Session.new(lifetime_expires_at: 1.day.from_now, last_seen_at: 1.minute.ago).expired?
12
+ assert QuoVadis::Session.new(lifetime_expires_at: 1.day.from_now, last_seen_at: 10.minutes.ago).expired?
13
+ end
14
+
15
+
16
+ test 'logout_other_sessions' do
17
+ user = User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
18
+ account = user.qv_account
19
+ s0 = account.sessions.create! ip: 'ip', user_agent: 'useragent'
20
+ s1 = account.sessions.create! ip: 'ip', user_agent: 'useragent'
21
+
22
+ s0.logout_other_sessions
23
+
24
+ refute s0.destroyed?
25
+ assert s1.destroyed?
26
+ end
27
+
28
+
29
+ test 'reset authenticated with second factor' do
30
+ user = User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
31
+ account = user.qv_account
32
+ session = account.sessions.create! ip: 'ip', user_agent: 'useragent'
33
+
34
+ refute session.second_factor_authenticated?
35
+ session.authenticated_with_second_factor
36
+ assert session.second_factor_authenticated?
37
+ session.reset_authenticated_with_second_factor
38
+ refute session.second_factor_authenticated?
39
+ end
40
+
41
+
42
+ test 'replace' do
43
+ user = User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
44
+ account = user.qv_account
45
+
46
+ session = account.sessions.create! ip: 'ip', user_agent: 'useragent'
47
+ sess = session.replace
48
+
49
+ assert_instance_of QuoVadis::Session, sess
50
+ assert session.destroyed?
51
+ refute_equal session.id, sess.id
52
+
53
+ refute_includes account.sessions, session
54
+ assert_includes account.sessions, sess
55
+
56
+ session
57
+ .attributes
58
+ .reject { |name, _| %w[id created_at created_on updated_at updated_on].include? name }
59
+ .each do |name, value|
60
+ if value.nil?
61
+ assert_nil sess.send(name)
62
+ else
63
+ assert_equal value, sess.send(name)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,70 @@
1
+ require 'test_helper'
2
+
3
+ class TokenTest < ActiveSupport::TestCase
4
+
5
+ setup do
6
+ u = User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
7
+ @account = u.qv_account
8
+ end
9
+
10
+
11
+ test 'account confirmation' do
12
+ token = QuoVadis::AccountConfirmationToken.generate @account
13
+ assert_match /^\d+-\d+--\h+$/, token
14
+ assert_equal @account, QuoVadis::AccountConfirmationToken.find_account(token)
15
+ end
16
+
17
+ test 'account confirmation expired' do
18
+ token = QuoVadis::AccountConfirmationToken.generate @account
19
+ travel QuoVadis.account_confirmation_token_lifetime + 1.second
20
+ assert_nil QuoVadis::AccountConfirmationToken.find_account(token)
21
+ end
22
+
23
+ test 'account confirmation already done' do
24
+ token = QuoVadis::AccountConfirmationToken.generate @account
25
+ @account.confirmed!
26
+ assert_nil QuoVadis::AccountConfirmationToken.find_account(token)
27
+ end
28
+
29
+ test 'account confirmation token tampered with' do
30
+ assert_nil QuoVadis::AccountConfirmationToken.find_account(nil)
31
+ assert_nil QuoVadis::AccountConfirmationToken.find_account('')
32
+ assert_nil QuoVadis::AccountConfirmationToken.find_account('asdf')
33
+
34
+ token = QuoVadis::AccountConfirmationToken.generate @account
35
+ id, expires_at, hash = token.match(/^(\d+)-(\d+)--(\h+)$/).captures
36
+ fake_token = "#{id}-#{expires_at.to_i + 1}--#{hash}"
37
+ assert_nil QuoVadis::AccountConfirmationToken.find_account(fake_token)
38
+ end
39
+
40
+
41
+ test 'password reset' do
42
+ token = QuoVadis::PasswordResetToken.generate @account
43
+ assert_match /^\d+-\d+--\h+$/, token
44
+ assert_equal @account, QuoVadis::PasswordResetToken.find_account(token)
45
+ end
46
+
47
+ test 'password reset expired' do
48
+ token = QuoVadis::PasswordResetToken.generate @account
49
+ travel QuoVadis.password_reset_token_lifetime + 1.second
50
+ assert_nil QuoVadis::PasswordResetToken.find_account(token)
51
+ end
52
+
53
+ test 'password reset already done' do
54
+ token = QuoVadis::PasswordResetToken.generate @account
55
+ @account.password.reset 'secretsecret', 'secretsecret'
56
+ assert_nil QuoVadis::PasswordResetToken.find_account(token)
57
+ end
58
+
59
+ test 'password reset token tampered with' do
60
+ assert_nil QuoVadis::PasswordResetToken.find_account(nil)
61
+ assert_nil QuoVadis::PasswordResetToken.find_account('')
62
+ assert_nil QuoVadis::PasswordResetToken.find_account('asdf')
63
+
64
+ token = QuoVadis::PasswordResetToken.generate @account
65
+ id, expires_at, hash = token.match(/^(\d+)-(\d+)--(\h+)$/).captures
66
+ fake_token = "#{id}-#{expires_at.to_i + 1}--#{hash}"
67
+ assert_nil QuoVadis::PasswordResetToken.find_account(fake_token)
68
+ end
69
+
70
+ end