-
Notifications
You must be signed in to change notification settings - Fork 30
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
fix: GeoIP Interceptor + Test Cases #137
Open
DarrenDsouza7273
wants to merge
92
commits into
SamagraX-Stencil:dev
Choose a base branch
from
DarrenDsouza7273:Geoip-updated
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 79 commits
Commits
Show all changes
92 commits
Select commit
Hold shift + click to select a range
e8d0359
fix: file upload sample
techsavvyash a431b51
(skip-ci) docs: update README
techsavvyash 0e018d7
Create codeql.yml
Himasnhu-AT 93806ed
implement geoip blocking
Himasnhu-AT 680bcce
updated minor changes
RiyaRaj28 e268f1f
added config service for geoIp url
RiyaRaj28 92e65be
Delete sample/07-geopip-blocking/src/app.controller.spec.ts
RiyaRaj28 7b2a280
Delete sample/07-geopip-blocking/test/app.e2e-spec.ts
RiyaRaj28 4382416
fix: Consists of Suggested changes and even added test cases
DarrenDsouza7273 cbf686e
fix: Update geoip.interceptor.ts file
DarrenDsouza7273 0c64956
fix: Removed unnecessary files
DarrenDsouza7273 43afc36
fix: added e2e test cases
DarrenDsouza7273 30e4c52
added capp.controller.spec.ts
DarrenDsouza7273 2a2ea0e
Adds a dev setup and dev workflow guide to CONTRIBUTIING.md
gnaaruag 6b1fc7f
Merge pull request #163 from gnaaruag/contribution-guide-add
techsavvyash d6a56e0
feat: tweaks + spec file
techsavvyash 82507d1
added dto files
RiyaRaj28 ac039b3
updated in dev
RiyaRaj28 f7138d9
Fix : Readme temporal package setup (#116)
ShivamAher30 5c44725
Updated package.json (#119)
RiyaRaj28 e20c146
Fix-NPM scripts changed from nest to stencil [05-temporal-package] (#…
ShivamAher30 e9cae67
Update file-upload.service.ts (#115)
SurAyush 9164759
Update README.md (#112)
SurAyush be4b892
Implemented Tests for different packages under common folder (#107)
SHARJIDH 2f183ab
chore: add pull request template
techsavvyash e56c57b
updates cli and monitoring docs (#123)
RiyaRaj28 1e5399a
Created CONTRIBUTING.md (#133)
RiyaRaj28 41e3624
Created test.yml
RiyaRaj28 3a56e81
Update app.service.ts
RiyaRaj28 05836cd
chore: fix nits
techsavvyash 8fa2fa4
fix: updated format
Savio629 fa639c8
fix: added test for response-format
Savio629 2334184
fix: made required changes
Savio629 5513899
fix: changed variable name
Savio629 d5a7b03
fix: failing tests and logic
techsavvyash 06359ee
chore: cleanup'
techsavvyash feae927
chore: restructure
techsavvyash 7e6b6e7
fix: file upload sample
techsavvyash de3c19e
fix: minio not picking up SSL var
techsavvyash 63e5159
fix: minio
cfd74e0
Implemented Tests for different packages under common folder (#107)
SHARJIDH 3923316
fix:updated all env vars
b0019d9
fix:added tests for download endpoint
a3a8bff
fix: added unit tests
ed7cf9d
fix: credentials changed
83e1225
Added Multiple FIles Upload Feature
SurAyush 750f591
docs: merge #129
techsavvyash 09c5f73
fix: resolve review comments
techsavvyash 27fe777
fix: Updated File Upload
DarrenDsouza7273 e8d949a
fix: Updated Readme
DarrenDsouza7273 d0d2e94
Update README.md
DarrenDsouza7273 5e4d41f
fix: fixed conflicts
Savio629 fd01e55
fix: extra conflicts
Savio629 6eb56f5
fix: removed console.log
Savio629 c20fe3c
fix: enabled upload-file to have stability
Savio629 b1ee3e1
branch updated
Savio629 1d76839
fix: updated the branch
Savio629 472bf8b
fix: file upload service
techsavvyash b4d6d98
feat: tweaks + spec file
techsavvyash bc44c4c
Implemented Tests for different packages under common folder (#107)
SHARJIDH 167e827
fix: added test for response-format
Savio629 d82b364
fix: failing tests and logic
techsavvyash 2ec9d16
fix: minio not picking up SSL var
techsavvyash 7852883
Implemented Tests for different packages under common folder (#107)
SHARJIDH c1ce0be
fix:updated all env vars
dc3da02
fix: added unit tests
e30e474
Added Multiple FIles Upload Feature
SurAyush db622dc
fix: extra conflicts
Savio629 1e6b2ea
fix: file upload service
techsavvyash 7ba71a9
fix: update filename regex to allow spaces
techsavvyash af92616
feat: tweaks + spec file
techsavvyash 81a0f4d
Implemented Tests for different packages under common folder (#107)
SHARJIDH c6127b0
fix: added test for response-format
Savio629 595e527
fix: minio not picking up SSL var
techsavvyash ab6e3a3
Implemented Tests for different packages under common folder (#107)
SHARJIDH 3e87551
fix:updated all env vars
2c6c9f6
fix: added unit tests
00096d5
Added Multiple FIles Upload Feature
SurAyush 763fea0
fix: file upload service
techsavvyash d456dc5
feat: tweaks + spec file
techsavvyash d09cf23
Merge branch 'dev' into Geoip-updated
Savio629 49bd406
chore: add commitlint config
techsavvyash 034f683
fix: Deleted src/interceptors/test/response-time.interceptor.spec.ts
Savio629 cd1b608
Merge branch 'SamagraX-Stencil:main' into Geoip-updated
Savio629 37430b4
fix: fixed failing ci
Savio629 a5a72cf
fix: updated the test and some fixes
Savio629 5deae9f
feat: support for coord, geofence,cities
Savio629 935db66
fix: fixed failing yarn test
Savio629 e31ebe9
fix: updated the test
Savio629 a1b04ae
fix: updated the test1
Savio629 3fc07e3
fix: removed console.log
Savio629 8fd357c
fix: updated e2e tests
Savio629 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,14 +1,10 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testMatch: ['**/test/**/*.spec.ts'], | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'json'], | ||
transform: { | ||
'^.+\\.(t|j)s$': [ | ||
'ts-jest', | ||
{ | ||
tsconfig: 'tsconfig.json', | ||
}, | ||
], | ||
}, | ||
}; | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testMatch: ['**/test/**/*.spec.ts'], | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'json'], | ||
transform: { | ||
transform_regex: ['ts-jest', {tsconfig:'tsconfig.json'}], | ||
}, | ||
}; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { | ||
Injectable, | ||
NestInterceptor, | ||
ExecutionContext, | ||
CallHandler, | ||
Logger, | ||
HttpStatus, | ||
InternalServerErrorException, | ||
HttpException, | ||
} from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { Observable } from 'rxjs'; | ||
import { HttpService } from '@nestjs/axios'; | ||
|
||
@Injectable() | ||
export class GeoIPInterceptor implements NestInterceptor { | ||
private readonly httpService: HttpService; | ||
private allowedCountries: string[]; | ||
private readonly configService: ConfigService; | ||
private readonly logger: Logger; | ||
private readonly accessDeniedMessage: string; | ||
private readonly accessDeniedStatus: number; | ||
|
||
constructor( | ||
allowedCountries?: string[], | ||
accessDeniedStatus: number = HttpStatus.FORBIDDEN, | ||
) { | ||
this.logger = new Logger('GeoIPInterceptor'); | ||
this.httpService = new HttpService(); | ||
this.allowedCountries = allowedCountries ?? ['India']; | ||
this.configService = new ConfigService(); | ||
this.accessDeniedMessage = 'Access Denied'; | ||
this.accessDeniedStatus = accessDeniedStatus; | ||
} | ||
|
||
async intercept( | ||
context: ExecutionContext, | ||
next: CallHandler, | ||
): Promise<Observable<any>> { | ||
const request = context.switchToHttp().getRequest(); | ||
|
||
// Extract IP address | ||
const clientIp = | ||
request.headers['request.ip'] || | ||
request.ip || | ||
request.headers['x-forwarded-for']; | ||
|
||
this.logger.verbose('Using IP address for geolocation:', clientIp); | ||
|
||
try { | ||
// Call the geolocation service to get the country from the IP | ||
const { country, regionName } = await this.getLocation(clientIp); | ||
|
||
if ( | ||
this.allowedCountries.length > 0 && | ||
!this.allowedCountries.includes(country) | ||
) { | ||
this.logger.error( | ||
'Denying request from IP: ' + clientIp + ' country: ' + country, | ||
); | ||
|
||
throw new HttpException( | ||
this.accessDeniedMessage, | ||
this.accessDeniedStatus, | ||
); | ||
} | ||
|
||
this.logger.log( | ||
'Allowed request from IP: ' + clientIp + ' region: ' + regionName, | ||
); | ||
} catch (err) { | ||
this.logger.error('Error occurred while reading the geoip database', err); | ||
throw new InternalServerErrorException( | ||
'Error occurred while reading the geoip database', | ||
); | ||
} | ||
|
||
// Continue handling the request | ||
return next.handle(); | ||
} | ||
|
||
async getLocation(ip: string): Promise<any> { | ||
try { | ||
const geoIp = this.configService.get<string>('GEO_IP'); | ||
return await this.httpService.axiosRef.get( | ||
`http://geoip.samagra.io/city/${ip}`, | ||
); | ||
} catch (err) { | ||
this.logger.error('Error occurred while reading the geoip service', err); | ||
throw new InternalServerErrorException( | ||
'Error occurred while reading the geoip database', | ||
); | ||
} | ||
} | ||
} |
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
77 changes: 77 additions & 0 deletions
77
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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); | ||
}); | ||
|
||
it('should throw an error for invalid IP address', () => { | ||
const allowedCountries = ['US', 'CA']; | ||
const ip = 'invalid-ip'; | ||
|
||
interceptor['allowedCountries'] = allowedCountries; | ||
|
||
expect(() => interceptor['isCountryAllowed'](ip)).toThrowError(); | ||
}); | ||
|
||
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); | ||
|
||
const result = await interceptor['getLocation'](ip); | ||
|
||
expect(result).toEqual(expectedLocation); | ||
}); | ||
|
||
it('should throw an error for an invalid IP address', async () => { | ||
const ip = 'invalid-ip'; | ||
|
||
jest | ||
.spyOn(interceptor, 'getLocation') | ||
.mockRejectedValue(new Error('Invalid IP')); | ||
|
||
await expect(interceptor['getLocation'](ip)).rejects.toThrowError( | ||
'Invalid IP', | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where are the test cases to check for other input ways? coordinates? geofences? failure cases when the user request is outside the jurisdiction of these countries and not just from localhost?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will update the pr with the above testcases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed e2e test
Finding it difficult to create integration test...