Bläddra i källkod

test: add more tools test

M1Screw 1 år sedan
förälder
incheckning
1a5662ce0c
4 ändrade filer med 44 tillägg och 62 borttagningar
  1. 16 16
      composer.lock
  2. 1 9
      src/Utils/Tools.php
  3. 1 37
      tests/App/Services/FilterTest.php
  4. 26 0
      tests/App/Utils/ToolsTest.php

+ 16 - 16
composer.lock

@@ -622,16 +622,16 @@
         },
         {
             "name": "aws/aws-sdk-php",
-            "version": "3.302.0",
+            "version": "3.303.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/aws/aws-sdk-php.git",
-                "reference": "cb343ed4fc5d86c0ddf8e948f0271052f183f937"
+                "reference": "34ace61fdffcea032826b0aac61ff3135b24b727"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/cb343ed4fc5d86c0ddf8e948f0271052f183f937",
-                "reference": "cb343ed4fc5d86c0ddf8e948f0271052f183f937",
+                "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/34ace61fdffcea032826b0aac61ff3135b24b727",
+                "reference": "34ace61fdffcea032826b0aac61ff3135b24b727",
                 "shasum": ""
             },
             "require": {
@@ -711,9 +711,9 @@
             "support": {
                 "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
                 "issues": "https://github.com/aws/aws-sdk-php/issues",
-                "source": "https://github.com/aws/aws-sdk-php/tree/3.302.0"
+                "source": "https://github.com/aws/aws-sdk-php/tree/3.303.0"
             },
-            "time": "2024-03-29T18:07:04+00:00"
+            "time": "2024-04-01T18:48:47+00:00"
         },
         {
             "name": "bacon/bacon-qr-code",
@@ -2762,16 +2762,16 @@
         },
         {
             "name": "nesbot/carbon",
-            "version": "3.2.2",
+            "version": "3.2.3",
             "source": {
                 "type": "git",
                 "url": "https://github.com/briannesbitt/Carbon.git",
-                "reference": "2d69b6de67e2a3c0652d0c9dfcfda8b4563c4cee"
+                "reference": "4d599a6e2351d6b6bf21737accdfe1a4ce3fdbb1"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/2d69b6de67e2a3c0652d0c9dfcfda8b4563c4cee",
-                "reference": "2d69b6de67e2a3c0652d0c9dfcfda8b4563c4cee",
+                "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/4d599a6e2351d6b6bf21737accdfe1a4ce3fdbb1",
+                "reference": "4d599a6e2351d6b6bf21737accdfe1a4ce3fdbb1",
                 "shasum": ""
             },
             "require": {
@@ -2864,7 +2864,7 @@
                     "type": "tidelift"
                 }
             ],
-            "time": "2024-03-28T12:59:49+00:00"
+            "time": "2024-03-30T18:22:00+00:00"
         },
         {
             "name": "nikic/fast-route",
@@ -9414,16 +9414,16 @@
         },
         {
             "name": "squizlabs/php_codesniffer",
-            "version": "3.9.0",
+            "version": "3.9.1",
             "source": {
                 "type": "git",
                 "url": "https://github.com/PHPCSStandards/PHP_CodeSniffer.git",
-                "reference": "d63cee4890a8afaf86a22e51ad4d97c91dd4579b"
+                "reference": "267a4405fff1d9c847134db3a3c92f1ab7f77909"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/d63cee4890a8afaf86a22e51ad4d97c91dd4579b",
-                "reference": "d63cee4890a8afaf86a22e51ad4d97c91dd4579b",
+                "url": "https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/267a4405fff1d9c847134db3a3c92f1ab7f77909",
+                "reference": "267a4405fff1d9c847134db3a3c92f1ab7f77909",
                 "shasum": ""
             },
             "require": {
@@ -9490,7 +9490,7 @@
                     "type": "open_collective"
                 }
             ],
