Skip to content

Commit a47903e

Browse files
authored
test: Use mock API when running tests (#95)
1 parent 60d6279 commit a47903e

File tree

15 files changed

+1503
-455
lines changed

15 files changed

+1503
-455
lines changed

.github/workflows/test.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ jobs:
77
test:
88
runs-on: ubuntu-latest
99
strategy:
10+
fail-fast: false
1011
matrix:
1112
version: [14, 16, 18, 20, 22, "latest"]
1213
steps:
@@ -16,5 +17,7 @@ jobs:
1617
node-version: ${{ matrix.version }}
1718
- run: npm ci
1819
- run: npm test
20+
- run: npm run build
21+
- run: cd test-app && npm install && node index.js
1922
env:
2023
IPINFO_TOKEN: ${{ secrets.IPINFO_TOKEN }}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"ip": "198.51.100.1",
3+
"bogon": true
4+
}

__tests__/fixtures/8.8.8.8.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"ip": "8.8.8.8",
3+
"hostname": "dns.google",
4+
"anycast": true,
5+
"city": "Mountain View",
6+
"region": "California",
7+
"country": "US",
8+
"loc": "37.4056,-122.0775",
9+
"org": "AS15169 Google LLC",
10+
"postal": "94043",
11+
"timezone": "America/Los_Angeles",
12+
"asn": {
13+
"asn": "AS15169",
14+
"name": "Google LLC",
15+
"domain": "google.com",
16+
"route": "8.8.8.0/24",
17+
"type": "hosting"
18+
},
19+
"company": {
20+
"name": "Google LLC",
21+
"domain": "google.com",
22+
"type": "hosting"
23+
},
24+
"privacy": {
25+
"vpn": false,
26+
"proxy": false,
27+
"tor": false,
28+
"relay": false,
29+
"hosting": true,
30+
"service": ""
31+
},
32+
"abuse": {
33+
"address": "US, CA, Mountain View, 1600 Amphitheatre Parkway, 94043",
34+
"country": "US",
35+
"email": "[email protected]",
36+
"name": "Abuse",
37+
"network": "8.8.8.0/24",
38+
"phone": "+1-650-253-0000"
39+
},
40+
"domains": {
41+
"ip": "8.8.8.8",
42+
"total": 12095,
43+
"domains": [
44+
"hdchina.org",
45+
"musicool.cn",
46+
"allnet.cn",
47+
"aonode.com",
48+
"didifilm.com"
49+
]
50+
}
51+
}

__tests__/fixtures/AS7922.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"asn": "AS7922",
3+
"name": "Comcast Cable Communications, LLC",
4+
"country": "US",
5+
"allocated": "1997-02-14",
6+
"registry": "arin",
7+
"domain": "comcast.com",
8+
"num_ips": 70110720,
9+
"type": "isp",
10+
"prefixes": [
11+
{
12+
"netblock": "107.0.0.0/14",
13+
"id": "JUMPSTART-5",
14+
"name": "Comcast Cable Communications, LLC",
15+
"country": "US",
16+
"size": "262144",
17+
"status": "ALLOCATION",
18+
"domain": "comcast.com"
19+
},
20+
{
21+
"netblock": "107.4.0.0/15",
22+
"id": "MICHIGAN-39",
23+
"name": "Comcast Cable Communications, Inc.",
24+
"country": "US",
25+
"size": "131072",
26+
"status": "REASSIGNMENT",
27+
"domain": "comcast.net"
28+
}
29+
],
30+
"peers": ["1273", "109"],
31+
"upstreams": ["6453", "1299"],
32+
"downstreams": ["398465", "32244"]
33+
}

__tests__/fixtures/batch.json

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"8.8.8.8/hostname": "dns.google",
3+
"4.4.4.4": {
4+
"ip": "4.4.4.4",
5+
"city": "Weda",
6+
"region": "North Maluku",
7+
"country": "ID",
8+
"loc": "0.3295,127.8739",
9+
"org": "AS3356 Level 3 Parent, LLC",
10+
"timezone": "Asia/Jayapura",
11+
"asn": {
12+
"asn": "AS3356",
13+
"name": "Level 3 Parent, LLC",
14+
"domain": "lumen.com",
15+
"route": "4.0.0.0/9",
16+
"type": "isp"
17+
},
18+
"company": {
19+
"name": "Level 3 Parent, LLC",
20+
"domain": "level3.com",
21+
"type": "isp"
22+
},
23+
"privacy": {
24+
"vpn": false,
25+
"proxy": false,
26+
"tor": false,
27+
"relay": false,
28+
"hosting": false,
29+
"service": ""
30+
},
31+
"abuse": {
32+
"address": "US, LA, Monroe, 100 CenturyLink Drive, 71203",
33+
"country": "US",
34+
"email": "[email protected]",
35+
"name": "L3 Abuse Contact",
36+
"network": "4.0.0.0/9",
37+
"phone": "+1-877-453-8353"
38+
},
39+
"domains": {
40+
"ip": "4.4.4.4",
41+
"total": 114,
42+
"domains": [
43+
"itmanagementgroup.de",
44+
"safermoto.com",
45+
"progeni.com",
46+
"grahamhostedservices.com",
47+
"bhcentral.tech"
48+
]
49+
}
50+
},
51+
"AS123": {
52+
"asn": "AS123",
53+
"name": "Air Force Systems Networking",
54+
"country": "US",
55+
"allocated": "1987-08-24",
56+
"registry": "arin",
57+
"domain": "af.mil",
58+
"num_ips": 0,
59+
"type": "inactive",
60+
"prefixes": [],
61+
"prefixes6": [],
62+
"peers": null,
63+
"upstreams": null,
64+
"downstreams": null,
65+
"countryCode": "US"
66+
}
67+
}

