diff --git a/tests/Feature/LoginTest.php b/tests/Feature/LoginTest.php index 8a3966b4..0e3b868d 100644 --- a/tests/Feature/LoginTest.php +++ b/tests/Feature/LoginTest.php @@ -4,34 +4,67 @@ namespace Tests\Feature; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; -use Tests\TestCase; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Http; +use Tests\TestCase; class LoginTest extends TestCase { - public function test_it_stores_login_cookie() + public function setUp(): void + { + parent::setUp(); + } + + public function testItCanLoginWithANamiAccount() { $this->withoutExceptionHandling(); - $this->fakeNamiMembers([ - [ 'gruppierungId' => 11222, 'vorname' => 'Max', 'nachname' => 'Muster', 'id' => 123 ] - ]); - $this->fakeNamiPassword(123, 'secret', [11222]); + app('nami.backend')->fakeLogin(123, [], 'cookie-123'); $this->post('/login', [ 'mglnr' => 123, 'password' => 'secret' ]); - $cache = Cache::get('namicookie-123'); - $key = session()->get('auth_key'); $cache = Cache::get("namiauth-{$key}"); $this->assertEquals('secret', data_get($cache, 'credentials.password')); - $this->assertEquals(123, auth()->user()->getMglnr()); - $this->assertEquals('Max', auth()->user()->getFirstname()); - $this->assertEquals('Muster', auth()->user()->getLastname()); + $this->assertEquals(123, data_get($cache, 'credentials.mglnr')); + $this->assertTrue(auth()->check()); + } + + public function testItDoesntLoginTwoTimes() + { + $this->withoutExceptionHandling(); + app('nami.backend')->fakeLogin(123, [], 'cookie-123'); + + $this->post('/login', [ + 'mglnr' => 123, + 'password' => 'secret' + ]); + auth()->logout(); + $this->post('/login', [ + 'mglnr' => 123, + 'password' => 'secret' + ]); + $this->assertTrue(auth()->check()); + Http::assertSentCount(2); } + + public function testItThrowsExceptionWhenLoginFailed() + { + app('nami.backend')->fakeFailedLogin(123); + + $this->post('/login', [ + 'mglnr' => 123, + 'password' => 'secret' + ])->assertRedirect('/'); + + $this->assertFalse(auth()->check()); + + Http::assertSentCount(2); + } + }