Add RequestTest
This commit is contained in:
parent
58e398721b
commit
78c35f8311
|
@ -3,27 +3,19 @@
|
||||||
namespace Tests\Feature\Initialize;
|
namespace Tests\Feature\Initialize;
|
||||||
|
|
||||||
use App\Activity;
|
use App\Activity;
|
||||||
use App\Console\Commands\NamiInitializeCommand;
|
|
||||||
use App\Country;
|
use App\Country;
|
||||||
use App\Course\Models\Course;
|
use App\Course\Models\Course;
|
||||||
use App\Gender;
|
use App\Gender;
|
||||||
use App\Group;
|
use App\Group;
|
||||||
use App\Initialize\Actions\InitializeAction;
|
|
||||||
use App\Initialize\InitializeJob;
|
use App\Initialize\InitializeJob;
|
||||||
use App\Member\Member;
|
use App\Member\Member;
|
||||||
use App\Nationality;
|
use App\Nationality;
|
||||||
use App\Setting\GeneralSettings;
|
|
||||||
use App\Setting\NamiSettings;
|
|
||||||
use App\Subactivity;
|
use App\Subactivity;
|
||||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
use Illuminate\Support\Facades\Artisan;
|
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
use Illuminate\Support\Facades\Queue;
|
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
use Zoomyboy\LaravelNami\Authentication\Auth;
|
|
||||||
use Zoomyboy\LaravelNami\Backend\FakeBackend;
|
use Zoomyboy\LaravelNami\Backend\FakeBackend;
|
||||||
use Zoomyboy\LaravelNami\Fakes\GroupFake;
|
use Zoomyboy\LaravelNami\Fakes\GroupFake;
|
||||||
use Zoomyboy\LaravelNami\Fakes\SearchFake;
|
|
||||||
|
|
||||||
class InitializeTest extends TestCase
|
class InitializeTest extends TestCase
|
||||||
{
|
{
|
||||||
|
@ -73,83 +65,6 @@ class InitializeTest extends TestCase
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
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']]);
|
|
||||||
|
|
||||||
$response = $this->post('/initialize', [
|
|
||||||
'group_id' => 185,
|
|
||||||
'password' => 'secret',
|
|
||||||
'mglnr' => 12345,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$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();
|
|
||||||
InitializeAction::partialMock()->shouldReceive('handle')->never();
|
|
||||||
|
|
||||||
$response = $this->post('/initialize', [
|
|
||||||
'group_id' => null,
|
|
||||||
'password' => null,
|
|
||||||
'mglnr' => null,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$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();
|
|
||||||
Auth::fails(12345, 'secret');
|
|
||||||
InitializeAction::partialMock()->shouldReceive('handle')->never();
|
|
||||||
|
|
||||||
$response = $this->post('/initialize', [
|
|
||||||
'group_id' => 12345,
|
|
||||||
'password' => 'secret',
|
|
||||||
'mglnr' => 100102,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->assertErrors(['nami' => 'NaMi Login fehlgeschlagen.'], $response);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItValidatesGroupExistance(): void
|
|
||||||
{
|
|
||||||
$this->login();
|
|
||||||
InitializeAction::partialMock()->shouldReceive('handle')->never();
|
|
||||||
Auth::success(12345, 'secret');
|
|
||||||
app(GroupFake::class)->fetches(null, []);
|
|
||||||
|
|
||||||
$response = $this->post('/initialize', [
|
|
||||||
'group_id' => 185,
|
|
||||||
'password' => 'secret',
|
|
||||||
'mglnr' => 12345,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->assertErrors(['nami' => 'Gruppierung nicht gefunden.'], $response);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItFiresJobWhenRunningInitializer(): void
|
|
||||||
{
|
|
||||||
Queue::fake();
|
|
||||||
$this->withoutExceptionHandling()->login();
|
|
||||||
|
|
||||||
app(InitializeAction::class)->handle(12345, 'secret', 185);
|
|
||||||
|
|
||||||
Queue::assertPushed(InitializeJob::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItInitializesAll(): void
|
public function testItInitializesAll(): void
|
||||||
{
|
{
|
||||||
$this->withoutExceptionHandling()->login()->loginNami();
|
$this->withoutExceptionHandling()->login()->loginNami();
|
||||||
|
@ -206,20 +121,6 @@ class InitializeTest extends TestCase
|
||||||
Http::assertSentCount(17);
|
Http::assertSentCount(17);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItInitializesFromCommandLine(): void
|
|
||||||
{
|
|
||||||
$this->withoutExceptionHandling()->loginNami();
|
|
||||||
$this->initializeProvider();
|
|
||||||
GeneralSettings::fake(['allowed_nami_accounts' => [123]]);
|
|
||||||
|
|
||||||
Artisan::call(NamiInitializeCommand::class);
|
|
||||||
|
|
||||||
$this->assertDatabaseHas('regions', [
|
|
||||||
'name' => 'nrw',
|
|
||||||
'nami_id' => 304,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testSyncCoursesOfMember(): void
|
public function testSyncCoursesOfMember(): void
|
||||||
{
|
{
|
||||||
$this->withoutExceptionHandling()->login()->loginNami();
|
$this->withoutExceptionHandling()->login()->loginNami();
|
||||||
|
@ -496,21 +397,6 @@ class InitializeTest extends TestCase
|
||||||
$this->assertDatabaseCount('members', $num);
|
$this->assertDatabaseCount('members', $num);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRenderErrorInConsoleWhenUsingArtisan(): void
|
|
||||||
{
|
|
||||||
$this->withoutExceptionHandling()->login()->loginNami();
|
|
||||||
$this->initializeProvider(function ($backend) {
|
|
||||||
app(SearchFake::class)->fetchFails($page = 1, $start = 0, 'search error');
|
|
||||||
});
|
|
||||||
$this->login();
|
|
||||||
|
|
||||||
$command = $this->artisan(NamiInitializeCommand::class);
|
|
||||||
|
|
||||||
$command->assertFailed();
|
|
||||||
$command->expectsOutput('response: {"success":false,"message":"search error"}');
|
|
||||||
$command->expectsOutput('Search failed');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array<string, mixed> $overwrites
|
* @param array<string, mixed> $overwrites
|
||||||
*
|
*
|
||||||
|
|
|
@ -0,0 +1,104 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Initialize;
|
||||||
|
|
||||||
|
use App\Console\Commands\NamiInitializeCommand;
|
||||||
|
use App\Initialize\Actions\InitializeAction;
|
||||||
|
use App\Initialize\InitializeJob;
|
||||||
|
use App\Initialize\Initializer;
|
||||||
|
use App\Setting\NamiSettings;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
use Illuminate\Support\Facades\Artisan;
|
||||||
|
use Illuminate\Support\Facades\Queue;
|
||||||
|
use Tests\TestCase;
|
||||||
|
use Zoomyboy\LaravelNami\Authentication\Auth;
|
||||||
|
use Zoomyboy\LaravelNami\Fakes\GroupFake;
|
||||||
|
|
||||||
|
class RequestTest extends TestCase
|
||||||
|
{
|
||||||
|
use DatabaseTransactions;
|
||||||
|
|
||||||
|
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']]);
|
||||||
|
|
||||||
|
$response = $this->post('/initialize', [
|
||||||
|
'group_id' => 185,
|
||||||
|
'password' => 'secret',
|
||||||
|
'mglnr' => 12345,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$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();
|
||||||
|
InitializeAction::partialMock()->shouldReceive('handle')->never();
|
||||||
|
|
||||||
|
$response = $this->post('/initialize', [
|
||||||
|
'group_id' => null,
|
||||||
|
'password' => null,
|
||||||
|
'mglnr' => null,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$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();
|
||||||
|
Auth::fails(12345, 'secret');
|
||||||
|
InitializeAction::partialMock()->shouldReceive('handle')->never();
|
||||||
|
|
||||||
|
$response = $this->post('/initialize', [
|
||||||
|
'group_id' => 12345,
|
||||||
|
'password' => 'secret',
|
||||||
|
'mglnr' => 100102,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertErrors(['nami' => 'NaMi Login fehlgeschlagen.'], $response);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItValidatesGroupExistance(): void
|
||||||
|
{
|
||||||
|
$this->login();
|
||||||
|
InitializeAction::partialMock()->shouldReceive('handle')->never();
|
||||||
|
Auth::success(12345, 'secret');
|
||||||
|
app(GroupFake::class)->fetches(null, []);
|
||||||
|
|
||||||
|
$response = $this->post('/initialize', [
|
||||||
|
'group_id' => 185,
|
||||||
|
'password' => 'secret',
|
||||||
|
'mglnr' => 12345,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertErrors(['nami' => 'Gruppierung nicht gefunden.'], $response);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItFiresJobWhenRunningInitializer(): void
|
||||||
|
{
|
||||||
|
Queue::fake();
|
||||||
|
$this->withoutExceptionHandling()->login();
|
||||||
|
|
||||||
|
app(InitializeAction::class)->handle(12345, 'secret', 185);
|
||||||
|
|
||||||
|
Queue::assertPushed(InitializeJob::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItInitializesFromCommandLine(): void
|
||||||
|
{
|
||||||
|
$this->stubIo(Initializer::class, fn ($mock) => $mock->shouldReceive('run')->once());
|
||||||
|
|
||||||
|
Artisan::call(NamiInitializeCommand::class);
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,6 +9,8 @@ use App\User;
|
||||||
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||||
use Illuminate\Http\RedirectResponse;
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Testing\TestResponse;
|
use Illuminate\Testing\TestResponse;
|
||||||
|
use Mockery as M;
|
||||||
|
use Mockery\MockInterface;
|
||||||
use Tests\Lib\TestsInertia;
|
use Tests\Lib\TestsInertia;
|
||||||
use Zoomyboy\LaravelNami\Authentication\Auth;
|
use Zoomyboy\LaravelNami\Authentication\Auth;
|
||||||
|
|
||||||
|
@ -88,4 +90,19 @@ abstract class TestCase extends BaseTestCase
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param <class-string> $class
|
||||||
|
* @param callable(MockInterface $mock): void $mocker
|
||||||
|
*/
|
||||||
|
public function stubIo(string $class, callable $mocker): self
|
||||||
|
{
|
||||||
|
$mock = M::mock($class);
|
||||||
|
|
||||||
|
$mocker($mock);
|
||||||
|
|
||||||
|
app()->instance($class, $mock);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue