status_length_validator_spec.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe StatusLengthValidator do
  4. describe '#validate' do
  5. before { stub_const("#{described_class}::MAX_CHARS", 500) } # Example values below are relative to this baseline
  6. it 'does not add errors onto remote statuses' do
  7. status = instance_double(Status, local?: false)
  8. allow(status).to receive(:errors)
  9. subject.validate(status)
  10. expect(status).to_not have_received(:errors)
  11. end
  12. it 'does not add errors onto local reblogs' do
  13. status = instance_double(Status, local?: false, reblog?: true)
  14. allow(status).to receive(:errors)
  15. subject.validate(status)
  16. expect(status).to_not have_received(:errors)
  17. end
  18. it 'adds an error when content warning is over character limit' do
  19. status = status_double(spoiler_text: 'a' * 520)
  20. subject.validate(status)
  21. expect(status.errors).to have_received(:add)
  22. end
  23. it 'adds an error when text is over character limit' do
  24. status = status_double(text: 'a' * 520)
  25. subject.validate(status)
  26. expect(status.errors).to have_received(:add)
  27. end
  28. it 'adds an error when text and content warning are over character limit total' do
  29. status = status_double(spoiler_text: 'a' * 250, text: 'b' * 251)
  30. subject.validate(status)
  31. expect(status.errors).to have_received(:add)
  32. end
  33. it 'reduces calculated length of auto-linkable space-separated URLs' do
  34. text = [starting_string, example_link].join(' ')
  35. status = status_double(text: text)
  36. subject.validate(status)
  37. expect(status.errors).to_not have_received(:add)
  38. end
  39. it 'does not reduce calculated length of non-autolinkable URLs' do
  40. text = [starting_string, example_link].join
  41. status = status_double(text: text)
  42. subject.validate(status)
  43. expect(status.errors).to have_received(:add)
  44. end
  45. it 'does not reduce calculated length of count overly long URLs' do
  46. text = "http://example.com/valid?#{'#foo?' * 1000}"
  47. status = status_double(text: text)
  48. subject.validate(status)
  49. expect(status.errors).to have_received(:add)
  50. end
  51. it 'counts only the front part of remote usernames' do
  52. text = ('a' * 475) + " @alice@#{'b' * 30}.com"
  53. status = status_double(text: text)
  54. subject.validate(status)
  55. expect(status.errors).to_not have_received(:add)
  56. end
  57. it 'does count both parts of remote usernames for overly long domains' do
  58. text = "@alice@#{'b' * 500}.com"
  59. status = status_double(text: text)
  60. subject.validate(status)
  61. expect(status.errors).to have_received(:add)
  62. end
  63. end
  64. private
  65. def starting_string
  66. 'a' * 476
  67. end
  68. def example_link
  69. "http://#{'b' * 30}.com/example"
  70. end
  71. def status_double(spoiler_text: '', text: '')
  72. instance_double(
  73. Status,
  74. spoiler_text: spoiler_text,
  75. text: text,
  76. errors: activemodel_errors,
  77. local?: true,
  78. reblog?: false
  79. )
  80. end
  81. def activemodel_errors
  82. instance_double(ActiveModel::Errors, add: nil)
  83. end
  84. end