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
src/Authentication
tests/Unit

View File

@ -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;

View File

@ -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);

View File

@ -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);