|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# rubocop:disable RSpec/MultipleExpectations |
| 4 | +RSpec.describe Devise::Webauthn::CredentialsHelper, type: :helper do |
| 5 | + let(:user) do |
| 6 | + Account.create!( |
| 7 | + email: "testuser@example.com", |
| 8 | + password: "$3cretp@ssword123" |
| 9 | + ) |
| 10 | + end |
| 11 | + |
| 12 | + before do |
| 13 | + allow(helper).to receive_messages(resource: user, session: {}) |
| 14 | + end |
| 15 | + |
| 16 | + def parsed(html) |
| 17 | + Capybara.string(html) |
| 18 | + end |
| 19 | + |
| 20 | + def have_hidden_credential_field |
| 21 | + have_css( |
| 22 | + "input[type='hidden'][name='public_key_credential'][data-webauthn-target='response']", |
| 23 | + visible: :hidden |
| 24 | + ) |
| 25 | + end |
| 26 | + |
| 27 | + describe "#passkey_creation_form_for" do |
| 28 | + it "renders a form with webauthn_create element" do |
| 29 | + html = helper.passkey_creation_form_for(user) do |form| |
| 30 | + form.submit "Create Passkey" |
| 31 | + end |
| 32 | + |
| 33 | + page = parsed(html) |
| 34 | + expect(page).to have_css("form") |
| 35 | + expect(page).to have_css("webauthn-create[data-options-url='/accounts/passkey_registration_options']") |
| 36 | + expect(page).to have_hidden_credential_field |
| 37 | + expect(page).to have_button("Create Passkey") |
| 38 | + end |
| 39 | + |
| 40 | + it "accepts form_classes option" do |
| 41 | + html = helper.passkey_creation_form_for(user, form_classes: "custom-form") do |form| |
| 42 | + form.submit "Create" |
| 43 | + end |
| 44 | + |
| 45 | + expect(parsed(html)).to have_css("form.custom-form") |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + describe "#login_with_passkey_form_for" do |
| 50 | + it "renders a form with webauthn_get element" do |
| 51 | + html = helper.login_with_passkey_form_for(session_path: "/accounts/sign_in") do |form| |
| 52 | + form.submit "Log in with passkeys" |
| 53 | + end |
| 54 | + |
| 55 | + page = parsed(html) |
| 56 | + expect(page).to have_css("form[action='/accounts/sign_in']") |
| 57 | + expect(page).to have_css("webauthn-get[data-options-url='/accounts/passkey_authentication_options']") |
| 58 | + expect(page).to have_hidden_credential_field |
| 59 | + expect(page).to have_button("Log in with passkeys") |
| 60 | + end |
| 61 | + |
| 62 | + it "accepts form_classes option" do |
| 63 | + html = helper.login_with_passkey_form_for(session_path: "/sign_in", form_classes: "passkey-form") do |form| |
| 64 | + form.submit "Login" |
| 65 | + end |
| 66 | + |
| 67 | + expect(parsed(html)).to have_css("form.passkey-form") |
| 68 | + end |
| 69 | + |
| 70 | + it "allows custom content in the block" do |
| 71 | + html = helper.login_with_passkey_form_for(session_path: "/sign_in") do |form| |
| 72 | + helper.content_tag(:div, class: "button-wrapper") do |
| 73 | + form.submit "Sign in", class: "btn-primary" |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + page = parsed(html) |
| 78 | + expect(page).to have_css("div.button-wrapper") |
| 79 | + expect(page).to have_css("input.btn-primary[type='submit']") |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + describe "#security_key_creation_form_for" do |
| 84 | + it "renders a form with webauthn_create element" do |
| 85 | + html = helper.security_key_creation_form_for(user) do |form| |
| 86 | + form.submit "Add Security Key" |
| 87 | + end |
| 88 | + |
| 89 | + page = parsed(html) |
| 90 | + expect(page).to have_css("form") |
| 91 | + expect(page).to have_css("webauthn-create[data-options-url='/accounts/security_key_registration_options']") |
| 92 | + expect(page).to have_hidden_credential_field |
| 93 | + expect(page).to have_button("Add Security Key") |
| 94 | + end |
| 95 | + |
| 96 | + it "accepts form_classes option" do |
| 97 | + html = helper.security_key_creation_form_for(user, form_classes: "security-key-form") do |form| |
| 98 | + form.submit "Add" |
| 99 | + end |
| 100 | + |
| 101 | + expect(parsed(html)).to have_css("form.security-key-form") |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + describe "#login_with_security_key_form_for" do |
| 106 | + it "renders a form with webauthn_get element" do |
| 107 | + html = helper.login_with_security_key_form_for(resource: user) do |form| |
| 108 | + form.submit "Use security key" |
| 109 | + end |
| 110 | + |
| 111 | + page = parsed(html) |
| 112 | + expect(page).to have_css("form") |
| 113 | + expect(page).to have_css("webauthn-get[data-options-url='/accounts/security_key_authentication_options']") |
| 114 | + expect(page).to have_hidden_credential_field |
| 115 | + expect(page).to have_button("Use security key") |
| 116 | + end |
| 117 | + |
| 118 | + it "accepts form_classes option" do |
| 119 | + html = helper.login_with_security_key_form_for(resource: user, form_classes: "two-factor-form") do |form| |
| 120 | + form.submit "Verify" |
| 121 | + end |
| 122 | + |
| 123 | + expect(parsed(html)).to have_css("form.two-factor-form") |
| 124 | + end |
| 125 | + |
| 126 | + it "allows custom content in the block" do |
| 127 | + html = helper.login_with_security_key_form_for(resource: user) do |form| |
| 128 | + helper.safe_join([helper.content_tag(:p, "Authenticate with your security key"), form.submit("Authenticate")]) |
| 129 | + end |
| 130 | + |
| 131 | + page = parsed(html) |
| 132 | + expect(page).to have_css("p", text: "Authenticate with your security key") |
| 133 | + expect(page).to have_button("Authenticate") |
| 134 | + end |
| 135 | + end |
| 136 | +end |
| 137 | +# rubocop:enable RSpec/MultipleExpectations |
0 commit comments