Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleCIBot committed Sep 21, 2023
1 parent 767e15f commit e1f54ec
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 97 deletions.
6 changes: 3 additions & 3 deletions src/BannedIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class BannedIP extends AbstractModel
protected $dates = ['created_at', 'deleted_at'];

/**
* @param $creatorId
* @param $userId
* @param $address
* @param $creatorId
* @param $userId
* @param $address
* @param null $reason
*
* @return BannedIP
Expand Down
1 change: 0 additions & 1 deletion src/Middleware/RegisterMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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
27 changes: 18 additions & 9 deletions tests/fixtures/IPAddressesTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of fof/ban-ips.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\BanIPs\Tests\fixtures;

use Carbon\Carbon;
Expand All @@ -10,28 +19,28 @@ trait IPAddressesTrait
'192.168.1.1',
'192.168.1.100',
'10.0.0.5',
'172.16.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'
'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'
'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'
'fe80:0000:0000:0000:0204:61ff:fe9d:f159',
];

public function getIPv4Banned(): array
Expand Down Expand Up @@ -72,12 +81,12 @@ public function getBannedIPsForDB(): array

foreach ($bannedIPs as $bannedIP) {
$entries[] = [
'id' => $id,
'id' => $id,
'creator_id' => 1,
'address' => $bannedIP,
'reason' => "Testing #{$id}",
'user_id' => 3,
'created_at' => Carbon::now()
'address' => $bannedIP,
'reason' => "Testing #{$id}",
'user_id' => 3,
'created_at' => Carbon::now(),
];

$id++;
Expand Down
14 changes: 12 additions & 2 deletions tests/fixtures/IPRequestTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of fof/ban-ips.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\BanIPs\Tests\fixtures;

use Flarum\Testing\integration\BuildsHttpRequests;
Expand All @@ -9,13 +18,14 @@
trait IPRequestTrait
{
use BuildsHttpRequests;

/**
* Build a HTTP request that can be passed through middleware.
*
* @param string $method
* @param string $path
* @param array $options
* @param array $options
*
* @return ServerRequestInterface
*/
protected function enhancedRequest(string $method, string $path, array $options = []): ServerRequestInterface
Expand Down
43 changes: 26 additions & 17 deletions tests/integration/BannedIPRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<?php

