Skip to content

Commit

Permalink
wip: add tests for existing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Sep 21, 2023
1 parent a54b8ce commit 1e27614
Show file tree
Hide file tree
Showing 14 changed files with 978 additions and 6 deletions.
31 changes: 30 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,39 @@
"flarum/flags",
"flarum/tags",
"flarum/approval"
]
]
},
"flagrow": {
"discuss": "https://discuss.flarum.org/d/20949"
},
"flarum-cli": {
"modules": {
"backendTesting": true
}
}
},
"autoload-dev": {
"psr-4": {
"FoF\\BanIPs\\Tests\\": "tests/"
}
},
"scripts": {
"test": [
"@test:unit",
"@test:integration"
],
"test:unit": "phpunit -c tests/phpunit.unit.xml",
"test:integration": "phpunit -c tests/phpunit.integration.xml",
"test:setup": "@php tests/integration/setup.php"
},
"scripts-descriptions": {
"test": "Runs all tests.",
"test:unit": "Runs all unit tests.",
"test:integration": "Runs all integration tests.",
"test:setup": "Sets up a database for use with integration tests. Execute this only once."
},
"require-dev": {
"flarum/testing": "^1.0.0",
"symfony/var-dumper": "^6.3"
}
}
5 changes: 4 additions & 1 deletion src/Commands/EditBannedIPHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function handle(EditBannedIP $command)

$attributes = Arr::get($data, 'attributes', []);

/** @var BannedIP $bannedIP */
$bannedIP = BannedIP::find($command->bannedId);

$actor->assertCan('banIP');
Expand All @@ -80,7 +81,9 @@ public function handle(EditBannedIP $command)

$this->validator->assertValid($bannedIP->getDirty());

$bannedIP->save();
if ($bannedIP->isDirty()) {
$bannedIP->save();
}

return $bannedIP;
}
Expand Down
4 changes: 1 addition & 3 deletions src/Listeners/RemoveAccessToBannedUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ public function handle(IPWasBanned $event)
$users = $this->bannedIPs->findUsers($bannedIP->address);

foreach ($users as $user) {
/*
* @var User $user
*/
/** @var User $user */
$user->accessTokens()->delete();
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Middleware/RegisterMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Flarum\Foundation\ErrorHandling\Registry;
use Flarum\Foundation\ValidationException;
use Flarum\Http\RequestUtil;
use Flarum\Locale\Translator;
use Flarum\User\UserRepository;
use FoF\BanIPs\Repositories\BannedIPRepository;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -78,7 +79,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
->format(
resolve(Registry::class)
->handle(new ValidationException([
'ip' => resolve('translator')->trans('fof-ban-ips.error.banned_ip_message'),
'ip' => 'Authorization failed.',
])),
$request
);
Expand Down
1 change: 1 addition & 0 deletions tests/.phpunit.result.cache

Large diffs are not rendered by default.

Empty file added tests/fixtures/.gitkeep
Empty file.
116 changes: 116 additions & 0 deletions tests/fixtures/IPAddressesTrait.php
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)];
}
}
53 changes: 53 additions & 0 deletions tests/fixtures/IPRequestTrait.php
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;
}
}
Loading

0 comments on commit 1e27614

Please sign in to comment.