about summary refs log tree commit diff
path: root/spec/validators
diff options
context:
space:
mode:
authorStarfall <us@starfall.systems>2023-04-14 19:22:47 -0500
committerStarfall <us@starfall.systems>2023-04-14 19:22:47 -0500
commit4fe1689de43f4404eb9530fcfbcbfb26d6c1c13a (patch)
tree6811b845bb7f4966b10dcefa3dea404246f161c7 /spec/validators
parent65c1e53a32cabcdbb7bca57002bb0f6acdebe07e (diff)
parentbed63f6dae0879ac840066b031229e0d139089cd (diff)
Merge remote-tracking branch 'glitch/main' HEAD main
Diffstat (limited to 'spec/validators')
-rw-r--r--spec/validators/blacklisted_email_validator_spec.rb8
-rw-r--r--spec/validators/disallowed_hashtags_validator_spec.rb6
-rw-r--r--spec/validators/email_mx_validator_spec.rb43
-rw-r--r--spec/validators/follow_limit_validator_spec.rb4
-rw-r--r--spec/validators/note_length_validator_spec.rb4
-rw-r--r--spec/validators/poll_validator_spec.rb5
-rw-r--r--spec/validators/status_length_validator_spec.rb6
-rw-r--r--spec/validators/unreserved_username_validator_spec.rb10
-rw-r--r--spec/validators/url_validator_spec.rb2
9 files changed, 66 insertions, 22 deletions
diff --git a/spec/validators/blacklisted_email_validator_spec.rb b/spec/validators/blacklisted_email_validator_spec.rb
index 351de0707..a642405ae 100644
--- a/spec/validators/blacklisted_email_validator_spec.rb
+++ b/spec/validators/blacklisted_email_validator_spec.rb
@@ -4,16 +4,16 @@ require 'rails_helper'
 
 RSpec.describe BlacklistedEmailValidator, type: :validator do
   describe '#validate' do
+    subject { described_class.new.validate(user); errors }
+
     let(:user)   { double(email: 'info@mail.com', sign_up_ip: '1.2.3.4', errors: errors) }
     let(:errors) { double(add: nil) }
 
     before do
-      allow(user).to receive(:valid_invitation?) { false }
+      allow(user).to receive(:valid_invitation?).and_return(false)
       allow_any_instance_of(described_class).to receive(:blocked_email_provider?) { blocked_email }
     end
 
-    subject { described_class.new.validate(user); errors }
-
     context 'when e-mail provider is blocked' do
       let(:blocked_email) { true }
 
@@ -26,7 +26,7 @@ RSpec.describe BlacklistedEmailValidator, type: :validator do
       let(:blocked_email) { false }
 
       it 'does not add errors' do
-        expect(subject).not_to have_received(:add).with(:email, :blocked)
+        expect(subject).to_not have_received(:add).with(:email, :blocked)
       end
 
       context 'when canonical e-mail is blocked' do
diff --git a/spec/validators/disallowed_hashtags_validator_spec.rb b/spec/validators/disallowed_hashtags_validator_spec.rb
index 9deec0bb9..896fd4fc5 100644
--- a/spec/validators/disallowed_hashtags_validator_spec.rb
+++ b/spec/validators/disallowed_hashtags_validator_spec.rb
@@ -11,7 +11,7 @@ RSpec.describe DisallowedHashtagsValidator, type: :validator do
       described_class.new.validate(status)
     end
 
-    let(:status) { double(errors: errors, local?: local, reblog?: reblog, text: disallowed_tags.map { |x| '#' + x }.join(' ')) }
+    let(:status) { double(errors: errors, local?: local, reblog?: reblog, text: disallowed_tags.map { |x| "##{x}" }.join(' ')) }
     let(:errors) { double(add: nil) }
 
     context 'for a remote reblog' do
@@ -19,7 +19,7 @@ RSpec.describe DisallowedHashtagsValidator, type: :validator do
       let(:reblog) { true }
 
       it 'does not add errors' do
-        expect(errors).not_to have_received(:add).with(:text, any_args)
+        expect(errors).to_not have_received(:add).with(:text, any_args)
       end
     end
 
@@ -31,7 +31,7 @@ RSpec.describe DisallowedHashtagsValidator, type: :validator do
         let(:disallowed_tags) { [] }
 
         it 'does not add errors' do
