You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.3 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe StatusLengthValidator do
  4. describe '#validate' do
  5. it 'does not add errors onto remote statuses' do
  6. status = double(local?: false)
  7. subject.validate(status)
  8. expect(status).not_to receive(:errors)
  9. end
  10. it 'does not add errors onto local reblogs' do
  11. status = double(local?: false, reblog?: true)
  12. subject.validate(status)
  13. expect(status).not_to receive(:errors)
  14. end
  15. it 'adds an error when content warning is over MAX_CHARS characters' do
  16. chars = StatusLengthValidator::MAX_CHARS + 1
  17. status = double(spoiler_text: 'a' * chars, text: '', errors: double(add: nil), local?: true, reblog?: false)
  18. subject.validate(status)
  19. expect(status.errors).to have_received(:add)
  20. end
  21. it 'adds an error when text is over MAX_CHARS characters' do
  22. chars = StatusLengthValidator::MAX_CHARS + 1
  23. status = double(spoiler_text: '', text: 'a' * chars, errors: double(add: nil), local?: true, reblog?: false)
  24. subject.validate(status)
  25. expect(status.errors).to have_received(:add)
  26. end
  27. it 'adds an error when text and content warning are over MAX_CHARS characters total' do
  28. chars1 = 20
  29. chars2 = StatusLengthValidator::MAX_CHARS + 1 - chars1
  30. status = double(spoiler_text: 'a' * chars1, text: 'b' * chars2, errors: double(add: nil), local?: true, reblog?: false)
  31. subject.validate(status)
  32. expect(status.errors).to have_received(:add)
  33. end
  34. it 'counts URLs as 23 characters flat' do
  35. chars = StatusLengthValidator::MAX_CHARS - 1 - 23
  36. text = ('a' * chars) + " http://#{'b' * 30}.com/example"
  37. status = double(spoiler_text: '', text: text, errors: double(add: nil), local?: true, reblog?: false)
  38. subject.validate(status)
  39. expect(status.errors).to_not have_received(:add)
  40. end
  41. it 'counts only the front part of remote usernames' do
  42. username = '@alice'
  43. chars = StatusLengthValidator::MAX_CHARS - 1 - username.length
  44. text = ('a' * 475) + " #{username}@#{'b' * 30}.com"
  45. status = double(spoiler_text: '', text: text, errors: double(add: nil), local?: true, reblog?: false)
  46. subject.validate(status)
  47. expect(status.errors).to_not have_received(:add)
  48. end
  49. end
  50. end