-
Notifications
You must be signed in to change notification settings - Fork 20
/
app.test.js
144 lines (115 loc) · 3.22 KB
/
app.test.js
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
const request = require("supertest")
const { initializeDatabase } = require("./dbInit")
// Initialize the database before requiring the app
initializeDatabase()
const app = require("./app")
describe("GET /", () => {
let ip, req
beforeEach(() => {
ip = "9.9.9.9"
req = request(app).get("/").set("x-forwarded-for", `${ip},1.2.3.4`)
})
it("returns ip", async () => {
const res = await req
expect(res.body.ip).toBe(ip)
})
it("returns country", async () => {
const res = await req
expect(res.body.country).toBe("US")
})
it("sets cache control to no-cache", async () => {
const res = await req
expect(res.headers["cache-control"]).toBe("no-cache")
})
describe("with cloudflare", () => {
let country
beforeEach(() => {
country = "DE"
req.set("cf-ipcountry", country)
})
it("returns ip", async () => {
const res = await req
expect(res.body.ip).toBe(ip)
})
it("returns country", async () => {
const res = await req
expect(res.body.country).toBe(country)
})
})
})
describe("GET /:ip", () => {
let ip, req
describe("given a good ip", () => {
beforeEach(() => {
ip = "9.9.9.9"
req = request(app).get(`/${ip}`).set("x-forwarded-for", "1.2.3.4")
})
it("returns ip", async () => {
const res = await req
expect(res.body.ip).toBe(ip)
})
it("returns country", async () => {
const res = await req
expect(res.body.country).toBe("US")
})
it("sets cache control to public", async () => {
const res = await req
expect(res.headers["cache-control"]).toContain("public")
})
describe("with cloudflare", () => {
beforeEach(() => {
req.set("cf-ipcountry", "DE")
})
it("returns country", async () => {
const res = await req
expect(res.body.country).toBe("US")
})
})
})
describe("given an ip that doesn't return a match", () => {
beforeEach(() => {
ip = "192.168.0.1"
req = request(app).get(`/${ip}`).set("x-forwarded-for", "1.2.3.4")
})
it("returns an error", async () => {
const res = await req
expect(res.status).toBe(404)
})
})
describe("given an invalid ip", () => {
beforeEach(() => {
ip = "9.9.9.9boom"
req = request(app).get(`/${ip}`).set("x-forwarded-for", "1.2.3.4")
})
it("returns an error", async () => {
const res = await req
expect(res.status).toBe(422)
})
})
})
describe("GET /info", () => {
let req
beforeEach(() => {
req = request(app).get("/info")
})
it("returns when last updated", async () => {
const res = await req
expect(res.body.lastUpdated).toBeDefined()
})
it("returns data sources", async () => {
const res = await req.set("cf-ipcountry", "DE")
expect(res.body.dataSources).toBeDefined()
})
it("sets cache-control to public", async () => {
const res = await req
expect(res.headers["cache-control"]).toContain("public")
})
})
it("handles bad routes", async () => {
const res = await request(app).get("/foo/bar")
expect(res.status).toBe(422)
})
it("returns a robots.txt", async () => {
const res = await request(app).get("/robots.txt")
expect(res.status).toBe(200)
})