Add separate login for mglnr

This commit is contained in:
philipp lang 2024-02-24 01:25:24 +01:00
parent cf50705d5a
commit 0dd2c64c38
3 changed files with 26 additions and 56 deletions

View File

@ -12,8 +12,8 @@ class MainCookie extends Authenticator
{ {
private CookieJar $cookie; private CookieJar $cookie;
private string $url = 'https://nami.dpsg.de'; private string $url = 'https://nami.dpsg.de';
private ?int $mglnr = null; private int $mglnr;
private ?string $password = null; private string $password;
public function login(int $mglnr, string $password): self public function login(int $mglnr, string $password): self
{ {
@ -64,10 +64,8 @@ class MainCookie extends Authenticator
public function refresh(): void 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 public function http(): PendingRequest
{ {
@ -76,12 +74,12 @@ class MainCookie extends Authenticator
private function newFileName(): string private function newFileName(): string
{ {
return parent::$path . '/' . now()->timestamp . '.txt'; return parent::$path . '/' . $this->mglnr . '_' . now()->timestamp . '.txt';
} }
private function isExpired(): bool 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(); return $lastLoginTime->addMinutes(50)->isPast();
} }
@ -93,7 +91,7 @@ class MainCookie extends Authenticator
*/ */
private function file(): ?string private function file(): ?string
{ {
$files = glob(parent::$path . '/*'); $files = glob(parent::$path . '/' . $this->mglnr . '_*');
if (!count($files)) { if (!count($files)) {
return null; return null;

View File

@ -69,13 +69,6 @@ class CourseTest extends TestCase
$this->login()->coursesOf(11111); $this->login()->coursesOf(11111);
} }
public function testItNeedsLoginToGetCourses(): void
{
$this->expectException(NotAuthenticatedException::class);
Nami::coursesOf(11111);
}
public function testStoreACourse(): void public function testStoreACourse(): void
{ {
app(CourseFake::class)->createsSuccessfully(123, 999); 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 public function testUpdateACourse(): void
{ {
app(CourseFake::class)->updatesSuccessfully(123, 999); 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 public function testItThrowsLoginExceptionWhenFetchingWithWrongCredentials(): void
{ {
$this->expectException(LoginException::class); $this->expectException(LoginException::class);

View File

@ -27,7 +27,7 @@ class LoginTest extends TestCase
app(MainCookie::class)->login(12345, 'secret'); app(MainCookie::class)->login(12345, 'secret');
Http::assertSentCount(1); 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); $this->assertEquals('lala-testsession.srv-nami06', $cookie);
app(LoginFake::class)->assertSent(12345, 'secret'); app(LoginFake::class)->assertSent(12345, 'secret');
} }
@ -52,26 +52,15 @@ class LoginTest extends TestCase
$this->assertEmpty(glob(__DIR__ . '/../../.cookies_test/*')); $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 public function testItDoesntResetCookieWhenAlreadyLoggedIn(): void
{ {
app(CourseFake::class)->fetches(103, []); 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'); Nami::login(90100, 'secret');
Http::assertSentCount(0); 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/*')); $this->assertCount(1, glob(__DIR__ . '/../../.cookies_test/*'));
} }
@ -79,24 +68,36 @@ class LoginTest extends TestCase
{ {
app(CourseFake::class)->fetches(103, []); app(CourseFake::class)->fetches(103, []);
app(LoginFake::class)->succeeds('newlogin'); 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'); Nami::login(90100, 'secret');
Http::assertSentCount(1); 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->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/*')); $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 public function testItRefreshesLogin(): void
{ {
app(LoginFake::class)->succeeds('newlogin'); app(LoginFake::class)->succeeds('newlogin');
$auth = app(Authenticator::class)->login(12345, 'secret'); $auth = app(Authenticator::class)->login(12345, 'secret');
rename( rename(
__DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt', __DIR__ . '/../../.cookies_test/12345_' . now()->timestamp . '.txt',
__DIR__ . '/../../.cookies_test/' . now()->subMinutes(51)->timestamp . '.txt' __DIR__ . '/../../.cookies_test/12345_' . now()->subMinutes(51)->timestamp . '.txt'
); );
$auth->refresh(); $auth->refresh();
Http::assertSentCount(2); Http::assertSentCount(2);