ajout app
This commit is contained in:
26
SNIPE-IT/tests/Support/AssertsAgainstSlackNotifications.php
Normal file
26
SNIPE-IT/tests/Support/AssertsAgainstSlackNotifications.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Support;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Notifications\AnonymousNotifiable;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
|
||||
trait AssertsAgainstSlackNotifications
|
||||
{
|
||||
public function assertSlackNotificationSent(string $notificationClass)
|
||||
{
|
||||
Notification::assertSentTo(
|
||||
new AnonymousNotifiable,
|
||||
$notificationClass,
|
||||
function ($notification, $channels, $notifiable) {
|
||||
return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function assertNoSlackNotificationSent(string $notificationClass)
|
||||
{
|
||||
Notification::assertNotSentTo(new AnonymousNotifiable, $notificationClass);
|
||||
}
|
||||
}
|
91
SNIPE-IT/tests/Support/CustomTestMacros.php
Normal file
91
SNIPE-IT/tests/Support/CustomTestMacros.php
Normal file
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Support;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Testing\TestResponse;
|
||||
use PHPUnit\Framework\Assert;
|
||||
use RuntimeException;
|
||||
|
||||
trait CustomTestMacros
|
||||
{
|
||||
protected function registerCustomMacros()
|
||||
{
|
||||
$guardAgainstNullProperty = function (Model $model, string $property) {
|
||||
if (is_null($model->{$property})) {
|
||||
throw new RuntimeException(
|
||||
"The property ({$property}) either does not exist or is null on the model which isn't helpful for comparison."
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
TestResponse::macro(
|
||||
'assertResponseContainsInRows',
|
||||
function (Model $model, string $property = 'name') use ($guardAgainstNullProperty) {
|
||||
$guardAgainstNullProperty($model, $property);
|
||||
|
||||
Assert::assertTrue(
|
||||
collect($this['rows'])->pluck($property)->contains(e($model->{$property})),
|
||||
"Response did not contain the expected value: {$model->{$property}}"
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
);
|
||||
|
||||
TestResponse::macro(
|
||||
'assertResponseDoesNotContainInRows',
|
||||
function (Model $model, string $property = 'name') use ($guardAgainstNullProperty) {
|
||||
$guardAgainstNullProperty($model, $property);
|
||||
|
||||
Assert::assertFalse(
|
||||
collect($this['rows'])->pluck($property)->contains(e($model->{$property})),
|
||||
"Response contained unexpected value: {$model->{$property}}"
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
);
|
||||
|
||||
TestResponse::macro(
|
||||
'assertResponseContainsInResults',
|
||||
function (Model $model, string $property = 'id') use ($guardAgainstNullProperty) {
|
||||
$guardAgainstNullProperty($model, $property);
|
||||
|
||||
Assert::assertTrue(
|
||||
collect($this->json('results'))->pluck('id')->contains(e($model->{$property})),
|
||||
"Response did not contain the expected value: {$model->{$property}}"
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
);
|
||||
|
||||
TestResponse::macro(
|
||||
'assertResponseDoesNotContainInResults',
|
||||
function (Model $model, string $property = 'id') use ($guardAgainstNullProperty) {
|
||||
$guardAgainstNullProperty($model, $property);
|
||||
|
||||
Assert::assertFalse(
|
||||
collect($this->json('results'))->pluck('id')->contains(e($model->{$property})),
|
||||
"Response contained unexpected value: {$model->{$property}}"
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
);
|
||||
|
||||
TestResponse::macro(
|
||||
'assertStatusMessageIs',
|
||||
function (string $message) {
|
||||
Assert::assertEquals(
|
||||
$message,
|
||||
$this['status'],
|
||||
"Response status message was not {$message}"
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
17
SNIPE-IT/tests/Support/InitializesSettings.php
Normal file
17
SNIPE-IT/tests/Support/InitializesSettings.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Support;
|
||||
|
||||
use App\Models\Setting;
|
||||
|
||||
trait InitializesSettings
|
||||
{
|
||||
protected Settings $settings;
|
||||
|
||||
public function initializeSettings()
|
||||
{
|
||||
$this->settings = Settings::initialize();
|
||||
|
||||
$this->beforeApplicationDestroyed(fn() => Setting::$_cache = null);
|
||||
}
|
||||
}
|
16
SNIPE-IT/tests/Support/InteractsWithAuthentication.php
Normal file
16
SNIPE-IT/tests/Support/InteractsWithAuthentication.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Support;
|
||||
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Laravel\Passport\Passport;
|
||||
|
||||
trait InteractsWithAuthentication
|
||||
{
|
||||
protected function actingAsForApi(Authenticatable $user)
|
||||
{
|
||||
Passport::actingAs($user);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
140
SNIPE-IT/tests/Support/Settings.php
Normal file
140
SNIPE-IT/tests/Support/Settings.php
Normal file
@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Support;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
|
||||
class Settings
|
||||
{
|
||||
private Setting $setting;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
$this->setting = Setting::factory()->create();
|
||||
}
|
||||
|
||||
public static function initialize(): Settings
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
public function enableAlertEmail(string $email = 'notifications@afcrichmond.com'): Settings
|
||||
{
|
||||
return $this->update(['alert_email' => $email]);
|
||||
}
|
||||
|
||||
public function disableAlertEmail(): Settings
|
||||
{
|
||||
return $this->update(['alert_email' => null]);
|
||||
}
|
||||
|
||||
public function enableMultipleFullCompanySupport(): Settings
|
||||
{
|
||||
return $this->update(['full_multiple_companies_support' => 1]);
|
||||
}
|
||||
|
||||
public function disableMultipleFullCompanySupport(): Settings
|
||||
{
|
||||
return $this->update(['full_multiple_companies_support' => 0]);
|
||||
}
|
||||
|
||||
public function enableSlackWebhook(): Settings
|
||||
{
|
||||
return $this->update([
|
||||
'webhook_selected' => 'slack',
|
||||
'webhook_botname' => 'SnipeBot5000',
|
||||
'webhook_endpoint' => 'https://hooks.slack.com/services/NZ59/Q446/672N',
|
||||
'webhook_channel' => '#it',
|
||||
]);
|
||||
}
|
||||
|
||||
public function disableSlackWebhook(): Settings
|
||||
{
|
||||
return $this->update([
|
||||
'webhook_selected' => '',
|
||||
'webhook_botname' => '',
|
||||
'webhook_endpoint' => '',
|
||||
'webhook_channel' => '',
|
||||
]);
|
||||
}
|
||||
|
||||
public function enableAutoIncrement(): Settings
|
||||
{
|
||||
return $this->update([
|
||||
'auto_increment_assets' => 1,
|
||||
'auto_increment_prefix' => 'ABCD',
|
||||
'next_auto_tag_base' => 123,
|
||||
'zerofill_count' => 5
|
||||
]);
|
||||
}
|
||||
|
||||
public function disableAutoIncrement(): Settings
|
||||
{
|
||||
return $this->update([
|
||||
'auto_increment_assets' => 0,
|
||||
'auto_increment_prefix' => 0,
|
||||
'next_auto_tag_base' => 0,
|
||||
'zerofill_count' => 0
|
||||
]);
|
||||
}
|
||||
|
||||
public function enableUniqueSerialNumbers(): Settings
|
||||
{
|
||||
return $this->update(['unique_serial' => 1]);
|
||||
}
|
||||
|
||||
public function disableUniqueSerialNumbers(): Settings
|
||||
{
|
||||
return $this->update(['unique_serial' => 0]);
|
||||
}
|
||||
|
||||
public function enableLdap(): Settings
|
||||
{
|
||||
return $this->update([
|
||||
'ldap_enabled' => 1,
|
||||
'ldap_server' => 'ldaps://ldap.example.com',
|
||||
'ldap_uname' => 'fake_username',
|
||||
'ldap_pword' => Crypt::encrypt("fake_password"),
|
||||
'ldap_basedn' => 'CN=Users,DC=ad,DC=example,Dc=com'
|
||||
]);
|
||||
}
|
||||
|
||||
public function enableAnonymousLdap(): Settings
|
||||
{
|
||||
return $this->update([
|
||||
'ldap_enabled' => 1,
|
||||
'ldap_server' => 'ldaps://ldap.example.com',
|
||||
// 'ldap_uname' => 'fake_username',
|
||||
'ldap_pword' => Crypt::encrypt("fake_password"),
|
||||
'ldap_basedn' => 'CN=Users,DC=ad,DC=example,Dc=com'
|
||||
]);
|
||||
}
|
||||
|
||||
public function enableBadPasswordLdap(): Settings
|
||||
{
|
||||
return $this->update([
|
||||
'ldap_enabled' => 1,
|
||||
'ldap_server' => 'ldaps://ldap.example.com',
|
||||
'ldap_uname' => 'fake_username',
|
||||
'ldap_pword' => "badly_encrypted_password!",
|
||||
'ldap_basedn' => 'CN=Users,DC=ad,DC=example,Dc=com'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $attributes Attributes to modify in the application's settings.
|
||||
*/
|
||||
public function set(array $attributes): Settings
|
||||
{
|
||||
return $this->update($attributes);
|
||||
}
|
||||
|
||||
private function update(array $attributes): Settings
|
||||
{
|
||||
Setting::unguarded(fn() => $this->setting->update($attributes));
|
||||
Setting::$_cache = null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user