From 0dd2c64c3873ee57fa26d5726e3296b7311cce7c Mon Sep 17 00:00:00 2001 From: philipp lang <philipp@aweos.de> Date: Sat, 24 Feb 2024 01:25:24 +0100 Subject: [PATCH] Add separate login for mglnr --- src/Authentication/MainCookie.php | 14 +++++------ tests/Unit/CourseTest.php | 29 ----------------------- tests/Unit/LoginTest.php | 39 ++++++++++++++++--------------- 3 files changed, 26 insertions(+), 56 deletions(-) diff --git a/src/Authentication/MainCookie.php b/src/Authentication/MainCookie.php index a76056a..b57804e 100644 --- a/src/Authentication/MainCookie.php +++ b/src/Authentication/MainCookie.php @@ -12,8 +12,8 @@ class MainCookie extends Authenticator { private CookieJar $cookie; private string $url = 'https://nami.dpsg.de'; - private ?int $mglnr = null; - private ?string $password = null; + private int $mglnr; + private string $password; public function login(int $mglnr, string $password): self { @@ -64,9 +64,7 @@ class MainCookie extends Authenticator public function refresh(): void { - if ($this->mglnr && $this->password) { - $this->login($this->mglnr, $this->password); - } + $this->login($this->mglnr, $this->password); } public function http(): PendingRequest @@ -76,12 +74,12 @@ class MainCookie extends Authenticator private function newFileName(): string { - return parent::$path . '/' . now()->timestamp . '.txt'; + return parent::$path . '/' . $this->mglnr . '_' . now()->timestamp . '.txt'; } private function isExpired(): bool { - $lastLoginTime = Carbon::createFromTimestamp(pathinfo($this->file(), PATHINFO_FILENAME)); + $lastLoginTime = Carbon::createFromTimestamp(str(pathinfo($this->file(), PATHINFO_FILENAME))->replace($this->mglnr . '_', '')->toString()); return $lastLoginTime->addMinutes(50)->isPast(); } @@ -93,7 +91,7 @@ class MainCookie extends Authenticator */ private function file(): ?string { - $files = glob(parent::$path . '/*'); + $files = glob(parent::$path . '/' . $this->mglnr . '_*'); if (!count($files)) { return null; diff --git a/tests/Unit/CourseTest.php b/tests/Unit/CourseTest.php index 57cdcd5..d856feb 100644 --- a/tests/Unit/CourseTest.php +++ b/tests/Unit/CourseTest.php @@ -69,13 +69,6 @@ class CourseTest extends TestCase $this->login()->coursesOf(11111); } - public function testItNeedsLoginToGetCourses(): void - { - $this->expectException(NotAuthenticatedException::class); - - Nami::coursesOf(11111); - } - public function testStoreACourse(): void { app(CourseFake::class)->createsSuccessfully(123, 999); @@ -94,17 +87,6 @@ class CourseTest extends TestCase ]); } - public function testNeedsLoginToStoreACourse(): void - { - $this->expectException(NotAuthenticatedException::class); - Nami::createCourse(123, [ - 'event_name' => '::event::', - 'completed_at' => '2021-01-02 00:00:00', - 'organizer' => '::org::', - 'course_id' => 456, - ]); - } - public function testUpdateACourse(): void { app(CourseFake::class)->updatesSuccessfully(123, 999); @@ -140,17 +122,6 @@ class CourseTest extends TestCase ]); } - public function testItNeedsValidCredentialsToStoreACourse(): void - { - $this->expectException(NotAuthenticatedException::class); - Nami::createCourse(123, [ - 'event_name' => '::event::', - 'completed_at' => '2021-01-02 00:00:00', - 'organizer' => '::org::', - 'course_id' => 456, - ]); - } - public function testItThrowsLoginExceptionWhenFetchingWithWrongCredentials(): void { $this->expectException(LoginException::class); diff --git a/tests/Unit/LoginTest.php b/tests/Unit/LoginTest.php index ae5b865..59f3d76 100644 --- a/tests/Unit/LoginTest.php +++ b/tests/Unit/LoginTest.php @@ -27,7 +27,7 @@ class LoginTest extends TestCase app(MainCookie::class)->login(12345, 'secret'); Http::assertSentCount(1); - $cookie = file_get_contents(__DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt'); + $cookie = file_get_contents(__DIR__ . '/../../.cookies_test/12345_' . now()->timestamp . '.txt'); $this->assertEquals('lala-testsession.srv-nami06', $cookie); app(LoginFake::class)->assertSent(12345, 'secret'); } @@ -52,26 +52,15 @@ class LoginTest extends TestCase $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'); + file_put_contents(__DIR__ . '/../../.cookies_test/90100_' . 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->assertFileExists(__DIR__ . '/../../.cookies_test/90100_' . now()->subMinutes(5)->timestamp . '.txt'); $this->assertCount(1, glob(__DIR__ . '/../../.cookies_test/*')); } @@ -79,24 +68,36 @@ class LoginTest extends TestCase { app(CourseFake::class)->fetches(103, []); app(LoginFake::class)->succeeds('newlogin'); - file_put_contents(__DIR__ . '/../../.cookies_test/' . now()->subHour()->timestamp . '.txt', 'oldlogin-testsession.srv-nami06'); + file_put_contents(__DIR__ . '/../../.cookies_test/90100_' . 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'); + $cookie = file_get_contents(__DIR__ . '/../../.cookies_test/90100_' . now()->timestamp . '.txt'); $this->assertEquals('newlogin.srv-nami06', $cookie); - $this->assertFileExists(__DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt'); + $this->assertFileExists(__DIR__ . '/../../.cookies_test/90100_' . now()->timestamp . '.txt'); $this->assertCount(1, glob(__DIR__ . '/../../.cookies_test/*')); } + public function testItDoesntUseOtherCookieForLogin(): void + { + app(CourseFake::class)->fetches(103, []); + app(LoginFake::class)->succeeds('newlogin'); + file_put_contents(__DIR__ . '/../../.cookies_test/11111_' . now()->timestamp . '.txt', 'oldlogin-testsession.srv-nami06'); + app(Authenticator::class)->login(12345, 'secret'); + + app(LoginFake::class)->assertSent(12345, 'secret'); + $this->assertFileExists(__DIR__ . '/../../.cookies_test/11111_' . now()->timestamp . '.txt'); + $this->assertFileExists(__DIR__ . '/../../.cookies_test/12345_' . now()->timestamp . '.txt'); + } + 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' + __DIR__ . '/../../.cookies_test/12345_' . now()->timestamp . '.txt', + __DIR__ . '/../../.cookies_test/12345_' . now()->subMinutes(51)->timestamp . '.txt' ); $auth->refresh(); Http::assertSentCount(2);