This commit is contained in:
Saeed Vaziry
2024-03-24 09:56:34 +01:00
committed by GitHub
parent 884f18db63
commit 4d051330d6
1055 changed files with 14493 additions and 20278 deletions

View File

@ -1,78 +0,0 @@
<?php
namespace App\Support\SocialiteProviders;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Arr;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\User;
class DropboxProvider extends AbstractProvider implements ProviderInterface
{
/**
* Unique Provider Identifier.
*/
public const IDENTIFIER = 'DROPBOX';
/**
* {@inheritdoc}
*/
protected $scopeSeparator = ' ';
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state): string
{
return $this->buildAuthUrlFromBase('https://www.dropbox.com/oauth2/authorize', $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl(): string
{
return 'https://api.dropboxapi.com/oauth2/token';
}
/**
* {@inheritdoc}
*/
protected function getTokenFields($code): array
{
return array_merge(parent::getTokenFields($code), [
'grant_type' => 'authorization_code',
]);
}
/**
* {@inheritdoc}
*
* @throws GuzzleException
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->post('https://api.dropboxapi.com/2/users/get_current_account', [
'headers' => [
'Authorization' => 'Bearer '.$token,
],
]);
return json_decode($response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user): User
{
return (new User)->setRaw($user)->map([
'id' => $user['account_id'],
'nickname' => null,
'name' => $user['name']['display_name'],
'email' => $user['email'],
'avatar' => Arr::get($user, 'profile_photo_url'),
]);
}
}

View File

@ -2,53 +2,46 @@
namespace App\Support\Testing;
use App\Contracts\SSHCommand;
use App\Models\Server;
use App\Exceptions\SSHConnectionError;
use App\Helpers\SSH;
use Illuminate\Support\Traits\ReflectsClosures;
use PHPUnit\Framework\Assert as PHPUnit;
use PHPUnit\Framework\Assert;
class SSHFake
class SSHFake extends SSH
{
use ReflectsClosures;
protected array $commands;
protected array $commands = [];
protected string $output = '';
protected ?string $output;
public function init(Server $server, ?string $asUser = null): self
{
return $this;
}
protected bool $connectionWillFail = false;
public function outputShouldBe(string $output): self
public function __construct(?string $output = null)
{
$this->output = $output;
return $this;
}
public function assertExecuted(array|string $commands): void
public function connectionWillFail(): void
{
if (! $this->commands) {
PHPUnit::fail('No commands are executed');
}
if (! is_array($commands)) {
$commands = [$commands];
}
$allExecuted = true;
foreach ($commands as $command) {
if (! in_array($command, $commands)) {
$allExecuted = false;
}
}
if (! $allExecuted) {
PHPUnit::fail('The expected commands are not executed');
}
PHPUnit::assertTrue(true, $allExecuted);
$this->connectionWillFail = true;
}
public function exec(string|array|SSHCommand $commands, string $log = '', ?int $siteId = null): string
public function connect(bool $sftp = false): void
{
if ($this->connectionWillFail) {
throw new SSHConnectionError('Connection failed');
}
}
public function exec(string|array $commands, string $log = '', ?int $siteId = null): string
{
if ($log) {
$this->setLog($log, $siteId);
} else {
$this->log = null;
}
if (! is_array($commands)) {
$commands = [$commands];
}
@ -61,6 +54,54 @@ public function exec(string|array|SSHCommand $commands, string $log = '', ?int $
}
}
return 'fake output';
$output = $this->output ?? 'fake output';
$this->log?->write($output);
return $output;
}
public function upload(string $local, string $remote): void
{
$this->log = null;
}
public function assertExecuted(array|string $commands): void
{
if (! $this->commands) {
Assert::fail('No commands are executed');
}
if (! is_array($commands)) {
$commands = [$commands];
}
$allExecuted = true;
foreach ($commands as $command) {
if (! in_array($command, $commands)) {
$allExecuted = false;
}
}
if (! $allExecuted) {
Assert::fail('The expected commands are not executed. executed commands: '.implode(', ', $this->commands));
}
Assert::assertTrue(true, $allExecuted);
}
public function assertExecutedContains(string $command): void
{
if (! $this->commands) {
Assert::fail('No commands are executed');
}
$executed = false;
foreach ($this->commands as $executedCommand) {
if (str($executedCommand)->contains($command)) {
$executed = true;
break;
}
}
if (! $executed) {
Assert::fail(
'The expected command is not executed in the executed commands: '.implode(', ', $this->commands)
);
}
Assert::assertTrue(true, $executed);
}
}

View File

@ -1,36 +1,6 @@
<?php
use Illuminate\Contracts\Database\Query\Expression;
use Illuminate\Support\Str;
function random_color(): string
{
return '#'.str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
}
function get_hostname_from_repo($repo): string
{
$repo = Str::after($repo, '@');
return Str::before($repo, ':');
}
function generate_uid(): string
{
return hash('sha256', uniqid(rand(1111111111, 9999999999), true).Str::random(64).strtotime('now'));
}
// /**
// * @param $privateKeyPath
// * @return array|string|string[]
// */
// function generate_public_key($privateKeyPath)
// {
// $privateKey = openssl_pkey_get_private(file_get_contents($privateKeyPath));
// $publicKey = openssl_pkey_get_details($privateKey);
//
// return $publicKey['key'];
// }
use App\Helpers\HtmxResponse;
function generate_public_key($privateKeyPath, $publicKeyPath): void
{
@ -44,16 +14,6 @@ function generate_key_pair($path): void
chmod($path, 0400);
}
function make_bash_script($commands): string
{
$script = '';
foreach (preg_split("/((\r?\n)|(\r\n?))/", $commands) as $line) {
$script .= 'if ! '.$line."; then\n echo 'VITO_SSH_ERROR' && exit 1 \nfi"."\n";
}
return $script;
}
/**
* @throws Exception
*/
@ -65,9 +25,7 @@ function date_with_timezone($date, $timezone): string
return $dt->format('Y-m-d H:i:s');
}
function cast_to_json(array $json): Illuminate\Database\Query\Expression|Expression
function htmx(): HtmxResponse
{
$json = addslashes(json_encode($json));
return DB::raw("CAST('{$json}' AS JSON)");
return new HtmxResponse();
}