/*
* This file is part of fof/ban-ips.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\BanIPs\Tests\integration;

use Carbon\Carbon;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
use FoF\BanIPs\Repositories\BannedIPRepository;
use Flarum\User\User;
use FoF\BanIPs\BannedIP;
use FoF\BanIPs\Repositories\BannedIPRepository;
use FoF\BanIPs\Tests\fixtures\IPAddressesTrait;

class BannedIPRepositoryTest extends TestCase
Expand All @@ -25,11 +34,11 @@ protected function setUp(): void

$this->prepareDatabase([
'banned_ips' => $bannedIPs,
'users' => [
'users' => [
$this->normalUser(),
['id' => 3, 'username' => 'ipBanned', 'password' => '$2y$10$LO59tiT7uggl6Oe23o/O6.utnF6ipngYjvMvaxo1TciKqBttDNKim', 'email' => '[email protected]', 'is_email_confirmed' => 1, 'last_seen_at' => Carbon::now()->subSecond(),],
['id' => 4, 'username' => 'doubleIPUser', 'password' => '$2y$10$LO59tiT7uggl6Oe23o/O6.utnF6ipngYjvMvaxo1TciKqBttDNKim', 'email' => '[email protected]', 'is_email_confirmed' => 1, 'last_seen_at' => Carbon::now()->subSecond(),],
['id' => 5, 'username' => 'noPostsUser', 'password' => '$2y$10$LO59tiT7uggl6Oe23o/O6.utnF6ipngYjvMvaxo1TciKqBttDNKim', 'email' => '[email protected]', 'is_email_confirmed' => 1, 'last_seen_at' => Carbon::now()->subSecond(),],
['id' => 3, 'username' => 'ipBanned', 'password' => '$2y$10$LO59tiT7uggl6Oe23o/O6.utnF6ipngYjvMvaxo1TciKqBttDNKim', 'email' => '[email protected]', 'is_email_confirmed' => 1, 'last_seen_at' => Carbon::now()->subSecond()],
['id' => 4, 'username' => 'doubleIPUser', 'password' => '$2y$10$LO59tiT7uggl6Oe23o/O6.utnF6ipngYjvMvaxo1TciKqBttDNKim', 'email' => '[email protected]', 'is_email_confirmed' => 1, 'last_seen_at' => Carbon::now()->subSecond()],
['id' => 5, 'username' => 'noPostsUser', 'password' => '$2y$10$LO59tiT7uggl6Oe23o/O6.utnF6ipngYjvMvaxo1TciKqBttDNKim', 'email' => '[email protected]', 'is_email_confirmed' => 1, 'last_seen_at' => Carbon::now()->subSecond()],
],
'discussions' => [
['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'last_posted_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1],
Expand Down Expand Up @@ -65,11 +74,11 @@ public function it_checks_if_user_is_banned()
{
$user = User::find(2);
$isBanned = $this->repository->isUserBanned($user);
$this->assertFalse($isBanned, "User should not be banned");
$this->assertFalse($isBanned, 'User should not be banned');

$bannedUser = User::find(3);
$isBanned = $this->repository->isUserBanned($bannedUser);
$this->assertTrue($isBanned, "User should be banned");
$this->assertTrue($isBanned, 'User should be banned');
}

/**
Expand All @@ -79,8 +88,8 @@ public function it_retrieves_user_ips()
{
$user = User::find(3);
$ips = $this->repository->getUserIPs($user);
$this->assertContains($this->getIPv4Banned()[1], $ips, "The IPs should contain the banned IPv4");
$this->assertContains($this->getIPv6Banned()[1], $ips, "The IPs should contain the banned IPv6");
$this->assertContains($this->getIPv4Banned()[1], $ips, 'The IPs should contain the banned IPv4');
$this->assertContains($this->getIPv6Banned()[1], $ips, 'The IPs should contain the banned IPv6');
}

/**
Expand Down Expand Up @@ -171,8 +180,8 @@ public function it_handles_multiple_posts_with_same_ip_correctly()
{
$user = User::find(4); // doubleIPUser
$ips = $this->repository->getUserIPs($user);
$this->assertCount(1, $ips, "There should be only one unique IP for the user");
$this->assertContains($this->getIPv4NotBanned()[0], $ips, "The IPs should contain the IPv4 used twice");
$this->assertCount(1, $ips, 'There should be only one unique IP for the user');
$this->assertContains($this->getIPv4NotBanned()[0], $ips, 'The IPs should contain the IPv4 used twice');
}

/**
Expand All @@ -193,7 +202,7 @@ public function it_does_not_flag_non_banned_user_as_banned()
{
$user = User::find(2); // normal user
$isBanned = $this->repository->isUserBanned($user);
$this->assertFalse($isBanned, "User should not be banned");
$this->assertFalse($isBanned, 'User should not be banned');
}

/**
Expand All @@ -203,7 +212,7 @@ public function it_handles_users_with_no_posts()
{
$userWithoutPosts = User::find(5);
$ips = $this->repository->getUserIPs($userWithoutPosts);
$this->assertCount(0, $ips, "A user with no posts should have no IP records");
$this->assertCount(0, $ips, 'A user with no posts should have no IP records');
}

/**
Expand All @@ -222,7 +231,7 @@ public function it_identifies_multiple_banned_ips_for_user()
*/
public function it_returns_null_for_invalid_ip_format()
{
$bannedIP = $this->repository->findByIPAddress("InvalidIP");
$bannedIP = $this->repository->findByIPAddress('InvalidIP');
$this->assertNull($bannedIP);
}

Expand All @@ -234,7 +243,7 @@ public function it_handles_large_list_of_ips_efficiently()
// Generate a large list of IPs
$largeListOfIps = range(1, 30000); // Adjust as needed, this is just for demonstration
$users = $this->repository->findUsers($largeListOfIps);
$this->assertNotNull($users, "The system should handle large IP lists without crashing");
$this->assertNotNull($users, 'The system should handle large IP lists without crashing');
}

/**
Expand All @@ -244,7 +253,7 @@ public function it_does_not_ban_admins_based_on_ip()
{
$admin = User::find(1);
$isBanned = $this->repository->isUserBanned($admin);
$this->assertFalse($isBanned, "Admins should not be banned based on IP");
$this->assertFalse($isBanned, 'Admins should not be banned based on IP');
}

/**
Expand All @@ -254,6 +263,6 @@ public function it_does_ban_normal_user_based_on_ip()
{
$user = User::find(3);
$isBanned = $this->repository->isUserBanned($user);
$this->assertTrue($isBanned, "Normal user with a banned IP should be banned");
$this->assertTrue($isBanned, 'Normal user with a banned IP should be banned');
}
}
Loading

0 comments on commit e1f54ec

Please sign in to comment.