-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: add tests for existing functionality
- Loading branch information
Showing
14 changed files
with
978 additions
and
6 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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
Empty file.
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,116 @@ | ||
<?php | ||
|
||
namespace FoF\BanIPs\Tests\fixtures; | ||
|
||
use Carbon\Carbon; | ||
|
||
trait IPAddressesTrait | ||
{ | ||
protected static $IPv4Banned = [ | ||
'192.168.1.1', | ||
'192.168.1.100', | ||
'10.0.0.5', | ||
'172.16.0.5' | ||
]; | ||
|
||
protected static $IPv4NotBanned = [ | ||
'192.168.1.2', | ||
'192.168.1.101', | ||
'10.0.0.6', | ||
'172.16.0.6' | ||
]; | ||
|
||
protected static $IPv6Banned = [ | ||
'2001:0db8:85a3:0000:0000:8a2e:0370:7334', | ||
'fe80:0000:0000:0000:0204:61ff:fe9d:f156', | ||
'2001:0db8:0000:0042:0000:8a2e:0370:7334', | ||
'fe80:0000:0000:0000:0204:61ff:fe9d:f157' | ||
]; | ||
|
||
protected static $IPv6NotBanned = [ | ||
'2001:0db8:85a3:0000:0000:8a2e:0370:7335', | ||
'fe80:0000:0000:0000:0204:61ff:fe9d:f158', | ||
'2001:0db8:0000:0042:0000:8a2e:0370:7335', | ||
'fe80:0000:0000:0000:0204:61ff:fe9d:f159' | ||
]; | ||
|
||
public function getIPv4Banned(): array | ||
{ | ||
return self::$IPv4Banned; | ||
} | ||
|
||
public function getIPv4NotBanned(): array | ||
{ | ||
return self::$IPv4NotBanned; | ||
} | ||
|
||
public function getIPv6Banned(): array | ||
{ | ||
return self::$IPv6Banned; | ||
} | ||
|
||
public function getIPv6NotBanned(): array | ||
{ | ||
return self::$IPv6NotBanned; | ||
} | ||
|
||
public function getAllBanned(): array | ||
{ | ||
return array_merge(self::$IPv4Banned, self::$IPv6Banned); | ||
} | ||
|
||
public function getAllNotBanned(): array | ||
{ | ||
return array_merge(self::$IPv4NotBanned, self::$IPv6NotBanned); | ||
} | ||
|
||
public function getBannedIPsForDB(): array | ||
{ | ||
$bannedIPs = $this->getAllBanned(); | ||
$id = 1; | ||
$entries = []; | ||
|
||
foreach ($bannedIPs as $bannedIP) { | ||
$entries[] = [ | ||
'id' => $id, | ||
'creator_id' => 1, | ||
'address' => $bannedIP, | ||
'reason' => "Testing #{$id}", | ||
'user_id' => 3, | ||
'created_at' => Carbon::now() | ||
]; | ||
|
||
$id++; | ||
} | ||
|
||
return $entries; | ||
} | ||
|
||
public function getRandomBannedIPv4(): string | ||
{ | ||
$bannedIPs = $this->getIPv4Banned(); | ||
|
||
return $bannedIPs[array_rand($bannedIPs)]; | ||
} | ||
|
||
public function getRandomNotBannedIPv4(): string | ||
{ | ||
$notBannedIPs = $this->getIPv4NotBanned(); | ||
|
||
return $notBannedIPs[array_rand($notBannedIPs)]; | ||
} | ||
|
||
public function getRandomBannedIPv6(): string | ||
{ | ||
$bannedIPs = $this->getIPv6Banned(); | ||
|
||
return $bannedIPs[array_rand($bannedIPs)]; | ||
} | ||
|
||
public function getRandomNotBannedIPv6(): string | ||
{ | ||
$notBannedIPs = $this->getIPv6NotBanned(); | ||
|
||
return $notBannedIPs[array_rand($notBannedIPs)]; | ||
} | ||
} |
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,53 @@ | ||
<?php | ||
|
||
namespace FoF\BanIPs\Tests\fixtures; | ||
|
||
use Flarum\Testing\integration\BuildsHttpRequests; | ||
use Laminas\Diactoros\ServerRequest; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
trait IPRequestTrait | ||
{ | ||
use BuildsHttpRequests; | ||
|
||
/** | ||
* Build a HTTP request that can be passed through middleware. | ||
* | ||
* @param string $method | ||
* @param string $path | ||
* @param array $options | ||
* @return ServerRequestInterface | ||
*/ | ||
protected function enhancedRequest(string $method, string $path, array $options = []): ServerRequestInterface | ||
{ | ||
$serverParams = $options['serverParams'] ?? []; | ||
|
||
$request = new ServerRequest($serverParams, [], $path, $method); | ||
|
||
// Do we want a JSON request body? | ||
if (isset($options['json'])) { | ||
$request = $this->requestWithJsonBody( | ||
$request, | ||
$options['json'] | ||
); | ||
} | ||
|
||
// Authenticate as a given user | ||
if (isset($options['authenticatedAs'])) { | ||
$request = $this->requestAsUser( | ||
$request, | ||
$options['authenticatedAs'] | ||
); | ||
} | ||
|
||
// Let's copy the cookies from a previous response | ||
if (isset($options['cookiesFrom'])) { | ||
$request = $this->requestWithCookiesFrom( | ||
$request, | ||
$options['cookiesFrom'] | ||
); | ||
} | ||
|
||
return $request; | ||
} | ||
} |
Oops, something went wrong.