File: validate_email_spec.rb

package info (click to toggle)
ruby-valid-email 0.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 280 kB
  • sloc: ruby: 575; makefile: 4
file content (139 lines) | stat: -rw-r--r-- 4,871 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# encoding: utf-8
require 'spec_helper'

describe ValidateEmail do
  describe '.valid?' do
    it 'returns true when passed email has valid format' do
      expect(ValidateEmail.valid?('user@gmail.com')).to be_truthy
      expect(ValidateEmail.valid?('valid.user@gmail.com')).to be_truthy
    end

    it 'returns false when passed email has invalid format' do
      expect(ValidateEmail.valid?('user@gmail.com.')).to be_falsey
      expect(ValidateEmail.valid?('user.@gmail.com')).to be_falsey
      expect(ValidateEmail.valid?('Hgft@(()).com')).to be_falsey
    end

    context 'when mx: true option passed' do
      let(:dns) { Resolv::DNS.new }

      def mock_dns_mx
        allow(dns).to receive(:getresources).and_return([])
        allow(Resolv::DNS).to receive(:new).and_return(dns)
      end

      xit 'returns true when mx record exist' do
        expect(ValidateEmail.valid?('user@gmail.com', mx: true)).to be_truthy
      end

      it "returns false when mx record doesn't exist" do
        mock_dns_mx
        expect(ValidateEmail.valid?('user@example.com', mx: true)).to be_falsey
      end

      it "IDN-encodes the domain name if it contains multibyte characters" do
        mock_dns_mx
        ValidateEmail.valid?("user@\u{1F600}.com", mx: true)
        expect(dns).to have_received(:getresources).with('xn--e28h.com', anything)
      end
    end

    context 'when domain: true option passed' do
      context 'with valid domains' do
        valid_domains = [
          'example.org',
          '0-mail.com',
          '0815.ru',
          '0clickemail.com',
          'test.co.uk',
          'fux0ringduh.com',
          'girlsundertheinfluence.com',
          'h.mintemail.com',
          'mail-temporaire.fr',
          'mt2009.com',
          'mega.zik.dj',
          '0.test.com',
          'e.test.com',
          'mail.e.test.com',
          'a.aa',
          'test.xn--clchc0ea0b2g2a9gcd',
          'my-domain.com',
          'тест.рф',
          'mail.тест.рф',
          'umläüt-domain.de',
        ]

        valid_domains.each do |valid_domain|
          it "returns true for #{valid_domain}" do
            email = "john@#{valid_domain}"
            expect(ValidateEmail.valid?(email, domain: true)).to be_truthy
          end
        end
      end

      context 'with invalid domain' do
        invalid_domains = [
          '-eouae.test',
          'oue-.test',
          'oeuoue.-oeuoue',
          'oueaaoeu.oeue-',
          'ouoeu.eou_ueoe',
          '.test.com',
          'test..com',
          'test@test.com',
          "example.org$\'",
          "foo bar.com",
        ]

        invalid_domains.each do |invalid_domain|
          it "returns false for #{invalid_domain}" do
            email = "john@#{invalid_domain}"
            expect(ValidateEmail.valid?(email, domain: true)).to be_falsey
          end
        end
      end
    end
  end

  describe '.valid_local?' do
    it 'returns false if the local segment is too long' do
      expect(
        ValidateEmail.valid_local?(
          'abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde'
        )
      ).to be_falsey
    end

    it 'returns false if the local segment has an empty dot atom' do
      expect(ValidateEmail.valid_local?('.user')).to be_falsey
      expect(ValidateEmail.valid_local?('.user.')).to be_falsey
      expect(ValidateEmail.valid_local?('user.')).to be_falsey
      expect(ValidateEmail.valid_local?('us..er')).to be_falsey
    end

    it 'returns false if the local segment has a special character in an unquoted dot atom' do
      expect(ValidateEmail.valid_local?('us@er')).to be_falsey
      expect(ValidateEmail.valid_local?('user.\\.name')).to be_falsey
      expect(ValidateEmail.valid_local?('user."name')).to be_falsey
    end

    it 'returns false if the local segment has an unescaped special character in a quoted dot atom' do
      expect(ValidateEmail.valid_local?('test." test".test')).to be_falsey
      expect(ValidateEmail.valid_local?('test."test\".test')).to be_falsey
      expect(ValidateEmail.valid_local?('test."te"st".test')).to be_falsey
      expect(ValidateEmail.valid_local?('test."\".test')).to be_falsey
    end

    it 'returns true if special characters exist but are properly quoted and escaped' do
      expect(ValidateEmail.valid_local?('"\ test"')).to be_truthy
      expect(ValidateEmail.valid_local?('"\\\\"')).to be_truthy
      expect(ValidateEmail.valid_local?('test."te@st".test')).to be_truthy
      expect(ValidateEmail.valid_local?('test."\\\\\"".test')).to be_truthy
      expect(ValidateEmail.valid_local?('test."blah\"\ \\\\"')).to be_truthy
    end

    it 'returns true if all characters are within the set of allowed characters' do
      expect(ValidateEmail.valid_local?('!#$%&\'*+-/=?^_`{|}~."\\\\\ \"(),:;<>@[]"')).to be_truthy
    end
  end
end