unique_username_validator_spec.rb 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.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 = instance_double(Account, username: nil, domain: nil, persisted?: false, errors: activemodel_errors)
  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 = instance_double(Account, username: 'abcDEF', domain: nil, persisted?: false, errors: activemodel_errors)
  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 = instance_double(Account, username: 'abcdef', domain: nil, persisted?: false, errors: activemodel_errors)
  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 = instance_double(Account, username: nil, domain: 'example.com', persisted?: false, errors: activemodel_errors)
  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 = instance_double(Account, username: 'abcDEF', domain: 'example.com', persisted?: false, errors: activemodel_errors)
  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 = instance_double(Account, username: 'ABCdef', domain: 'EXAMPLE.COM', persisted?: false, errors: activemodel_errors)
  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 = instance_double(Account, username: 'abcdef', domain: 'example2.com', persisted?: false, errors: activemodel_errors)
  54. subject.validate(account)
  55. expect(account.errors).to_not have_received(:add)
  56. end
  57. end
  58. private
  59. def activemodel_errors
  60. instance_double(ActiveModel::Errors, add: nil)
  61. end
  62. end