status_length_validator_spec.rb 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 500 characters' do
  16. status = double(spoiler_text: 'a' * 520, text: '', errors: double(add: nil), local?: true, reblog?: false)
  17. subject.validate(status)
  18. expect(status.errors).to have_received(:add)
  19. end
  20. it 'adds an error when text is over 500 characters' do
  21. status = double(spoiler_text: '', text: 'a' * 520, errors: double(add: nil), local?: true, reblog?: false)
  22. subject.validate(status)
  23. expect(status.errors).to have_received(:add)
  24. end
  25. it 'adds an error when text and content warning are over 500 characters total' do
  26. status = double(spoiler_text: 'a' * 250, text: 'b' * 251, errors: double(add: nil), local?: true, reblog?: false)
  27. subject.validate(status)
  28. expect(status.errors).to have_received(:add)
  29. end
  30. it 'counts URLs as 23 characters flat' do
  31. text = ('a' * 476) + " http://#{'b' * 30}.com/example"
  32. status = double(spoiler_text: '', text: text, errors: double(add: nil), local?: true, reblog?: false)
  33. subject.validate(status)
  34. expect(status.errors).to_not have_received(:add)
  35. end
  36. it 'does not count non-autolinkable URLs as 23 characters flat' do
  37. text = ('a' * 476) + "http://#{'b' * 30}.com/example"
  38. status = double(spoiler_text: '', text: text, errors: double(add: nil), local?: true, reblog?: false)
  39. subject.validate(status)
  40. expect(status.errors).to have_received(:add)
  41. end
  42. it 'does not count overly long URLs as 23 characters flat' do
  43. text = "http://example.com/valid?#{'#foo?' * 1000}"
  44. status = double(spoiler_text: '', text: text, errors: double(add: nil), local?: true, reblog?: false)
  45. subject.validate(status)
  46. expect(status.errors).to have_received(:add)
  47. end
  48. it 'counts only the front part of remote usernames' do
  49. text = ('a' * 475) + " @alice@#{'b' * 30}.com"
  50. status = double(spoiler_text: '', text: text, errors: double(add: nil), local?: true, reblog?: false)
  51. subject.validate(status)
  52. expect(status.errors).to_not have_received(:add)
  53. end
  54. it 'does count both parts of remote usernames for overly long domains' do
  55. text = "@alice@#{'b' * 500}.com"
  56. status = double(spoiler_text: '', text: text, errors: double(add: nil), local?: true, reblog?: false)
  57. subject.validate(status)
  58. expect(status.errors).to have_received(:add)
  59. end
  60. end
  61. end