2023-09-03 22:07:20 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-02-01 18:32:45 -05:00
|
|
|
describe "User preferences | Security", type: :system do
|
2023-09-03 22:07:20 -04:00
|
|
|
fab!(:password) { "kungfukenny" }
|
|
|
|
fab!(:email) { "email@user.com" }
|
|
|
|
fab!(:user) { Fabricate(:user, email: email, password: password) }
|
|
|
|
let(:user_preferences_security_page) { PageObjects::Pages::UserPreferencesSecurity.new }
|
|
|
|
let(:user_menu) { PageObjects::Components::UserMenu.new }
|
|
|
|
|
|
|
|
before do
|
|
|
|
user.activate
|
2024-02-15 12:29:16 -05:00
|
|
|
# testing the enforced 2FA flow requires a user that was created > 5 minutes ago
|
|
|
|
user.created_at = 6.minutes.ago
|
|
|
|
user.save!
|
2023-09-03 22:07:20 -04:00
|
|
|
sign_in(user)
|
2023-10-13 12:24:06 -04:00
|
|
|
|
|
|
|
# system specs run on their own host + port
|
|
|
|
DiscourseWebauthn.stubs(:origin).returns(current_host + ":" + Capybara.server_port.to_s)
|
2023-09-03 22:07:20 -04:00
|
|
|
end
|
|
|
|
|
2023-10-26 20:55:41 -04:00
|
|
|
shared_examples "security keys" do
|
2023-10-13 12:24:06 -04:00
|
|
|
it "adds a 2FA security key and logs in with it" do
|
2023-09-03 22:07:20 -04:00
|
|
|
options = ::Selenium::WebDriver::VirtualAuthenticatorOptions.new
|
2023-10-13 12:24:06 -04:00
|
|
|
authenticator = page.driver.browser.add_virtual_authenticator(options)
|
2023-09-03 22:07:20 -04:00
|
|
|
|
|
|
|
user_preferences_security_page.visit(user)
|
|
|
|
user_preferences_security_page.visit_second_factor(password)
|
|
|
|
|
|
|
|
find(".security-key .new-security-key").click
|
|
|
|
expect(user_preferences_security_page).to have_css("input#security-key-name")
|
|
|
|
|
2023-11-15 05:14:47 -05:00
|
|
|
find(".d-modal__body input#security-key-name").fill_in(with: "First Key")
|
2023-09-03 22:07:20 -04:00
|
|
|
find(".add-security-key").click
|
|
|
|
|
|
|
|
expect(user_preferences_security_page).to have_css(".security-key .second-factor-item")
|
|
|
|
|
|
|
|
user_menu.sign_out
|
|
|
|
|
|
|
|
# login flow
|
|
|
|
find(".d-header .login-button").click
|
|
|
|
find("input#login-account-name").fill_in(with: user.username)
|
|
|
|
find("input#login-account-password").fill_in(with: password)
|
|
|
|
|
2023-11-15 05:14:47 -05:00
|
|
|
find(".d-modal__footer .btn-primary").click
|
2023-09-03 22:07:20 -04:00
|
|
|
find("#security-key .btn-primary").click
|
|
|
|
|
|
|
|
expect(page).to have_css(".header-dropdown-toggle.current-user")
|
2024-06-06 02:30:42 -04:00
|
|
|
ensure
|
2023-10-13 12:24:06 -04:00
|
|
|
# clear authenticator (otherwise it will interfere with other tests)
|
2024-06-06 02:30:42 -04:00
|
|
|
authenticator&.remove!
|
2023-10-13 12:24:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-26 20:55:41 -04:00
|
|
|
shared_examples "passkeys" do
|
2023-11-13 15:04:15 -05:00
|
|
|
before { SiteSetting.enable_passkeys = true }
|
2023-10-13 12:24:06 -04:00
|
|
|
|
|
|
|
it "adds a passkey and logs in with it" do
|
|
|
|
options =
|
|
|
|
::Selenium::WebDriver::VirtualAuthenticatorOptions.new(
|
|
|
|
user_verification: true,
|
|
|
|
user_verified: true,
|
|
|
|
resident_key: true,
|
|
|
|
)
|
|
|
|
authenticator = page.driver.browser.add_virtual_authenticator(options)
|
|
|
|
|
2023-10-31 11:35:36 -04:00
|
|
|
page.driver.browser.manage.add_cookie(
|
|
|
|
domain: Discourse.current_hostname,
|
|
|
|
name: "destination_url",
|
|
|
|
value: "/new",
|
|
|
|
path: "/",
|
|
|
|
)
|
|
|
|
|
2023-10-13 12:24:06 -04:00
|
|
|
user_preferences_security_page.visit(user)
|
|
|
|
|
|
|
|
find(".pref-passkeys__add .btn").click
|
|
|
|
expect(user_preferences_security_page).to have_css("input#password")
|
|
|
|
|
|
|
|
find(".dialog-body input#password").fill_in(with: password)
|
|
|
|
find(".confirm-session .btn-primary").click
|
|
|
|
|
|
|
|
expect(user_preferences_security_page).to have_css(".rename-passkey__form")
|
|
|
|
|
|
|
|
find(".dialog-close").click
|
|
|
|
|
|
|
|
expect(user_preferences_security_page).to have_css(".pref-passkeys__rows .row")
|
|
|
|
|
|
|
|
select_kit = PageObjects::Components::SelectKit.new(".passkey-options-dropdown")
|
|
|
|
select_kit.expand
|
|
|
|
select_kit.select_row_by_name("Delete")
|
|
|
|
|
|
|
|
# confirm deletion screen shown without requiring session confirmation
|
|
|
|
# since this was already done when adding the passkey
|
|
|
|
expect(user_preferences_security_page).to have_css(".dialog-footer .btn-danger")
|
|
|
|
|
|
|
|
# close the dialog (don't delete the key, we need it to login in the next step)
|
|
|
|
find(".dialog-close").click
|
|
|
|
|
|
|
|
user_menu.sign_out
|
|
|
|
|
|
|
|
# login with the key we just created
|
2023-10-26 20:55:41 -04:00
|
|
|
# this triggers the conditional UI for passkeys
|
|
|
|
# which uses the virtual authenticator
|
2023-10-13 12:24:06 -04:00
|
|
|
find(".d-header .login-button").click
|
|
|
|
|
|
|
|
expect(page).to have_css(".header-dropdown-toggle.current-user")
|
|
|
|
|
2023-10-31 11:35:36 -04:00
|
|
|
# ensures that we are redirected to the destination_url cookie
|
|
|
|
expect(page.driver.current_url).to include("/new")
|
2024-06-06 02:30:42 -04:00
|
|
|
ensure
|
2023-10-13 12:24:06 -04:00
|
|
|
# clear authenticator (otherwise it will interfere with other tests)
|
2024-06-06 02:30:42 -04:00
|
|
|
authenticator&.remove!
|
2023-09-03 22:07:20 -04:00
|
|
|
end
|
|
|
|
end
|
2023-10-26 20:55:41 -04:00
|
|
|
|
2023-11-16 11:52:12 -05:00
|
|
|
shared_examples "enforced second factor" do
|
|
|
|
it "allows user to add 2FA" do
|
|
|
|
SiteSetting.enforce_second_factor = "all"
|
|
|
|
|
|
|
|
visit("/")
|
|
|
|
|
|
|
|
expect(page).to have_selector(
|
|
|
|
".alert-error",
|
|
|
|
text: "You are required to enable two-factor authentication before accessing this site.",
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(page).to have_css(".user-preferences .totp")
|
|
|
|
expect(page).to have_css(".user-preferences .security-key")
|
|
|
|
|
|
|
|
find(".user-preferences .totp .btn.new-totp").click
|
|
|
|
|
|
|
|
find(".dialog-body input#password").fill_in(with: password)
|
|
|
|
find(".confirm-session .btn-primary").click
|
|
|
|
|
|
|
|
expect(page).to have_css(".qr-code")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-26 20:55:41 -04:00
|
|
|
context "when desktop" do
|
|
|
|
include_examples "security keys"
|
|
|
|
include_examples "passkeys"
|
2023-11-16 11:52:12 -05:00
|
|
|
include_examples "enforced second factor"
|
2023-10-26 20:55:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when mobile", mobile: true do
|
|
|
|
include_examples "security keys"
|
|
|
|
include_examples "passkeys"
|
2023-11-16 11:52:12 -05:00
|
|
|
include_examples "enforced second factor"
|
2023-10-26 20:55:41 -04:00
|
|
|
end
|
2023-09-03 22:07:20 -04:00
|
|
|
end
|