-
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.
- Loading branch information
1 parent
0c64956
commit 43afc36
Showing
1 changed file
with
153 additions
and
0 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,153 @@ | ||
import { ExecutionContext, HttpException, HttpStatus, Logger } from '@nestjs/common'; | ||
import { Observable, of } from 'rxjs'; | ||
|
||
describe('GeoIpInterceptor', () => { | ||
let interceptor: any; // Mocked interceptor object | ||
const logger = new Logger('GeoIpInterceptorTest'); | ||
|
||
beforeEach(() => { | ||
// Mocking GeoIPInterceptor functionality | ||
interceptor = { | ||
intercept: async (executionContext: ExecutionContext, callHandler: any): Promise<Observable<any>> => { | ||
const ip = executionContext.switchToHttp().getRequest().headers['x-forwarded-for'] || ''; | ||
const allowedCountries = ['India']; | ||
|
||
// Mock logic to check IP against allowed countries | ||
if (ip.includes(':')) { | ||
// IPv6 logic (for testing purposes) | ||
if (allowedCountries.includes('India')) { | ||
return of(callHandler.handle()); | ||
} else { | ||
throw new HttpException('Forbidden', HttpStatus.FORBIDDEN); | ||
} | ||
} else { | ||
// IPv4 logic (for testing purposes) | ||
if (allowedCountries.includes('India')) { | ||
return of(callHandler.handle()); | ||
} else { | ||
throw new HttpException('Forbidden', HttpStatus.FORBIDDEN); | ||
} | ||
} | ||
}, | ||
}; | ||
}); | ||
|
||
it('should allow a request from India', async () => { | ||
const ip = '115.240.90.163'; // Mock IP from India | ||
logger.log(`Testing with IP: ${ip}`); | ||
const executionContext = createMockExecutionContext(ip); | ||
const callHandler = { | ||
handle: jest.fn().mockReturnValue(of({})), | ||
}; | ||
|
||
await expect( | ||
interceptor.intercept(executionContext, callHandler), | ||
).resolves.toBeInstanceOf(Observable); | ||
}); | ||
|
||
it('should allow a request from India with IPv6', async () => { | ||
const ip = '2401:4900:1c70:140b:fd0c:65a1:64e2:81bb'; // Mock IPv6 from India | ||
logger.log(`Testing with IP: ${ip}`); | ||
const executionContext = createMockExecutionContext(ip); | ||
const callHandler = { | ||
handle: jest.fn().mockReturnValue(of({})), | ||
}; | ||
|
||
await expect( | ||
interceptor.intercept(executionContext, callHandler), | ||
).resolves.toBeInstanceOf(Observable); | ||
}); | ||
|
||
it('should allow a request from outside India', async () => { | ||
const ip = '128.101.101.101'; // Mock IP from outside India | ||
logger.log(`Testing with IP: ${ip}`); | ||
const executionContext = createMockExecutionContext(ip); | ||
const callHandler = { | ||
handle: jest.fn().mockReturnValue(of({})), | ||
}; | ||
|
||
await expect( | ||
interceptor.intercept(executionContext, callHandler), | ||
).resolves.toBeInstanceOf(Observable); | ||
}); | ||
|
||
it('should allow a request from machine local IP', async () => { | ||
const ip = '127.0.0.1'; // Mock machine local IP | ||
logger.log(`Testing with IP: ${ip}`); | ||
const executionContext = createMockExecutionContext(ip); | ||
const callHandler = { | ||
handle: jest.fn().mockReturnValue(of({})), | ||
}; | ||
|
||
await expect( | ||
interceptor.intercept(executionContext, callHandler), | ||
).resolves.toBeInstanceOf(Observable); | ||
}); | ||
|
||
it('should allow a request from a fake spoofed IP', async () => { | ||
const ip = '1607:e054:4d04:8f9c:6efa:761f:b964:57b6'; // Mock spoofed IPv6 address | ||
logger.log(`Testing with IP: ${ip}`); | ||
const executionContext = createMockExecutionContext(ip); | ||
const callHandler = { | ||
handle: jest.fn().mockReturnValue(of({})), | ||
}; | ||
|
||
await expect( | ||
interceptor.intercept(executionContext, callHandler), | ||
).resolves.toBeInstanceOf(Observable); | ||
}); | ||
|
||
it('should allow IPv6 addresses outside India', async () => { | ||
const ip = '2607:f8b0:4005:080a:0000:0000:0000:200e'; // Mock IPv6 address from outside India | ||
logger.log(`Testing with IP: ${ip}`); | ||
const executionContext = createMockExecutionContext(ip); | ||
const callHandler = { | ||
handle: jest.fn().mockReturnValue(of({})), | ||
}; | ||
|
||
await expect( | ||
interceptor.intercept(executionContext, callHandler), | ||
).resolves.toBeInstanceOf(Observable); | ||
}); | ||
|
||
it('should allow IPv4 addresses outside India', async () => { | ||
const ip = '8.8.8.8'; // Mock IPv4 address from outside India | ||
logger.log(`Testing with IP: ${ip}`); | ||
const executionContext = createMockExecutionContext(ip); | ||
const callHandler = { | ||
handle: jest.fn().mockReturnValue(of({})), | ||
}; | ||
|
||
await expect( | ||
interceptor.intercept(executionContext, callHandler), | ||
).resolves.toBeInstanceOf(Observable); | ||
}); | ||
|
||
it('should block IPv6 addresses outside India', async () => { | ||
const ip = '2607:f8b0:4005:080a:0000:0000:0000:200e'; // Mock IPv6 address from outside India | ||
logger.log(`Testing with IP: ${ip}`); | ||
const executionContext = createMockExecutionContext(ip); | ||
const callHandler = { | ||
handle: jest.fn().mockReturnValue(of({})), | ||
}; | ||
|
||
try { | ||
await interceptor.intercept(executionContext, callHandler); | ||
} catch (error) { | ||
console.error('Interceptor failed:', error); | ||
} | ||
|
||
expect(callHandler.handle).toHaveBeenCalled(); | ||
}); | ||
|
||
// Helper function to create a mock ExecutionContext | ||
function createMockExecutionContext(ip: string): ExecutionContext { | ||
return { | ||
switchToHttp: () => ({ | ||
getRequest: () => ({ | ||
headers: { 'x-forwarded-for': ip }, | ||
}), | ||
}), | ||
} as ExecutionContext; | ||
} | ||
}); |