-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for country code retrieval from IP
- Loading branch information
Showing
2 changed files
with
59 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import 'dotenv/config' | ||
import request from 'supertest' | ||
import * as env from '../src/config/env.config' | ||
import app from '../src/app' | ||
import * as databaseHelper from '../src/common/databaseHelper' | ||
import * as testHelper from './testHelper' | ||
|
||
// | ||
// Connecting and initializing the database before running the test suite | ||
// | ||
beforeAll(async () => { | ||
testHelper.initializeLogger() | ||
|
||
const res = await databaseHelper.connect(env.DB_URI, false, false) | ||
expect(res).toBeTruthy() | ||
await testHelper.initialize() | ||
}) | ||
|
||
// | ||
// Closing and cleaning the database connection after running the test suite | ||
// | ||
afterAll(async () => { | ||
await testHelper.close() | ||
await databaseHelper.close() | ||
}) | ||
|
||
describe('GET /api/country-code', () => { | ||
it('should get country from ip', async () => { | ||
// test without x-forwarded-for | ||
let res = await request(app) | ||
.get('/api/country-code') | ||
expect(res.statusCode).toBe(200) | ||
expect(res.body).toBe('US') | ||
|
||
// test with x-forwarded-for | ||
res = await request(app) | ||
.get('/api/country-code') | ||
.set('x-forwarded-for', '51.91.60.241') // OVH France (Roubaix Data Center) | ||
expect(res.statusCode).toBe(200) | ||
expect(res.body).toBe('FR') | ||
|
||
// test with x-forwarded-for IPv6-mapped IPv4 address | ||
res = await request(app) | ||
.get('/api/country-code') | ||
.set('x-forwarded-for', '::ffff:51.91.60.241') | ||
expect(res.statusCode).toBe(200) | ||
expect(res.body).toBe('FR') | ||
|
||
// test wrong ip address | ||
res = await request(app) | ||
.get('/api/country-code') | ||
.set('x-forwarded-for', 'wrong ip address') | ||
expect(res.statusCode).toBe(200) | ||
expect(res.body).toBe('US') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters