webauthn_credentials_spec.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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