Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Use mock API when running tests #95

Merged
merged 7 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
version: [14, 16, 18, 20, 22, "latest"]
steps:
Expand All @@ -16,5 +17,7 @@ jobs:
node-version: ${{ matrix.version }}
- run: npm ci
- run: npm test
- run: npm run build
- run: cd test-app && npm install && node index.js
env:
IPINFO_TOKEN: ${{ secrets.IPINFO_TOKEN }}
4 changes: 4 additions & 0 deletions __tests__/fixtures/198.51.100.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ip": "198.51.100.1",
"bogon": true
}
51 changes: 51 additions & 0 deletions __tests__/fixtures/8.8.8.8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"ip": "8.8.8.8",
"hostname": "dns.google",
"anycast": true,
"city": "Mountain View",
"region": "California",
"country": "US",
"loc": "37.4056,-122.0775",
"org": "AS15169 Google LLC",
"postal": "94043",
"timezone": "America/Los_Angeles",
"asn": {
"asn": "AS15169",
"name": "Google LLC",
"domain": "google.com",
"route": "8.8.8.0/24",
"type": "hosting"
},
"company": {
"name": "Google LLC",
"domain": "google.com",
"type": "hosting"
},
"privacy": {
"vpn": false,
"proxy": false,
"tor": false,
"relay": false,
"hosting": true,
"service": ""
},
"abuse": {
"address": "US, CA, Mountain View, 1600 Amphitheatre Parkway, 94043",
"country": "US",
"email": "network-abuse@google.com",
"name": "Abuse",
"network": "8.8.8.0/24",
"phone": "+1-650-253-0000"
},
"domains": {
"ip": "8.8.8.8",
"total": 12095,
"domains": [
"hdchina.org",
"musicool.cn",
"allnet.cn",
"aonode.com",
"didifilm.com"
]
}
}
33 changes: 33 additions & 0 deletions __tests__/fixtures/AS7922.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"asn": "AS7922",
"name": "Comcast Cable Communications, LLC",
"country": "US",
"allocated": "1997-02-14",
"registry": "arin",
"domain": "comcast.com",
"num_ips": 70110720,
"type": "isp",
"prefixes": [
{
"netblock": "107.0.0.0/14",
"id": "JUMPSTART-5",
"name": "Comcast Cable Communications, LLC",
"country": "US",
"size": "262144",
"status": "ALLOCATION",
"domain": "comcast.com"
},
{
"netblock": "107.4.0.0/15",
"id": "MICHIGAN-39",
"name": "Comcast Cable Communications, Inc.",
"country": "US",
"size": "131072",
"status": "REASSIGNMENT",
"domain": "comcast.net"
}
],
"peers": ["1273", "109"],
"upstreams": ["6453", "1299"],
"downstreams": ["398465", "32244"]
}
67 changes: 67 additions & 0 deletions __tests__/fixtures/batch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"8.8.8.8/hostname": "dns.google",
"4.4.4.4": {
"ip": "4.4.4.4",
"city": "Weda",
"region": "North Maluku",
"country": "ID",
"loc": "0.3295,127.8739",
"org": "AS3356 Level 3 Parent, LLC",
"timezone": "Asia/Jayapura",
"asn": {
"asn": "AS3356",
"name": "Level 3 Parent, LLC",
"domain": "lumen.com",
"route": "4.0.0.0/9",
"type": "isp"
},
"company": {
"name": "Level 3 Parent, LLC",
"domain": "level3.com",
"type": "isp"
},
"privacy": {
"vpn": false,
"proxy": false,
"tor": false,
"relay": false,
"hosting": false,
"service": ""
},
"abuse": {
"address": "US, LA, Monroe, 100 CenturyLink Drive, 71203",
"country": "US",
"email": "abuse@level3.com",
"name": "L3 Abuse Contact",
"network": "4.0.0.0/9",
"phone": "+1-877-453-8353"
},
"domains": {
"ip": "4.4.4.4",
"total": 114,
"domains": [
"itmanagementgroup.de",
"safermoto.com",
"progeni.com",
"grahamhostedservices.com",
"bhcentral.tech"
]
}
},
"AS123": {
"asn": "AS123",
"name": "Air Force Systems Networking",
"country": "US",
"allocated": "1987-08-24",
"registry": "arin",
"domain": "af.mil",
"num_ips": 0,
"type": "inactive",
"prefixes": [],
"prefixes6": [],
"peers": null,
"upstreams": null,
"downstreams": null,
"countryCode": "US"
}
}
4 changes: 4 additions & 0 deletions __tests__/fixtures/tools/map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status": "Report Generated",
"reportUrl": "https://ipinfo.io/tools/map/example"
}
29 changes: 16 additions & 13 deletions __tests__/ipinfoWrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ let ipinfoWrapper: IPinfoWrapper;

