quo_vadis 1.4.1 → 2.1.0

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 +31 -0
  4. data/Gemfile +11 -1
  5. data/LICENSE.txt +21 -0
  6. data/README.md +455 -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 +59 -0
  18. data/app/models/quo_vadis/account_confirmation_token.rb +17 -0
  19. data/app/models/quo_vadis/log.rb +57 -0
  20. data/app/models/quo_vadis/password.rb +52 -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/bin/console +15 -0
  27. data/bin/rails +21 -0
  28. data/bin/setup +8 -0
  29. data/config/locales/quo_vadis.en.yml +79 -24
  30. data/config/routes.rb +46 -12
  31. data/db/migrate/202102150904_setup.rb +48 -0
  32. data/lib/generators/quo_vadis/install_generator.rb +4 -23
  33. data/lib/quo_vadis.rb +103 -97
  34. data/lib/quo_vadis/controller.rb +227 -0
  35. data/lib/quo_vadis/crypt.rb +43 -0
  36. data/lib/quo_vadis/current_request_details.rb +11 -0
  37. data/lib/quo_vadis/defaults.rb +18 -0
  38. data/lib/quo_vadis/encrypted_type.rb +17 -0
  39. data/lib/quo_vadis/engine.rb +9 -11
  40. data/lib/quo_vadis/hmacable.rb +26 -0
  41. data/lib/quo_vadis/ip_masking.rb +31 -0
  42. data/lib/quo_vadis/model.rb +79 -0
  43. data/lib/quo_vadis/version.rb +3 -1
  44. data/quo_vadis.gemspec +20 -25
  45. data/test/dummy/README.markdown +1 -0
  46. data/test/dummy/Rakefile +2 -6
  47. data/test/dummy/app/controllers/application_controller.rb +0 -1
  48. data/test/dummy/app/controllers/articles_controller.rb +8 -11
  49. data/test/dummy/app/controllers/sign_ups_controller.rb +42 -0
  50. data/test/dummy/app/controllers/users_controller.rb +13 -5
  51. data/test/dummy/app/models/application_record.rb +3 -0
  52. data/test/dummy/app/models/article.rb +2 -1
  53. data/test/dummy/app/models/person.rb +5 -2
  54. data/test/dummy/app/models/user.rb +4 -1
  55. data/test/dummy/app/views/articles/also_secret.html.erb +1 -0
  56. data/test/dummy/app/views/articles/index.html.erb +1 -1
  57. data/test/dummy/app/views/articles/secret.html.erb +1 -0
  58. data/test/dummy/app/views/articles/very_secret.html.erb +2 -0
  59. data/test/dummy/app/views/layouts/application.html.erb +41 -25
  60. data/test/dummy/app/views/quo_vadis/confirmations/edit.html.erb +10 -0
  61. data/test/dummy/app/views/quo_vadis/confirmations/edit_email.html.erb +14 -0
  62. data/test/dummy/app/views/quo_vadis/confirmations/index.html.erb +14 -0
  63. data/test/dummy/app/views/quo_vadis/confirmations/new.html.erb +16 -0
  64. data/test/dummy/app/views/quo_vadis/logs/index.html.erb +30 -0
  65. data/test/dummy/app/views/quo_vadis/mailer/account_confirmation.text.erb +4 -0
  66. data/test/dummy/app/views/quo_vadis/mailer/email_change_notification.text.erb +8 -0
  67. data/test/dummy/app/views/quo_vadis/mailer/identifier_change_notification.text.erb +8 -0
  68. data/test/dummy/app/views/quo_vadis/mailer/password_change_notification.text.erb +8 -0
  69. data/test/dummy/app/views/quo_vadis/mailer/password_reset_notification.text.erb +8 -0
  70. data/test/dummy/app/views/quo_vadis/mailer/recovery_codes_generation_notification.text.erb +8 -0
  71. data/test/dummy/app/views/quo_vadis/mailer/reset_password.text.erb +4 -0
  72. data/test/dummy/app/views/quo_vadis/mailer/totp_reuse_notification.text.erb +6 -0
  73. data/test/dummy/app/views/quo_vadis/mailer/totp_setup_notification.text.erb +8 -0
  74. data/test/dummy/app/views/quo_vadis/mailer/twofa_deactivated_notification.text.erb +8 -0
  75. data/test/dummy/app/views/quo_vadis/password_resets/edit.html.erb +17 -0
  76. data/test/dummy/app/views/quo_vadis/password_resets/index.html.erb +5 -0
  77. data/test/dummy/app/views/quo_vadis/password_resets/new.html.erb +12 -0
  78. data/test/dummy/app/views/quo_vadis/passwords/edit.html.erb +22 -0
  79. data/test/dummy/app/views/quo_vadis/recovery_codes/challenge.html.erb +11 -0
  80. data/test/dummy/app/views/quo_vadis/recovery_codes/index.html.erb +25 -0
  81. data/test/dummy/app/views/quo_vadis/sessions/index.html.erb +26 -0
  82. data/test/dummy/app/views/quo_vadis/sessions/new.html.erb +24 -0
  83. data/test/dummy/app/views/quo_vadis/totps/challenge.html.erb +11 -0
  84. data/test/dummy/app/views/quo_vadis/totps/new.html.erb +17 -0
  85. data/test/dummy/app/views/quo_vadis/twofas/show.html.erb +20 -0
  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 +235 -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 +34 -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 +166 -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,99 @@