__tests__/fixtures/tools/map.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"status": "Report Generated",
3+
"reportUrl": "https://ipinfo.io/tools/map/example"
4+
}

__tests__/ipinfoWrapper.test.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ let ipinfoWrapper: IPinfoWrapper;
66

77
beforeEach(() => {
88
dotenv.config();
9-
const token = process.env.IPINFO_TOKEN || "";
109

11-
if (!token) {
12-
throw new Error(
13-
"Tests require a token in the IPINFO_TOKEN Environment Variable."
14-
);
15-
}
16-
17-
ipinfoWrapper = new IPinfoWrapper(token);
10+
ipinfoWrapper = new IPinfoWrapper(
11+
"example-token",
12+
undefined,
13+
undefined,
14+
{},
15+
"http://127.0.0.1:9090"
16+
);
1817
});
1918

2019
describe("IPinfoWrapper", () => {
@@ -90,6 +89,10 @@ describe("IPinfoWrapper", () => {
9089
expect(data.domain).toEqual("comcast.com");
9190
expect(data.num_ips).not.toBeFalsy();
9291
expect(data.type).toEqual("isp");
92+
expect(data.prefixes.length).toEqual(2);
93+
expect(data.peers.length).toEqual(2);
94+
expect(data.upstreams.length).toEqual(2);
95+
expect(data.downstreams.length).toEqual(2);
9396
}
9497
});
9598

@@ -121,6 +124,7 @@ describe("IPinfoWrapper", () => {
121124
city: "Weda",
122125
region: "North Maluku",
123126
country: "Indonesia",
127+
countryCode: "ID",
124128
loc: "0.3295,127.8739",
125129
org: "AS3356 Level 3 Parent, LLC",
126130
timezone: "Asia/Jayapura",
@@ -147,24 +151,23 @@ describe("IPinfoWrapper", () => {
147151
abuse: {
148152
address: "US, LA, Monroe, 100 CenturyLink Drive, 71203",
149153
country: "United States",
154+
countryCode: "US",
150155
151156
name: "L3 Abuse Contact",
152157
network: "4.0.0.0/9",
153-
phone: "+1-877-453-8353",
154-
countryCode: "US"
158+
phone: "+1-877-453-8353"
155159
},
156160
domains: {
157161
ip: "4.4.4.4",
158-
total: 111,
162+
total: 114,
159163
domains: [
160164
"itmanagementgroup.de",
161165
"safermoto.com",
162166
"progeni.com",
163167
"grahamhostedservices.com",
164168
"bhcentral.tech"
165169
]
166-
},
167-
countryCode: "ID"
170+
}
168171
});
169172

170173
expect(data["AS123"]).toEqual({

__tests__/jest.setup.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const fakeServer = require("test-fake-server");
2+
const path = require("path");
3+
4+
module.exports = async function globalSetup() {
5+
const model = {
6+
port: 9090,
7+
api: [
8+
{
9+
method: "GET",
10+
path: "/8.8.8.8/json",
11+
response: path.resolve(__dirname, "./fixtures/8.8.8.8.json")
12+
},
13+
{
14+
method: "GET",
15+
path: "/198.51.100.1/json",
16+
response: path.resolve(
17+
__dirname,
18+
"./fixtures/198.51.100.1.json"
19+
)
20+
},
21+
{
22+
method: "GET",
23+
path: "/AS7922/json",
24+
response: path.resolve(__dirname, "./fixtures/AS7922.json")
25+
},
26+
{
27+
method: "POST",
28+
path: "/batch",
29+
response: path.resolve(__dirname, "./fixtures/batch.json")
30+
},
31+
{
32+
method: "POST",
33+
path: "/tools/map",
34+
response: path.resolve(__dirname, "./fixtures/tools/map.json")
35+
}
36+
]
37+
};
38+
39+
globalThis.server = await fakeServer(model);
40+
};

__tests__/jest.teardown.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = async function globalTeardown() {
2+
await globalThis.server.stop();
3+
};

jest.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ module.exports = {
22
preset: "ts-jest",
33
testEnvironment: "node",
44
verbose: true,
5-
roots: ["<rootDir>/"]
5+
roots: ["<rootDir>/"],
6+
testMatch: ["**/*.test.ts"],
7+
globalSetup: "./__tests__/jest.setup.js",
8+
globalTeardown: "./__tests__/jest.teardown.js"
69
};

0 commit comments

Comments
 (0)