webauthn_credentials_spec.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. require 'rails_helper'
  2. RSpec.describe WebauthnCredential, type: :model do
  3. describe 'validations' do
  4. it 'is invalid without an external id' do
  5. webauthn_credential = Fabricate.build(:webauthn_credential, external_id: nil)
  6. webauthn_credential.valid?
  7. expect(webauthn_credential).to model_have_error_on_field(:external_id)
  8. end
  9. it 'is invalid without a public key' do
  10. webauthn_credential = Fabricate.build(:webauthn_credential, public_key: nil)
  11. webauthn_credential.valid?
  12. expect(webauthn_credential).to model_have_error_on_field(:public_key)
  13. end
  14. it 'is invalid without a nickname' do
  15. webauthn_credential = Fabricate.build(:webauthn_credential, nickname: nil)
  16. webauthn_credential.valid?
  17. expect(webauthn_credential).to model_have_error_on_field(:nickname)
  18. end
  19. it 'is invalid without a sign_count' do
  20. webauthn_credential = Fabricate.build(:webauthn_credential, sign_count: nil)
  21. webauthn_credential.valid?
  22. expect(webauthn_credential).to model_have_error_on_field(:sign_count)
  23. end
  24. it 'is invalid if already exist a webauthn credential with the same external id' do
  25. existing_webauthn_credential = Fabricate(:webauthn_credential, external_id: "_Typ0ygudDnk9YUVWLQayw")
  26. new_webauthn_credential = Fabricate.build(:webauthn_credential, external_id: "_Typ0ygudDnk9YUVWLQayw")
  27. new_webauthn_credential.valid?
  28. expect(new_webauthn_credential).to model_have_error_on_field(:external_id)
  29. end
  30. it 'is invalid if user already registered a webauthn credential with the same nickname' do
  31. user = Fabricate(:user)
  32. existing_webauthn_credential = Fabricate(:webauthn_credential, user_id: user.id, nickname: 'USB Key')
  33. new_webauthn_credential = Fabricate.build(:webauthn_credential, user_id: user.id, nickname: 'USB Key')
  34. new_webauthn_credential.valid?
  35. expect(new_webauthn_credential).to model_have_error_on_field(:nickname)
  36. end
  37. it 'is invalid if sign_count is not a number' do
  38. webauthn_credential = Fabricate.build(:webauthn_credential, sign_count: 'invalid sign_count')
  39. webauthn_credential.valid?
  40. expect(webauthn_credential).to model_have_error_on_field(:sign_count)
  41. end
  42. it 'is invalid if sign_count is negative number' do
  43. webauthn_credential = Fabricate.build(:webauthn_credential, sign_count: -1)
  44. webauthn_credential.valid?
  45. expect(webauthn_credential).to model_have_error_on_field(:sign_count)
  46. end
  47. it 'is invalid if sign_count is greater 2**63 - 1' do
  48. webauthn_credential = Fabricate.build(:webauthn_credential, sign_count: 2**63)
  49. webauthn_credential.valid?
  50. expect(webauthn_credential).to model_have_error_on_field(:sign_count)
  51. end
  52. end
  53. end