adrema/tests/Feature/Initialize/InitializeActionTest.php

78 lines
2.6 KiB
PHP
Raw Normal View History

2022-09-06 17:51:18 +02:00
<?php
namespace Tests\Feature\Initialize;
use App\Initialize\Actions\InitializeAction;
use App\Setting\NamiSettings;
use Illuminate\Foundation\Testing\DatabaseTransactions;
2023-02-07 00:43:27 +01:00
use Tests\RequestFactories\InitializeRequestFactory;
2022-09-06 17:51:18 +02:00
use Tests\TestCase;
use Zoomyboy\LaravelNami\Authentication\Auth;
use Zoomyboy\LaravelNami\Fakes\GroupFake;
2023-02-07 00:43:27 +01:00
class InitializeActionTest extends TestCase
2022-09-06 17:51:18 +02:00
{
use DatabaseTransactions;
2023-02-07 00:43:27 +01:00
public function testItCannotInitializeWhenNotLoggedIn(): void
{
InitializeAction::partialMock()->shouldReceive('handle')->never();
$response = $this->post('/initialize', $this->factory()->create());
$response->assertRedirect('/login');
}
2022-09-06 17:51:18 +02:00
public function testItSetsSettingsBeforeRunningInitializer(): void
{
$this->withoutExceptionHandling()->login();
InitializeAction::partialMock()->shouldReceive('handle')->with(12345, 'secret', 185)->once()->andReturn(true);
Auth::success(12345, 'secret');
app(GroupFake::class)->fetches(null, [185 => ['name' => 'testgroup']]);
2023-02-07 00:43:27 +01:00
$response = $this->post('/initialize', $this->factory()->withCredentials(12345, 'secret')->withGroup(185)->create());
2022-09-06 17:51:18 +02:00
$response->assertRedirect('/');
$settings = app(NamiSettings::class);
$this->assertEquals(12345, $settings->mglnr);
$this->assertEquals('secret', $settings->password);
$this->assertEquals(185, $settings->default_group_id);
}
public function testItValidatesSetupInfo(): void
{
$this->login();
2023-02-07 00:43:27 +01:00
$response = $this->post('/initialize', $this->factory()->invalid()->create());
2022-09-06 17:51:18 +02:00
$this->assertErrors(['password' => 'Passwort ist erforderlich.'], $response);
$this->assertErrors(['mglnr' => 'Mitgliedsnummer ist erforderlich.'], $response);
$this->assertErrors(['group_id' => 'Gruppierungsnr ist erforderlich.'], $response);
}
public function testItValidatesLogin(): void
{
$this->login();
2023-02-07 00:43:27 +01:00
$response = $this->post('/initialize', $this->factory()->withCredentials(12345, 'secret')->create());
2022-09-06 17:51:18 +02:00
$this->assertErrors(['nami' => 'NaMi Login fehlgeschlagen.'], $response);
}
public function testItValidatesGroupExistance(): void
{
$this->login();
Auth::success(12345, 'secret');
app(GroupFake::class)->fetches(null, []);
2023-02-07 00:43:27 +01:00
$response = $this->post('/initialize', $this->factory()->withCredentials(12345, 'secret')->withGroup(185)->create());
2022-09-06 17:51:18 +02:00
$this->assertErrors(['nami' => 'Gruppierung nicht gefunden.'], $response);
}
2023-02-07 00:43:27 +01:00
private function factory(): InitializeRequestFactory
2022-09-06 17:51:18 +02:00
{
2023-02-07 00:43:27 +01:00
return InitializeRequestFactory::new();
2022-09-06 17:51:18 +02:00
}
}