-
-
Notifications
You must be signed in to change notification settings - Fork 0
Stop generating button inside login helper method #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
aaac2ef
feat!: rename login helpers to form_for pattern and require explicit …
RenzoMinelli 9f73cd4
rename method name
RenzoMinelli e421ec4
update test names
RenzoMinelli aa83641
fix merge conflicts
RenzoMinelli 930b570
add custom content test
RenzoMinelli fea49ec
add a test to verify we can add more than one element using helper
RenzoMinelli 9b7f15b
fix tests by reloading routes
RenzoMinelli 1d4ffd1
use try
RenzoMinelli 6c0dd03
pr feedback
RenzoMinelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| <%= login_with_security_key_button_for(resource_name, 'Use security key') %> | ||
| <%= login_with_security_key_form_for(resource_name) do |form| %> | ||
| <%= form.submit 'Use security key' %> | ||
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
spec/helpers/devise/webauthn/credentials_helper_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # rubocop:disable RSpec/MultipleExpectations | ||
| RSpec.describe Devise::Webauthn::CredentialsHelper, type: :helper do | ||
| let(:user) do | ||
| Account.create!( | ||
| email: "testuser@example.com", | ||
| password: "$3cretp@ssword123" | ||
| ) | ||
| end | ||
|
|
||
| before do | ||
| require "devise/version" | ||
| Rails.application.reload_routes_unless_loaded if Rails::VERSION::MAJOR >= 8 && Devise::VERSION < "5" | ||
| end | ||
|
|
||
| def parse(html) | ||
| Capybara.string(html) | ||
| end | ||
|
|
||
| def have_hidden_credential_field | ||
| have_css( | ||
| "input[type='hidden'][name='public_key_credential'][data-webauthn-target='response']", | ||
| visible: :hidden | ||
| ) | ||
| end | ||
|
|
||
| describe "#passkey_creation_form_for" do | ||
| it "renders a form with webauthn_create element and hidden credential field" do | ||
| html = helper.passkey_creation_form_for(user) do |form| | ||
| form.submit "Create Passkey" | ||
| end | ||
|
|
||
| page = parse(html) | ||
| expect(page).to have_css("form") | ||
| expect(page).to have_css("webauthn-create[data-options-url='/accounts/passkey_registration_options']") | ||
| expect(page).to have_hidden_credential_field | ||
| expect(page).to have_button("Create Passkey") | ||
| end | ||
|
|
||
| it "accepts form_classes option" do | ||
| html = helper.passkey_creation_form_for(user, form_classes: "custom-form") do |form| | ||
| form.submit "Create" | ||
| end | ||
|
|
||
| expect(parse(html)).to have_css("form.custom-form") | ||
| end | ||
|
|
||
| it "allows custom content in the block" do | ||
| html = helper.passkey_creation_form_for(user) do |form| | ||
| helper.content_tag(:div, class: "button-wrapper") do | ||
| form.submit "Create", class: "btn-primary" | ||
| end | ||
| end | ||
|
|
||
| page = parse(html) | ||
| expect(page).to have_css("div.button-wrapper") | ||
| expect(page).to have_css("input.btn-primary[type='submit']") | ||
| end | ||
| end | ||
|
|
||
| describe "#login_with_passkey_form_for" do | ||
| it "renders a form with webauthn_get element and hidden credential field" do | ||
| html = helper.login_with_passkey_form_for(:account) do |form| | ||
| form.submit "Log in with passkeys" | ||
| end | ||
|
|
||
| page = parse(html) | ||
| expect(page).to have_css("form[action='/accounts/sign_in']") | ||
| expect(page).to have_css("webauthn-get[data-options-url='/accounts/passkey_authentication_options']") | ||
| expect(page).to have_hidden_credential_field | ||
| expect(page).to have_button("Log in with passkeys") | ||
| end | ||
|
|
||
| it "accepts form_classes option" do | ||
| html = helper.login_with_passkey_form_for(:account, form_classes: "passkey-form") do |form| | ||
| form.submit "Login" | ||
| end | ||
|
|
||
| expect(parse(html)).to have_css("form.passkey-form") | ||
| end | ||
|
|
||
| it "allows custom content in the block" do | ||
| html = helper.login_with_passkey_form_for(:account) do |form| | ||
| # artifact of the test, concat simulates ERB's <%= %> | ||
| helper.concat form.submit("Sign in", class: "btn-primary") | ||
| helper.concat form.check_box(:remember_me) | ||
| helper.concat form.label(:remember_me, "Remember me") | ||
| end | ||
|
|
||
| page = parse(html) | ||
|
|
||
| expect(page).to have_css("input.btn-primary[type='submit']") | ||
| expect(page).to have_css("input[type='checkbox'][name='remember_me']") | ||
| expect(page).to have_css("label[for='remember_me']", text: "Remember me") | ||
| end | ||
|
santiagorodriguez96 marked this conversation as resolved.
|
||
| end | ||
|
|
||
| describe "#security_key_creation_form_for" do | ||
| it "renders a form with webauthn_create element" do | ||
| html = helper.security_key_creation_form_for(user) do |form| | ||
| form.submit "Add Security Key" | ||
| end | ||
|
|
||
| page = parse(html) | ||
| expect(page).to have_css("form") | ||
| expect(page).to have_css("webauthn-create[data-options-url='/accounts/security_key_registration_options']") | ||
| expect(page).to have_hidden_credential_field | ||
| expect(page).to have_button("Add Security Key") | ||
| end | ||
|
|
||
| it "accepts form_classes option" do | ||
| html = helper.security_key_creation_form_for(user, form_classes: "security-key-form") do |form| | ||
| form.submit "Add" | ||
| end | ||
|
|
||
| expect(parse(html)).to have_css("form.security-key-form") | ||
| end | ||
|
|
||
| it "allows custom content in the block" do | ||
| html = helper.security_key_creation_form_for(user) do |form| | ||
| helper.content_tag(:div, class: "button-wrapper") do | ||
| form.submit "Add", class: "btn-primary" | ||
| end | ||
| end | ||
|
|
||
| page = parse(html) | ||
| expect(page).to have_css("div.button-wrapper") | ||
| expect(page).to have_css("input.btn-primary[type='submit']") | ||
| end | ||
| end | ||
|
|
||
| describe "#login_with_security_key_form_for" do | ||
| it "renders a form with webauthn_get element" do | ||
| html = helper.login_with_security_key_form_for(:account) do |form| | ||
| form.submit "Use security key" | ||
| end | ||
|
|
||
| page = parse(html) | ||
| expect(page).to have_css("form") | ||
| expect(page).to have_css("webauthn-get[data-options-url='/accounts/security_key_authentication_options']") | ||
| expect(page).to have_hidden_credential_field | ||
| expect(page).to have_button("Use security key") | ||
| end | ||
|
|
||
| it "accepts form_classes option" do | ||
| html = helper.login_with_security_key_form_for(:account, form_classes: "two-factor-form") do |form| | ||
| form.submit "Verify" | ||
| end | ||
|
|
||
| expect(parse(html)).to have_css("form.two-factor-form") | ||
| end | ||
|
|
||
| it "allows custom content in the block" do | ||
| html = helper.login_with_security_key_form_for(:account) do |form| | ||
| helper.safe_join([helper.content_tag(:p, "Authenticate with your security key"), form.submit("Authenticate")]) | ||
| end | ||
|
|
||
| page = parse(html) | ||
| expect(page).to have_css("p", text: "Authenticate with your security key") | ||
| expect(page).to have_button("Authenticate") | ||
| end | ||
| end | ||
| end | ||
| # rubocop:enable RSpec/MultipleExpectations | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.