1
0

webauthn_credentials_spec.rb 2.7 KB

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