trek 2.5.0 → 2.7.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.
- checksums.yaml +4 -4
- data/.node-version +1 -1
- data/AGENTS.md +18 -14
- data/CHANGELOG.md +88 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +135 -124
- data/Makefile +9 -1
- data/README.md +17 -21
- data/app/components/trek/dashboard/tile_component/tile_component.css +9 -0
- data/app/components/trek/dashboard/tile_component/tile_component.html.slim +4 -2
- data/app/components/trek/dashboard/tile_component.rb +12 -2
- data/app/components/trek/form/content_editor_component/content_editor_component.css +9 -8
- data/app/components/trek/form/content_editor_component/content_editor_component.html.slim +19 -93
- data/app/components/trek/form/content_editor_component/content_editor_component.js +94 -251
- data/app/components/trek/form/content_editor_component/extensions/block_view.js +40 -0
- data/app/components/trek/form/content_editor_component/extensions/image_block.js +28 -26
- data/app/components/trek/form/content_editor_component/extensions/prompts_block.js +15 -26
- data/app/components/trek/form/content_editor_component/extensions/table_of_contents.js +1 -3
- data/app/components/trek/form/content_editor_component.rb +15 -0
- data/app/components/trek/form/image_field_component/image_field_component.css +40 -1
- data/app/components/trek/form/image_field_component/image_field_component.html.slim +0 -9
- data/app/components/trek/form/image_field_component/image_field_component.js +1 -2
- data/app/components/trek/form/image_field_component.rb +36 -9
- data/app/components/trek/form/password_strength_component/password_strength_component.css +2 -0
- data/app/components/trek/form/sound_field_component/sound_field_component.js +1 -2
- data/app/components/trek/security_check_component/security_check_component.css +69 -0
- data/app/components/trek/security_check_component/security_check_component.html.slim +19 -0
- data/app/components/trek/security_check_component/security_check_component.js +15 -0
- data/app/components/trek/security_check_component.rb +19 -0
- data/app/content/trek/content/cleaner.rb +53 -0
- data/app/content/trek/content/document.rb +2 -2
- data/app/content/trek/content/list_item.rb +1 -1
- data/app/content/trek/content/paragraph.rb +7 -2
- data/app/content/trek/content/prompts_block.rb +1 -2
- data/app/content/trek/content.rb +6 -0
- data/app/controllers/concerns/trek/auditable.rb +5 -2
- data/app/controllers/concerns/trek/localization.rb +6 -2
- data/app/controllers/concerns/trek/reorderable.rb +24 -6
- data/app/controllers/trek/csp_reports_controller.rb +14 -1
- data/app/controllers/trek/panels/prompts_controller.rb +1 -1
- data/app/controllers/trek/resource_controller.rb +5 -5
- data/app/jobs/trek/purge_expired_sessions_job.rb +4 -3
- data/app/jobs/trek/purge_expired_sign_in_codes_job.rb +15 -0
- data/app/models/concerns/trek/contentable.rb +15 -1
- data/app/models/concerns/trek/users/sign_in_code.rb +70 -0
- data/app/models/concerns/trek/users/sign_in_lockout.rb +4 -2
- data/app/policies/trek/security_check_policy.rb +8 -0
- data/app/uploaders/image_uploader.rb +77 -15
- data/app/views/trek/panels/links/_form.html.slim +2 -4
- data/app/views/trek/panels/prompts/create.turbo_stream.slim +0 -1
- data/config/locales/trek.en.yml +97 -11
- data/config/locales/trek.fr.yml +97 -11
- data/docs/.vitepress/config.mjs +5 -1
- data/docs/background-jobs.md +74 -0
- data/docs/content-editor.md +219 -0
- data/docs/customize.md +2 -0
- data/docs/deploy.md +69 -6
- data/docs/getting-started.md +16 -18
- data/docs/index.md +2 -2
- data/docs/personal-data.md +61 -0
- data/docs/proxies.md +2 -2
- data/docs/reference/components.md +4 -4
- data/docs/reference/concerns/contentable.md +10 -4
- data/docs/reference/concerns/formattable.md +1 -1
- data/docs/reference/concerns/fragmentable.md +12 -6
- data/docs/reference/concerns/orderable.md +2 -2
- data/docs/reference/concerns/search-engine-optimizable.md +1 -1
- data/docs/reference/concerns/searchable.md +14 -1
- data/docs/reference/concerns/sectionable.md +7 -3
- data/docs/reference/concerns/versionable.md +1 -1
- data/docs/reference/controllers.md +33 -1
- data/docs/reference/environment-variables.md +3 -2
- data/docs/reference/formatters.md +4 -31
- data/docs/reference/forms.md +7 -4
- data/docs/reference/generators/admin-user.md +1 -1
- data/docs/reference/generators/audit-log.md +12 -13
- data/docs/reference/generators/install.md +23 -11
- data/docs/reference/generators/public-accounts.md +159 -0
- data/docs/reference/generators/scaffold.md +28 -9
- data/docs/reference/generators/security-headers.md +5 -4
- data/docs/reference/generators/taxonomies.md +1 -1
- data/docs/reference/icons.md +5 -3
- data/docs/reference/index.md +10 -7
- data/docs/reference/models/current.md +5 -3
- data/docs/reference/models/page-version.md +1 -1
- data/docs/reference/models/user.md +7 -8
- data/docs/reference/uploaders.md +25 -0
- data/docs/reference/validators.md +1 -1
- data/docs/security.md +30 -9
- data/docs/what-is-trek.md +4 -4
- data/lib/generators/trek/USAGE +1 -1
- data/lib/generators/trek/audit_log_generator.rb +62 -0
- data/lib/generators/trek/helpers.rb +17 -0
- data/lib/generators/trek/install/admin/routes_generator.rb +2 -1
- data/lib/generators/trek/install/admin/security_check_generator.rb +36 -0
- data/lib/generators/trek/install/admin/users_generator.rb +6 -1
- data/lib/generators/trek/install/audit_log_generator.rb +9 -45
- data/lib/generators/trek/install/ci_generator.rb +19 -0
- data/lib/generators/trek/install/good_job_generator.rb +161 -0
- data/lib/generators/trek/install/model_specs_generator.rb +3 -3
- data/lib/generators/trek/install/postcss_generator.rb +1 -2
- data/lib/generators/trek/install/ruby_generator.rb +1 -1
- data/lib/generators/trek/install/scalingo_generator.rb +7 -45
- data/lib/generators/trek/install/translations_generator.rb +1 -2
- data/lib/generators/trek/install_generator.rb +18 -2
- data/lib/generators/trek/public_accounts_generator.rb +294 -0
- data/lib/generators/trek/scaffold_generator.rb +22 -21
- data/lib/generators/trek/scalingo_generator.rb +67 -0
- data/lib/generators/trek/templates/.gitlab-ci.yml +10 -1
- data/lib/generators/trek/templates/AGENTS.md +2 -2
- data/lib/generators/trek/templates/Makefile +1 -0
- data/lib/generators/trek/templates/config/i18n-tasks.yml.erb +0 -8
- data/lib/generators/trek/templates/controllers/admin/password_renewals_controller.rb +11 -0
- data/lib/generators/trek/templates/controllers/admin/security_checks_controller.rb +11 -0
- data/lib/generators/trek/templates/controllers/admin/user_sessions_controller.rb +1 -1
- data/lib/generators/trek/templates/controllers/admin/users_controller.rb +0 -8
- data/lib/generators/trek/templates/controllers/concerns/trek/authentication.rb +13 -4
- data/lib/generators/trek/templates/initializers/content_security_policy.rb +3 -2
- data/lib/generators/trek/templates/initializers/good_job.rb.tt +40 -0
- data/lib/generators/trek/templates/jobs/mail_delivery_job.rb +6 -0
- data/lib/generators/trek/templates/layouts/admin.html.slim +10 -0
- data/lib/generators/trek/templates/mailers/admin_mailer.rb +4 -2
- data/lib/generators/trek/templates/package.json.tt +1 -1
- data/lib/generators/trek/templates/policies/security_check.rb +4 -0
- data/lib/generators/trek/templates/public_accounts/api/authentication.rb.tt +76 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/accounts_controller.rb.tt +22 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/active_sessions_controller.rb.tt +21 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/base_controller.rb.tt +71 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/email_changes_controller.rb.tt +55 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/email_confirmations_controller.rb.tt +19 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/password_resets_controller.rb.tt +28 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/passwords_controller.rb.tt +17 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/registrations_controller.rb.tt +17 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/sessions_controller.rb.tt +57 -0
- data/lib/generators/trek/templates/public_accounts/api/controllers/sign_in_codes_controller.rb.tt +20 -0
- data/lib/generators/trek/templates/public_accounts/jobs/sign_in_code_job.rb.tt +23 -0
- data/lib/generators/trek/templates/public_accounts/locales/accounts.en.yml.tt +215 -0
- data/lib/generators/trek/templates/public_accounts/locales/accounts.fr.yml.tt +215 -0
- data/lib/generators/trek/templates/public_accounts/mailers/account_mailer.rb.tt +85 -0
- data/lib/generators/trek/templates/public_accounts/migrations/create_accounts.rb.tt +46 -0
- data/lib/generators/trek/templates/public_accounts/models/account/session.rb.tt +47 -0
- data/lib/generators/trek/templates/public_accounts/models/account/sign_in_code.rb.tt +8 -0
- data/lib/generators/trek/templates/public_accounts/models/account.rb.tt +87 -0
- data/lib/generators/trek/templates/public_accounts/spec/api/accounts_spec.rb.tt +90 -0
- data/lib/generators/trek/templates/public_accounts/spec/api/sign_ins_spec.rb.tt +188 -0
- data/lib/generators/trek/templates/public_accounts/spec/models/account/sign_in_code_spec.rb.tt +66 -0
- data/lib/generators/trek/templates/public_accounts/spec/models/account_spec.rb.tt +129 -0
- data/lib/generators/trek/templates/public_accounts/spec/web/accounts_spec.rb.tt +126 -0
- data/lib/generators/trek/templates/public_accounts/spec/web/authentication_spec.rb.tt +43 -0
- data/lib/generators/trek/templates/public_accounts/spec/web/sign_ins_spec.rb.tt +237 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change.en.html.slim.tt +10 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change.en.text.erb.tt +8 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change.fr.html.slim.tt +13 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change.fr.text.erb.tt +8 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change_code.en.html.slim.tt +9 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change_code.en.text.erb.tt +11 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change_code.fr.html.slim.tt +11 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_change_code.fr.text.erb.tt +11 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_confirmation.en.html.slim.tt +10 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_confirmation.en.text.erb.tt +8 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_confirmation.fr.html.slim.tt +10 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/email_confirmation.fr.text.erb.tt +8 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/password_reset.en.html.slim.tt +10 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/password_reset.en.text.erb.tt +8 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/password_reset.fr.html.slim.tt +13 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/password_reset.fr.text.erb.tt +8 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_code.en.html.slim.tt +9 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_code.en.text.erb.tt +11 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_code.fr.html.slim.tt +9 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_code.fr.text.erb.tt +11 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_locked.en.html.slim.tt +23 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_locked.en.text.erb.tt +14 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_locked.fr.html.slim.tt +24 -0
- data/lib/generators/trek/templates/public_accounts/views/mailer/sign_in_locked.fr.text.erb.tt +14 -0
- data/lib/generators/trek/templates/public_accounts/web/authentication.rb.tt +97 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/accounts_controller.rb.tt +33 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/active_sessions_controller.rb.tt +15 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/base_controller.rb.tt +52 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/email_changes_controller.rb.tt +85 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/email_confirmations_controller.rb.tt +19 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/password_resets_controller.rb.tt +41 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/passwords_controller.rb.tt +23 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/registrations_controller.rb.tt +24 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/sessions_controller.rb.tt +85 -0
- data/lib/generators/trek/templates/public_accounts/web/controllers/sign_in_codes_controller.rb.tt +32 -0
- data/lib/generators/trek/templates/public_accounts/web/views/_flash.html.slim.tt +5 -0
- data/lib/generators/trek/templates/public_accounts/web/views/accounts/show.html.slim.tt +52 -0
- data/lib/generators/trek/templates/public_accounts/web/views/email_changes/edit.html.slim.tt +17 -0
- data/lib/generators/trek/templates/public_accounts/web/views/email_changes/new.html.slim.tt +15 -0
- data/lib/generators/trek/templates/public_accounts/web/views/password_resets/edit.html.slim.tt +29 -0
- data/lib/generators/trek/templates/public_accounts/web/views/password_resets/new.html.slim.tt +15 -0
- data/lib/generators/trek/templates/public_accounts/web/views/passwords/edit.html.slim.tt +28 -0
- data/lib/generators/trek/templates/public_accounts/web/views/registrations/new.html.slim.tt +30 -0
- data/lib/generators/trek/templates/public_accounts/web/views/sessions/new.html.slim.tt +40 -0
- data/lib/generators/trek/templates/public_accounts/web/views/sign_in_codes/new.html.slim.tt +17 -0
- data/lib/generators/trek/templates/spec/jobs/mail_delivery_job_spec.rb +11 -0
- data/lib/generators/trek/templates/spec/mailers/admin_mailer_spec.rb +10 -0
- data/lib/generators/trek/templates/spec/requests/admin/fragments_spec.rb +1 -1
- data/lib/generators/trek/templates/spec/requests/admin/good_job_spec.rb +34 -0
- data/lib/generators/trek/templates/spec/requests/admin/pages_spec.rb +12 -1
- data/lib/generators/trek/templates/spec/requests/admin/panels_spec.rb +16 -0
- data/lib/generators/trek/templates/spec/requests/admin/password_renewal_spec.rb +2 -2
- data/lib/generators/trek/templates/spec/requests/admin/security_checks_spec.rb +24 -0
- data/lib/generators/trek/templates/spec/requests/admin/sign_in_lockout_spec.rb +1 -1
- data/lib/generators/trek/templates/spec/requests/admin/sign_in_notices_spec.rb +33 -0
- data/lib/generators/trek/templates/spec/requests/admin/user_sessions_spec.rb +2 -2
- data/lib/generators/trek/templates/views/admin/pages/_form.html.slim +1 -1
- data/lib/generators/trek/templates/views/admin/security_checks/show.html.slim +9 -0
- data/lib/generators/trek/templates/views/admin/user_password_resets/edit.html.slim +6 -1
- data/lib/generators/trek/templates/views/admin/user_sessions/new.html.slim +4 -1
- data/lib/generators/trek/templates/views/admin/users/index.html.slim +1 -1
- data/lib/generators/trek/templates/views/admin_mailer/new_sign_in.en.html.slim +1 -1
- data/lib/generators/trek/templates/views/admin_mailer/new_sign_in.en.text.erb +1 -1
- data/lib/generators/trek/templates/views/admin_mailer/new_sign_in.fr.html.slim +1 -1
- data/lib/generators/trek/templates/views/admin_mailer/new_sign_in.fr.text.erb +1 -1
- data/lib/tasks/trek/attachments.rake +12 -0
- data/lib/tasks/trek/security.rake +10 -0
- data/lib/trek/csp_reports.rb +22 -4
- data/lib/trek/disposable_email_domains.rb +9 -20
- data/lib/trek/engine.rb +5 -0
- data/lib/trek/json_compatibility.rb +20 -0
- data/lib/trek/security_check.rb +241 -0
- data/lib/trek/stored_image_metadata.rb +88 -0
- data/lib/trek/typography_formatter.rb +35 -4
- data/lib/trek/version.rb +1 -1
- data/lib/trek.rb +2 -1
- data/package.json +16 -13
- data/postcss.config.js +0 -3
- data/trek.gemspec +6 -7
- data/vitest.config.mjs +24 -0
- data/yarn.lock +1667 -1010
- metadata +122 -33
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
<% if invite_only? -%>
|
|
2
|
+
# A <%= human_name.downcase %>'s account, apart from the admin's User: it signs in with a code emailed to it
|
|
3
|
+
# (<%= class_name %>::SignInCode). Only the accounts the app created sign in (an admin, an invitation):
|
|
4
|
+
# signing in never creates one.
|
|
5
|
+
<% elsif code? -%>
|
|
6
|
+
# A <%= human_name.downcase %>'s account, apart from the admin's User: it signs in with a code emailed to it
|
|
7
|
+
# (<%= class_name %>::SignInCode), which creates the account the first time.
|
|
8
|
+
<% else -%>
|
|
9
|
+
# A <%= human_name.downcase %>'s account, apart from the admin's User: it signs up and in with a password, its
|
|
10
|
+
# email confirmed by a link (confirmed_at), and a forgotten password reset by one.
|
|
11
|
+
<% end -%>
|
|
12
|
+
class <%= class_name %> < ApplicationRecord
|
|
13
|
+
<% if password? -%>
|
|
14
|
+
# Required, at most 72 bytes; password_reset_token (15 minutes, used up by a new password).
|
|
15
|
+
has_secure_password
|
|
16
|
+
# The password rules (length, complexity, common passwords…); set password_min_length to change them.
|
|
17
|
+
include Trek::Users::PasswordPolicy
|
|
18
|
+
# Locks signing in for a while after 5 failures in a row, longer on each further failure.
|
|
19
|
+
include Trek::Users::SignInLockout
|
|
20
|
+
self.sign_in_lockout_mailer = "<%= class_name %>Mailer"
|
|
21
|
+
|
|
22
|
+
# The link confirming the email, used up once it is confirmed or changed.
|
|
23
|
+
generates_token_for :email_confirmation, expires_in: 1.day do
|
|
24
|
+
[email, confirmed_at]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# The link confirming a new email (unconfirmed_email), used up once it is changed.
|
|
28
|
+
generates_token_for :email_change, expires_in: 1.hour do
|
|
29
|
+
[email, unconfirmed_email]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
<% end -%>
|
|
33
|
+
has_many :sessions, dependent: :delete_all
|
|
34
|
+
<% if code? -%>
|
|
35
|
+
has_many :sign_in_codes, dependent: :delete_all
|
|
36
|
+
# A sign-in code is its email's, not linked to an account, which may not exist yet: those waiting
|
|
37
|
+
# for the account's email go with it too.
|
|
38
|
+
after_destroy { <%= class_name %>::SignInCode.where(email:, purpose: "sign_in").delete_all }
|
|
39
|
+
<% end -%>
|
|
40
|
+
|
|
41
|
+
# Also applies to lookups (find_by(email:)), so signing in works whatever the case typed.
|
|
42
|
+
normalizes :email<% if password? %>, :unconfirmed_email<% end %>, with: ->(email) { email.strip.downcase }
|
|
43
|
+
|
|
44
|
+
validates :email, presence: true, uniqueness: { case_sensitive: false }, email: { mode: :strict }
|
|
45
|
+
<% if password? -%>
|
|
46
|
+
validates :unconfirmed_email, email: { mode: :strict }, allow_nil: true
|
|
47
|
+
<% end -%>
|
|
48
|
+
|
|
49
|
+
# The accounts nobody used since `time`, e.g. to delete them: they can only be <%= human_name.pluralize.downcase %>.
|
|
50
|
+
scope :inactive_since, ->(time) { where(last_activity_at: ...time).or(where(last_activity_at: nil, created_at: ...time)) }
|
|
51
|
+
<% if invite_only? -%>
|
|
52
|
+
|
|
53
|
+
# The account `email` signs in to, nil if it has none: no code is sent to it then. Narrow it to
|
|
54
|
+
# let only some accounts in, e.g. `active.find_by(email:)`.
|
|
55
|
+
def self.for_sign_in(email)
|
|
56
|
+
find_by(email:)
|
|
57
|
+
end
|
|
58
|
+
<% elsif code? -%>
|
|
59
|
+
|
|
60
|
+
# The account of `email`, created if it has none: the code it was sent proved the email is theirs.
|
|
61
|
+
def self.for_verified_email!(email)
|
|
62
|
+
find_by(email:) || create!(email:)
|
|
63
|
+
rescue ActiveRecord::RecordNotUnique
|
|
64
|
+
# Created in parallel, e.g. a double submit
|
|
65
|
+
find_by!(email:)
|
|
66
|
+
end
|
|
67
|
+
<% else -%>
|
|
68
|
+
|
|
69
|
+
def confirmed?
|
|
70
|
+
confirmed_at.present?
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def confirm_email!
|
|
74
|
+
update!(confirmed_at: Time.current)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# The new email once its link is followed, confirmed by it. False when another account took it
|
|
78
|
+
# in the meantime.
|
|
79
|
+
def change_email!
|
|
80
|
+
update(email: unconfirmed_email, unconfirmed_email: nil, confirmed_at: Time.current)
|
|
81
|
+
end
|
|
82
|
+
<% end -%>
|
|
83
|
+
|
|
84
|
+
def to_s
|
|
85
|
+
name.presence || email
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
require "rails_helper"
|
|
2
|
+
|
|
3
|
+
RSpec.describe "<%= human_name.pluralize %>' accounts through the API", type: :request do
|
|
4
|
+
include ActiveSupport::Testing::TimeHelpers
|
|
5
|
+
|
|
6
|
+
<% if code? -%>
|
|
7
|
+
let(:<%= file_name %>) { <%= class_name %>.create!(email: "ada@example.com") }
|
|
8
|
+
<% else -%>
|
|
9
|
+
let(:<%= file_name %>) { <%= class_name %>.create!(email: "ada@example.com", password: "correct horse battery staple") }
|
|
10
|
+
<% end -%>
|
|
11
|
+
let(:token) { sign_in(<%= file_name %>) }
|
|
12
|
+
let(:headers) { { "Authorization" => "Bearer #{token}" } }
|
|
13
|
+
|
|
14
|
+
<% if code? -%>
|
|
15
|
+
def emailed_code
|
|
16
|
+
ActionMailer::Base.deliveries.last.text_part.body.to_s[/\b\d{6}\b/]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def sign_in(<%= file_name %>)
|
|
20
|
+
perform_enqueued_jobs { post <%= route_prefix %>_sign_in_path, params: { email: <%= file_name %>.email }, as: :json }
|
|
21
|
+
post <%= route_prefix %>_sign_in_code_path, params: { email: <%= file_name %>.email, otp: emailed_code }, as: :json
|
|
22
|
+
response.parsed_body["token"]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def change_email_to(email)
|
|
26
|
+
perform_enqueued_jobs { post <%= route_prefix %>_email_change_path, params: { email: }, headers:, as: :json }
|
|
27
|
+
patch <%= route_prefix %>_email_change_path, params: { email:, otp: emailed_code }, headers:, as: :json
|
|
28
|
+
end
|
|
29
|
+
<% else -%>
|
|
30
|
+
def emailed_token
|
|
31
|
+
CGI.unescape(ActionMailer::Base.deliveries.last.text_part.body.to_s[/token=([^\s&]+)/, 1].to_s)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def sign_in(<%= file_name %>)
|
|
35
|
+
post <%= route_prefix %>_sign_in_path, params: { email: <%= file_name %>.email, password: "correct horse battery staple" }, as: :json
|
|
36
|
+
response.parsed_body["token"]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def change_email_to(email)
|
|
40
|
+
perform_enqueued_jobs { post <%= route_prefix %>_email_change_path, params: { email: }, headers:, as: :json }
|
|
41
|
+
patch <%= route_prefix %>_email_change_path, params: { token: emailed_token }, as: :json
|
|
42
|
+
end
|
|
43
|
+
<% end -%>
|
|
44
|
+
|
|
45
|
+
it "changes the name" do
|
|
46
|
+
patch <%= route_prefix %>_account_path, params: { name: "Ada Lovelace" }, headers:, as: :json
|
|
47
|
+
|
|
48
|
+
expect(response.parsed_body["name"]).to eq("Ada Lovelace")
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it "changes the email once the new one is confirmed", :aggregate_failures do
|
|
52
|
+
change_email_to("ada@lovelace.org")
|
|
53
|
+
|
|
54
|
+
expect(ActionMailer::Base.deliveries.last.to).to eq(["ada@lovelace.org"])
|
|
55
|
+
expect(<%= file_name %>.reload.email).to eq("ada@lovelace.org")
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "lists the signed-in devices, and signs the others out", :aggregate_failures do
|
|
59
|
+
other = <%= file_name %>.sessions.create!
|
|
60
|
+
get <%= route_prefix %>_active_sessions_path, headers: headers
|
|
61
|
+
|
|
62
|
+
expect(response.parsed_body.pluck("current")).to contain_exactly(true, false)
|
|
63
|
+
delete others_<%= route_prefix %>_active_sessions_path, headers: headers
|
|
64
|
+
expect(<%= class_name %>::Session.exists?(other.id)).to be(false)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it "deletes the account after a recent sign-in", :aggregate_failures do
|
|
68
|
+
headers
|
|
69
|
+
|
|
70
|
+
expect { delete <%= route_prefix %>_account_path, headers: }.to change(<%= class_name %>, :count).by(-1)
|
|
71
|
+
expect(response).to have_http_status(:no_content)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it "asks to sign in again before deleting the account", :aggregate_failures do
|
|
75
|
+
headers
|
|
76
|
+
travel <%= class_name %>::Session::RECENT + 1.minute
|
|
77
|
+
|
|
78
|
+
expect { delete <%= route_prefix %>_account_path, headers: }.not_to change(<%= class_name %>, :count)
|
|
79
|
+
expect(response.parsed_body["error"]).to eq("recent_sign_in_needed")
|
|
80
|
+
end
|
|
81
|
+
<% if password? -%>
|
|
82
|
+
|
|
83
|
+
it "changes the password after a recent sign-in", :aggregate_failures do
|
|
84
|
+
patch <%= route_prefix %>_password_path, params: { password: "a brand new password 1!" }, headers:, as: :json
|
|
85
|
+
|
|
86
|
+
expect(response).to have_http_status(:no_content)
|
|
87
|
+
expect(<%= file_name %>.reload.authenticate("a brand new password 1!")).to be_truthy
|
|
88
|
+
end
|
|
89
|
+
<% end -%>
|
|
90
|
+
end
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
require "rails_helper"
|
|
2
|
+
|
|
3
|
+
RSpec.describe "<%= human_name.pluralize %>' sign-ins through the API", type: :request do
|
|
4
|
+
include ActiveSupport::Testing::TimeHelpers
|
|
5
|
+
|
|
6
|
+
<% if code? -%>
|
|
7
|
+
def emailed_code
|
|
8
|
+
ActionMailer::Base.deliveries.last.text_part.body.to_s[/\b\d{6}\b/]
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# The response's status
|
|
12
|
+
def ask_for_code(email)
|
|
13
|
+
perform_enqueued_jobs { post <%= route_prefix %>_sign_in_path, params: { email: }, as: :json }
|
|
14
|
+
response.status
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def sign_in(email)
|
|
18
|
+
<% if invite_only? -%>
|
|
19
|
+
# Accounts are the app's to create: signing in never does
|
|
20
|
+
<%= class_name %>.find_or_create_by!(email:)
|
|
21
|
+
<% end -%>
|
|
22
|
+
ask_for_code(email)
|
|
23
|
+
post <%= route_prefix %>_sign_in_code_path, params: { email:, otp: emailed_code }, as: :json
|
|
24
|
+
response.parsed_body["token"]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
<% if invite_only? -%>
|
|
28
|
+
it "emails no code to an email without an account, with the same answer as for an account", :aggregate_failures do
|
|
29
|
+
<%= class_name %>.create!(email: "ada@example.com")
|
|
30
|
+
|
|
31
|
+
expect(ask_for_code("grace@example.com")).to eq(ask_for_code("ada@example.com")).and eq(202)
|
|
32
|
+
expect(ActionMailer::Base.deliveries.flat_map(&:to)).to eq(%w[ada@example.com])
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "creates no account for an email without one", :aggregate_failures do
|
|
36
|
+
ask_for_code("grace@example.com")
|
|
37
|
+
|
|
38
|
+
expect { post <%= route_prefix %>_sign_in_code_path, params: { email: "grace@example.com", otp: "123456" }, as: :json }
|
|
39
|
+
.not_to change(<%= class_name %>, :count)
|
|
40
|
+
expect(response).to have_http_status(:unauthorized)
|
|
41
|
+
end
|
|
42
|
+
<% else -%>
|
|
43
|
+
it "emails a code to an email without an account, with the same answer as for an account", :aggregate_failures do
|
|
44
|
+
<%= class_name %>.create!(email: "ada@example.com")
|
|
45
|
+
|
|
46
|
+
expect(ask_for_code("grace@example.com")).to eq(ask_for_code("ada@example.com")).and eq(202)
|
|
47
|
+
expect(ActionMailer::Base.deliveries.flat_map(&:to)).to eq(%w[grace@example.com ada@example.com])
|
|
48
|
+
end
|
|
49
|
+
<% end -%>
|
|
50
|
+
|
|
51
|
+
it "refuses an invalid email", :aggregate_failures do
|
|
52
|
+
post <%= route_prefix %>_sign_in_path, params: { email: "not an email" }, as: :json
|
|
53
|
+
|
|
54
|
+
expect(response).to have_http_status(:unprocessable_content)
|
|
55
|
+
expect(response.parsed_body["error"]).to eq("invalid_email")
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
<% if invite_only? -%>
|
|
59
|
+
it "signs in to the email's account and answers with a token", :aggregate_failures do
|
|
60
|
+
token = sign_in("ada@example.com")
|
|
61
|
+
|
|
62
|
+
expect(response).to have_http_status(:created)
|
|
63
|
+
expect(token).to be_present
|
|
64
|
+
end
|
|
65
|
+
<% else -%>
|
|
66
|
+
it "creates the account and answers with a token", :aggregate_failures do
|
|
67
|
+
token = nil
|
|
68
|
+
|
|
69
|
+
expect { token = sign_in("grace@example.com") }.to change(<%= class_name %>, :count).by(1)
|
|
70
|
+
expect(response).to have_http_status(:created)
|
|
71
|
+
expect(token).to be_present
|
|
72
|
+
end
|
|
73
|
+
<% end -%>
|
|
74
|
+
|
|
75
|
+
it "refuses a wrong code", :aggregate_failures do
|
|
76
|
+
ask_for_code("ada@example.com")
|
|
77
|
+
post <%= route_prefix %>_sign_in_code_path, params: { email: "ada@example.com", otp: "000000" }, as: :json
|
|
78
|
+
|
|
79
|
+
expect(response).to have_http_status(:unauthorized)
|
|
80
|
+
expect(response.parsed_body["error"]).to eq("invalid_code")
|
|
81
|
+
end
|
|
82
|
+
<% else -%>
|
|
83
|
+
let(:password) { "correct horse battery staple" }
|
|
84
|
+
let!(:<%= file_name %>) { <%= class_name %>.create!(email: "ada@example.com", password:) }
|
|
85
|
+
|
|
86
|
+
def emailed_token
|
|
87
|
+
CGI.unescape(ActionMailer::Base.deliveries.last.text_part.body.to_s[/token=([^\s&]+)/, 1].to_s)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def sign_in(email, password: self.password)
|
|
91
|
+
post <%= route_prefix %>_sign_in_path, params: { email:, password: }, as: :json
|
|
92
|
+
response.parsed_body["token"]
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it "signs up and in, answering with a token, and emails a link to confirm the email", :aggregate_failures do
|
|
96
|
+
perform_enqueued_jobs { post <%= route_prefix %>_sign_up_path, params: { email: "grace@example.com", password: }, as: :json }
|
|
97
|
+
|
|
98
|
+
expect(response).to have_http_status(:created)
|
|
99
|
+
expect(response.parsed_body["token"]).to be_present
|
|
100
|
+
expect(ActionMailer::Base.deliveries.last.to).to eq(["grace@example.com"])
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
it "confirms the email with the token of the emailed link" do
|
|
104
|
+
perform_enqueued_jobs { post <%= route_prefix %>_sign_up_path, params: { email: "grace@example.com", password: }, as: :json }
|
|
105
|
+
|
|
106
|
+
patch <%= route_prefix %>_email_confirmation_path, params: { token: emailed_token }, as: :json
|
|
107
|
+
|
|
108
|
+
expect(<%= class_name %>.find_by(email: "grace@example.com")).to be_confirmed
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
it "answers with a token" do
|
|
112
|
+
expect(sign_in(<%= file_name %>.email)).to be_present
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it "refuses a wrong password", :aggregate_failures do
|
|
116
|
+
sign_in(<%= file_name %>.email, password: "wrong horse battery staple")
|
|
117
|
+
|
|
118
|
+
expect(response).to have_http_status(:unauthorized)
|
|
119
|
+
expect(response.parsed_body["error"]).to eq("invalid_password")
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
it "resets the password with the token of the emailed link", :aggregate_failures do
|
|
123
|
+
perform_enqueued_jobs { post <%= route_prefix %>_password_reset_path, params: { email: <%= file_name %>.email }, as: :json }
|
|
124
|
+
expect(response).to have_http_status(:accepted)
|
|
125
|
+
|
|
126
|
+
patch <%= route_prefix %>_password_reset_path, params: { token: emailed_token, password: "a brand new password 1!" }, as: :json
|
|
127
|
+
expect(<%= file_name %>.reload.authenticate("a brand new password 1!")).to be_truthy
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
it "answers a password reset the same for an unknown email, and sends nothing", :aggregate_failures do
|
|
131
|
+
perform_enqueued_jobs { post <%= route_prefix %>_password_reset_path, params: { email: "nobody@example.com" }, as: :json }
|
|
132
|
+
|
|
133
|
+
expect(response).to have_http_status(:accepted)
|
|
134
|
+
expect(ActionMailer::Base.deliveries).to be_empty
|
|
135
|
+
end
|
|
136
|
+
<% end -%>
|
|
137
|
+
|
|
138
|
+
<% if audit_log? -%>
|
|
139
|
+
it "records the sign-in in the audit log" do
|
|
140
|
+
sign_in("ada@example.com")
|
|
141
|
+
|
|
142
|
+
expect(AuditLog.where(action: "<%= controllers_path %>/sessions#create", actor: <%= class_name %>.last)).to exist
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
<% end -%>
|
|
146
|
+
it "uses the token for the next requests" do
|
|
147
|
+
token = sign_in("ada@example.com")
|
|
148
|
+
|
|
149
|
+
get <%= route_prefix %>_account_path, headers: { "Authorization" => "Bearer #{token}" }
|
|
150
|
+
|
|
151
|
+
expect(response.parsed_body["email"]).to eq("ada@example.com")
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
it "refuses a request without a token, or with a forged one", :aggregate_failures do
|
|
155
|
+
get <%= route_prefix %>_account_path
|
|
156
|
+
expect(response).to have_http_status(:unauthorized)
|
|
157
|
+
|
|
158
|
+
token = sign_in("ada@example.com")
|
|
159
|
+
get <%= route_prefix %>_account_path, headers: { "Authorization" => "Bearer #{token.chop}x" }
|
|
160
|
+
expect(response).to have_http_status(:unauthorized)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
it "ends the token's session when signing out" do
|
|
164
|
+
token = sign_in("ada@example.com")
|
|
165
|
+
|
|
166
|
+
delete <%= route_prefix %>_session_path, headers: { "Authorization" => "Bearer #{token}" }
|
|
167
|
+
get <%= route_prefix %>_account_path, headers: { "Authorization" => "Bearer #{token}" }
|
|
168
|
+
|
|
169
|
+
expect(response).to have_http_status(:unauthorized)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
it "refuses the token after the session's idle timeout" do
|
|
173
|
+
token = sign_in("ada@example.com")
|
|
174
|
+
|
|
175
|
+
travel <%= class_name %>::Session.idle_timeout + 1.minute
|
|
176
|
+
get <%= route_prefix %>_account_path, headers: { "Authorization" => "Bearer #{token}" }
|
|
177
|
+
|
|
178
|
+
expect(response).to have_http_status(:unauthorized)
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
it "refuses a token signed with another key" do
|
|
182
|
+
forged = JWT.encode({ sub: <%= class_name %>::Session.maximum(:id).to_i + 1 }, "wrong key", "HS256")
|
|
183
|
+
|
|
184
|
+
get <%= route_prefix %>_account_path, headers: { "Authorization" => "Bearer #{forged}" }
|
|
185
|
+
|
|
186
|
+
expect(response).to have_http_status(:unauthorized)
|
|
187
|
+
end
|
|
188
|
+
end
|
data/lib/generators/trek/templates/public_accounts/spec/models/account/sign_in_code_spec.rb.tt
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
require "rails_helper"
|
|
2
|
+
|
|
3
|
+
RSpec.describe <%= class_name %>::SignInCode, type: :model do
|
|
4
|
+
include ActiveSupport::Testing::TimeHelpers
|
|
5
|
+
|
|
6
|
+
it "keeps a digest of the code, not the code", :aggregate_failures do
|
|
7
|
+
sign_in_code = described_class.issue("ada@example.com")
|
|
8
|
+
|
|
9
|
+
expect(sign_in_code.code).to match(/\A\d{6}\z/)
|
|
10
|
+
expect(described_class.find(sign_in_code.id).code_digest).not_to include(sign_in_code.code)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "signs in once with the right code", :aggregate_failures do
|
|
14
|
+
code = described_class.issue("ada@example.com").code
|
|
15
|
+
|
|
16
|
+
expect(described_class.redeem("Ada@example.com", code)).to be_present
|
|
17
|
+
expect(described_class.redeem("ada@example.com", code)).to be_nil
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "accepts the code with spaces" do
|
|
21
|
+
code = described_class.issue("ada@example.com").code
|
|
22
|
+
|
|
23
|
+
expect(described_class.redeem("ada@example.com", "#{code[0, 3]} #{code[3..]}")).to be_present
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "replaces the email's earlier code" do
|
|
27
|
+
first = described_class.issue("ada@example.com").code
|
|
28
|
+
described_class.issue("ada@example.com")
|
|
29
|
+
|
|
30
|
+
expect(described_class.redeem("ada@example.com", first)).to be_nil
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "stops working after its validity" do
|
|
34
|
+
code = described_class.issue("ada@example.com").code
|
|
35
|
+
|
|
36
|
+
travel described_class.validity + 1.second do
|
|
37
|
+
expect(described_class.redeem("ada@example.com", code)).to be_nil
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Each holds an email, and nothing else deletes the last ones requested
|
|
42
|
+
it "is deleted by the nightly purge once expired" do
|
|
43
|
+
current = described_class.issue("grace@example.com")
|
|
44
|
+
described_class.issue("ada@example.com").update!(expires_at: 1.minute.ago)
|
|
45
|
+
|
|
46
|
+
Trek::PurgeExpiredSignInCodesJob.perform_now("<%= class_name %>::SignInCode")
|
|
47
|
+
|
|
48
|
+
expect(described_class.all).to contain_exactly(current)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it "stops working after too many wrong tries, even with the right code" do
|
|
52
|
+
code = described_class.issue("ada@example.com").code
|
|
53
|
+
wrong = code.succ.rjust(6, "0")[-6..]
|
|
54
|
+
described_class.max_attempts.times { described_class.redeem("ada@example.com", wrong) }
|
|
55
|
+
|
|
56
|
+
expect(described_class.redeem("ada@example.com", code)).to be_nil
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it "doesn't sign in with a code confirming a new email", :aggregate_failures do
|
|
60
|
+
<%= file_name %> = <%= class_name %>.create!(email: "ada@example.com")
|
|
61
|
+
code = described_class.issue("new@example.com", purpose: "email_change", <%= file_name %>:).code
|
|
62
|
+
|
|
63
|
+
expect(described_class.redeem("new@example.com", code)).to be_nil
|
|
64
|
+
expect(described_class.redeem("new@example.com", code, purpose: "email_change", <%= file_name %>:)).to be_present
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
require "rails_helper"
|
|
2
|
+
|
|
3
|
+
RSpec.describe <%= class_name %>, type: :model do
|
|
4
|
+
include ActiveSupport::Testing::TimeHelpers
|
|
5
|
+
|
|
6
|
+
<% if password? -%>
|
|
7
|
+
let(:password) { "correct horse battery staple" }
|
|
8
|
+
|
|
9
|
+
<% end -%>
|
|
10
|
+
def create_<%= file_name %>(email, **attributes)
|
|
11
|
+
described_class.create!(email:<% if password? %>, password:<% end %>, **attributes)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "keeps emails in lower case, without spaces" do
|
|
15
|
+
expect(create_<%= file_name %>(" Ada@Example.com ").email).to eq("ada@example.com")
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "refuses an email another account has, whatever its case" do
|
|
19
|
+
create_<%= file_name %>("ada@example.com")
|
|
20
|
+
|
|
21
|
+
expect(described_class.new(email: "ADA@example.com")).not_to be_valid
|
|
22
|
+
end
|
|
23
|
+
<% if invite_only? -%>
|
|
24
|
+
|
|
25
|
+
it "finds the account an email signs in to, whatever its case, without creating any", :aggregate_failures do
|
|
26
|
+
<%= file_name %> = create_<%= file_name %>("ada@example.com")
|
|
27
|
+
|
|
28
|
+
expect(described_class.for_sign_in("Ada@example.com")).to eq(<%= file_name %>)
|
|
29
|
+
expect(described_class.for_sign_in("grace@example.com")).to be_nil
|
|
30
|
+
end
|
|
31
|
+
<% elsif code? -%>
|
|
32
|
+
|
|
33
|
+
it "creates the account of a verified email once" do
|
|
34
|
+
first = described_class.for_verified_email!("ada@example.com")
|
|
35
|
+
|
|
36
|
+
expect(described_class.for_verified_email!("Ada@example.com")).to eq(first)
|
|
37
|
+
end
|
|
38
|
+
<% end -%>
|
|
39
|
+
|
|
40
|
+
it "finds the accounts nobody used since a date", :aggregate_failures do
|
|
41
|
+
used = create_<%= file_name %>("used@example.com", last_activity_at: 1.day.ago)
|
|
42
|
+
idle = create_<%= file_name %>("idle@example.com", last_activity_at: 2.years.ago)
|
|
43
|
+
never = travel_to(2.years.ago) { create_<%= file_name %>("never@example.com") }
|
|
44
|
+
|
|
45
|
+
expect(described_class.inactive_since(1.year.ago)).to contain_exactly(idle, never)
|
|
46
|
+
expect(described_class.inactive_since(1.year.ago)).not_to include(used)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "deletes its sessions with it" do
|
|
50
|
+
<%= file_name %> = create_<%= file_name %>("ada@example.com")
|
|
51
|
+
<%= file_name %>.sessions.create!
|
|
52
|
+
|
|
53
|
+
expect { <%= file_name %>.destroy }.to change(<%= class_name %>::Session, :count).by(-1)
|
|
54
|
+
end
|
|
55
|
+
<% if code? -%>
|
|
56
|
+
|
|
57
|
+
it "deletes the sign-in codes waiting for its email with it, which it doesn't own" do
|
|
58
|
+
<%= file_name %> = create_<%= file_name %>("ada@example.com")
|
|
59
|
+
<%= class_name %>::SignInCode.issue("ada@example.com")
|
|
60
|
+
other = <%= class_name %>::SignInCode.issue("grace@example.com")
|
|
61
|
+
|
|
62
|
+
<%= file_name %>.destroy
|
|
63
|
+
|
|
64
|
+
expect(<%= class_name %>::SignInCode.all).to contain_exactly(other)
|
|
65
|
+
end
|
|
66
|
+
<% end -%>
|
|
67
|
+
|
|
68
|
+
it "loses its expired sessions to the daily purge" do
|
|
69
|
+
<%= file_name %> = create_<%= file_name %>("ada@example.com")
|
|
70
|
+
<%= file_name %>.sessions.create!(created_at: 2.years.ago, last_activity_at: 2.years.ago)
|
|
71
|
+
current = <%= file_name %>.sessions.create!
|
|
72
|
+
|
|
73
|
+
Trek::PurgeExpiredSessionsJob.perform_now("<%= class_name %>::Session")
|
|
74
|
+
|
|
75
|
+
expect(<%= file_name %>.sessions.reload).to contain_exactly(current)
|
|
76
|
+
end
|
|
77
|
+
<% if password? -%>
|
|
78
|
+
|
|
79
|
+
describe "password" do
|
|
80
|
+
it "is required" do
|
|
81
|
+
expect(described_class.new(email: "ada@example.com")).not_to be_valid
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it "follows the password rules" do
|
|
85
|
+
expect(described_class.new(email: "ada@example.com", password: "short")).not_to be_valid
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
describe "email confirmation" do
|
|
90
|
+
let(:<%= file_name %>) { create_<%= file_name %>("ada@example.com") }
|
|
91
|
+
|
|
92
|
+
it "is pending after signing up" do
|
|
93
|
+
expect(<%= file_name %>).not_to be_confirmed
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
it "finds the account by its link, once", :aggregate_failures do
|
|
97
|
+
token = <%= file_name %>.generate_token_for(:email_confirmation)
|
|
98
|
+
|
|
99
|
+
expect(described_class.find_by_token_for(:email_confirmation, token)).to eq(<%= file_name %>)
|
|
100
|
+
<%= file_name %>.confirm_email!
|
|
101
|
+
expect(described_class.find_by_token_for(:email_confirmation, token)).to be_nil
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
describe "email change" do
|
|
106
|
+
let(:<%= file_name %>) { create_<%= file_name %>("ada@example.com", unconfirmed_email: "ada@lovelace.org") }
|
|
107
|
+
|
|
108
|
+
it "takes the new email, confirmed", :aggregate_failures do
|
|
109
|
+
<%= file_name %>.change_email!
|
|
110
|
+
|
|
111
|
+
expect(<%= file_name %>.reload.email).to eq("ada@lovelace.org")
|
|
112
|
+
expect(<%= file_name %>).to be_confirmed
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it "keeps the email when another account has the new one" do
|
|
116
|
+
create_<%= file_name %>("ada@lovelace.org")
|
|
117
|
+
|
|
118
|
+
expect(<%= file_name %>.change_email!).to be(false)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
it "uses its link up once the email changed" do
|
|
122
|
+
token = <%= file_name %>.generate_token_for(:email_change)
|
|
123
|
+
<%= file_name %>.change_email!
|
|
124
|
+
|
|
125
|
+
expect(described_class.find_by_token_for(:email_change, token)).to be_nil
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
<% end -%>
|
|
129
|
+
end
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
require "rails_helper"
|
|
2
|
+
|
|
3
|
+
RSpec.describe "<%= human_name.pluralize %>' accounts", type: :request do
|
|
4
|
+
include ActiveSupport::Testing::TimeHelpers
|
|
5
|
+
|
|
6
|
+
<% if code? -%>
|
|
7
|
+
let(:<%= file_name %>) { <%= class_name %>.create!(email: "ada@example.com") }
|
|
8
|
+
|
|
9
|
+
def emailed_code
|
|
10
|
+
ActionMailer::Base.deliveries.last.text_part.body.to_s[/\b\d{6}\b/]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def change_email_to(email)
|
|
14
|
+
perform_enqueued_jobs { post <%= route_prefix %>_email_change_path, params: { email: } }
|
|
15
|
+
patch <%= route_prefix %>_email_change_path, params: { otp: emailed_code }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def sign_in_as(<%= file_name %>)
|
|
19
|
+
perform_enqueued_jobs { post <%= route_prefix %>_sign_in_path, params: { email: <%= file_name %>.email } }
|
|
20
|
+
post <%= route_prefix %>_sign_in_code_path, params: { otp: emailed_code }
|
|
21
|
+
end
|
|
22
|
+
<% else -%>
|
|
23
|
+
let(:<%= file_name %>) { <%= class_name %>.create!(email: "ada@example.com", password: "correct horse battery staple") }
|
|
24
|
+
|
|
25
|
+
def emailed_link
|
|
26
|
+
ActionMailer::Base.deliveries.last.text_part.body.to_s[%r{https?://\S+}]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def change_email_to(email)
|
|
30
|
+
perform_enqueued_jobs { post <%= route_prefix %>_email_change_path, params: { email: } }
|
|
31
|
+
get emailed_link
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def sign_in_as(<%= file_name %>)
|
|
35
|
+
post <%= route_prefix %>_sign_in_path, params: { email: <%= file_name %>.email, password: "correct horse battery staple" }
|
|
36
|
+
end
|
|
37
|
+
<% end -%>
|
|
38
|
+
|
|
39
|
+
it "asks to sign in" do
|
|
40
|
+
get <%= route_prefix %>_account_path
|
|
41
|
+
|
|
42
|
+
expect(response).to redirect_to(<%= route_prefix %>_sign_in_path)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "shows the account" do
|
|
46
|
+
sign_in_as(<%= file_name %>)
|
|
47
|
+
get <%= route_prefix %>_account_path
|
|
48
|
+
|
|
49
|
+
expect(response.body).to include(<%= file_name %>.email)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "changes the name" do
|
|
53
|
+
sign_in_as(<%= file_name %>)
|
|
54
|
+
patch <%= route_prefix %>_account_path, params: { <%= file_name %>: { name: "Ada Lovelace" } }
|
|
55
|
+
|
|
56
|
+
expect(<%= file_name %>.reload.name).to eq("Ada Lovelace")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it "changes the email once the <%= code? ? "code" : "link" %> sent to the new one is <%= code? ? "typed" : "followed" %>", :aggregate_failures do
|
|
60
|
+
sign_in_as(<%= file_name %>)
|
|
61
|
+
change_email_to("ada@lovelace.org")
|
|
62
|
+
|
|
63
|
+
expect(ActionMailer::Base.deliveries.last.to).to eq(["ada@lovelace.org"])
|
|
64
|
+
expect(<%= file_name %>.reload.email).to eq("ada@lovelace.org")
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it "keeps the email when another account has the new one", :aggregate_failures do
|
|
68
|
+
<%= class_name %>.create!(email: "taken@example.com"<% if password? %>, password: "correct horse battery staple"<% end %>)
|
|
69
|
+
sign_in_as(<%= file_name %>)
|
|
70
|
+
change_email_to("taken@example.com")
|
|
71
|
+
|
|
72
|
+
expect(<%= file_name %>.reload.email).to eq("ada@example.com")
|
|
73
|
+
expect(flash[:alert]).to be_present
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it "signs the other browsers out", :aggregate_failures do
|
|
77
|
+
other = <%= file_name %>.sessions.create!
|
|
78
|
+
sign_in_as(<%= file_name %>)
|
|
79
|
+
|
|
80
|
+
delete others_<%= route_prefix %>_active_sessions_path
|
|
81
|
+
|
|
82
|
+
expect(<%= class_name %>::Session.exists?(other.id)).to be(false)
|
|
83
|
+
expect(<%= file_name %>.sessions.count).to eq(1)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
it "deletes the account after a recent sign-in" do
|
|
87
|
+
sign_in_as(<%= file_name %>)
|
|
88
|
+
|
|
89
|
+
expect { delete <%= route_prefix %>_account_path }.to change(<%= class_name %>, :count).by(-1)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it "asks to sign in again before deleting the account", :aggregate_failures do
|
|
93
|
+
sign_in_as(<%= file_name %>)
|
|
94
|
+
travel <%= class_name %>::Session::RECENT + 1.minute
|
|
95
|
+
|
|
96
|
+
expect { delete <%= route_prefix %>_account_path }.not_to change(<%= class_name %>, :count)
|
|
97
|
+
expect(response).to redirect_to(<%= route_prefix %>_sign_in_path)
|
|
98
|
+
end
|
|
99
|
+
<% if password? -%>
|
|
100
|
+
|
|
101
|
+
it "sends the confirmation link again" do
|
|
102
|
+
sign_in_as(<%= file_name %>)
|
|
103
|
+
|
|
104
|
+
perform_enqueued_jobs { post <%= route_prefix %>_email_confirmation_path }
|
|
105
|
+
|
|
106
|
+
expect(ActionMailer::Base.deliveries.last.to).to eq([<%= file_name %>.email])
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it "changes the password after a recent sign-in, and signs the other browsers out", :aggregate_failures do
|
|
110
|
+
other = <%= file_name %>.sessions.create!
|
|
111
|
+
sign_in_as(<%= file_name %>)
|
|
112
|
+
|
|
113
|
+
patch <%= route_prefix %>_password_path, params: { <%= file_name %>: { password: "a brand new password 1!" } }
|
|
114
|
+
|
|
115
|
+
expect(<%= file_name %>.reload.authenticate("a brand new password 1!")).to be_truthy
|
|
116
|
+
expect(<%= class_name %>::Session.exists?(other.id)).to be(false)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
it "refuses a password breaking the rules" do
|
|
120
|
+
sign_in_as(<%= file_name %>)
|
|
121
|
+
patch <%= route_prefix %>_password_path, params: { <%= file_name %>: { password: "short" } }
|
|
122
|
+
|
|
123
|
+
expect(response).to have_http_status(:unprocessable_content)
|
|
124
|
+
end
|
|
125
|
+
<% end -%>
|
|
126
|
+
end
|