From 5802ccf33210170db21a62603ac0d8adebb2ce22 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sat, 24 Feb 2024 00:37:11 +0100 Subject: [PATCH] Add Login test --- tests/Unit/LoginTest.php | 124 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 tests/Unit/LoginTest.php diff --git a/tests/Unit/LoginTest.php b/tests/Unit/LoginTest.php new file mode 100644 index 0000000..b881f78 --- /dev/null +++ b/tests/Unit/LoginTest.php @@ -0,0 +1,124 @@ + 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(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); + } + + 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' + ]), + ]); + + try { + app(MainCookie::class)->login(12345, 'secret'); + } catch (LoginException $e) { + } + + Http::assertSentCount(1); + $this->assertEmpty(glob(__DIR__ . '/../../.cookies_test/*')); + } + + public function testItUsesSavedCookieForLogin(): void + { + app(CourseFake::class)->fetches(103, []); + file_put_contents(__DIR__ . '/../../.cookies_test/' . now()->subMinutes(5)->timestamp . '.txt', 'cook-testsession.srv-nami06'); + + Nami::coursesOf(103); + + Http::assertSentCount(1); + Http::assertSent(fn ($request) => $request->header('Cookie')[0] === 'JSESSIONID=cook-testsession.srv-nami06'); + } + + public function testItDoesntResetCookieWhenAlreadyLoggedIn(): void + { + app(CourseFake::class)->fetches(103, []); + file_put_contents(__DIR__ . '/../../.cookies_test/' . now()->subMinutes(5)->timestamp . '.txt', 'cook-testsession.srv-nami06'); + + Nami::login(90100, 'secret'); + + Http::assertSentCount(0); + $this->assertFileExists(__DIR__ . '/../../.cookies_test/' . now()->subMinutes(5)->timestamp . '.txt'); + $this->assertCount(1, glob(__DIR__ . '/../../.cookies_test/*')); + } + + public function testItRefreshesExpiredCookie(): 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' + ]), + ]); + file_put_contents(__DIR__ . '/../../.cookies_test/' . now()->subHour()->timestamp . '.txt', 'oldlogin-testsession.srv-nami06'); + + Nami::login(90100, 'secret'); + + Http::assertSentCount(1); + $cookie = file_get_contents(__DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt'); + $this->assertEquals('newlogin.srv-nami06', $cookie); + $this->assertFileExists(__DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt'); + $this->assertCount(1, glob(__DIR__ . '/../../.cookies_test/*')); + } +}