-          expect(errors).not_to have_received(:add).with(:text, any_args)
+          expect(errors).to_not have_received(:add).with(:text, any_args)
         end
       end
 
diff --git a/spec/validators/email_mx_validator_spec.rb b/spec/validators/email_mx_validator_spec.rb
index 4feedd0c7..a11b8e01e 100644
--- a/spec/validators/email_mx_validator_spec.rb
+++ b/spec/validators/email_mx_validator_spec.rb
@@ -28,6 +28,49 @@ describe EmailMxValidator do
       end
     end
 
+    it 'adds no error if there are DNS records for the e-mail domain' do
+      resolver = double
+
+      allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([])
+      allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([Resolv::DNS::Resource::IN::A.new('192.0.2.42')])
+      allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
+      allow(resolver).to receive(:timeouts=).and_return(nil)
+      allow(Resolv::DNS).to receive(:open).and_yield(resolver)
+
+      subject.validate(user)
+      expect(user.errors).to_not have_received(:add)
+    end
+
+    it 'adds an error if the TagManager fails to normalize domain' do
+      double = instance_double(TagManager)
+      allow(TagManager).to receive(:instance).and_return(double)
+      allow(double).to receive(:normalize_domain).with('example.com').and_raise(Addressable::URI::InvalidURIError)
+
+      user = double(email: 'foo@example.com', errors: double(add: nil))
+      subject.validate(user)
+      expect(user.errors).to have_received(:add)
+    end
+
+    it 'adds an error if the domain email portion is blank' do
+      user = double(email: 'foo@', errors: double(add: nil))
+      subject.validate(user)
+      expect(user.errors).to have_received(:add)
+    end
+
+    it 'adds an error if the email domain name contains empty labels' do
+      resolver = double
+
+      allow(resolver).to receive(:getresources).with('example..com', Resolv::DNS::Resource::IN::MX).and_return([])
+      allow(resolver).to receive(:getresources).with('example..com', Resolv::DNS::Resource::IN::A).and_return([Resolv::DNS::Resource::IN::A.new('192.0.2.42')])
+      allow(resolver).to receive(:getresources).with('example..com', Resolv::DNS::Resource::IN::AAAA).and_return([])
+      allow(resolver).to receive(:timeouts=).and_return(nil)
+      allow(Resolv::DNS).to receive(:open).and_yield(resolver)
+
+      user = double(email: 'foo@example..com', sign_up_ip: '1.2.3.4', errors: double(add: nil))
+      subject.validate(user)
+      expect(user.errors).to have_received(:add)
+    end
+
     it 'adds an error if there are no DNS records for the e-mail domain' do
       resolver = double
 
diff --git a/spec/validators/follow_limit_validator_spec.rb b/spec/validators/follow_limit_validator_spec.rb
index cc8fbb631..94ba0c47f 100644
--- a/spec/validators/follow_limit_validator_spec.rb
+++ b/spec/validators/follow_limit_validator_spec.rb
@@ -22,7 +22,7 @@ RSpec.describe FollowLimitValidator, type: :validator do
       let(:_nil)    { true }
 
       it 'not calls errors.add' do
-        expect(errors).not_to have_received(:add).with(:base, any_args)
+        expect(errors).to_not have_received(:add).with(:base, any_args)
       end
     end
 
@@ -43,7 +43,7 @@ RSpec.describe FollowLimitValidator, type: :validator do
         let(:limit_reached) { false }
 
         it 'not calls errors.add' do
-          expect(errors).not_to have_received(:add).with(:base, any_args)
+          expect(errors).to_not have_received(:add).with(:base, any_args)
         end
       end
     end
diff --git a/spec/validators/note_length_validator_spec.rb b/spec/validators/note_length_validator_spec.rb
index 6e9b4e132..390ac8d90 100644
--- a/spec/validators/note_length_validator_spec.rb
+++ b/spec/validators/note_length_validator_spec.rb
@@ -15,7 +15,7 @@ describe NoteLengthValidator do
     end
 
     it 'counts URLs as 23 characters flat' do
-      text   = ('a' * 476) + " http://#{'b' * 30}.com/example"
+      text = ('a' * 476) + " http://#{'b' * 30}.com/example"
       account = double(note: text, errors: double(add: nil))
 
       subject.validate_each(account, 'note', text)
