-
Notifications
You must be signed in to change notification settings - Fork 32
/
ip_info_spec.rb
69 lines (55 loc) · 1.8 KB
/
ip_info_spec.rb
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
require 'rails_helper'
RSpec.describe IpInfo, type: :model do
context "exceptions" do
let(:subject) { described_class.new("73.11.237.17") }
it "is unsuccessful when the network times out" do
allow(Net::HTTP).to receive(:get).and_raise(Net::OpenTimeout)
expect(subject.successful?).to eq false
end
it "is unsuccessful when the json is invalid" do
allow(Net::HTTP).to receive(:get).and_return("") # invalid JSON
expect(subject.successful?).to eq false
end
it "is unsuccessful when the timeout is reached" do
allow(Net::HTTP).to receive(:get).and_raise(Timeout::Error)
expect(subject.successful?).to eq false
end
end
context "invalid ip addresses" do
let(:subject) { described_class.new("invalid") }
it "is unsuccessful" do
expect(subject.successful?).to eq false
end
it "sets a message about why" do
expect(subject.message).to match(/invalid/)
end
end
context "private ip addresses" do
let(:subject) { described_class.new("127.0.0.1") }
it "is unsuccessful" do
expect(subject.successful?).to eq false
end
it "sets a message about why" do
expect(subject.message).to match(/reserved/)
end
end
context "valid ip addresses" do
let(:subject) { described_class.new("73.11.237.17") }
it "is successful" do
expect(subject.successful?).to eq true
end
it "sets a variety of key/values" do
aggregate_failures do
expect(subject.city).to eq "Olympia"
expect(subject.region).to eq "WA"
expect(subject.country).to eq "United States"
end
end
it "knows how to convert key names" do
aggregate_failures do
expect(subject.country_code).to eq "US"
expect(subject.region_name).to eq "Washington"
end
end
end
end