-            "time": "2024-02-16T15:06:51+00:00"
+            "time": "2024-03-31T21:03:09+00:00"
         },
         {
             "name": "symfony/cache",

+ 1 - 9
src/Utils/Tools.php

@@ -5,7 +5,6 @@ declare(strict_types=1);
 namespace App\Utils;
 
 use App\Models\Config;
-use App\Models\Link;
 use App\Models\User;
 use App\Services\GeoIP2;
 use GeoIp2\Exception\AddressNotFoundException;
@@ -169,14 +168,7 @@ final class Tools
 
     public static function genSubToken(): string
     {
-        $token = self::genRandomChar($_ENV['sub_token_len']);
-        $is_token_used = (new Link())->where('token', $token)->first();
-
-        if ($is_token_used === null) {
-            return $token;
-        }
-
-        return "couldn't alloc token";
+        return self::genRandomChar(max($_ENV['sub_token_len'], 8));
     }
 
     public static function genRandomChar(int $length = 8): string

+ 1 - 37
tests/App/Services/FilterTest.php

@@ -4,7 +4,6 @@ declare(strict_types=1);
 
 namespace App\Services;
 
-use App\Services\Filter;
 use PHPUnit\Framework\TestCase;
 
 final class FilterTest extends TestCase
@@ -12,55 +11,20 @@ final class FilterTest extends TestCase
     /**
      * @covers App\Services\Filter::checkEmailFilter
      */
-    public function testCheckEmailFilterWithValidEmailAndWhitelist(): void
+    public function testCheckEmailFilter(): void
     {
         $_ENV['mail_filter'] = 1;
         $_ENV['mail_filter_list'] = ['example.com'];
         $this->assertTrue(Filter::checkEmailFilter('[email protected]'));
-    }
-
-    /**
-     * @covers App\Services\Filter::checkEmailFilter
-     */
-    public function testCheckEmailFilterWithValidEmailAndBlacklist(): void
-    {
         $_ENV['mail_filter'] = 2;
         $_ENV['mail_filter_list'] = ['example.com'];
         $this->assertFalse(Filter::checkEmailFilter('[email protected]'));
-    }
-
-    /**
-     * @covers App\Services\Filter::checkEmailFilter
-     */
-    public function testCheckEmailFilterWithInvalidEmail(): void
-    {
         $this->assertFalse(Filter::checkEmailFilter('invalid_email'));
-    }
-
-    /**
-     * @covers App\Services\Filter::checkEmailFilter
-     */
-    public function testCheckEmailFilterWithNoMailFilter(): void
-    {
         $_ENV['mail_filter'] = 0;
         $this->assertTrue(Filter::checkEmailFilter('[email protected]'));
-    }
-
-    /**
-     * @covers App\Services\Filter::checkEmailFilter
-     */
-    public function testCheckEmailFilterWithValidEmailNotInWhitelist(): void
-    {
         $_ENV['mail_filter'] = 1;
         $_ENV['mail_filter_list'] = ['example.com'];
         $this->assertFalse(Filter::checkEmailFilter('[email protected]'));
-    }
-
-    /**
-     * @covers App\Services\Filter::checkEmailFilter
-     */
-    public function testCheckEmailFilterWithValidEmailNotInBlacklist(): void
-    {
         $_ENV['mail_filter'] = 2;
         $_ENV['mail_filter_list'] = ['example.com'];
         $this->assertTrue(Filter::checkEmailFilter('[email protected]'));

+ 26 - 0
tests/App/Utils/ToolsTest.php

@@ -7,6 +7,7 @@ namespace App\Utils;
 use MaxMind\Db\Reader\InvalidDatabaseException;
 use PHPUnit\Framework\TestCase;
 use function date_default_timezone_set;
+use function strlen;
 
 class ToolsTest extends TestCase
 {
@@ -102,15 +103,38 @@ class ToolsTest extends TestCase
         $this->assertEquals($traffic / $mb, $result);
     }
 
+    /**
+     * @covers App\Utils\Tools::genSubToken
+     */
+    public function testGenSubToken()
+    {
+        $_ENV['sub_token_len'] = 10;
+        $token = Tools::genSubToken();
+        $this->assertEquals(10, strlen($token));
+        $_ENV['sub_token_len'] = 0;
+        $token = Tools::genSubToken();
+        $this->assertEquals(8, strlen($token));
+        $_ENV['sub_token_len'] = -5;
+        $token = Tools::genSubToken();
+        $this->assertEquals(8, strlen($token));
+    }
+
     /**
      * @covers App\Utils\Tools::genRandomChar
      */
     public function testGenRandomChar()
     {
+        $randomString = Tools::genRandomChar();
+        $this->assertIsString($randomString);
+        $this->assertEquals(8, strlen($randomString));
         $length = 10;
         $randomString = Tools::genRandomChar($length);
         $this->assertIsString($randomString);
         $this->assertEquals($length, strlen($randomString));
+        $length = 1;
+        $randomString = Tools::genRandomChar($length);
+        $this->assertIsString($randomString);
+        $this->assertEquals(2, strlen($randomString));
     }
 
     /**
@@ -236,6 +260,7 @@ class ToolsTest extends TestCase
     {
         $this->assertTrue(Tools::isIPv4('192.168.0.1'));
         $this->assertFalse(Tools::isIPv4('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
+        $this->assertFalse(Tools::isIPv4('UwU'));
     }
 
     /**
@@ -245,6 +270,7 @@ class ToolsTest extends TestCase
     {
         $this->assertTrue(Tools::isIPv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
         $this->assertFalse(Tools::isIPv6('192.168.0.1'));
+        $this->assertFalse(Tools::isIPv6('hmm'));
     }
 
     /**