From cf50705d5a619713b3d01211477995cb03d9d386 Mon Sep 17 00:00:00 2001 From: philipp lang <philipp@aweos.de> Date: Sat, 24 Feb 2024 01:05:18 +0100 Subject: [PATCH] Add login test --- src/Fakes/LoginFake.php | 53 +++++++++++++++++++++++++++++ tests/Unit/LoginTest.php | 72 +++++++++++++++------------------------- 2 files changed, 79 insertions(+), 46 deletions(-) create mode 100644 src/Fakes/LoginFake.php diff --git a/src/Fakes/LoginFake.php b/src/Fakes/LoginFake.php new file mode 100644 index 0000000..ae9f5fe --- /dev/null +++ b/src/Fakes/LoginFake.php @@ -0,0 +1,53 @@ +<?php + +namespace Zoomyboy\LaravelNami\Fakes; + +use Illuminate\Support\Facades\Http; + +class LoginFake extends Fake +{ + public function succeeds(string $token): void + { + $this->fakeLogin($token, 0); + } + + public function fails(string $token): void + { + $this->fakeLogin($token, 500); + } + + protected function fakeLogin(string $token, int $statusCode): void + { + Http::fake(function ($request) use ($token, $statusCode) { + if ($request->url() !== 'https://nami.dpsg.de/ica/rest/nami/auth/manual/sessionStartup') { + return; + } + + return Http::response(json_encode([ + "servicePrefix" => null, + "methodCall" => null, + "response" => null, + "statusCode" => $statusCode, + "statusMessage" => "", + "apiSessionName" => "JSESSIONID", + "apiSessionToken" => $token, + "minorNumber" => 2, + "majorNumber" => 1, + ]), 200, [ + 'Set-Cookie' => 'JSESSIONID=' . $token . '.srv-nami06; path=/ica; secure; HttpOnly; Max-Age=9000; Expires=Sat, 24-Feb-2024 01:18:00 GMT' + ]); + }); + } + + public function assertSent(int $mglnr, string $password): void + { + Http::assertSent(function ($request) use ($mglnr, $password) { + return $request->url() === 'https://nami.dpsg.de/ica/rest/nami/auth/manual/sessionStartup' + && $request->header('Content-Type')[0] === 'application/x-www-form-urlencoded' + && $request['Login'] === 'API' + && $request['redirectTo'] === './app.jsp' + && $request['username'] === $mglnr + && $request['password'] === $password; + }); + } +} diff --git a/tests/Unit/LoginTest.php b/tests/Unit/LoginTest.php index b881f78..ae5b865 100644 --- a/tests/Unit/LoginTest.php +++ b/tests/Unit/LoginTest.php @@ -4,8 +4,10 @@ namespace Zoomyboy\LaravelNami\Tests\Unit; use Carbon\Carbon; use Illuminate\Support\Facades\Http; +use Zoomyboy\LaravelNami\Authentication\Authenticator; use Zoomyboy\LaravelNami\Authentication\MainCookie; use Zoomyboy\LaravelNami\Fakes\CourseFake; +use Zoomyboy\LaravelNami\Fakes\LoginFake; use Zoomyboy\LaravelNami\LoginException; use Zoomyboy\LaravelNami\Nami; use Zoomyboy\LaravelNami\Tests\TestCase; @@ -20,46 +22,26 @@ class LoginTest extends TestCase public function testItLoggsInAndSavesCookie(): void { - Http::fake([ - 'https://nami.dpsg.de/ica/rest/nami/auth/manual/sessionStartup' => Http::response(json_encode([ - "servicePrefix" => null, - "methodCall" => null, - "response" => null, - "statusCode" => 0, - "statusMessage" => "", - "apiSessionName" => "JSESSIONID", - "apiSessionToken" => "lala-testsession", - "minorNumber" => 2, - "majorNumber" => 1, - ]), 200, [ - 'Set-Cookie' => 'JSESSIONID=lala-testsession.srv-nami06; path=/ica; secure; HttpOnly; Max-Age=9000; Expires=Sat, 24-Feb-2024 01:18:00 GMT' - ]), - ]); + app(LoginFake::class)->succeeds('lala-testsession'); app(MainCookie::class)->login(12345, 'secret'); Http::assertSentCount(1); $cookie = file_get_contents(__DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt'); $this->assertEquals('lala-testsession.srv-nami06', $cookie); + app(LoginFake::class)->assertSent(12345, 'secret'); + } + + public function testItThrowsExceptionWhenLoginFails(): void + { + app(LoginFake::class)->fails('::token::'); + $this->expectException(LoginException::class); + app(MainCookie::class)->login(12345, 'secret'); } public function testItDoesntSaveCookieWhenLoginFails(): void { - Http::fake([ - 'https://nami.dpsg.de/ica/rest/nami/auth/manual/sessionStartup' => Http::response(json_encode([ - "servicePrefix" => null, - "methodCall" => null, - "response" => null, - "statusCode" => 500, - "statusMessage" => "", - "apiSessionName" => "JSESSIONID", - "apiSessionToken" => "lala-testsession", - "minorNumber" => 2, - "majorNumber" => 1, - ]), 200, [ - 'Set-Cookie' => 'JSESSIONID=lala-testsession.srv-nami06; path=/ica; secure; HttpOnly; Max-Age=9000; Expires=Sat, 24-Feb-2024 01:18:00 GMT' - ]), - ]); + app(LoginFake::class)->fails('::token::'); try { app(MainCookie::class)->login(12345, 'secret'); @@ -93,24 +75,10 @@ class LoginTest extends TestCase $this->assertCount(1, glob(__DIR__ . '/../../.cookies_test/*')); } - public function testItRefreshesExpiredCookie(): void + public function testItIgnoresExpiredCookie(): void { app(CourseFake::class)->fetches(103, []); - Http::fake([ - 'https://nami.dpsg.de/ica/rest/nami/auth/manual/sessionStartup' => Http::response(json_encode([ - "servicePrefix" => null, - "methodCall" => null, - "response" => null, - "statusCode" => 0, - "statusMessage" => "", - "apiSessionName" => "JSESSIONID", - "apiSessionToken" => "lala-testsession", - "minorNumber" => 2, - "majorNumber" => 1, - ]), 200, [ - 'Set-Cookie' => 'JSESSIONID=newlogin.srv-nami06; path=/ica; secure; HttpOnly; Max-Age=9000; Expires=Sat, 24-Feb-2024 01:18:00 GMT' - ]), - ]); + app(LoginFake::class)->succeeds('newlogin'); file_put_contents(__DIR__ . '/../../.cookies_test/' . now()->subHour()->timestamp . '.txt', 'oldlogin-testsession.srv-nami06'); Nami::login(90100, 'secret'); @@ -121,4 +89,16 @@ class LoginTest extends TestCase $this->assertFileExists(__DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt'); $this->assertCount(1, glob(__DIR__ . '/../../.cookies_test/*')); } + + public function testItRefreshesLogin(): void + { + app(LoginFake::class)->succeeds('newlogin'); + $auth = app(Authenticator::class)->login(12345, 'secret'); + rename( + __DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt', + __DIR__ . '/../../.cookies_test/' . now()->subMinutes(51)->timestamp . '.txt' + ); + $auth->refresh(); + Http::assertSentCount(2); + } }