Skip to content

Commit

Permalink
Add unit tests for country code retrieval from IP
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Feb 12, 2025
1 parent 0dc112a commit 5c8e7e7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
56 changes: 56 additions & 0 deletions api/__tests__/ipinfo.test.ts
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')
})
})
13 changes: 3 additions & 10 deletions api/src/controllers/ipinfoController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Request, Response } from 'express'
import i18n from '../lang/i18n'
import * as ipinfoHelper from '../common/ipinfoHelper'
import * as logger from '../common/logger'

/**
* Returns ISO 2 country code from IP.
Expand All @@ -12,12 +10,7 @@ import * as logger from '../common/logger'
* @returns {unknown}
*/
export const getCountryCode = async (req: Request, res: Response) => {
try {
const clientIp = ipinfoHelper.getClientIp(req)
const countryCode = await ipinfoHelper.getCountryCode(clientIp)
return res.json(countryCode)
} catch (err) {
logger.error(`[paypal.createPayPalOrder] ${i18n.t('ERROR')}`, err)
return res.status(400).send(i18n.t('ERROR') + err)
}
const clientIp = ipinfoHelper.getClientIp(req)
const countryCode = await ipinfoHelper.getCountryCode(clientIp)
return res.json(countryCode)
}

0 comments on commit 5c8e7e7

Please sign in to comment.