2020-04-14 19:53:41 +00:00
< ? php
2022-08-01 10:22:04 +00:00
namespace Tests\Unit\Network\Validators ;
2020-04-14 19:53:41 +00:00
2025-04-14 11:56:42 +00:00
use Appwrite\Network\Platform ;
2020-06-11 19:36:10 +00:00
use Appwrite\Network\Validator\Origin ;
2020-04-14 19:53:41 +00:00
use PHPUnit\Framework\TestCase ;
2022-12-14 15:42:25 +00:00
use Utopia\Database\Helpers\ID ;
2020-04-14 19:53:41 +00:00
class OriginTest extends TestCase
{
2022-08-01 10:22:04 +00:00
public function testValues () : void
2020-04-14 19:53:41 +00:00
{
$validator = new Origin ([
[
2022-08-14 10:33:36 +00:00
'$collection' => ID :: custom ( 'platforms' ),
2020-04-14 19:53:41 +00:00
'name' => 'Production' ,
2025-04-14 11:56:42 +00:00
'type' => Platform :: TYPE_WEB ,
2020-04-14 19:53:41 +00:00
'hostname' => 'appwrite.io' ,
],
[
2022-08-14 10:33:36 +00:00
'$collection' => ID :: custom ( 'platforms' ),
2020-04-14 19:53:41 +00:00
'name' => 'Development' ,
2025-04-14 11:56:42 +00:00
'type' => Platform :: TYPE_WEB ,
2020-04-14 19:53:41 +00:00
'hostname' => 'appwrite.test' ,
],
[
2022-08-14 10:33:36 +00:00
'$collection' => ID :: custom ( 'platforms' ),
2020-04-14 19:53:41 +00:00
'name' => 'Localhost' ,
2025-04-14 11:56:42 +00:00
'type' => Platform :: TYPE_WEB ,
2020-04-14 19:53:41 +00:00
'hostname' => 'localhost' ,
],
2023-01-11 23:38:09 +00:00
[
'$collection' => ID :: custom ( 'platforms' ),
'name' => 'Flutter' ,
2025-04-14 11:56:42 +00:00
'type' => Platform :: TYPE_FLUTTER_WEB ,
2023-01-11 23:38:09 +00:00
'hostname' => 'appwrite.flutter' ,
],
2025-04-14 11:56:42 +00:00
[
'$collection' => ID :: custom ( 'platforms' ),
'name' => 'Expo' ,
'type' => Platform :: TYPE_SCHEME ,
'key' => 'exp' ,
],
[
'$collection' => ID :: custom ( 'platforms' ),
'name' => 'Appwrite Callback' ,
'type' => Platform :: TYPE_SCHEME ,
'key' => 'appwrite-callback-123' ,
],
2020-04-14 19:53:41 +00:00
]);
2025-04-14 12:50:48 +00:00
$this -> assertEquals ( false , $validator -> isValid ( '' ));
$this -> assertEquals ( false , $validator -> isValid ( '/' ));
2025-04-14 11:56:42 +00:00
$this -> assertEquals ( true , $validator -> isValid ( 'https://localhost' ));
$this -> assertEquals ( true , $validator -> isValid ( 'http://localhost' ));
$this -> assertEquals ( true , $validator -> isValid ( 'http://localhost:80' ));
$this -> assertEquals ( true , $validator -> isValid ( 'https://appwrite.io' ));
$this -> assertEquals ( true , $validator -> isValid ( 'http://appwrite.io' ));
$this -> assertEquals ( true , $validator -> isValid ( 'http://appwrite.io:80' ));
$this -> assertEquals ( true , $validator -> isValid ( 'https://appwrite.test' ));
$this -> assertEquals ( true , $validator -> isValid ( 'http://appwrite.test' ));
$this -> assertEquals ( true , $validator -> isValid ( 'http://appwrite.test:80' ));
2020-04-14 19:53:41 +00:00
2025-04-14 11:56:42 +00:00
$this -> assertEquals ( true , $validator -> isValid ( 'https://appwrite.flutter' ));
$this -> assertEquals ( true , $validator -> isValid ( 'http://appwrite.flutter' ));
$this -> assertEquals ( true , $validator -> isValid ( 'http://appwrite.flutter:80' ));
2020-04-14 19:53:41 +00:00
2025-04-14 11:56:42 +00:00
$this -> assertEquals ( false , $validator -> isValid ( 'https://example.com' ));
$this -> assertEquals ( false , $validator -> isValid ( 'http://example.com' ));
$this -> assertEquals ( false , $validator -> isValid ( 'http://example.com:80' ));
2020-04-14 19:53:41 +00:00
2025-04-14 11:56:42 +00:00
$this -> assertEquals ( true , $validator -> isValid ( 'exp://' ));
$this -> assertEquals ( true , $validator -> isValid ( 'exp:///' ));
$this -> assertEquals ( true , $validator -> isValid ( 'exp://index' ));
2023-01-11 23:38:09 +00:00
2025-04-14 11:56:42 +00:00
$this -> assertEquals ( true , $validator -> isValid ( 'appwrite-callback-123://' ));
$this -> assertEquals ( false , $validator -> isValid ( 'appwrite-callback-456://' ));
2020-04-14 19:53:41 +00:00
2025-04-14 11:56:42 +00:00
$this -> assertEquals ( false , $validator -> isValid ( 'appwrite-ios://com.company.appname' ));
$this -> assertEquals ( 'Invalid Origin. Register your new client (com.company.appname) as a new iOS platform on your project console dashboard' , $validator -> getDescription ());
2022-05-23 14:54:50 +00:00
2025-04-14 11:56:42 +00:00
$this -> assertEquals ( false , $validator -> isValid ( 'appwrite-android://com.company.appname' ));
$this -> assertEquals ( 'Invalid Origin. Register your new client (com.company.appname) as a new Android platform on your project console dashboard' , $validator -> getDescription ());
2022-05-23 14:54:50 +00:00
2025-04-14 11:56:42 +00:00
$this -> assertEquals ( false , $validator -> isValid ( 'appwrite-macos://com.company.appname' ));
$this -> assertEquals ( 'Invalid Origin. Register your new client (com.company.appname) as a new macOS platform on your project console dashboard' , $validator -> getDescription ());
2022-05-23 14:54:50 +00:00
2025-04-14 11:56:42 +00:00
$this -> assertEquals ( false , $validator -> isValid ( 'appwrite-linux://com.company.appname' ));
$this -> assertEquals ( 'Invalid Origin. Register your new client (com.company.appname) as a new Linux platform on your project console dashboard' , $validator -> getDescription ());
2020-04-14 19:53:41 +00:00
2025-04-14 11:56:42 +00:00
$this -> assertEquals ( false , $validator -> isValid ( 'appwrite-windows://com.company.appname' ));
$this -> assertEquals ( 'Invalid Origin. Register your new client (com.company.appname) as a new Windows platform on your project console dashboard' , $validator -> getDescription ());
2020-04-14 19:53:41 +00:00
}
2022-05-23 14:54:50 +00:00
}