@@ -23,7 +23,7 @@ describe NoteLengthValidator do
     end
 
     it 'does not count non-autolinkable URLs as 23 characters flat' do
-      text   = ('a' * 476) + "http://#{'b' * 30}.com/example"
+      text = ('a' * 476) + "http://#{'b' * 30}.com/example"
       account = double(note: text, errors: double(add: nil))
 
       subject.validate_each(account, 'note', text)
diff --git a/spec/validators/poll_validator_spec.rb b/spec/validators/poll_validator_spec.rb
index 941b83401..f3f4b1288 100644
--- a/spec/validators/poll_validator_spec.rb
+++ b/spec/validators/poll_validator_spec.rb
@@ -15,13 +15,14 @@ RSpec.describe PollValidator, type: :validator do
     let(:expires_at) { 1.day.from_now }
 
     it 'have no errors' do
-      expect(errors).not_to have_received(:add)
+      expect(errors).to_not have_received(:add)
     end
 
     context 'expires just 5 min ago' do
       let(:expires_at) { 5.minutes.from_now }
+
       it 'not calls errors add' do
-        expect(errors).not_to have_received(:add)
+        expect(errors).to_not have_received(:add)
       end
     end
   end
diff --git a/spec/validators/status_length_validator_spec.rb b/spec/validators/status_length_validator_spec.rb
index 4c80a59e6..7e06b9bd9 100644
--- a/spec/validators/status_length_validator_spec.rb
+++ b/spec/validators/status_length_validator_spec.rb
@@ -7,13 +7,13 @@ describe StatusLengthValidator do
     it 'does not add errors onto remote statuses' do
       status = double(local?: false)
       subject.validate(status)
-      expect(status).not_to receive(:errors)
+      expect(status).to_not 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)
+      expect(status).to_not receive(:errors)
     end
 
     it 'adds an error when content warning is over MAX_CHARS characters' do
@@ -65,7 +65,7 @@ describe StatusLengthValidator do
     it 'counts only the front part of remote usernames' do
       username = '@alice'
       chars = StatusLengthValidator::MAX_CHARS - 1 - username.length
-      text   = ('a' * 475) + " #{username}@#{'b' * 30}.com"
+      text   = ('a' * chars) + " #{username}@#{'b' * 30}.com"
       status = double(spoiler_text: '', text: text, errors: double(add: nil), local?: true, reblog?: false)
 
       subject.validate(status)
diff --git a/spec/validators/unreserved_username_validator_spec.rb b/spec/validators/unreserved_username_validator_spec.rb
index 746b3866c..3c6f71c59 100644
--- a/spec/validators/unreserved_username_validator_spec.rb
+++ b/spec/validators/unreserved_username_validator_spec.rb
@@ -11,18 +11,18 @@ RSpec.describe UnreservedUsernameValidator, type: :validator do
 
     let(:validator) { described_class.new }
     let(:account)   { double(username: username, errors: errors) }
-    let(:errors )   { double(add: nil) }
+    let(:errors) { double(add: nil) }
 
     context '@username.blank?' do
-      let(:username)  { nil }
+      let(:username) { nil }
 
       it 'not calls errors.add' do
-        expect(errors).not_to have_received(:add).with(:username, any_args)
+        expect(errors).to_not have_received(:add).with(:username, any_args)
       end
     end
 
     context '!@username.blank?' do
-      let(:username)  { 'f' }
+      let(:username) { 'f' }
 
       context 'reserved_username?' do
         let(:reserved_username) { true }
@@ -36,7 +36,7 @@ RSpec.describe UnreservedUsernameValidator, type: :validator do
         let(:reserved_username) { false }
 
         it 'not calls errors.add' do
-          expect(errors).not_to have_received(:add).with(:username, any_args)
+          expect(errors).to_not have_received(:add).with(:username, any_args)
         end
       end
     end
diff --git a/spec/validators/url_validator_spec.rb b/spec/validators/url_validator_spec.rb
index 85eadeb63..966261b50 100644
--- a/spec/validators/url_validator_spec.rb
+++ b/spec/validators/url_validator_spec.rb
@@ -27,7 +27,7 @@ RSpec.describe URLValidator, type: :validator do
       let(:compliant) { true }
 
       it 'not calls errors.add' do
-        expect(errors).not_to have_received(:add).with(attribute, any_args)
+        expect(errors).to_not have_received(:add).with(attribute, any_args)
       end
     end
   end