File: maxminddb_spec.rb

package info (click to toggle)
ruby-maxminddb 0.1.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 200 kB
  • sloc: ruby: 926; makefile: 3
file content (233 lines) | stat: -rw-r--r-- 7,181 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
require 'maxminddb'

describe MaxMindDB do
  [
    ['default file reader', MaxMindDB::DEFAULT_FILE_READER],
    ['low memory file reader', MaxMindDB::LOW_MEMORY_FILE_READER],
  ].each do |file_reader|
    description, reader = file_reader

    describe description do
      let(:city_db) { MaxMindDB.new('spec/cache/GeoLite2-City.mmdb', reader) }
      let(:country_db) { MaxMindDB.new('spec/cache/GeoLite2-Country.mmdb', reader) }
      let(:rec32_db) { MaxMindDB.new('spec/data/32bit_record_data.mmdb', reader) }

      context 'for the ip 74.125.225.224' do
        let(:ip) { '74.125.225.224' }

        it 'returns a MaxMindDB::Result' do
          expect(city_db.lookup(ip)).to be_kind_of(MaxMindDB::Result)
        end

        it 'finds data' do
          expect(city_db.lookup(ip)).to be_found
        end

        it 'returns Mountain View as the English name' do
          expect(city_db.lookup(ip).city.name).to eq('Alameda')
        end

        it 'returns -122.0574 as the longitude' do
          expect(city_db.lookup(ip).location.longitude).to eq(-122.2788)
        end

        it 'returns nil for is_anonymous_proxy' do
          expect(city_db.lookup(ip).traits.is_anonymous_proxy).to eq(nil)
        end

        it 'returns United States as the English country name' do
          expect(country_db.lookup(ip).country.name).to eq('United States')
        end

        it 'returns false for the is_in_european_union' do
          expect(country_db.lookup(ip).country.is_in_european_union).to eq(nil)
        end

        it 'returns US as the country iso code' do
          expect(country_db.lookup(ip).country.iso_code).to eq('US')
        end
        
        it 'returns 74.125.192.0/18 as network' do 
          expect(country_db.lookup(ip).network).to eq('74.125.192.0/18')
        end

        context 'as a Integer' do
          let(:integer_ip) { IPAddr.new(ip).to_i }

          it 'returns a MaxMindDB::Result' do
            expect(city_db.lookup(integer_ip)).to be_kind_of(MaxMindDB::Result)
          end

          it 'returns Mountain View as the English name' do
            expect(city_db.lookup(integer_ip).city.name).to eq('Alameda')
          end

          it 'returns United States as the English country name' do
            expect(country_db.lookup(integer_ip).country.name).to eq('United States')
          end
        end
      end

      context 'for the ip 2001:708:510:8:9a6:442c:f8e0:7133' do
        let(:ip) { '2001:708:510:8:9a6:442c:f8e0:7133' }

        it 'finds data' do
          expect(city_db.lookup(ip)).to be_found
        end

        it 'returns true for the is_in_european_union' do
          expect(country_db.lookup(ip).country.is_in_european_union).to eq(true)
        end

        it 'returns FI as the country iso code' do
          expect(country_db.lookup(ip).country.iso_code).to eq('FI')
        end
        
        it 'returns 2001:708::/32 as network' do 
          expect(country_db.lookup(ip).network).to eq('2001:708::/32')
        end

        context 'as an integer' do
          let(:integer_ip) { IPAddr.new(ip).to_i }

          it 'returns FI as the country iso code' do
            expect(country_db.lookup(integer_ip).country.iso_code).to eq('FI')
          end
        end
      end

      context 'for a Canadian ipv6' do
        let(:ip) { '2607:5300:60:72ba::' }

        it 'finds data' do
          expect(city_db.lookup(ip)).to be_found
        end

        it 'returns true for the is_in_european_union' do
          expect(country_db.lookup(ip).country.is_in_european_union).to be_falsey
        end

        it 'returns CA as the country iso code' do
          expect(country_db.lookup(ip).country.iso_code).to eq('CA')
        end
      end

      context 'for a German IPv6' do
        let(:ip) { '2a01:488:66:1000:2ea3:495e::1' }

        it 'finds data' do
          expect(city_db.lookup(ip)).to be_found
        end

        it 'returns true for the is_in_european_union' do
          expect(country_db.lookup(ip).country.is_in_european_union).to eq(true)
        end

        it 'returns DE as the country iso code' do
          expect(country_db.lookup(ip).country.iso_code).to eq('DE')
        end
      end

      context 'for the ip 127.0.0.1' do
        let(:ip) { '127.0.0.1' }

        it 'returns a MaxMindDB::Result' do
          expect(city_db.lookup(ip)).to be_kind_of(MaxMindDB::Result)
        end

        it "doesn't find data" do
          expect(city_db.lookup(ip)).to_not be_found
        end
      end

      context 'for local ip addresses' do
        let(:ip) { '127.0.0.1' }

        context 'for city_db' do
          before do
            city_db.local_ip_alias = '74.125.225.224'
          end

          it 'returns a MaxMindDB::Result' do
            expect(city_db.lookup(ip)).to be_kind_of(MaxMindDB::Result)
          end

          it 'finds data' do
            expect(city_db.lookup(ip)).to be_found
          end

          it 'returns Mountain View as the English name' do
            expect(city_db.lookup(ip).city.name).to eq('Alameda')
          end

          it 'returns -122.0574 as the longitude' do
            expect(city_db.lookup(ip).location.longitude).to eq(-122.2788)
          end

          it 'returns nil for is_anonymous_proxy' do
            expect(city_db.lookup(ip).traits.is_anonymous_proxy).to eq(nil)
          end
        end

        context 'for country_db' do
          before do
            country_db.local_ip_alias = '74.125.225.224'
          end

          it 'returns United States as the English country name' do
            expect(country_db.lookup(ip).country.name).to eq('United States')
          end

          it 'returns false for the is_in_european_union' do
            expect(country_db.lookup(ip).country.is_in_european_union).to eq(nil)
          end

          it 'returns US as the country iso code' do
            expect(country_db.lookup(ip).country.iso_code).to eq('US')
          end
        end
      end

      context 'for 32bit record data' do
        let(:ip) { '1.0.16.1' }

        it 'finds data' do
          expect(rec32_db.lookup(ip)).to be_found
        end
      end

      context 'test ips' do
        [
          ['185.23.124.1', 'SA'],
          ['178.72.254.1', 'CZ'],
          ['95.153.177.210', 'RU'],
          ['200.148.105.119', 'BR'],
          ['195.59.71.43', 'GB'],
          ['179.175.47.87', 'BR'],
          ['202.67.40.50', 'ID'],
        ].each do |ip, iso|
          it 'returns a MaxMindDB::Result' do
            expect(city_db.lookup(ip)).to be_kind_of(MaxMindDB::Result)
          end

          it "returns #{iso} as the country iso code" do
            expect(country_db.lookup(ip).country.iso_code).to eq(iso)
          end
        end
      end

      context 'test boolean data' do
        let(:ip) { '41.194.0.1' }

        it 'returns true for the is_satellite_provider trait' do
          expect(city_db.lookup(ip).traits.is_satellite_provider).to eq(nil)
        end

        # There are no false booleans in the database that we can test.
        # False values are simply omitted.
      end
    end
  end
end

# vim: et ts=2 sw=2 ff=unix