-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: updated the test and some fixes
- Loading branch information
Showing
10 changed files
with
15,108 additions
and
3,281 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
142 changes: 77 additions & 65 deletions
142
packages/common/src/interceptors/test/geoip.interceptor.spec.ts
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 |
---|---|---|
@@ -1,77 +1,89 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { GeoIPInterceptor } from '../geoip.interceptor'; | ||
import { HttpModule, HttpService } from '@nestjs/axios'; | ||
|
||
describe('Unit tests for geoIP interceptor', () => { | ||
describe('Unit tests for geoIP interceptor', () => { | ||
let interceptor: GeoIPInterceptor; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [GeoIPInterceptor], | ||
}).compile(); | ||
|
||
interceptor = module.get<GeoIPInterceptor>(GeoIPInterceptor); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(interceptor).toBeDefined(); | ||
}); | ||
|
||
it('should allow requests from allowed countries', () => { | ||
const allowedCountries = ['US', 'CA']; | ||
const ip = '192.168.0.1'; | ||
|
||
interceptor['allowedCountries'] = allowedCountries; | ||
|
||
const result = interceptor['isCountryAllowed'](ip); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should deny requests from disallowed countries', () => { | ||
const allowedCountries = ['US', 'CA']; | ||
const ip = '192.168.0.1'; | ||
|
||
interceptor['allowedCountries'] = allowedCountries; | ||
|
||
const result = interceptor['isCountryAllowed'](ip); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
import { | ||
ExecutionContext, | ||
HttpStatus, | ||
CallHandler, | ||
HttpException, | ||
InternalServerErrorException, | ||
Logger, | ||
} from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { HttpService } from '@nestjs/axios'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { Observable, of } from 'rxjs'; | ||
|
||
describe('GeoIPInterceptor', () => { | ||
let geoIPInterceptor: GeoIPInterceptor; | ||
let httpService: HttpService; | ||
let logger: Logger; | ||
|
||
beforeEach(async () => { | ||
const allowedCountries = ['India']; | ||
|
||
const mockHttpService = { | ||
axiosRef: { | ||
get: jest.fn(), | ||
}, | ||
}; | ||
|
||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
{ | ||
provide: HttpService, | ||
useValue: mockHttpService, | ||
}, | ||
ConfigService, | ||
{ | ||
provide: Logger, | ||
useValue: { | ||
verbose: jest.fn(), | ||
error: jest.fn(), | ||
log: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: GeoIPInterceptor, | ||
useFactory: () => new GeoIPInterceptor(allowedCountries), | ||
}, | ||
], | ||
}).compile(); | ||
|
||
geoIPInterceptor = module.get<GeoIPInterceptor>(GeoIPInterceptor); | ||
httpService = module.get<HttpService>(HttpService); | ||
logger = module.get<Logger>(Logger); | ||
}); | ||
|
||
it('should throw an error for invalid IP address', () => { | ||
const allowedCountries = ['US', 'CA']; | ||
const ip = 'invalid-ip'; | ||
it('should be defined', () => { | ||
expect(geoIPInterceptor).toBeDefined(); | ||
}); | ||
|
||
interceptor['allowedCountries'] = allowedCountries; | ||
it('should return the location data from the API for India', async () => { | ||
const geoData = { country: 'India', regionName: 'Maharashtra' }; | ||
(httpService.axiosRef.get as jest.Mock).mockResolvedValueOnce({ | ||
data: geoData, | ||
}); | ||
|
||
expect(() => interceptor['isCountryAllowed'](ip)).toThrowError(); | ||
const result = await geoIPInterceptor.getLocation('203.194.97.144'); | ||
expect(result.country).toEqual(geoData.country); | ||
expect(result.regionName).toEqual(geoData.regionName); | ||
}); | ||
|
||
it('should return the location for a valid IP address', async () => { | ||
const ip = '192.168.0.1'; | ||
const expectedLocation = { country: 'US', city: 'New York' }; | ||
|
||
jest | ||
.spyOn(interceptor, 'getLocation') | ||
.mockResolvedValue(expectedLocation); | ||
it('should return the location data from the API for USA', async () => { | ||
const geoData = { country: 'United States', regionName: 'California' }; | ||
(httpService.axiosRef.get as jest.Mock).mockResolvedValueOnce({ | ||
data: geoData, | ||
}); | ||
|
||
const result = await interceptor['getLocation'](ip); | ||
|
||
expect(result).toEqual(expectedLocation); | ||
const result = await geoIPInterceptor.getLocation('104.166.80.160'); | ||
expect(result.country).toEqual(geoData.country); | ||
expect(result.regionName).toEqual(geoData.regionName); | ||
}); | ||
|
||
it('should throw an error for an invalid IP address', async () => { | ||
const ip = 'invalid-ip'; | ||
|
||
jest | ||
.spyOn(interceptor, 'getLocation') | ||
.mockRejectedValue(new Error('Invalid IP')); | ||
it('should throw an error if the API call fails', async () => { | ||
(httpService.axiosRef.get as jest.Mock).mockRejectedValueOnce(new Error()); | ||
|
||
await expect(interceptor['getLocation'](ip)).rejects.toThrowError( | ||
'Invalid IP', | ||
); | ||
await expect( | ||
geoIPInterceptor.getLocation('127.0.0.1'), | ||
).rejects.toThrow(InternalServerErrorException); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.