|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | RSpec.describe Devise::Models::WebauthnTwoFactorAuthenticatable, type: :model do |
4 | | - describe "webauthn_id initialization" do |
5 | | - it "generates a webauthn_id on create" do |
| 4 | + describe "#ensure_webauthn_id!" do |
| 5 | + it "does not generate a webauthn_id on create" do |
6 | 6 | user = Account.create!(email: "user@example.com", password: "password", password_confirmation: "password") |
7 | | - expect(user.webauthn_id).to be_present |
| 7 | + expect(user.webauthn_id).to be_nil |
8 | 8 | end |
9 | 9 |
|
10 | | - it "does not generate a webauthn_id on initialize" do |
11 | | - user = Account.new(email: "user@example.com", password: "password", password_confirmation: "password") |
12 | | - expect(user.webauthn_id).to be_nil |
| 10 | + it "generates and persists a webauthn_id when missing" do |
| 11 | + user = Account.create!(email: "user@example.com", password: "password", password_confirmation: "password") |
| 12 | + |
| 13 | + webauthn_id = user.ensure_webauthn_id! |
| 14 | + |
| 15 | + expect(webauthn_id).to be_present |
| 16 | + expect(user.reload.webauthn_id).to eq(webauthn_id) |
13 | 17 | end |
14 | 18 |
|
15 | | - it "keeps webauthn_id if created with one" do |
| 19 | + it "does not replace an existing webauthn_id" do |
16 | 20 | user = Account.create!(email: "user@example.com", password: "password", password_confirmation: "password", |
17 | 21 | webauthn_id: "custom") |
18 | | - expect(user.webauthn_id).to eq("custom") |
19 | | - end |
20 | | - |
21 | | - it "generates a webauthn_id on update if missing" do |
22 | | - user = Account.create!(email: "user@example.com", password: "password", password_confirmation: "password") |
23 | | - user.update_column(:webauthn_id, nil) # rubocop:disable Rails/SkipsModelValidations |
24 | | - user.reload |
25 | 22 |
|
26 | | - user.update!(email: "updated@example.com") |
27 | | - expect(user.webauthn_id).to be_present |
| 23 | + expect(user.ensure_webauthn_id!).to eq("custom") |
| 24 | + expect(user.reload.webauthn_id).to eq("custom") |
28 | 25 | end |
29 | 26 | end |
30 | 27 |
|
|
66 | 63 |
|
67 | 64 | describe "validations" do |
68 | 65 | it "validates uniqueness of webauthn_id" do |
69 | | - existing = Account.create!(email: "existing@example.com", password: "password", password_confirmation: "password") |
| 66 | + existing = Account.create!(email: "existing@example.com", password: "password", |
| 67 | + password_confirmation: "password", webauthn_id: WebAuthn.generate_user_id) |
70 | 68 | user = Account.new(email: "new@example.com", webauthn_id: existing.webauthn_id) |
71 | 69 | expect(user).not_to be_valid |
72 | 70 | expect(user.errors[:webauthn_id]).to include("has already been taken") |
|
0 commit comments