about summary refs log tree commit diff
path: root/spec/validators
diff options
context:
space:
mode:
Diffstat (limited to 'spec/validators')
-rw-r--r--spec/validators/blacklisted_email_validator_spec.rb31
-rw-r--r--spec/validators/disallowed_hashtags_validator_spec.rb46
-rw-r--r--spec/validators/follow_limit_validator_spec.rb51
-rw-r--r--spec/validators/status_length_validator_spec.rb13
-rw-r--r--spec/validators/status_pin_validator_spec.rb57
5 files changed, 196 insertions, 2 deletions
diff --git a/spec/validators/blacklisted_email_validator_spec.rb b/spec/validators/blacklisted_email_validator_spec.rb
new file mode 100644
index 000000000..d2e442f4a
--- /dev/null
+++ b/spec/validators/blacklisted_email_validator_spec.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+RSpec.describe BlacklistedEmailValidator, type: :validator do
+  describe '#validate' do
+    let(:user)   { double(email: 'info@mail.com', errors: errors) }
+    let(:errors) { double(add: nil) }
+
+    before do
+      allow_any_instance_of(described_class).to receive(:blocked_email?) { blocked_email }
+      described_class.new.validate(user)
+    end
+
+    context 'blocked_email?' do
+      let(:blocked_email) { true }
+
+      it 'calls errors.add' do
+        expect(errors).to have_received(:add).with(:email, I18n.t('users.invalid_email'))
+      end
+    end
+
+    context '!blocked_email?' do
+      let(:blocked_email) { false }
+
+      it 'not calls errors.add' do
+        expect(errors).not_to have_received(:add).with(:email, I18n.t('users.invalid_email'))
+      end
+    end
+  end
+end
diff --git a/spec/validators/disallowed_hashtags_validator_spec.rb b/spec/validators/disallowed_hashtags_validator_spec.rb
new file mode 100644
index 000000000..8ec1302ab
--- /dev/null
+++ b/spec/validators/disallowed_hashtags_validator_spec.rb
@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+RSpec.describe DisallowedHashtagsValidator, type: :validator do
+  describe '#validate' do
+    before do
+      allow_any_instance_of(described_class).to receive(:select_tags) { tags }
+      described_class.new.validate(status)
+    end
+
+    let(:status) { double(errors: errors, local?: local, reblog?: reblog, text: '') }
+    let(:errors) { double(add: nil) }
+
+    context 'unless status.local? && !status.reblog?' do
+      let(:local)  { false }
+      let(:reblog) { true }
+
+      it 'not calls errors.add' do
+        expect(errors).not_to have_received(:add).with(:text, any_args)
+      end
+    end
+
+    context 'status.local? && !status.reblog?' do
+      let(:local)  { true }
+      let(:reblog) { false }
+
+      context 'tags.empty?' do
+        let(:tags) { [] }
+
+        it 'not calls errors.add' do
+          expect(errors).not_to have_received(:add).with(:text, any_args)
+        end
+      end
+
+      context '!tags.empty?' do
+        let(:tags) { %w(a b c) }
+
+        it 'calls errors.add' do
+          expect(errors).to have_received(:add)
+            .with(:text, I18n.t('statuses.disallowed_hashtags', tags: tags.join(', '), count: tags.size))
+        end
+      end
+    end
+  end
+end
diff --git a/spec/validators/follow_limit_validator_spec.rb b/spec/validators/follow_limit_validator_spec.rb
new file mode 100644
index 000000000..cc8fbb631
--- /dev/null
+++ b/spec/validators/follow_limit_validator_spec.rb
@@ -0,0 +1,51 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+RSpec.describe FollowLimitValidator, type: :validator do
+  describe '#validate' do
+    before do
+      allow_any_instance_of(described_class).to receive(:limit_reached?).with(account) do
+        limit_reached
+      end
+
+      described_class.new.validate(follow)
+    end
+
+    let(:follow)  { double(account: account, errors: errors) }
+    let(:errors)  { double(add: nil) }
+    let(:account) { double(nil?: _nil, local?: local, following_count: 0, followers_count: 0) }
+    let(:_nil)    { true }
+    let(:local)   { false }
+
+    context 'follow.account.nil? || !follow.account.local?' do
+      let(:_nil)    { true }
+
+      it 'not calls errors.add' do
+        expect(errors).not_to have_received(:add).with(:base, any_args)
+      end
+    end
+
+    context '!(follow.account.nil? || !follow.account.local?)' do
+      let(:_nil)    { false }
+      let(:local)   { true }
+
+      context 'limit_reached?' do
+        let(:limit_reached) { true }
+
+        it 'calls errors.add' do
+          expect(errors).to have_received(:add)
+            .with(:base, I18n.t('users.follow_limit_reached', limit: FollowLimitValidator::LIMIT))
+        end
+      end
+
+      context '!limit_reached?' do
+        let(:limit_reached) { false }
+
+        it 'not calls errors.add' do
+          expect(errors).not_to have_received(:add).with(:base, any_args)
+        end
+      end
+    end
+  end
+end
diff --git a/spec/validators/status_length_validator_spec.rb b/spec/validators/status_length_validator_spec.rb
index 9355c7e3f..62791cd2f 100644
--- a/spec/validators/status_length_validator_spec.rb
+++ b/spec/validators/status_length_validator_spec.rb
@@ -4,8 +4,17 @@ require 'rails_helper'
 
 describe StatusLengthValidator do
   describe '#validate' do