1
+ require 'test_helper'
2
+
3
+ class PasswordChangeTest < IntegrationTest
4
+
5
+ setup do
6
+ QuoVadis.two_factor_authentication_mandatory false
7
+ User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
8
+ login
9
+ end
10
+
11
+
12
+ test 'requires login' do
13
+ logout
14
+
15
+ put quo_vadis.password_path
16
+ assert_redirected_to quo_vadis.login_path
17
+ end
18
+
19
+
20
+ test 'incorrect password' do
21
+ put quo_vadis.password_path(password: {password: 'x'})
22
+ assert_response :unprocessable_entity
23
+ assert_equal ['is incorrect'], password_instance.errors[:password]
24
+ end
25
+
26
+
27
+ test 'new password empty' do
28
+ put quo_vadis.password_path(password: {password: '123456789abc', new_password: ''})
29
+ assert_response :unprocessable_entity
30
+ assert_equal ["can't be blank"], password_instance.errors[:new_password]
31
+ end
32
+
33
+
34
+ test 'new password too short' do
35
+ put quo_vadis.password_path(password: {password: '123456789abc', new_password: 'x'})
36
+ assert_response :unprocessable_entity
37
+ assert_equal ["is too short (minimum is #{QuoVadis.password_minimum_length} characters)"], password_instance.errors[:new_password]
38
+ end
39
+
40
+
41
+ test 'new password confirmation does not match' do
42
+ put quo_vadis.password_path(password: {
43
+ password: '123456789abc', new_password: 'xxxxxxxxxxxx', new_password_confirmation: 'y'
44
+ })
45
+ assert_response :unprocessable_entity
46
+ assert_equal ["doesn't match Password"], password_instance.errors[:new_password_confirmation]
47
+ end
48
+
49
+
50
+ test 'success' do
51
+ assert_emails 1 do
52
+ assert_session_replaced do
53
+ put quo_vadis.password_path(password: {
54
+ password: '123456789abc', new_password: 'xxxxxxxxxxxx', new_password_confirmation: 'xxxxxxxxxxxx'
55
+ })
56
+ assert_response :redirect
57
+ assert_equal 'Your password has been changed.', flash[:notice]
58
+ end
59
+ end
60
+ end
61
+
62
+
63
+ test 'logs out other sessions' do
64
+ desktop = session_login
65
+ phone = session_login
66
+
67
+ desktop.put quo_vadis.password_path(password: {
68
+ password: '123456789abc', new_password: 'xxxxxxxxxxxx', new_password_confirmation: 'xxxxxxxxxxxx'
69
+ })
70
+ desktop.follow_redirect!
71
+ assert desktop.controller.logged_in?
72
+
73
+ phone.get articles_path
74
+ refute phone.controller.logged_in?
75
+ end
76
+
77
+
78
+ private
79
+
80
+ # starts a new rails session and logs in
81
+ def session_login
82
+ open_session do |sess|
83
+ sess.post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
84
+ end
85
+ end
86
+
87
+ def login
88
+ post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
89
+ end
90
+
91
+ def logout
92
+ delete quo_vadis.logout_path
93
+ end
94
+
95
+ def password_instance
96
+ controller.instance_variable_get :@password
97
+ end
98
+
99
+ end
@@ -0,0 +1,137 @@
1
+ require 'test_helper'
2
+
3
+ class PasswordLoginTest < IntegrationTest
4
+
5
+ setup do
6
+ QuoVadis.session_lifetime :session
7
+ end
8
+
9
+
10
+ test 'successful login' do
11
+ get quo_vadis.login_path
12
+ assert_response :success
13
+
14
+ User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
15
+ post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
16
+
17
+ assert_redirected_to secret_articles_path
18
+ assert_equal after_login_path, secret_articles_path
19
+ end
20
+
21
+
22
+ test 'successful login redirects to original path' do
23
+ get also_secret_articles_path
24
+ refute_nil session[:qv_bookmark]
25
+
26
+ User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
27
+ post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
28
+
29
+ assert_redirected_to also_secret_articles_path
30
+ assert_nil session[:qv_bookmark]
31
+ end
32
+
33
+
34
+ test 'successful login does not redirect to login path' do
35
+ get quo_vadis.login_path
36
+ assert_nil session[:qv_bookmark]
37
+
38
+ User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
39
+ post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
40
+
41
+ assert_redirected_to after_login_path
42
+ end
43
+
44
+
45
+ test 'failed login' do
46
+ User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
47
+ post quo_vadis.login_path(email: 'bob@example.com', password: 'wrong')
48
+
49
+ assert_response :unprocessable_entity
50
+ assert_equal quo_vadis.login_path, path
51
+ end
52
+
53
+
54
+ test 'unknown login' do
55
+ post quo_vadis.login_path(email: 'bob@example.com', password: 'wrong')
56
+
57
+ assert_response :unprocessable_entity
58
+ assert_equal quo_vadis.login_path, path
59
+ end
60
+
61
+
62
+ test 'logout' do
63
+ User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
64
+ post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
65
+
66
+ # logout
67
+ assert jar.encrypted[QuoVadis.cookie_name]
68
+ assert_difference 'QuoVadis::Session.count', -1 do
69
+ delete quo_vadis.logout_path
70
+ end
71
+ refute jar.encrypted[QuoVadis.cookie_name]
72
+ assert_redirected_to root_path
73
+ end
74
+
75
+
76
+ test 'login for browser session' do
77
+ User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
78
+
79
+ open_session do |sess|
80
+ sess.post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
81
+ assert sess.controller.logged_in?
82
+ end
83
+
84
+ open_session do |sess|
85
+ sess.get articles_path
86
+ refute sess.controller.logged_in?
87
+ end
88
+ end
89
+
90
+
91
+ # Ideally we would use multiple sessions to distinguish this from a single
92
+ # browser session. We would log in in one session then assert we can log in
93
+ # in another session within the timeframe. But I can't find a way to share
94
+ # a cookie between test sessions (I tried nesting sessions). So we do all
95
+ # this within a single session, even though it has the same effect as being
96
+ # within a single browser session.
97
+ test 'login for 1 week' do
98
+ QuoVadis.session_lifetime 1.week
99
+ User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
100
+
101
+ post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
102
+ refute QuoVadis::Session.last.send(:browser_session?)
103
+ assert controller.logged_in?
104
+
105
+ travel 5.days
106
+
107
+ get articles_path
108
+ assert controller.logged_in? # flakey
109
+
110
+ travel 3.days
111
+
112
+ get articles_path
113
+ refute controller.logged_in?
114
+ end
115
+
116
+
117
+ test 'optional remember not opted in' do
118
+ QuoVadis.session_lifetime 1.week
119
+ User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
120
+
121
+ post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc', remember: '0')
122
+ assert QuoVadis::Session.last.send(:browser_session?)
123
+
124
+ # Cannot test this fully without being able to share cookies between test sessions.
125
+ end
126
+
127
+
128
+ test 'optional remember opted in' do
129
+ QuoVadis.session_lifetime 1.week
130
+ User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
131
+
132
+ post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc', remember: '1')
133
+ refute QuoVadis::Session.last.send(:browser_session?)
134
+
135
+ # Cannot test this fully without being able to share cookies between test sessions.
136
+ end
137
+ end
@@ -0,0 +1,136 @@
1
+ require 'test_helper'
2
+
3
+ class PasswordResetTest < IntegrationTest
4
+
5
+ setup do
6
+ @user = User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
7
+ end
8
+
9
+
10
+ test 'new password reset' do
11
+ get quo_vadis.new_password_reset_path
12
+ assert_response :success
13
+ end
14
+
15
+
16
+ test 'unknown identifier' do
17
+ post quo_vadis.password_resets_path(email: 'foo@example.com')
18
+ assert_redirected_to quo_vadis.password_resets_path
19
+ assert_equal 'A link to change your password has been emailed to you.', flash[:notice]
20
+ end
21
+
22
+
23
+ test 'known identifier' do
24
+ assert_emails 1 do
25
+ post quo_vadis.password_resets_path(email: 'bob@example.com')
26
+ end
27
+ assert_redirected_to quo_vadis.password_resets_path
28
+ assert_equal 'A link to change your password has been emailed to you.', flash[:notice]
29
+ end
30
+
31
+
32
+ test 'click link in email' do
33
+ assert_emails 1 do
34
+ post quo_vadis.password_resets_path(email: 'bob@example.com')
35
+ end
36
+ get extract_url_from_email
37
+ assert_response :success
38
+ end
39
+
40
+
41
+ test 'expired link' do
42
+ assert_emails 1 do
43
+ post quo_vadis.password_resets_path(email: 'bob@example.com')
44
+ end
45
+ travel QuoVadis.password_reset_token_lifetime + 1.minute
46
+ get extract_url_from_email
47
+ assert_redirected_to quo_vadis.new_password_reset_path
48
+ assert_equal 'Either the link has expired or you have already reset your password.', flash[:alert]
49
+ end
50
+
51
+
52
+ test 'link cannot be reused' do
53
+ assert_emails 1 do
54
+ post quo_vadis.password_resets_path(email: 'bob@example.com')
55
+ end
56
+ put quo_vadis.password_reset_path(extract_token_from_email, password: {password: 'xxxxxxxxxxxx', password_confirmation: 'xxxxxxxxxxxx'})
57
+ assert controller.logged_in?
58
+
59
+ get quo_vadis.edit_password_reset_url(extract_token_from_email)
60
+ assert_redirected_to quo_vadis.new_password_reset_path
61
+ assert_equal 'Either the link has expired or you have already reset your password.', flash[:alert]
62
+ end
63
+
64
+
65
+ test 'new password invalid' do
66
+ digest = @user.qv_account.password.password_digest
67
+
68
+ assert_emails 1 do
69
+ post quo_vadis.password_resets_path(email: 'bob@example.com')
70
+ end
71
+
72
+ assert_no_difference 'QuoVadis::Session.count' do
73
+ put quo_vadis.password_reset_path(extract_token_from_email, password: {password: '', password_confirmation: ''})
74
+ end
75
+
76
+ assert_equal digest, @user.qv_account.password.reload.password_digest
77
+ assert_response :unprocessable_entity
78
+ assert_equal quo_vadis.password_reset_path(extract_token_from_email), path
79
+ end
80
+
81
+
82
+ test 'new password valid' do
83
+ QuoVadis.two_factor_authentication_mandatory false
84
+
85
+ digest = @user.qv_account.password.password_digest
86
+
87
+ desktop = session_login
88
+ phone = session_login
89
+
90
+ get articles_url
91
+ refute controller.logged_in?
92
+
93
+ assert_emails 1 do
94
+ post quo_vadis.password_resets_path(email: 'bob@example.com')
95
+ end
96
+
97
+ assert_difference 'QuoVadis::Session.count', (- 2 + 1) do
98
+ put quo_vadis.password_reset_path(extract_token_from_email, password: {password: 'xxxxxxxxxxxx', password_confirmation: 'xxxxxxxxxxxx'})
99
+ end
100
+
101
+ assert controller.logged_in?
102
+
103
+ desktop.get articles_url
104
+ refute desktop.controller.logged_in? # NOTE: flaky; if this fails, re-migrate the database.
105
+
106
+ phone.get articles_url
107
+ refute phone.controller.logged_in?
108
+
109
+ refute_equal digest, @user.qv_account.password.reload.password_digest
110
+
111
+ assert_redirected_to '/articles/secret'
112
+ assert_equal 'Your password has been changed and you are logged in.', flash[:notice]
113
+ end
114
+
115
+
116
+ private
117
+
118
+ def session_login
119
+ open_session do |sess|
120
+ sess.post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
121
+ end
122
+ end
123
+
124
+ def extract_url_from_email
125
+ ActionMailer::Base.deliveries.last.decoded[%r{^http://.*$}, 0]
126
+ end
127
+
128
+ def extract_path_from_email
129
+ extract_url_from_email.sub 'http://www.example.com', ''
130
+ end
131
+
132
+ def extract_token_from_email
133
+ extract_url_from_email[%r{/([^/]*)$}, 1]
134
+ end
135
+
136
+ end
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+
3
+ class RecoveryCodesTest < IntegrationTest
4
+
5
+ setup do
6
+ u = User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
7
+ u.qv_account.create_totp last_used_at: 1.day.ago
8
+ QuoVadis.two_factor_authentication_mandatory true
9
+ @codes = u.qv_account.generate_recovery_codes
10
+ login
11
+ end
12
+
13
+
14
+ test 'use recovery code' do
15
+ get quo_vadis.challenge_recovery_codes_path
16
+ assert_response :success
17
+
18
+ assert_difference 'QuoVadis::Totp.count', -1 do
19
+ assert_difference 'QuoVadis::RecoveryCode.count', -1 do
20
+ assert_session_replaced do
21
+ post quo_vadis.authenticate_recovery_codes_path(code: @codes.first)
22
+ end
23
+ end
24
+ end
25
+
26
+ assert_nil User.last.qv_account.totp
27
+
28
+ assert_redirected_to '/articles/secret'
29
+
30
+ # use another recovery code to verify another TOTP-reset doesn't error
31
+ post quo_vadis.authenticate_recovery_codes_path(code: @codes[1])
32
+ end
33
+
34
+
35
+ test 'generate recovery codes' do
36
+ assert_emails 1 do
37
+ post quo_vadis.generate_recovery_codes_path
38
+ end
39
+ assert_redirected_to quo_vadis.recovery_codes_path
40
+ end
41
+
42
+
43
+ private
44
+
45
+ def login
46
+ post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
47
+ end
48
+ end
@@ -0,0 +1,86 @@
1
+ require 'test_helper'
2
+
3
+ # sqlite: primary key needs AUTOINCREMENT to not reuse previous values
4
+ # https://www.sqlite.org/faq.html#q1
5
+ # AR should do this by default (but it seems sometimes doesn't)
6
+
7
+ class SessionsTest < IntegrationTest
8
+
9
+ setup do
10
+ User.create! name: 'bob', email: 'bob@example.com', password: '123456789abc'
11
+ QuoVadis.session_lifetime :session
12
+ QuoVadis.two_factor_authentication_mandatory false
13
+ end
14
+
15
+
16
+ test 'sessions require authentication' do
17
+ get quo_vadis.sessions_path
18
+ assert_redirected_to quo_vadis.login_path
19
+ end
20
+
21
+
22
+ test "user's sessions are independent" do
23
+ desktop = login
24
+ phone = login
25
+
26
+ refute_equal jar(desktop).encrypted[QuoVadis.cookie_name],
27
+ jar(phone).encrypted[QuoVadis.cookie_name]
28
+ end
29
+
30
+
31
+ test 'user can logout without logging out another session' do
32
+ desktop = login
33
+ phone = login
34
+
35
+ # logout on phone
36
+ phone.delete quo_vadis.logout_path
37
+
38
+ # assert phone logged out
39
+ phone.assert_response :redirect
40
+ assert_equal 'You have logged out.', phone.flash[:notice]
41
+ refute jar(phone).encrypted[QuoVadis.cookie_name]
42
+ refute phone.controller.logged_in?
43
+
44
+ # assert desktop still logged in
45
+ assert jar(desktop).encrypted[QuoVadis.cookie_name]
46
+ end
47
+
48
+
49
+ test "user can log out a separate session" do
50
+ desktop = login
51
+ phone = login
52
+
53
+ # on phone, list sessions
54
+ phone.get quo_vadis.sessions_path
55
+ phone.assert_response :success
56
+ phone.assert_select 'td', 'This session'
57
+ phone.assert_select 'td input[type=submit][value="Log out"]', 1
58
+
59
+ # on phone, log out the desktop session
60
+ phone.delete quo_vadis.session_path(QuoVadis::Session.first.id)
61
+ phone.assert_redirected_to quo_vadis.sessions_path
62
+
63
+ # phone is still logged in
64
+ assert_equal 'You have logged out of the other session.', phone.flash[:notice]
65
+
66
+ # desktop is logged out
67
+ desktop.get '/articles'
68
+ refute desktop.controller.logged_in?
69
+ end
70
+
71
+
72
+ private
73
+
74
+ # starts a new rails session and logs in
75
+ def login
76
+ open_session do |sess|
77
+ sess.post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
78
+ end
79
+ end
80
+
81
+ # logs in in current session
82
+ def plain_login
83
+ post quo_vadis.login_path(email: 'bob@example.com', password: '123456789abc')
84
+ end
85
+
86
+ end