status_length_validator_spec.rb 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 = instance_double(Status, local?: false)
  7. allow(status).to receive(:errors)
  8. subject.validate(status)
  9. expect(status).to_not have_received(:errors)
  10. end
  11. it 'does not add errors onto local reblogs' do
  12. status = instance_double(Status, local?: false, reblog?: true)
  13. allow(status).to receive(:errors)
  14. subject.validate(status)
  15. expect(status).to_not have_received(:errors)
  16. end
  17. it 'adds an error when content warning is over 500 characters' do
  18. status = instance_double(Status, spoiler_text: 'a' * 520, text: '', errors: activemodel_errors, local?: true, reblog?: false)
  19. subject.validate(status)
  20. expect(status.errors).to have_received(:add)
  21. end
  22. it 'adds an error when text is over 500 characters' do
  23. status = instance_double(Status, spoiler_text: '', text: 'a' * 520, errors: activemodel_errors, 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 500 characters total' do
  28. status = instance_double(Status, spoiler_text: 'a' * 250, text: 'b' * 251, errors: activemodel_errors, local?: true, reblog?: false)
  29. subject.validate(status)
  30. expect(status.errors).to have_received(:add)
  31. end
  32. it 'counts URLs as 23 characters flat' do
  33. text = ('a' * 476) + " http://#{'b' * 30}.com/example"
  34. status = instance_double(Status, spoiler_text: '', text: text, errors: activemodel_errors, local?: true, reblog?: false)
  35. subject.validate(status)
  36. expect(status.errors).to_not have_received(:add)
  37. end
  38. it 'does not count non-autolinkable URLs as 23 characters flat' do
  39. text = ('a' * 476) + "http://#{'b' * 30}.com/example"
  40. status = instance_double(Status, spoiler_text: '', text: text, errors: activemodel_errors, local?: true, reblog?: false)
  41. subject.validate(status)
  42. expect(status.errors).to have_received(:add)
  43. end
  44. it 'does not count overly long URLs as 23 characters flat' do
  45. text = "http://example.com/valid?#{'#foo?' * 1000}"
  46. status = instance_double(Status, spoiler_text: '', text: text, errors: activemodel_errors, local?: true, reblog?: false)
  47. subject.validate(status)
  48. expect(status.errors).to have_received(:add)
  49. end
  50. it 'counts only the front part of remote usernames' do
  51. text = ('a' * 475) + " @alice@#{'b' * 30}.com"
  52. status = instance_double(Status, spoiler_text: '', text: text, errors: activemodel_errors, local?: true, reblog?: false)
  53. subject.validate(status)
  54. expect(status.errors).to_not have_received(:add)
  55. end
  56. it 'does count both parts of remote usernames for overly long domains' do
  57. text = "@alice@#{'b' * 500}.com"
  58. status = instance_double(Status, spoiler_text: '', text: text, errors: activemodel_errors, local?: true, reblog?: false)
  59. subject.validate(status)
  60. expect(status.errors).to have_received(:add)
  61. end
  62. end
  63. private
  64. def activemodel_errors
  65. instance_double(ActiveModel::Errors, add: nil)
  66. end
  67. end