Skip to content

Commit

Permalink
Updates endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
LoganTFox committed Oct 8, 2024
1 parent 5c1b688 commit ed6d365
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/Services/IPAddressDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ public function __construct(

public function handle(): void
{
$endpoint = config('security-notifications.ip_api_key')
? 'https://pro.ip-api.com/json/'
: 'https://ip-api.com/json/';

$ipLocationData = Http::retry(3)
->withQueryParameters(['api_key' => config('security-notifications.ip_api_key')])
->get('http://ip-api.com/json/'.$this->ipAddress)
->withQueryParameters(['key' => config('security-notifications.ip_api_key')])
->get($endpoint.$this->ipAddress)
?->json();

throw_if(is_null($ipLocationData), new Exception('Failed to get IP location data for: '.$this->ipAddress));
Expand Down
44 changes: 38 additions & 6 deletions tests/Unit/IPAddressHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
Http::shouldReceive('withQueryParameters')
->andReturnSelf();
Http::shouldReceive('get')
->with('http://ip-api.com/json/127.0.0.1')
->with('https://ip-api.com/json/127.0.0.1')
->once()
->andReturnSelf();
Http::shouldReceive('json')
Expand Down Expand Up @@ -67,7 +67,7 @@
Http::shouldReceive('withQueryParameters')
->andReturnSelf();
Http::shouldReceive('get')
->with('http://ip-api.com/json/'.$login->ip_address)
->with('https://ip-api.com/json/'.$login->ip_address)
->once()
->andReturnSelf();
Http::shouldReceive('json')
Expand Down Expand Up @@ -108,7 +108,7 @@
Http::shouldReceive('withQueryParameters')
->andReturnSelf();
Http::shouldReceive('get')
->with('http://ip-api.com/json/127.0.0.1')
->with('https://ip-api.com/json/127.0.0.1')
->once()
->andReturnSelf();
Http::shouldReceive('json')
Expand Down Expand Up @@ -149,7 +149,7 @@
Http::shouldReceive('withQueryParameters')
->andReturnSelf();
Http::shouldReceive('get')
->with('http://ip-api.com/json/127.0.0.1')
->with('https://ip-api.com/json/127.0.0.1')
->once()
->andReturnSelf();
Http::shouldReceive('json')
Expand Down Expand Up @@ -209,7 +209,7 @@
Http::shouldReceive('withQueryParameters')
->andReturnSelf();
Http::shouldReceive('get')
->with('http://ip-api.com/json/128.0.0.1')
->with('https://ip-api.com/json/128.0.0.1')
->once()
->andReturnSelf();
Http::shouldReceive('json')
Expand Down Expand Up @@ -269,4 +269,36 @@
'userId' => $login->user_id,
'userType' => $login->user_type,
]);
})->expectException(Exception::class, 'Failed to get IP location data for: 1234567890');
})->expectException(Exception::class, 'Failed to get IP location data for: 1234567890');

it('uses pro endpoint if api key is set', function () {
Bus::fake();

Config::set('security-notifications.ip_api_key', 'test-key');

$user = User::factory()->create();

Http::shouldReceive('retry')
->andReturnSelf();
Http::shouldReceive('withQueryParameters')
->with(['key' => 'test-key'])
->andReturnSelf();
Http::shouldReceive('get')
->with('https://pro.ip-api.com/json/127.0.0.1')
->once()
->andReturnSelf();
Http::shouldReceive('json')
->once()
->andReturn([
'query' => '127.0.0.1',
'city' => 'Minneapolis',
'region' => 'MN',
'countryCode' => 'US',
]);

IPAddress::process([
'ipAddress' => '127.0.0.1',
'userId' => $user->getKey(),
'userType' => $user->getMorphClass(),
]);
});

0 comments on commit ed6d365

Please sign in to comment.