-    it 'does not add errors onto remote statuses'
-    it 'does not add errors onto local reblogs'
+    it 'does not add errors onto remote statuses' do
+      status = double(local?: false)
+      subject.validate(status)
+      expect(status).not_to receive(:errors)
+    end
+
+    it 'does not add errors onto local reblogs' do
+      status = double(local?: false, reblog?: true)
+      subject.validate(status)
+      expect(status).not_to receive(:errors)
+    end
 
     it 'adds an error when content warning is over MAX_CHARS characters' do
       chars = StatusLengthValidator::MAX_CHARS + 1
diff --git a/spec/validators/status_pin_validator_spec.rb b/spec/validators/status_pin_validator_spec.rb
new file mode 100644
index 000000000..06532e5b3
--- /dev/null
+++ b/spec/validators/status_pin_validator_spec.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+RSpec.describe StatusPinValidator, type: :validator do
+  describe '#validate' do
+    before do
+      subject.validate(pin)
+    end
+
+    let(:pin) { double(account: account, errors: errors, status: status, account_id: pin_account_id) }
+    let(:status) { double(reblog?: reblog, account_id: status_account_id, visibility: visibility) }
+    let(:account)     { double(status_pins: status_pins, local?: local) }
+    let(:status_pins) { double(count: count) }
+    let(:errors)      { double(add: nil) }
+    let(:pin_account_id)    { 1 }
+    let(:status_account_id) { 1 }
+    let(:visibility)  { 'public' }
+    let(:local)       { false }
+    let(:reblog)      { false }
+    let(:count)       { 0 }
+
+    context 'pin.status.reblog?' do
+      let(:reblog) { true }
+
+      it 'calls errors.add' do
+        expect(errors).to have_received(:add).with(:base, I18n.t('statuses.pin_errors.reblog'))
+      end
+    end
+
+    context 'pin.account_id != pin.status.account_id' do
+      let(:pin_account_id)    { 1 }
+      let(:status_account_id) { 2 }
+
+      it 'calls errors.add' do
+        expect(errors).to have_received(:add).with(:base, I18n.t('statuses.pin_errors.ownership'))
+      end
+    end
+
+    context 'unless %w(public unlisted).include?(pin.status.visibility)' do
+      let(:visibility) { '' }
+
+      it 'calls errors.add' do
+        expect(errors).to have_received(:add).with(:base, I18n.t('statuses.pin_errors.private'))
+      end
+    end
+
+    context 'pin.account.status_pins.count > 4 && pin.account.local?' do
+      let(:count) { 5 }
+      let(:local) { true }
+
+      it 'calls errors.add' do
+        expect(errors).to have_received(:add).with(:base, I18n.t('statuses.pin_errors.limit'))
+      end
+    end
+  end
+end