fix notification chanels and more tests (#108)

* fix notification chanels and more tests

* fix code style
This commit is contained in:
Saeed Vaziry
2024-02-16 21:10:17 +01:00
committed by GitHub
parent b75df8e1c5
commit f70963d6bb
103 changed files with 484 additions and 112 deletions

View File

@ -0,0 +1,30 @@
<?php
namespace Tests\Unit\SSHCommands\System;
use App\SSHCommands\System\CreateUserCommand;
use Tests\TestCase;
class CreateUserCommandTest extends TestCase
{
public function test_generate_command()
{
$command = new CreateUserCommand('vito', 'password', 'key');
$expected = <<<'EOD'
export DEBIAN_FRONTEND=noninteractive
echo "key" | sudo tee -a /home/root/.ssh/authorized_keys
sudo useradd -p $(openssl passwd -1 password) vito
sudo usermod -aG sudo vito
echo "vito ALL=(ALL) NOPASSWD:ALL" | sudo tee -a /etc/sudoers
sudo mkdir /home/vito
sudo mkdir /home/vito/.ssh
echo "key" | sudo tee -a /home/vito/.ssh/authorized_keys
sudo chown -R vito:vito /home/vito
sudo chsh -s /bin/bash vito
sudo su - vito -c "ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa" <<< y
EOD;
$this->assertStringContainsString($expected, $command->content());
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Tests\Unit\SSHCommands\System;
use App\SSHCommands\System\DeleteSshKeyCommand;
use Tests\TestCase;
class DeleteSshKeyCommandTest extends TestCase
{
public function test_generate_command()
{
$command = new DeleteSshKeyCommand('key');
$expected = <<<'EOD'
sudo sed -i 's/key//g' ~/.ssh/authorized_keys
EOD;
$this->assertStringContainsString($expected, $command->content());
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Tests\Unit\SSHCommands\System;
use App\SSHCommands\System\DeploySshKeyCommand;
use Tests\TestCase;
class DeploySshKeyCommandTest extends TestCase
{
public function test_generate_command()
{
$command = new DeploySshKeyCommand('key');
$expected = <<<'EOD'
if ! echo 'key' | sudo tee -a ~/.ssh/authorized_keys; then
echo 'VITO_SSH_ERROR' && exit 1
fi
EOD;
$this->assertStringContainsString($expected, $command->content());
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Tests\Unit\SSHCommands\System;
use App\SSHCommands\System\EditFileCommand;
use Tests\TestCase;
class EditFileCommandTest extends TestCase
{
public function test_generate_command()
{
$command = new EditFileCommand('/path/to/file', 'content');
$expected = <<<'EOD'
if ! echo "content" | tee /path/to/file; then
echo 'VITO_SSH_ERROR' && exit 1
fi
EOD;
$this->assertStringContainsString($expected, $command->content());
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Tests\Unit\SSHCommands\System;
use App\SSHCommands\System\GenerateSshKeyCommand;
use Tests\TestCase;
class GenerateSshKeyCommandTest extends TestCase
{
public function test_generate_command()
{
$command = new GenerateSshKeyCommand('key');
$expected = <<<'EOD'
ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/key
EOD;
$this->assertStringContainsString($expected, $command->content());
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Tests\Unit\SSHCommands\System;
use App\SSHCommands\System\GetPublicKeyCommand;
use Tests\TestCase;
class GetPublicKeyCommandTest extends TestCase
{
public function test_generate_command()
{
$command = new GetPublicKeyCommand();
$expected = <<<'EOD'
cat ~/.ssh/id_rsa.pub
EOD;
$this->assertStringContainsString($expected, $command->content());
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Tests\Unit\SSHCommands\System;
use App\SSHCommands\System\ReadFileCommand;
use Tests\TestCase;
class ReadFileCommandTest extends TestCase
{
public function test_generate_command()
{
$command = new ReadFileCommand('/path');
$expected = <<<'EOD'
cat /path;
EOD;
$this->assertStringContainsString($expected, $command->content());
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Tests\Unit\SSHCommands\System;
use App\SSHCommands\System\ReadSshKeyCommand;
use Tests\TestCase;
class ReadSshKeyCommandTest extends TestCase
{
public function test_generate_command()
{
$command = new ReadSshKeyCommand('name');
$expected = <<<'EOD'
cat ~/.ssh/name.pub
EOD;
$this->assertStringContainsString($expected, $command->content());
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Tests\Unit\SSHCommands\System;
use App\SSHCommands\System\RebootCommand;
use Tests\TestCase;
class RebootCommandTest extends TestCase
{
public function test_generate_command()
{
$command = new RebootCommand();
$expected = <<<'EOD'
echo "Rebooting..."
sudo reboot
EOD;
$this->assertStringContainsString($expected, $command->content());
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Tests\Unit\SSHCommands\System;
use App\SSHCommands\System\RunScriptCommand;
use Tests\TestCase;
class RunScriptCommandTest extends TestCase
{
public function test_generate_command()
{
$command = new RunScriptCommand('/path', 'script');
$expected = <<<'EOD'
if ! cd /path; then
echo 'VITO_SSH_ERROR' && exit 1
fi
script
EOD;
$this->assertStringContainsString($expected, $command->content());
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace Tests\Unit\SSHCommands\System;
use App\SSHCommands\System\UpgradeCommand;
use Tests\TestCase;
class UpgradeCommandTest extends TestCase
{
public function test_generate_command()
{
$command = new UpgradeCommand();
$expected = <<<'EOD'
sudo DEBIAN_FRONTEND=noninteractive apt clean
sudo DEBIAN_FRONTEND=noninteractive apt update
sudo DEBIAN_FRONTEND=noninteractive apt upgrade -y
sudo DEBIAN_FRONTEND=noninteractive apt autoremove -y
EOD;
$this->assertStringContainsString($expected, $command->content());
}
}