beforeEach(() => {
dotenv.config();
const token = process.env.IPINFO_TOKEN || "";

if (!token) {
throw new Error(
"Tests require a token in the IPINFO_TOKEN Environment Variable."
);
}

ipinfoWrapper = new IPinfoWrapper(token);
ipinfoWrapper = new IPinfoWrapper(
"example-token",
undefined,
undefined,
{},
"http://127.0.0.1:9090"
);
});

describe("IPinfoWrapper", () => {
Expand Down Expand Up @@ -90,6 +89,10 @@ describe("IPinfoWrapper", () => {
expect(data.domain).toEqual("comcast.com");
expect(data.num_ips).not.toBeFalsy();
expect(data.type).toEqual("isp");
expect(data.prefixes.length).toEqual(2);
expect(data.peers.length).toEqual(2);
expect(data.upstreams.length).toEqual(2);
expect(data.downstreams.length).toEqual(2);
}
});

Expand Down Expand Up @@ -121,6 +124,7 @@ describe("IPinfoWrapper", () => {
city: "Weda",
region: "North Maluku",
country: "Indonesia",
countryCode: "ID",
loc: "0.3295,127.8739",
org: "AS3356 Level 3 Parent, LLC",
timezone: "Asia/Jayapura",
Expand All @@ -147,24 +151,23 @@ describe("IPinfoWrapper", () => {
abuse: {
address: "US, LA, Monroe, 100 CenturyLink Drive, 71203",
country: "United States",
countryCode: "US",
email: "abuse@level3.com",
name: "L3 Abuse Contact",
network: "4.0.0.0/9",
phone: "+1-877-453-8353",
countryCode: "US"
phone: "+1-877-453-8353"
},
domains: {
ip: "4.4.4.4",
total: 111,
total: 114,
domains: [
"itmanagementgroup.de",
"safermoto.com",
"progeni.com",
"grahamhostedservices.com",
"bhcentral.tech"
]
},
countryCode: "ID"
}
});

expect(data["AS123"]).toEqual({
Expand Down
40 changes: 40 additions & 0 deletions __tests__/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const fakeServer = require("test-fake-server");
const path = require("path");

module.exports = async function globalSetup() {
const model = {
port: 9090,
api: [
{
method: "GET",
path: "/8.8.8.8/json",
response: path.resolve(__dirname, "./fixtures/8.8.8.8.json")
},
{
method: "GET",
path: "/198.51.100.1/json",
response: path.resolve(
__dirname,
"./fixtures/198.51.100.1.json"
)
},
{
method: "GET",
path: "/AS7922/json",
response: path.resolve(__dirname, "./fixtures/AS7922.json")
},
{
method: "POST",
path: "/batch",
response: path.resolve(__dirname, "./fixtures/batch.json")
},
{
method: "POST",
path: "/tools/map",
response: path.resolve(__dirname, "./fixtures/tools/map.json")
}
]
};

globalThis.server = await fakeServer(model);
};
3 changes: 3 additions & 0 deletions __tests__/jest.teardown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = async function globalTeardown() {
await globalThis.server.stop();
};
5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ module.exports = {
preset: "ts-jest",
testEnvironment: "node",
verbose: true,
roots: ["<rootDir>/"]
roots: ["<rootDir>/"],
testMatch: ["**/*.test.ts"],
globalSetup: "./__tests__/jest.setup.js",
globalTeardown: "./__tests__/jest.teardown.js"
};
Loading
Loading