unique_username_validator_spec.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe UniqueUsernameValidator do
  4. describe '#validate' do
  5. context 'when local account' do
  6. it 'does not add errors if username is nil' do
  7. account = double(username: nil, domain: nil, persisted?: false, errors: double(add: nil))
  8. subject.validate(account)
  9. expect(account.errors).to_not have_received(:add)
  10. end
  11. it 'does not add errors when existing one is subject itself' do
  12. account = Fabricate(:account, username: 'abcdef')
  13. expect(account).to be_valid
  14. end
  15. it 'adds an error when the username is already used with ignoring cases' do
  16. Fabricate(:account, username: 'ABCdef')
  17. account = double(username: 'abcDEF', domain: nil, persisted?: false, errors: double(add: nil))
  18. subject.validate(account)
  19. expect(account.errors).to have_received(:add)
  20. end
  21. it 'does not add errors when same username remote account exists' do
  22. Fabricate(:account, username: 'abcdef', domain: 'example.com')
  23. account = double(username: 'abcdef', domain: nil, persisted?: false, errors: double(add: nil))
  24. subject.validate(account)
  25. expect(account.errors).to_not have_received(:add)
  26. end
  27. end
  28. end
  29. context 'when remote account' do
  30. it 'does not add errors if username is nil' do
  31. account = double(username: nil, domain: 'example.com', persisted?: false, errors: double(add: nil))
  32. subject.validate(account)
  33. expect(account.errors).to_not have_received(:add)
  34. end
  35. it 'does not add errors when existing one is subject itself' do
  36. account = Fabricate(:account, username: 'abcdef', domain: 'example.com')
  37. expect(account).to be_valid
  38. end
  39. it 'adds an error when the username is already used with ignoring cases' do
  40. Fabricate(:account, username: 'ABCdef', domain: 'example.com')
  41. account = double(username: 'abcDEF', domain: 'example.com', persisted?: false, errors: double(add: nil))
  42. subject.validate(account)
  43. expect(account.errors).to have_received(:add)
  44. end
  45. it 'adds an error when the domain is already used with ignoring cases' do
  46. Fabricate(:account, username: 'ABCdef', domain: 'example.com')
  47. account = double(username: 'ABCdef', domain: 'EXAMPLE.COM', persisted?: false, errors: double(add: nil))
  48. subject.validate(account)
  49. expect(account.errors).to have_received(:add)
  50. end
  51. it 'does not add errors when account with the same username and another domain exists' do
  52. Fabricate(:account, username: 'abcdef', domain: 'example.com')
  53. account = double(username: 'abcdef', domain: 'example2.com', persisted?: false, errors: double(add: nil))
  54. subject.validate(account)
  55. expect(account.errors).to_not have_received(:add)
  56. end
  57. end
  58. end